def register(self, username = None, password = None): if username == None or password == None: msg = "User.Control.register needs both a username and a password" print msg return (-1,msg) #first we have to check if a user with that name already exists c = db.userData.columns statment = db.select([db.userData], (c.username == username)) conn = db.connect() result = conn.execute(statment) row = result.fetchone() result.close() conn.close() if row != None: self.valid = False self.loggedIn = False msg = "Player with username: %s already exists" % (username) print msg return (-1, msg) else: conn = db.connect() ins = db.userData.insert().values(username = username, password = password) result = conn.execute(ins) conn.close() #cheap way for now return self.login(username, password)
def login(self, username = None, password = None): if username == None or password == None: msg = "user.Control.login needs both a username and a password" print msg return (-1, msg) c = db.userData.columns statment = db.select([db.userData], (c.username == username) & (c.password == password)) conn = db.connect() result = conn.execute(statment) row = result.fetchone() result.close() conn.close() if row == None: self.loggedIn = False msg = "User failed to login with username: %s password: %s" % (username, password) print msg return (-1, msg) else: self.loggedIn = True self.userId = row['userId'] self.pullData() return (self.userId, 'Ok')
def pullWorldData(self, worldId=None, callback=None): conn = db.connect() tmp = khopeshpy.world.Model(conn) tmp.pullData() conn.close() return self.jsonpWrap(tmp.data, callback)
def getImage(self, zoom=None, x=None, y=None, z=None, callback=None): try: # can't do anything if they don't send the full address if zoom == None or x == None or y == None or z == None: msg = "model-server.getImage: Need full parameters" raise khopeshpy.exceptions.JSONException(msg) # first check that there really isn't an image imageName = "img-world/zoom%d-%dx-%dy-%dz.png" % (int(zoom), int(x), int(y), int(z)) if os.path.isfile(imageName): # file already exists, either it was created by another handler or somebody's mucking around # just return the image name return self.jsonpWrap(imageName, callback) conn = db.connect() # otherwise we have to pull it from the database if int(zoom) == 0: c = db.levelData.columns statment = db.select( [c.baseImage], (c.cellIdX == int(x)) & (c.cellIdY == int(y)) & (c.levelIdZ == int(z)) ) result = conn.execute(statment) level = result.fetchone() result.close() if level == None: ret = "img-src/void.png" else: ret = level["baseImage"] elif int(zoom) == 1: c = db.cellData.columns statment = db.select([c.cellImage], (c.cellIdX == int(x)) & (c.cellIdY == int(y))) result = conn.execute(statment) cell = result.fetchone() result.close() if cell == None: ret = "img-src/void.png" else: ret = cell["cellImage"] else: # note: all zoomed images above level 1 should have been pregenerated ret = "img-src/void.png" conn.close() return self.jsonpWrap(ret, callback) except khopeshpy.exceptions.JSONException as e: return self.errorHandler(e.msg)
def pullData(self): #NOTE: selecting old family id's may be a problem if self.familyId > 0: c = db.familyData.columns statment = db.select([db.familyData], c.familyId == self.familyId) conn = db.connect() result = conn.execute(statment) row = result.fetchone() result.close() conn.close() if row == None: msg = "No record found for a family with familyId: %d" % (self.familyId) raise Exception(msg) self.data.update(dict(row)) self.valid = True self.userId = self.data['userId'] elif self.userId > 0: c = db.familyData.columns statment = db.select([db.familyData], (c.userId == self.userId) & (c.state == States.ALIVE)) conn = db.connect() result = conn.execute(statment) row = result.fetchone() result.close() conn.close() if row == None: msg = "No record found for a living family for userId: %d" % (self.userId) raise Exception(msg) self.data.update(dict(row)) self.valid = True self.familyId = self.data['familyId'] else: msg = "Family objects must be initialized with a valid a userId or a valid familyId" raise Exception(msg)
def getPlayerList(self, callback=None): c = db.userData.columns statment = db.select([c.userId, c.username], 1) conn = db.connect() result = conn.execute(statment) rows = result.fetchall() result.close() conn.close() ret = {} for row in rows: ret[row.userId] = row.userName return self.jsonpWrap(ret, callback)
def pullData(self): if self.userId <= 0: msg = "pullData needs a userId greater than 0, id was: %d" % (self.userId) raise Exception(msg) c = db.userData.columns statment = db.select([db.userData], c.userId == self.userId) conn = db.connect() result = conn.execute(statment) row = result.fetchone() result.close() conn.close() if row == None: msg = "pullData did not find userData record for id: %d" % (self.userId) raise Exception(msg) self.data.update(dict(row)) self.valid = True
def getUpdate(self, objType=None, objId=None, callback=None): try: if objType == None or objId == None: msg = "model-server.getUpdate: ObjType or ObjId not passed" raise khopeshpy.exceptions.JSONException(msg) conn = db.connect() if objType == "UserModel": tmp = khopeshpy.user.Model(objId, conn) elif objType == "WorldModel": tmp = khopeshpy.world.Model(conn) else: msg = "model-server.getUpdate: Unknown object type" raise khopeshpy.exceptions.JSONException(msg) tmp.pullData() conn.close() return self.jsonpWrap(tmp.data, callback) except khopeshpy.exceptions.JSONException as e: return self.errorHandler(e.msg)
world.saveAll() print 'finished' print 'setDefaults...', world.setDefaults() print 'finished' trans.commit() except: trans.rollback() print 'Could not commit changes to database' raise if __name__=='__main__': conn = db.connect() random.seed() world = khopeshpy.world.Setup(conn) if len(sys.argv) == 1: setup(world, conn) world.genZoom() elif len(sys.argv) == 2 and sys.argv[1] == "db": setup(world, conn) elif len(sys.argv) == 2 and sys.argv[1] == "zoom": world.pullData() world.pullCells() world.genZoom()
def __init__(self): self.conn = db.connect()