def ImpGetUserSession(conn, job): dictRet = { dbs_def.FLAG: True, } szAuthKey = job.GetParam() sql = "select `SESSION_ID` FROM `account` WHERE `ACCOUNT_ID` = '%s'" % (szAuthKey) ret = dbs_common.SyncQueryTrans(EDbsOptType.eQuery, conn, sql) if ret is None: dictRet[dbs_def.FLAG] = False db_mgr.OnOneDbQueryDone(dictRet, job) return if len(ret) == 0: session_id = idmgr_.GenPlayerID(conn) sql = "INSERT INTO `account` VALUES('%s', '%s', now(), now())" % (szAuthKey, session_id) job.SetSession(session_id) ret = dbs_common.SyncQueryTrans(EDbsOptType.eInsert, conn, sql) if ret is None: ffext.ERROR('create session err %s' % (sql)) dictRet[dbs_def.FLAG] = False db_mgr.OnOneDbQueryDone(dictRet, job) return dictRet[dbs_def.RESULT] = session_id else: dictRet[dbs_def.RESULT] = ret[0][0] db_mgr.OnOneDbQueryDone(dictRet, job)
def init(self): self.db = ffext.ffdb_create(self.db_host) if None == self.db: print("数据库连接出错") return False ret = self.db.sync_query( "SELECT `AUTO_INC_ID`, `RUNING_FLAG` FROM `id_generator` WHERE `TYPE` = '%d' AND `SERVER_ID` = '%d'" % (self.type_id, self.server_id)) #print(ret.flag, ret.result, ret.column) if len(ret.result) == 0: #数据库中还没有这一行,插入 self.db.sync_query( "INSERT INTO `id_generator` SET `AUTO_INC_ID` = '0',`TYPE` = '%d', `SERVER_ID` = '%d', `RUNING_FLAG` = '1' " % (self.type_id, self.server_id)) return True else: self.auto_inc_id = int(ret.result[0][0]) self.runing_flag = int(ret.result[0][1]) if self.runing_flag != 0: self.auto_inc_id += 10000 ffext.ERROR('last idgen shut down not ok, inc 10000') self.db.sync_query( "UPDATE `id_generator` SET `RUNING_FLAG` = '1' WHERE `TYPE` = '%d' AND `SERVER_ID` = '%d'" % (self.type_id, self.server_id)) #if self.auto_inc_id < 65535: # self.auto_inc_id = 65535 return True
def verify_password(player): sql = "select `ID` FROM `player_register` WHERE `NAME` = '%s' AND `PASSWORD` = '%s'" % ( player.nick_name, player.password) ret = get_sync_db().sync_query(sql) if len(ret.result) == 0: ffext.ERROR('verify_password无此账号[%s]' % (sql)) return False player.set_id(int(ret.result[0][0])) return True
def ImpDbsLoadPlayerData(conn, job): dictSerial = { dbs_def.FLAG: True, } session = job.GetSession() sql = "SELECT DATA_INFO_BASE, DATA_INFO FROM `player` WHERE `SESSION_ID` = '%s'" % (session) ret = dbs_common.SyncQueryTrans(EDbsOptType.eQuery, conn, sql) if ret is None: ffext.ERROR('load_player载入数据出错%s' % (sql)) dictSerial[dbs_def.FLAG] = False db_mgr.OnOneDbQueryDone(dictSerial, job) return if len(ret) == 0: dictPlayerInfo = { table_property_def.Player.SESSION_ID: session, table_property_def.Player.NAME: "name_" + str(session), table_property_def.Player.SEX: session % 2, table_property_def.Player.CREATE_TIME: int(time.time()), } szDataInfo = json.dumps(dictPlayerInfo) szExtraInfo = json.dumps({table_property_def.Player.MONEY_LIST: []}) sql = "INSERT INTO `player` VALUES('%s', '%s', '%s')" % (session, szDataInfo, szExtraInfo) ret = dbs_common.SyncQueryTrans(EDbsOptType.eInsert, conn, sql) if ret is None: ffext.ERROR('load_player载入数据出错%s' % (sql)) dictSerial[dbs_def.FLAG] = False db_mgr.OnOneDbQueryDone(dictSerial, job) return else: szPlayerInfoBase = ret[0][0] szPlayerInfoExtra = ret[0][1] dictPlayerInfo = json.loads(szPlayerInfoBase) dictTmp = json.loads(szPlayerInfoExtra) util.dict_merge(dictTmp, dictPlayerInfo) dictSerial[dbs_def.RESULT] = dictPlayerInfo db_mgr.OnOneDbQueryDone(dictSerial, job)
def SyncQueryTrans(nType, conn, sql): try: cur = conn.cursor() ret = cur.execute(sql) if nType == EDbsOptType.eQuery: ret = cur.fetchmany(ret) cur.close() conn.commit() return ret except MySQLdb.Error, e: conn.rollback() try: sqlError = "Error %d:%s" % (e.args[0], e.args[1]) except IndexError: sqlError = "MySQL Error:%s" % str(e) ffext.ERROR(sqlError) return None
def RetGetIDAutoInc(self, dbRet): print("RetGetIDAutoInc ", dbRet) assert dbRet[dbs_def.FLAG] is True nAutoIncId, nRunningFlag = dbRet[dbs_def.RESULT] self.auto_inc_id = nAutoIncId self.runing_flag = nRunningFlag if self.runing_flag != 0: self.auto_inc_id += 50 ffext.ERROR('last idgen shut down not ok, inc 50') if self.type_id == EIdType.eIdTypeRoom: if self.auto_inc_id < 100000: self.auto_inc_id = 100000 else: if self.auto_inc_id < 100: self.auto_inc_id = 100 self.m_bInit = True
def cb(ret): if ret.flag == False or len(ret.result) == 0: ffext.ERROR('load_player载入数据出错%s' % (sql)) return ret.dump() callback()