def delete(obj): """ 删除对象 """ conn = DBConnection.getConnection() conn.excute('DELETE FROM ' + obj.__class__.__name__ + ' WHERE id = %s', [obj.id]) return
def saveLogin(self): """ 保存登入 """ self.last_login = currentTime() conn = DBConnection.getConnection() conn.excute("UPDATE account SET lastlogin = %s WHERE id = %s", [time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(self.last_login)), self.id])
def getRange(minLng, maxLng, minLat, maxLat, cnt): """ 得到天梯 """ sql = 'SELECT roleid, longitude, latitude FROM account WHERE longitude >= %s and longitude <= %s and latitude >= %s and latitude <= %s limit %s' conn = DBConnection.getConnection() return conn.query(sql, [minLng, maxLng, minLat, maxLat, cnt])
def db_set_level(roleid, level): """ 设置等级 """ conn = DBConnection.getConnection() sql = "INSERT INTO medal_level (roleid, level) VALUES (%s, %s) ON DUPLICATE KEY UPDATE level = %s" conn.excute(sql, [roleid, level, level])
def saveRoleId(self): """ 保存roleid """ conn = DBConnection.getConnection() self.roleid = self.id conn.excute('UPDATE user SET roleid = %s WHERE id = %s', [self.roleid, self.id])
def installObject(obj, roleid): conn = DBConnection.getConnection() conn.excute('INSERT INTO ' + obj.__class__.__name__ + '(roleid, object) VALUES (%s, %s)', [roleid, json.dumps(obj.getData())]) obj.id = conn.insert_id() obj.roleid = roleid for column in obj.extend_columns: setattr(obj, column['name'], column['value']) return obj.id
def installFacility(obj, name): conn = DBConnection.getConnection() conn.excute('INSERT INTO ' + obj.__class__.__name__ + '(name, object) VALUES (%s, %s)', [name, json.dumps(obj.getData())]) obj.id = conn.insert_id() obj.name = name for column in obj.extend_columns: setattr(obj, column['name'], column['value']) return obj.id
def locate(accountid, longitude, latitude): """ 定位 """ sql = 'UPDATE account SET longitude = %s, latitude = %s WHERE id=%s' conn = DBConnection.getConnection() conn.excute(sql, [longitude, latitude, accountid])
def delete(self): """ 删除 """ object.delete(self) conn = DBConnection.getConnection() if self.accountid: conn.excute('UPDATE account SET roleid = 0 ,nickname = Null, gender = Null, openid = "0", lastlogin = Null, longitude = Null, latitude = Null WHERE id=%s', [self.accountid])
def getRoleid(name): """ 得到roleid """ conn = DBConnection.getConnection() res = conn.query("SELECT * FROM account WHERE nickname = %s", [name]) if len(res) == 1: return res[0][5] return 0
def bind(self, roleid, nickname, gender): """ 绑定roleid """ try: conn = DBConnection.getConnection() conn.excute("UPDATE account SET roleid = %s, nickname = %s, gender = %s WHERE id = %s", [roleid, nickname, gender, self.id]) except IntegrityError: raise DuplicateNickname
def delete(self): """ 删除 """ object.delete(self) conn = DBConnection.getConnection() if self.accountid: conn.excute( 'UPDATE account SET roleid = 0 ,nickname = Null, gender = Null, openid = "0", lastlogin = Null, longitude = Null, latitude = Null WHERE id=%s', [self.accountid])
def db_remove_medal(roleid, medalid, chipnum): """ 移除勋章 """ conn = DBConnection.getConnection() sql = "DELETE FROM medal_holder WHERE roleid = %s AND medalid = %s AND chipnum = %s LIMIT 1" row_count = conn.excute(sql, [roleid, medalid, chipnum]) if row_count == 1: return 1 return row_count
def db_seek_medal_holder(roleid, medalid, chipnum, baseLevel, cnt): """ 寻找勋章持有者 """ conn = DBConnection.getConnection() sql = "SELECT distinct(medal_holder.roleid) FROM medal_holder INNER JOIN medal_level ON medal_holder.roleid = medal_level.roleid WHERE medal_level.level >= %s AND medal_holder.medalid = %s AND medal_holder.chipnum = %s AND medal_holder.roleid <> %s ORDER BY rand() LIMIT %s" res = conn.query(sql, [baseLevel, medalid, chipnum, roleid, cnt]) if len(res) > 0: return res[0] return []
def db_medal_set_protect_time(roleid, addProtectTime): """ 设置勋章保护时间 """ conn = DBConnection.getConnection() sql = "UPDATE medal_level SET shield_time = IFNULL(shield_time, NOW()) + INTERVAL %s SECOND WHERE roleid = %s" row_count = conn.excute(sql, [roleid, addProtectTime]) if row_count == 1: return True return False
def db_remove_medal(roleid, medalid, chipnum): """ 删除勋章 """ conn = DBConnection.getConnection() sql = "DELETE FROM medal_holder WHERE roleid = %s AND medalid = %s AND chipnum = %s LIMIT 1" row_count = conn.excute(sql, [roleid, medalid, chipnum]) if row_count == 1: return 1 return row_count
def db_medal_select_protect_time(roleid): """ 查询保护时间 """ conn = DBConnection.getConnection() sql = "SELECT shield_time FROM medal_level WHERE roleid = %s" res = conn.query(sql, [roleid]) if len(res): return res[0][0] return time_to_str(currentTime())
def installFacility(obj, name): """ 安装设施 """ conn = DBConnection.getConnection() conn.excute('INSERT INTO facility (name, object) VALUES (%s, %s)', [name, json.dumps(obj.getData())]) obj.id = conn.insert_id() obj.name = name for column in obj.extend_columns: setattr(obj, column['name'], column['value']) return obj.id
def db_seek_medal_holder(roleid, medalid, chipnum, baseLevel, cnt): """ 寻找勋章 """ conn = DBConnection.getConnection() sql = "SELECT distinct(medal_holder.roleid) FROM medal_holder INNER JOIN medal_level ON medal_holder.roleid = medal_level.roleid WHERE medal_level.level >= %s AND medal_holder.medalid = %s AND medal_holder.chipnum = %s AND medal_holder.roleid <> %s AND IFNULL(medal_level.shield_time, NOW()) <= NOW() ORDER BY rand() LIMIT %s" res = conn.query(sql, [baseLevel, medalid, chipnum, roleid, cnt]) if len(res) < cnt: res1 = conn.query(sql, [0, medalid, chipnum, roleid, cnt]) if res1: res = res + res1 return list(set([n[0] for n in res]))
def db_add_medal(roleid, level, medalid, chipnum, cnt): """ 添加勋章 """ conn = DBConnection.getConnection() conn.star_transaction() sql = "INSERT INTO medal_holder (roleid, medalid, chipnum) VALUES (%s, %s, %s)" for i in range(cnt): conn.excute_no_commit(sql, [roleid, medalid, chipnum]) sql = "INSERT INTO medal_level (roleid, level) VALUES (%s, %s) ON DUPLICATE KEY UPDATE level = %s" conn.excute_no_commit(sql, [roleid, level, level]) conn.commit()
def db_delete_medal(roleid, medalid, chipnum, cnt): """ 删除勋章 """ conn = DBConnection.getConnection() sql = "DELETE FROM medal_holder WHERE roleid = %s AND medalid = %s AND chipnum = %s LIMIT %s" row_count = conn.excute(sql, [roleid, medalid, chipnum, cnt]) if row_count == cnt: conn.commit() return row_count conn.rollback() return 0
def save(obj): conn = DBConnection.getConnection() data = obj.getData() dumpstr = json.dumps(data) update_columns = ['object = %s'] update_value = [dumpstr] for column in obj.extend_columns: update_columns.append(column['name'] + ' = %s') update_value.append(getattr(obj, column['name'])) sql = 'UPDATE ' + obj.__class__.__name__ + ' SET ' + ', '.join(update_columns) + ' WHERE id = %s' update_value.append(obj.id) conn.excute(sql, update_value)
def makeUserAndBind(self, nickname, gender): conn = DBConnection.getConnection() usr = self.userObject() usr.init(self) usr.last_login = self.last_login usr.name = nickname usr.gender = gender usr.install(0) self.roleid = usr.id self.bind(usr.id, nickname, gender) usr.saveRoleId() usr.onInit() return usr
def getFacility(tp, name): conn = DBConnection.getConnection() res = conn.query('SELECT * FROM ' + tp.__name__ + ' WHERE name = %s', [name]) if len(res) == 1: obj = tp() obj.id = res[0][0] obj.name = res[0][1] obj.load(name, json.loads(res[0][2])) i = 0 for column in obj.extend_columns: setattr(obj, column['name'], res[0][3 + i]) i = i + 1 return obj return None
def getObject(tp, roleid): conn = DBConnection.getConnection() res = conn.query('SELECT * FROM ' + tp.__name__ + ' WHERE roleid = %s', [roleid]) if len(res) == 1: obj = tp() obj.id = res[0][0] obj.roleid = res[0][1] obj.load(roleid, json.loads(res[0][2])) i = 0 for column in obj.extend_columns: setattr(obj, column['name'], res[0][3 + i]) i = i + 1 return obj return None
def db_medal_levelup(roleid, medalid, chipcnt): """ 勋章升级 """ conn = DBConnection.getConnection() conn.star_transaction() sql = "DELETE FROM medal_holder WHERE roleid = %s AND medalid = %s AND chipnum = %s LIMIT 1" for i in range(chipcnt): rc = conn.excute_no_commit(sql, [roleid, medalid, i]) if rc != 1: conn.rollback() return {'msg': 'medal_levelup_fail'} conn.commit() return {}
def installObject(obj, roleid): """ 安装对象 """ conn = DBConnection.getConnection() conn.excute( 'INSERT INTO ' + obj.__class__.__name__ + '(roleid, object) VALUES (%s, %s)', [roleid, json.dumps(obj.getData())]) obj.id = conn.insert_id() obj.roleid = roleid for column in obj.extend_columns: setattr(obj, column['name'], column['value']) return obj.id
def get(cls, accid): conn = DBConnection.getConnection() res = conn.query("SELECT * FROM account WHERE id = %s ", [accid]) if len(res) == 1: acc = cls.accountObject() acc.id = res[0][0] acc.username = res[0][1] acc.nickname = res[0][3] acc.gender = res[0][4] acc.roleid = res[0][5] acc.opendid = res[0][6] acc.saveLogin() return acc return None
def db_medal_levelup( roleid, medalid, chipcnt): """ 勋章升级 """ conn = DBConnection.getConnection() conn.star_transaction() sql = "DELETE FROM medal_holder WHERE roleid = %s AND medalid = %s AND chipnum = %s LIMIT 1" for i in range(chipcnt): rc = conn.excute_no_commit(sql, [roleid, medalid, i]) if rc != 1: conn.rollback() return conn.commit()
def saveFacility(obj): """ 保存设施 """ conn = DBConnection.getConnection() data = obj.getData() dumpstr = json.dumps(data) update_columns = ['object = %s'] update_value = [dumpstr] for column in obj.extend_columns: update_columns.append(column['name'] + ' = %s') update_value.append(getattr(obj, column['name'])) sql = 'UPDATE facility SET ' + ', '.join( update_columns) + ' WHERE id = %s' update_value.append(obj.id) conn.excute(sql, update_value)
def login(request): if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] conn = DBConnection.getConnection() res = conn.query("SELECT * FROM admin WHERE username = %s AND password = %s", [username, password]) if len(res) == 1: return render(request, 'index.html', {}) else: return HttpResponse(res) else: return render(request, 'login.html', {})
def get_by_account_name(cls, name) : """ 拿取帐号名 """ conn = DBConnection.getConnection() res = conn.query("SELECT * FROM account WHERE accountname = %s ", [name]) if len(res) == 1: acc = cls.accountObject() acc.id = res[0][0] acc.username = res[0][1] acc.nickname = res[0][3] acc.gender = res[0][4] acc.roleid = res[0][5] acc.openid = res[0][6] return acc return None
def new(cls, accountName, password): try: sql = "INSERT INTO account (email, password) VALUES (%s, %s)" conn = DBConnection.getConnection() conn.excute(sql, [accountName, password]) acc = cls.accountObject() acc.id = conn.insert_id() acc.username = accountName acc.nickname = '' acc.roleid = 0 acc.opendid = 0 acc.saveLogin() except IntegrityError: return {'msg':'account_already_exist'} return {'account_name':accountName}
def get(cls, accid): """ 拿取帐号 """ conn = DBConnection.getConnection() res = conn.query("SELECT * FROM account WHERE id = %s ", [accid]) if len(res) == 1: acc = cls.accountObject() acc.id = res[0][0] acc.username = res[0][1] acc.nickname = res[0][3] acc.gender = res[0][4] acc.roleid = res[0][5] acc.openid = res[0][6] acc.saveLogin() return acc return None
def getFacility(tp, name): """ 得到设施 """ conn = DBConnection.getConnection() res = conn.query('SELECT * FROM facility WHERE name = %s', [name]) if len(res) == 1: obj = tp() obj.id = res[0][0] obj.name = res[0][1] obj.load(name, json.loads(res[0][2])) i = 0 for column in obj.extend_columns: setattr(obj, column['name'], res[0][3 + i]) i = i + 1 return obj return None
def login(request): if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] conn = DBConnection.getConnection() res = conn.query( "SELECT * FROM admin WHERE username = %s AND password = %s", [username, password]) if len(res) == 1: return render(request, 'index.html', {}) else: return HttpResponse(res) else: return render(request, 'login.html', {})
def login(cls,usrname, password): """ 登入 """ conn = DBConnection.getConnection() res = conn.query("SELECT * FROM account WHERE accountname = %s AND password = %s", [usrname, password]) if len(res) == 1: acc = cls.accountObject() acc.id = res[0][0] acc.username = res[0][1] acc.nickname = res[0][3] acc.gender = res[0][4] acc.roleid = res[0][5] acc.openid = res[0][6] acc.longitude = res[0][8] acc.latitude = res[0][9] acc.saveLogin() return acc return None
def getVolunteer(self): usr = self.user excludeRoleids = usr.friends.keys() excludeRoleids.extend(self.reinforced_list) conn = DBConnection.getConnection() sql = '' if len(excludeRoleids) == 0: sql = "SELECT * FROM user WHERE roleid ORDER BY RAND() LIMIT 3" else: sql = "SELECT * FROM user WHERE roleid NOT IN (" + ','.join(excludeRoleids) + ") ORDER BY RAND() LIMIT 3" res = conn.query(sql, []) data = [] for record in res: if record[0] == usr.roleid: continue vol = usr.__class__() vol.load(record[0], gcjson.loads(record[2])) data.append(vol.getFriendData()) return data
def new(cls, accountName, password): """ 新帐号 """ try: sql = "INSERT INTO account (accountname, password) VALUES (%s, %s)" conn = DBConnection.getConnection() conn.excute(sql, [accountName, password]) acc = cls.accountObject() acc.id = conn.insert_id() acc.username = accountName acc.nickname = '' acc.roleid = 0 acc.openid = 0 acc.saveLogin() except IntegrityError: return {'msg':'account_already_exist'} return {'account_name':accountName}
def scene(request): """ 场影中玩家 """ idx = int(request.GET['index']) sql = 'SELECT roleid from account WHERE roleid <> 0 ORDER BY lastlogin, gender LIMIT %s, %s' conn = DBConnection.getConnection() gameConf = config.getConfig('game') data = {} data['player'] = [] res = conn.query(sql, [idx, gameConf['scene_player_count']]) for rid in res: try: u = user.get(rid[0]) if u: data['player'].append(u.getSceneData()) except: pass return data
def getObject(tp, roleid): """ 得到对象 """ conn = DBConnection.getConnection() res = conn.query('SELECT * FROM ' + tp.__name__ + ' WHERE roleid = %s', [roleid]) if len(res) == 1: obj = tp() obj.id = res[0][0] obj.roleid = res[0][1] try: obj.load(roleid, json.loads(res[0][2])) except: pass i = 0 for column in obj.extend_columns: setattr(obj, column['name'], res[0][3 + i]) i = i + 1 return obj return None
def recommend_friend(usr): """ 推荐好友 """ nw = usr.getNetwork() fl = nw.friend.keys() fl.extend(nw.request_list.keys()) fl.append('0') sql = '' if usr.gender == 'male': sql = 'SELECT roleid FROM account WHERE roleid not in ' + '(' + ( ','.join(fl)) + ')' + ' ORDER BY lastlogin, gender LIMIT 1' else: sql = 'SELECT roleid FROM account WHERE roleid not in ' + '(' + ( ','.join(fl) ) + ')' + ' ORDER BY lastlogin, gender DESC LIMIT 1' conn = DBConnection.getConnection() res = conn.query(sql, []) if res: friend = usr.__class__.get(res[0][0]) return friend.getFriendData() return {}
def getVolunteer(self): """ 得到义军 """ usr = self.user excludeRoleids = usr.friends.keys() excludeRoleids.extend(self.reinforced_list) conn = DBConnection.getConnection() sql = '' if len(excludeRoleids) == 0: sql = "SELECT * FROM user WHERE roleid ORDER BY RAND() LIMIT 3" else: sql = "SELECT * FROM user WHERE roleid NOT IN (" + ','.join( excludeRoleids) + ") ORDER BY RAND() LIMIT 3" res = conn.query(sql, []) data = [] for record in res: if record[0] == usr.roleid: continue vol = usr.__class__() vol.load(record[0], gcjson.loads(record[2])) data.append(vol.getFriendData()) return data
def saveRoleId(self): conn = DBConnection.getConnection() self.roleid = self.id conn.excute("UPDATE user SET roleid = %s WHERE id = %s", [self.roleid, self.id])
def bind(self, roleid, nickname, gender): conn = DBConnection.getConnection() conn.excute("UPDATE account SET roleid = %s, nickname = %s, gender = %s WHERE id = %s", [roleid, nickname, gender, self.id])