def _getTcTemplates(moduleKey, funDecode): # 取得redis中的所有原始数据 datas = {} xkey = 'game:%s:' + moduleKey + ':' + CLIENT_ID_TEMPLATE for gid in getConfigGameIds(): subdata = ftcon.getConfNoCache('GET', xkey % (gid)) if subdata: subdata = strutil.loads(subdata) for k, d in subdata.items(): if not k in datas: if isinstance(d, dict): datas[k] = {} else: datas[k] = [] if isinstance(d, dict): datas[k].update(d) elif isinstance(d, list): datas[k].extend(d) else: datas[k] = d datas = strutil.replaceObjRefValue(datas) if funDecode: cachedata = funDecode(moduleKey, datas) assert (isinstance(cachedata, dict)), 'the cache templates data must be a dict !' + str( moduleKey) + ' funDecode=' + str(funDecode) else: cachedata = datas['templates'] _templatesCache[moduleKey] = cachedata return cachedata
def _getTcTemplates(moduleKey, funDecode): # 取得redis中的所有原始数据 datas = {} xkey = 'game:%s:' + moduleKey + ':' + CLIENT_ID_TEMPLATE for gid in getConfigGameIds(): subdata = ftcon.getConfNoCache('GET', xkey % (gid)) if subdata: subdata = strutil.loads(subdata) for k, d in subdata.items(): if not k in datas: if isinstance(d, dict): datas[k] = {} else: datas[k] = [] if isinstance(d, dict): datas[k].update(d) elif isinstance(d, list): datas[k].extend(d) else: datas[k] = d datas = strutil.replaceObjRefValue(datas) if funDecode: cachedata = funDecode(moduleKey, datas) assert (isinstance( cachedata, dict)), 'the cache templates data must be a dict !' + str( moduleKey) + ' funDecode=' + str(funDecode) else: cachedata = datas['templates'] _templatesCache[moduleKey] = cachedata return cachedata
def _syncConfigure(_lockobj): global _CHANGE_INDEX, _LAST_ERRORS changindex = ftcon.getConfNoCache('LLEN', _CHANGE_KEYS_NAME) keys = [] ftlog.debug('_syncConfigure _CHANGE_INDEX=', _CHANGE_INDEX, 'changindex=', changindex) if changindex > _CHANGE_INDEX: keys = ftcon.getConfNoCache('LRANGE', _CHANGE_KEYS_NAME, _CHANGE_INDEX, changindex) if keys != None and len(keys) > 0: for x in xrange(len(keys)): keys[x] = str(keys[x]) errors = _reloadConfigLocked(keys, 0.1) _LAST_ERRORS = errors # 记录上次的失败信息,在获取更新状态时返回 if not errors: _CHANGE_INDEX = changindex else: # 发送报警邮件 ftlog.sendException(errors) elif changindex < _CHANGE_INDEX: _CHANGE_INDEX = changindex
def _getTcTemplatesByModuleKey(moduleKey, funDecode): # 取得redis中的所有原始数据 xkey = 'game:' + moduleKey + ':' + CLIENT_ID_TEMPLATE datas = ftcon.getConfNoCache('GET', xkey) if datas: datas = strutil.loads(datas) datas = strutil.replaceObjRefValue(datas) else: datas = {'templates': {}} if funDecode: cachedata = funDecode(moduleKey, datas) assert (isinstance(cachedata, dict)), 'the cache templates data must be a dict !' + str( moduleKey) + ' funDecode=' + str(funDecode) else: cachedata = datas['templates'] _templatesCache[moduleKey] = cachedata return cachedata
def _initialize(): global _CHANGE_INDEX if _CHANGE_INDEX < 0: _CHANGE_INDEX = ftcon.getConfNoCache('LLEN', _CHANGE_KEYS_NAME)
def _updateRoomDefine(keylist, sleepTime): import json from poker.entity.game import quick_start isall = 1 if len(keylist) == 1 and keylist[0] == 'all' else 0 if _DEBUG: debug('_syncConfigure._updateRoomDefine-> in', keylist) _sleepnb(sleepTime) keylist = set(keylist) changedGameIds = [] changedBigRoomIds = [] for gid, bigRoomIds in gdata.gameIdBigRoomidsMap().items(): rkey = 'game:' + str(gid) + ':room:0' if isall or rkey in keylist: changedGameIds.append([rkey, gid]) for bigRoomId in bigRoomIds: rkey = 'game:' + str(gid) + ':room:' + str(bigRoomId) if isall or rkey in keylist: changedBigRoomIds.append([rkey, bigRoomId]) changeRoomIds = set([]) for changed in changedGameIds: rkey = changed[0] gid = changed[1] if _DEBUG: debug('_syncConfigure._updateRoomDefine-> reload roomdef', rkey) roomdict = ftcon.getConfNoCache('GET', rkey) if roomdict: roomdict = json.loads(roomdict) if not isinstance(roomdict, dict): continue for roomIdStr, configure in roomdict.items(): bigRoomId = int(roomIdStr) # 所有BIGROOMID下的configure是同一个对象,update第一个即可 roomid0 = gdata.bigRoomidsMap()[bigRoomId][0] room0 = gdata.roomIdDefineMap()[roomid0] room0.configure.update(configure) changeRoomIds.add(bigRoomId) if _DEBUG: debug( '_syncConfigure._updateRoomDefine-> reload roomdef by 0.json', bigRoomId) _sleepnb(sleepTime) for changed in changedBigRoomIds: rkey = changed[0] bigRoomId = changed[1] if _DEBUG: debug('_syncConfigure._updateRoomDefine-> reload roomdef', rkey) extdict = ftcon.getConfNoCache('GET', rkey) if extdict: extdict = json.loads(extdict) if not isinstance(extdict, dict): continue # 所有BIGROOMID下的configure是同一个对象,update第一个即可 roomid0 = gdata.bigRoomidsMap()[bigRoomId][0] room0 = gdata.roomIdDefineMap()[roomid0] room0.configure.update(extdict) changeRoomIds.add(bigRoomId) if _DEBUG: debug( '_syncConfigure._updateRoomDefine-> reload roomdef by ' + str(bigRoomId) + '.json', bigRoomId) _sleepnb(sleepTime) if changeRoomIds: for _, roomIns in gdata.rooms().items(): if roomIns.bigRoomId in changeRoomIds: if _DEBUG: debug('_syncConfigure._updateRoomDefine-> reload roomins', roomIns.roomId) rdef = gdata.roomIdDefineMap().get(roomIns.roomId) roomIns.doReloadConf(rdef) _sleepnb(sleepTime) quick_start._CANDIDATE_ROOM_IDS = {} _sleepnb(sleepTime) if _DEBUG: debug('_syncConfigure._updateRoomDefine-> out')
def _loadRoomDefines(gdatas): ''' 需要整理一个全局的 serverid-roomid-roombaseinfo的大集合 取得server_rooms.json的配置内容, key为服务ID, value为房间配置内容 ''' if _DEBUG: debug('loadRoomDefines begin') # 首先整理进程ID, 展开多房间进程的进程ID srvid_rooms_map = {} srvidmap = {} allserver = gdata.allServersMap() for k in allserver: srvid_rooms_map[k] = [] if k.find('-') > 0: assert (k.find(gdata.SRV_TYPE_ROOM) == 0 or k.find(gdata.SRV_TYPE_TABLE) == 0) tail = '000' if k.find(gdata.SRV_TYPE_TABLE) == 0: tail = '001' ks = k.split('-') ps = int(ks[1]) pe = int(ks[2]) assert (ps > 0 and ps < 999) assert (pe > 0 and pe < 999) assert (ps < pe) for x in xrange(ps, pe + 1): playid = '%03d' % (x) sid = ks[0] + playid + ks[3] srvidmap[sid + tail] = k else: if k.find(gdata.SRV_TYPE_ROOM) == 0: srvidmap[k + '000'] = k if k.find(gdata.SRV_TYPE_TABLE) == 0: srvidmap[k] = k clsRoomDefine = namedtuple('RoomDefine', [ 'bigRoomId', 'parentId', 'roomId', 'gameId', 'configId', 'controlId', 'shadowId', 'serverId', 'tableCount', 'shadowRoomIds', 'configure' ]) roomid_define_map = {} big_roomids_map = {} gameid_big_roomids_map = {} # 取得说有挂接的游戏ID, 取得对应GAMEID的房间配置 gameids = gdata.games().keys() if _DEBUG: debug('the game ids=', gameids) for gid in gameids: gameid_big_roomids_map[gid] = [] roomdict = ftcon.getConfNoCache('GET', 'game:' + str(gid) + ':room:0') if roomdict: roomdict = strutil.loads(roomdict) if not isinstance(roomdict, dict): if _DEBUG: debug('the game of', gid, 'have no room !') continue for roomIdStr, configure in roomdict.items(): bigRoomId = int(roomIdStr) gameid, configid = strutil.parseBigRoomId(bigRoomId) configure = _auto_change_room_count(configure) controlServerCount = int(configure['controlServerCount']) controlTableCount = int(configure['controlTableCount']) gameServerCount = int(configure['gameServerCount']) gameTableCount = int(configure['gameTableCount']) assert (gameid == gid) assert (configid > 0 and configid < 999), '%s,%s' % (roomIdStr, str(configure)) assert (controlServerCount > 0 and controlServerCount < 9), '%s,%s' % (roomIdStr, str(configure)) assert (controlTableCount >= 0 and controlTableCount < 9999), '%s,%s' % (roomIdStr, str(configure)) assert (gameServerCount >= 0 and gameServerCount < 999), '%s,%s' % (roomIdStr, str(configure)) assert (gameTableCount > 0 and gameTableCount < 9999), '%s,%s' % (roomIdStr, str(configure)) assert (not bigRoomId in big_roomids_map) extconfig = ftcon.getConfNoCache( 'GET', 'game:' + str(gid) + ':room:' + str(bigRoomId)) if extconfig: extconfig = strutil.loads(extconfig) if isinstance(extconfig, dict): configure.update(extconfig) gameid_big_roomids_map[gid].append(bigRoomId) big_roomids_map[bigRoomId] = [] for m in xrange(controlServerCount): # 自动计算controlId, 重1开始 controlId = m + 1 controlRoomId = (bigRoomId * 10 + controlId) * 1000 shadowRooms = [] assert (not controlRoomId in big_roomids_map) assert (not controlRoomId in roomid_define_map) for n in xrange(gameServerCount): # 自动计算shadowId, 重1开始, 此处为桌子运行的房间 shadowId = n + 1 shadowRoomId = controlRoomId + shadowId assert (not shadowRoomId in roomid_define_map) processId = gdata.SRV_TYPE_TABLE + str(shadowRoomId) serverId = srvidmap[processId] srvid_rooms_map[serverId].append(shadowRoomId) shadowRooms.append(shadowRoomId) roomid_define_map[shadowRoomId] = clsRoomDefine( bigRoomId, controlRoomId, shadowRoomId, gameid, configid, controlId, shadowId, serverId, gameTableCount, tuple([]), configure) if _DEBUG: debug('load room define->bigRoomId=', bigRoomId, 'parentId=', controlRoomId, 'roomId=', shadowRoomId, 'gameId=', gameid, 'configId=', configid, 'controlId=', controlId, 'shadowId=', shadowId, 'tableCount=', gameTableCount, 'serverId=', serverId) # 此处为控制房间 processId = gdata.SRV_TYPE_ROOM + str(controlRoomId) serverId = srvidmap[processId] srvid_rooms_map[serverId].append(controlRoomId) big_roomids_map[bigRoomId].append(controlRoomId) roomid_define_map[controlRoomId] = clsRoomDefine( bigRoomId, 0, controlRoomId, gameid, configid, controlId, 0, serverId, controlTableCount, tuple(shadowRooms), configure) if _DEBUG: debug('load room define->bigRoomId=', bigRoomId, 'parentId=', 0, 'roomId=', controlRoomId, 'gameId=', gameid, 'configId=', configid, 'controlId=', controlId, 'shadowId=', 0, 'tableCount=', controlTableCount, 'serverId=', serverId, 'shadowRooms=', shadowRooms) gameid_big_roomids_map[gid].sort() # 整理打印配置的内容 if _DEBUG: debug('find big roomids=', big_roomids_map.keys()) for k, v in big_roomids_map.items(): if _DEBUG: debug('find big room id ', k, 'has childs:', v) for k in srvid_rooms_map.keys(): if not srvid_rooms_map[k]: del srvid_rooms_map[k] else: if _DEBUG: debug('find server', k, 'has roomids:', srvid_rooms_map[k]) gdatas['srvid_roomid_map'] = makeReadonly(srvid_rooms_map) gdatas['roomid_define_map'] = makeReadonly(roomid_define_map) gdatas['big_roomids_map'] = makeReadonly(big_roomids_map) gdatas['gameid_big_roomids_map'] = makeReadonly(gameid_big_roomids_map) if _DEBUG: debug('loadRoomDefines end')
def _updateRoomDefine(keylist, sleepTime): import json from poker.entity.game import quick_start isall = 1 if len(keylist) == 1 and keylist[0] == 'all' else 0 if _DEBUG: debug('_syncConfigure._updateRoomDefine-> in', keylist) _sleepnb(sleepTime) keylist = set(keylist) changedGameIds = [] changedBigRoomIds = [] for gid, bigRoomIds in gdata.gameIdBigRoomidsMap().items(): rkey = 'game:' + str(gid) + ':room:0' if isall or rkey in keylist: changedGameIds.append([rkey, gid]) for bigRoomId in bigRoomIds: rkey = 'game:' + str(gid) + ':room:' + str(bigRoomId) if isall or rkey in keylist: changedBigRoomIds.append([rkey, bigRoomId]) changeRoomIds = set([]) for changed in changedGameIds: rkey = changed[0] gid = changed[1] if _DEBUG: debug('_syncConfigure._updateRoomDefine-> reload roomdef', rkey) roomdict = ftcon.getConfNoCache('GET', rkey) if roomdict: roomdict = json.loads(roomdict) if not isinstance(roomdict, dict): continue for roomIdStr, configure in roomdict.items(): bigRoomId = int(roomIdStr) # 所有BIGROOMID下的configure是同一个对象,update第一个即可 roomid0 = gdata.bigRoomidsMap()[bigRoomId][0] room0 = gdata.roomIdDefineMap()[roomid0] room0.configure.update(configure) changeRoomIds.add(bigRoomId) if _DEBUG: debug('_syncConfigure._updateRoomDefine-> reload roomdef by 0.json', bigRoomId) _sleepnb(sleepTime) for changed in changedBigRoomIds: rkey = changed[0] bigRoomId = changed[1] if _DEBUG: debug('_syncConfigure._updateRoomDefine-> reload roomdef', rkey) extdict = ftcon.getConfNoCache('GET', rkey) if extdict: extdict = json.loads(extdict) if not isinstance(extdict, dict): continue # 所有BIGROOMID下的configure是同一个对象,update第一个即可 roomid0 = gdata.bigRoomidsMap()[bigRoomId][0] room0 = gdata.roomIdDefineMap()[roomid0] room0.configure.update(extdict) changeRoomIds.add(bigRoomId) if _DEBUG: debug('_syncConfigure._updateRoomDefine-> reload roomdef by ' + str(bigRoomId) + '.json', bigRoomId) _sleepnb(sleepTime) if changeRoomIds: for _, roomIns in gdata.rooms().items(): if roomIns.bigRoomId in changeRoomIds: if _DEBUG: debug('_syncConfigure._updateRoomDefine-> reload roomins', roomIns.roomId) rdef = gdata.roomIdDefineMap().get(roomIns.roomId) roomIns.doReloadConf(rdef) _sleepnb(sleepTime) quick_start._CANDIDATE_ROOM_IDS = {} _sleepnb(sleepTime) if _DEBUG: debug('_syncConfigure._updateRoomDefine-> out')
def __loadRoomDefines(gdatas): ''' 需要整理一个全局的 serverid-roomid-roombaseinfo的大集合 取得server_rooms.json的配置内容, key为服务ID, value为房间配置内容 ''' if _DEBUG: debug('loadRoomDefines begin') # 首先整理进程ID, 展开多房间进程的进程ID srvid_rooms_map = {} srvidmap = {} allserver = gdata.allServersMap() for k in allserver: srvid_rooms_map[k] = [] if k.find('-') > 0: assert (k.find(gdata.SRV_TYPE_ROOM) == 0 or k.find(gdata.SRV_TYPE_TABLE) == 0) tail = '000' if k.find(gdata.SRV_TYPE_TABLE) == 0: tail = '001' ks = k.split('-') ps = int(ks[1]) pe = int(ks[2]) assert (ps > 0 and ps < 999) assert (pe > 0 and pe < 999) assert (ps < pe) for x in xrange(ps, pe + 1): playid = '%03d' % (x) sid = ks[0] + playid + ks[3] srvidmap[sid + tail] = k else: if k.find(gdata.SRV_TYPE_ROOM) == 0: srvidmap[k + '000'] = k if k.find(gdata.SRV_TYPE_TABLE) == 0: srvidmap[k] = k clsRoomDefine = namedtuple('RoomDefine', ['bigRoomId', 'parentId', 'roomId', 'gameId', 'configId', 'controlId', 'shadowId', 'serverId', 'tableCount', 'shadowRoomIds', 'configure']) roomid_define_map = {} big_roomids_map = {} gameid_big_roomids_map = {} # 取得说有挂接的游戏ID, 取得对应GAMEID的房间配置 gameids = gdata.games().keys() if _DEBUG: debug('the game ids=', gameids) for gid in gameids: gameid_big_roomids_map[gid] = [] roomdict = ftcon.getConfNoCache('GET', 'game:' + str(gid) + ':room:0') if roomdict: roomdict = strutil.loads(roomdict) if not isinstance(roomdict, dict): if _DEBUG: debug('the game of', gid, 'have no room !') continue for roomIdStr, configure in roomdict.items(): bigRoomId = int(roomIdStr) gameid, configid = strutil.parseBigRoomId(bigRoomId) configure = __auto_change_room_count(configure) controlServerCount = int(configure['controlServerCount']) controlTableCount = int(configure['controlTableCount']) gameServerCount = int(configure['gameServerCount']) gameTableCount = int(configure['gameTableCount']) assert (gameid == gid) assert (configid > 0 and configid < 999), '%s,%s' % (roomIdStr, str(configure)) assert (controlServerCount > 0 and controlServerCount < 9), '%s,%s' % (roomIdStr, str(configure)) assert (controlTableCount >= 0 and controlTableCount < 9999), '%s,%s' % (roomIdStr, str(configure)) assert (gameServerCount >= 0 and gameServerCount < 999), '%s,%s' % (roomIdStr, str(configure)) assert (gameTableCount > 0 and gameTableCount < 9999), '%s,%s' % (roomIdStr, str(configure)) assert (not bigRoomId in big_roomids_map) extconfig = ftcon.getConfNoCache('GET', 'game:' + str(gid) + ':room:' + str(bigRoomId)) if extconfig: extconfig = strutil.loads(extconfig) if isinstance(extconfig, dict): configure.update(extconfig) gameid_big_roomids_map[gid].append(bigRoomId) big_roomids_map[bigRoomId] = [] for m in xrange(controlServerCount): # 自动计算controlId, 重1开始 controlId = m + 1 controlRoomId = (bigRoomId * 10 + controlId) * 1000 shadowRooms = [] assert (not controlRoomId in big_roomids_map) assert (not controlRoomId in roomid_define_map) for n in xrange(gameServerCount): # 自动计算shadowId, 重1开始, 此处为桌子运行的房间 shadowId = n + 1 shadowRoomId = controlRoomId + shadowId assert (not shadowRoomId in roomid_define_map) processId = gdata.SRV_TYPE_TABLE + str(shadowRoomId) serverId = srvidmap[processId] srvid_rooms_map[serverId].append(shadowRoomId) shadowRooms.append(shadowRoomId) roomid_define_map[shadowRoomId] = clsRoomDefine(bigRoomId, controlRoomId, shadowRoomId, gameid, configid, controlId, shadowId, serverId, gameTableCount, tuple([]), configure) if _DEBUG: debug('load room define->bigRoomId=', bigRoomId, 'parentId=', controlRoomId, 'roomId=', shadowRoomId, 'gameId=', gameid, 'configId=', configid, 'controlId=', controlId, 'shadowId=', shadowId, 'tableCount=', gameTableCount, 'serverId=', serverId) # 此处为控制房间 processId = gdata.SRV_TYPE_ROOM + str(controlRoomId) serverId = srvidmap[processId] srvid_rooms_map[serverId].append(controlRoomId) big_roomids_map[bigRoomId].append(controlRoomId) roomid_define_map[controlRoomId] = clsRoomDefine(bigRoomId, 0, controlRoomId, gameid, configid, controlId, 0, serverId, controlTableCount, tuple(shadowRooms), configure) if _DEBUG: debug('load room define->bigRoomId=', bigRoomId, 'parentId=', 0, 'roomId=', controlRoomId, 'gameId=', gameid, 'configId=', configid, 'controlId=', controlId, 'shadowId=', 0, 'tableCount=', controlTableCount, 'serverId=', serverId, 'shadowRooms=', shadowRooms) gameid_big_roomids_map[gid].sort() # 整理打印配置的内容 if _DEBUG: debug('find big roomids=', big_roomids_map.keys()) for k, v in big_roomids_map.items(): if _DEBUG: debug('find big room id ', k, 'has childs :', v) for k in srvid_rooms_map.keys(): if not srvid_rooms_map[k]: del srvid_rooms_map[k] else: if _DEBUG: debug('find server', k, 'has roomids :', srvid_rooms_map[k]) gdatas['srvid_roomid_map'] = makeReadonly(srvid_rooms_map) gdatas['roomid_define_map'] = makeReadonly(roomid_define_map) gdatas['big_roomids_map'] = makeReadonly(big_roomids_map) gdatas['gameid_big_roomids_map'] = makeReadonly(gameid_big_roomids_map) if _DEBUG: debug('loadRoomDefines end')