summaryrefslogtreecommitdiff
path: root/lib/creategamedb.py
blob: ee3a8dd0f307b9f6e8937ecbfb725b3e633c0836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# Murder in the Park - A Robotic Mystery
# Copyright (C) 2008 Jeffrey Armstrong (a.k.a. PrintStar)
# http://jeff.rainbow-100.com/
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
# A full version of the license terms is available in LICENSE.

from gamedb import *
import sqlite3
import time

class HoldRoom:
    def __init__(self,id):
        self.name = 'empty'
        self.description = 'empty'
        self.away_description = None
        self.id = id
        self.n = None
        self.s = None
        self.e = None
        self.w = None
        self.u = None
        self.d = None
        
    def getTuple(self):
        return (self.id,self.name,self.description,self.away_description,self.n,self.s,self.e,self.w,self.u,self.d)

def conjugate(dir):
    if dir=="north":
        return "south"
    elif dir=="south":
        return "north"
    elif dir=="east":
        return "west"
    elif dir=="west":
        return "east"
    elif dir=="up":
        return "down"
    elif dir=="down":
        return "up"

class CreateDB(GameDB):
    
    def __init__(self):
        GameDB.__init__(self,inplace=True)
        
        self.con.row_factory = sqlite3.Row
        
        cur = self.con.cursor()
        cur.execute('create table if not exists Rooms(id integer unique not null, name text not null, description text not null, away_description text, north integer, south integer, east integer, west integer, up integer, down integer)')
        cur.execute('create table if not exists RoomContents(objectid integer not null, roomid integer not null)')
        cur.execute('create table if not exists Objects(id integer unique not null, name text not null, description text not null, moveable integer, everywhere integer, person integer not null, presence text, explicit integer not null default 1)')
        cur.execute('create table if not exists Combine(id1 integer not null, id2 integer, verb text, result text not null, action text, state integer not null default 0)')
        cur.execute('create table if not exists NPC(id integer unique not null, state integer not null, max integer not null default 1)')
        cur.execute('create table if not exists Talk(id integer not null, verb text, object integer, result text not null)')
        cur.execute('create table if not exists Inventory(id integer unique not null)')
        cur.execute('create table if not exists State(name text unique not null, value text)')
        cur.execute('create table if not exists Block(id integer not null, room1 integer not null, room2 integer not null, description text not null, state integer not null default 1)')
        cur.execute('create table if not exists Synonyms(spoken text not null,name text not null)')
        cur.execute('create table if not exists Verbonyms(spoken text not null,name text not null)')
                
        self.lasttime = 0
        if self.getStart():
            self.getRoom(self.getStart())
        else:
            self.newRoom()

    def getDirectionId(self,dir):
        if dir=="north":
            return self.template.n
        elif dir=="south":
            return self.template.s
        elif dir=="east":
            return self.template.e
        elif dir=="west":
            return self.template.w
        elif dir=="up":
            return self.template.u
        elif dir=="down":
            return self.template.d
        
    def move(self,dir):
        self.saveRoom()
        if self.getDirectionId(dir):
            self.getRoom(self.getDirectionId(dir))
        else:
            originaltemplate = self.template
            self.newRoom()
            self.setId(dir,self.template.id,originaltemplate)
            self.setId(conjugate(dir),originaltemplate.id)
            self.saveRoom(originaltemplate)
            
        self.room = self.template.id
        
        return MOVE_OK
    
    def setId(self,dir,id,template=None):
        if template == None:
            template = self.template
        
        if dir=="north":
            template.n = id
        elif dir=="south":
            template.s = id
        elif dir=="east":
            template.e = id
        elif dir=="west":
            template.w = id
        elif dir=="up":
            template.u = id
        elif dir=="down":
            template.d = id
    
    def getRoomName(self):
        return self.template.name
    
    def getRoomDescription(self):
        return self.template.description
    
    def setRoomName(self,value):
        self.template.name = value
    
    def setRoomDescription(self,value):
        self.template.description = value
        
    def setRoomAwayDescription(self,value):
        if value == 'none':
            self.template.away_description = None
        else:
            self.template.away_description = value
    
    def getNewUID(self):
        uid = int(time.mktime(time.gmtime()))
        if uid == self.lasttime:
            self.lasttime = self.lasttime + 1
        else:
            self.lasttime = uid
            
        return self.lasttime
    
    def newRoom(self):
        self.template = HoldRoom(self.getNewUID())
        
    def saveRoom(self,room=None):
        cur = self.con.cursor()
        if not room:
            room = self.template
        cur.execute("insert or replace into Rooms(id,name,description,away_description,north,south,east,west,up,down) values(?,?,?,?,?,?,?,?,?,?)",
                    room.getTuple())
        self.con.commit()
    
    def getNeighbors(self):
        descriptions = []
        cur = self.con.cursor()
        for id in (self.template.n,self.template.s,self.template.e,self.template.w,self.template.u,self.template.d):
            cur.execute("select name from Rooms where id=?",(id,))
            name = "None"
            for value in cur:
                name = value[0]
                break
            descriptions.append(name)
        
        return descriptions
    
    def object(self,name,desc,presence,glob=False):
        everywhere = False
        if glob:
            everywhere=True
        id = self.getNewUID()
        cur = self.con.cursor()
        cur.execute("insert into Objects(id,name,description,presence,moveable,person,everywhere) values(?,?,?,?,?,?,?)",
                    (id,name,desc,presence,1,0,everywhere))
        cur.execute("insert into RoomContents(objectid,roomid) values(?,?)",(id,self.template.id))
    
    def person(self,name,desc,presence):
        id = self.getNewUID()
        cur = self.con.cursor()
        cur.execute("insert into Objects(id,name,description,presence,moveable,person,everywhere) values(?,?,?,?,?,?,?)",(id,name,desc,presence,0,1,0))
        cur.execute("insert into NPC(id,state,max) values(?,?,?)",(id,0,0))
        cur.execute("insert into RoomContents(objectid,roomid) values(?,?)",(id,self.template.id))
    
    def action(self,name,verb,result,action,state=0):
        objid = self.getObjId(name,False)
        if not objid:
            print "Object not found..."
            return
        cur = self.con.cursor()
        cur.execute("insert into Combine(id1,verb,result,action,state) values(?,?,?,?,?)",
                    (objid,verb,result,action,state))
    
    def synonym(self,name,synonym):
        cur = self.con.cursor()
        cur.execute("insert into Synonyms(spoken,name) values(?,?)",(synonym,name))
    
    def verbonym(self,name,synonym):
        cur = self.con.cursor()
        cur.execute("insert into Verbonyms(spoken,name) values(?,?)",(synonym,name))
    
    def actionCombine(self,name1,name2,verb,result,action,state=0):
        id1 = self.getObjId(name1)
        id2 = self.getObjId(name2)
        if not id1:
            print "Object not found: "+name1
            return
        if not id2:
            print "Object not found: "+name2
            return
        
        cur = self.con.cursor()
        cur.execute("insert into Combine(id1,id2,verb,result,action,state) values(?,?,?,?,?,?)",
                    (id1,id2,verb,result,action,state))
            
    
    def setBlock(self,dir,desc):
        id2 = None
        if dir=="n":
            id2 = self.template.n
        elif dir=="s":
            id2 = self.template.s
        elif dir=="e":
            id2 = self.template.e
        elif dir=="w":
            id2 = self.template.w
        elif dir=="u":
            id2 = self.template.u
        elif dir=="d":
            id2 = self.template.d
        if not id2:
            return
        
        cur = self.con.cursor()
        cur.execute("insert into Block(id,room1,room2,description) values(?,?,?,?)",(self.getNewUID(),self.template.id,id2,desc))
    
    def getRoom(self,id):
        cur = self.con.cursor()
        cur.execute("select * from Rooms where id=?",(id,))
        self.template = None
        for room in cur:
            self.template = HoldRoom(id)
            self.template.name = room["name"]
            self.template.description = room["description"]
            self.template.away_description = room["away_description"]
            self.template.n = room["north"]
            self.template.s = room["south"]
            self.template.e = room["east"]
            self.template.w = room["west"]
            self.template.u = room["up"]
            self.template.d = room["down"]
    
    def setStart(self):
        cur = self.con.cursor()
        cur.execute("insert or replace into State(name, value) values('Start',?)",(str(self.template.id),))
    
    def end(self):
        self.saveRoom()
        self.con.commit()
        self.con.close()