def _updateUserGameDataAuthorTime(userId, gameId): ctfull = timestamp.formatTimeMs() daobase.sendUserCmd( userId, 'HSET', daoconst.HKEY_GAMEDATA + str(gameId) + ':' + str(userId), 'authorTime', ctfull) return ctfull
def _setWeakData(userId, gameId, weakname, datas, cycleName, curCycle, expire): dataKey = UserWeakSchema.mkey(cycleName, weakname, gameId, userId) if datas: datas['_cycle_'] = curCycle if datas: ret = daobase.sendUserCmd(userId, 'SETEX', dataKey, expire, strutil.dumps(datas)) else: ret = daobase.sendUserCmd(userId, 'DEL', dataKey) return ret
def _setGameLeave(userId, gameId): ''' 设置用户离开一个游戏 通常再leave_game时调用此方法 数据库中, 存储的键值为: og:<userId> ''' daobase.sendUserCmd(userId, 'HSET', UserSessionSchema.mkey(userId), UserSessionSchema.LAST_GAME, 9999) daobase.sendUserCmd(userId, 'SREM', UserOnlineGameSchema.mkey(userId), gameId)
def _setGameEnter(userId, gameId): ''' 设置用户进入一个游戏 通常再bind_game时调用此方法 数据库中, 存储的键值为: og:<userId> ''' daobase.sendUserCmd(userId, 'SADD', UserOnlineGameSchema.mkey(userId), gameId) daobase.sendUserCmd(userId, 'HSET', UserSessionSchema.mkey(userId), UserSessionSchema.LAST_GAME, gameId)
def _setPlayTimeStop(userId, roomId, tableId): ptkey = UserPlayTimeSchema.mkey(userId) subkey = 'R.' + str(roomId) + '.' + str(tableId) st = daobase.executeUserCmd(userId, 'HGET', ptkey, subkey) if isinstance(st, int): dt = int(time()) - st if dt > 10 and dt < 86400: # 如何判定loc的时间变化是一个有效值? 桌子上最少待10秒, 最大不可能超过1天 # 增加游戏时间 _writePlayTime_(userId, dt) daobase.sendUserCmd(userId, 'HDEL', ptkey, subkey) daobase.sendUserCmd(userId, 'EXPIRE', ptkey, 86400)
def save(cls, userId, pluginId, deadline, notice, extparam): mainkey = cls.MAINKEY.format(userId) subvalue = { 'pluginId': pluginId, 'deadline': deadline, 'notice': notice } if extparam: subvalue['extparam'] = extparam subkey = daobase.executeUserCmd(userId, 'HINCRBY', mainkey, 'maxid', 1) daobase.sendUserCmd(userId, 'HSET', mainkey, subkey, strutil.dumps(subvalue)) return subkey
def _removeOnlineLoc(userId, roomId, tableId): ''' 移除一个用户的在线位置 通常此方法在用户真实离开某一个桌位后调用 ''' # 游戏时长计算, 1天后自动过期 subkey = 'R.' + str(roomId) + '.' + str(tableId) if ftlog.is_debug(): ftlog.debug('dbuser._removeOnlineLoc userId=', userId, 'roomId=', roomId, 'tableId=', tableId) daobase.sendUserCmd(userId, 'HDEL', UserLocationSchema.mkey(userId), subkey) dbplaytime._setPlayTimeStop(userId, roomId, tableId)
def _delTaskData(userId, gameId, taskId): ''' 删除用户的一个道具的数据 ''' dataKey = GameTaskSchema.mkey(gameId, userId) data = daobase.sendUserCmd(userId, 'HDEL', dataKey, taskId) return data
def _setTaskData(userId, gameId, taskId, taskData): ''' 设置用户的一个道具的数据 ''' dataKey = GameTaskSchema.mkey(gameId, userId) data = daobase.sendUserCmd(userId, 'HSET', dataKey, taskId, taskData) return data
def _delItemData(userId, gameId, itemId): ''' 删除用户的一个道具的数据 ''' dataKey = GameItemSchema.mkey(gameId, userId) data = daobase.sendUserCmd(userId, 'HDEL', dataKey, itemId) return data
def _setItemData(userId, gameId, itemId, itemData): ''' 设置用户的一个道具的数据 ''' dataKey = GameItemSchema.mkey(gameId, userId) data = daobase.sendUserCmd(userId, 'HSET', dataKey, itemId, itemData) return data
def load(cls, userId): mainkey = cls.MAINKEY.format(userId) kvlist = daobase.executeUserCmd(userId, 'HGETALL', mainkey) ret = {} dels = [] curtime = timestamp.getCurrentTimestamp() for idx in xrange(1, len(kvlist), 2): k = kvlist[idx - 1] if k == 'maxid': continue v = kvlist[idx] v = strutil.loads(v) if v['deadline'] <= curtime: dels.append(k) else: ret[k] = v if dels: daobase.sendUserCmd(userId, 'HDEL', mainkey, *dels) return ret
def _writePlayTime_(userId, detalTime): try: mkey = 'gamedata:9999:%d' % (userId) daobase.sendUserCmd(userId, 'HINCRBY', mkey, 'totaltime', detalTime) datas = daobase.executeUserCmd(userId, 'HGET', mkey, 'todaytime') datas = strutil.loads(datas, ignoreException=True, execptionValue={}) today = datetime.now().strftime('%Y%m%d')[-6:] if today in datas: datas[today] += detalTime else: datas[today] = detalTime oldday = (datetime.now() - timedelta(days=7)).strftime('%Y%m%d')[-6:] for k in datas.keys()[:]: if k < oldday: del datas[k] daobase.sendUserCmd(userId, 'HSET', mkey, 'todaytime', strutil.dumps(datas)) except: ftlog.error()
def _setWeakData(userId, gameId, weakname, datas, cycleName, curCycle, expire): """ 设置弱数据信息 :param userId: 玩家Id :param gameId: 游戏Id :param weakname: 设置数据key :param datas: 数据 :param cycleName: day、 week、 mothon :param curCycle: 200630 2002 2006 :param expire: 游戏期时长 :return: """ dataKey = UserWeakSchema.mkey(cycleName, weakname, gameId, userId) if datas: datas['_cycle_'] = curCycle if datas: ret = daobase.sendUserCmd(userId, 'SETEX', dataKey, expire, strutil.dumps(datas)) else: ret = daobase.sendUserCmd(userId, 'DEL', dataKey) return ret
def _setOnlineState(userId, state): ''' 设置用户的在线状态,即TCP的链接状态 用户ID将添加再online数据库的online:users集合 注意: 此方法通常由CONN服务进行调用,其他人禁止调用 ''' if state == daoconst.OFFLINE: dbplaytime._cleanalltime(userId) daobase.sendUserCmd(userId, 'DEL', UserOnlineGameSchema.mkey(userId)) daobase.sendUserCmd(userId, 'HDEL', UserSessionSchema.mkey(userId), UserSessionSchema.ONLINE_STATE) daobase.sendUserCmd(userId, 'HDEL', UserSessionSchema.mkey(userId), UserSessionSchema.LAST_GAME) else: daobase.sendUserCmd(userId, 'HSET', UserSessionSchema.mkey(userId), UserSessionSchema.ONLINE_STATE, daoconst.ONLINE) dbonline._setOnlineState(userId, state)
def _addOnlineLoc(userId, roomId, tableId, seatId, checkConfict): ''' 添加一个用户的在线位置, 注意: 子键值为roomId+'.'+tableId, 因此不允许用户再同一个桌子的不同桌位坐下 通常此方法在用户真实坐在某一个桌位后调用 ''' if checkConfict: onlineSeatId = _getOnlineLocSeatId(userId, roomId, tableId) assert not onlineSeatId or onlineSeatId == seatId # 设置游戏时长记录, 1天后自动过期 dbplaytime._setPlayTimeStart(userId, roomId, tableId) if ftlog.is_debug(): ftlog.debug('dbuser._addOnlineLoc userId=', userId, 'roomId=', roomId, 'tableId=', tableId, 'seatId=', seatId, 'checkConfict=', checkConfict) return daobase.sendUserCmd(userId, 'HSET', UserLocationSchema.mkey(userId), 'R.' + str(roomId) + '.' + str(tableId), seatId)
def _setSessionDatas(userId, datas): # TODO 补充user表中的几个数据至用户的session中sessionAppId, sessionDevId,city_code atts = [ UserDataSchema.SESSION_APPID, UserDataSchema.SESSION_DEVID, UserDataSchema.SESSION_CITY_CODE, UserDataSchema.SESSION_IP ] values = daobase.executeUserCmd(userId, 'HMGET', UserDataSchema.mkey(userId), *atts) values = UserDataSchema.checkDataList(atts, values, None) if not UserSessionSchema.APPID in datas: datas[UserSessionSchema.APPID] = values[0] if not UserSessionSchema.DEVICEID in datas: datas[UserSessionSchema.DEVICEID] = values[1] if not UserSessionSchema.CITYCODE in datas: datas[UserSessionSchema.CITYCODE] = values[2] datas[UserSessionSchema.IPADDRESS] = values[ 3] # TODO 加了阿里云代理后,CO服务的带的IP都是阿里的IP地址,只能在重SDK的数据中再取一次 dataKey = UserSessionSchema.mkey(userId) params = UserSessionSchema.paramsDict2List(datas) ret = daobase.sendUserCmd(userId, 'HMSET', dataKey, *params) return ret
def _updateUserDataAliveTime(userId): ctfull = timestamp.formatTimeMs() daobase.sendUserCmd(userId, 'HSET', daoconst.HKEY_USERDATA + str(userId), 'aliveTime', ctfull) return ctfull
def _setPlayTimeStart(userId, roomId, tableId): daobase.sendUserCmd(userId, 'HSET', UserPlayTimeSchema.mkey(userId), 'R.' + str(roomId) + '.' + str(tableId), int(time())) daobase.sendUserCmd(userId, 'EXPIRE', UserPlayTimeSchema.mkey(userId), 86400)
def _cleanalltime(userId): daobase.sendUserCmd(userId, 'DEL', UserPlayTimeSchema.mkey(userId))
def _cleanOnlineLoc(userId): ''' 移除一个用户的所有在线位置 ''' dbplaytime._cleanalltime(userId) daobase.sendUserCmd(userId, 'DEL', UserLocationSchema.mkey(userId))