def doCheckUserLoc(userId, gameId, roomId, tableId, clientId): assert (isinstance(userId, int)) assert (isinstance(roomId, int)) assert (isinstance(tableId, int)) serverId = rpccore.getRpcDstRoomServerId(roomId, 0) if not serverId: return -1, 0 assert (serverId in gdata.allServersMap()) ret = _doCheckUserLoc(serverId, userId, gameId, roomId, tableId, clientId) return ret
def _getConnOnlineUserCount(): datas = daobase._executeBiCmd('HGETALL', daoconst.BI_KEY_USER_ONLINES) allcount = 0 if datas: for x in xrange(len(datas) / 2): i = x * 2 connId = str(datas[i]) if connId in gdata.allServersMap(): allcount += int(datas[i + 1]) else: daobase._sendBiCmd('HDEL', daoconst.BI_KEY_USER_ONLINES, connId) return allcount
def _getConnOnlineUserCount(): datas = daobase._executeBiCmd('HGETALL', daoconst.BI_KEY_USER_ONLINES) allcount = 0 if datas: for x in xrange(len(datas) / 2): i = x * 2 connId = str(datas[i]) if connId in gdata.allServersMap(): allcount += int(datas[i + 1]) else: daobase._sendBiCmd('HDEL', daoconst.BI_KEY_USER_ONLINES, connId) return allcount
def reloadConfig(serverIds, keylist, reloadlist, sleepTime=0.01): assert (isinstance(serverIds, (list, tuple))) assert (isinstance(keylist, (list, tuple))) assert (isinstance(reloadlist, (list, tuple))) assert (isinstance(sleepTime, (int, float))) allSrvIds = gdata.allServersMap().keys() for srvId in serverIds: assert (srvId in allSrvIds) rets = {} for srvId in serverIds: rets[srvId] = _reloadConfig(srvId, keylist, reloadlist, sleepTime) return rets
def _check_param_serverIds(self, key, params): allIds = gdata.allServersMap().keys() serverIds = runhttp.getParamStr(key, '') if serverIds == 'all': return None, allIds slist = serverIds.split(',') if slist and len(slist) > 0: for s in slist: if not s in allIds: return 'param serverIds not is allServersMap ' + str(s) + ' allIds=' + str(allIds), None return None, slist return 'param serverIds error', None
def reloadConfig(serverIds, keylist, reloadlist, sleepTime=0.01): assert (isinstance(serverIds, (list, tuple))) assert (isinstance(keylist, (list, tuple))) assert (isinstance(reloadlist, (list, tuple))) assert (isinstance(sleepTime, (int, float))) allSrvIds = gdata.allServersMap().keys() for srvId in serverIds: assert (srvId in allSrvIds) rets = {} for srvId in serverIds: rets[srvId] = _reloadConfig(srvId, keylist, reloadlist, sleepTime) return rets
def _check_param_serverIds(self, key, params): allIds = gdata.allServersMap().keys() serverIds = runhttp.getParamStr(key, '') if serverIds == 'all': return None, allIds slist = serverIds.split(',') if slist and len(slist) > 0: for s in slist: if not s in allIds: return 'param serverIds not is allServersMap ' + str( s) + ' allIds=' + str(allIds), None return None, slist return 'param serverIds error', None
def _getServerIdOfMysql(userId, dbname): serverIds = _MYSQLPIDS.get(dbname, None) if not serverIds: serverIds = [] for sid, sdef in gdata.allServersMap().items(): ftlog.debug('sid, sdef=', sid, sdef) if sdef['type'] == gdata.SRV_TYPE_UTIL: mydbs = sdef.get('mysql', []) if dbname in mydbs: serverIds.append(sid) serverIds.sort() _MYSQLPIDS[dbname] = serverIds ftlog.debug('dbname=', dbname, 'serverIds=', serverIds) sid = serverIds[userId % (len(serverIds))] return sid
def hotFix(hotfixpy, serverIds, isWait, hotparams): assert (isinstance(serverIds, (list, tuple))) assert (isinstance(hotfixpy, (str, unicode))) assert (hotfixpy.startswith('file://') or hotfixpy.startswith('code://')) assert (isinstance(isWait, int)) allSrvIds = gdata.allServersMap().keys() for srvId in serverIds: assert (srvId in allSrvIds) rets = {} for srvId in serverIds: if isWait: oldt = _runenv._RPC_TIME_OUT # 对于目前配置更新超时,临时调整超时时间 _runenv._RPC_TIME_OUT = 24.5 rets[srvId] = _syncHotFix(srvId, hotfixpy, hotparams) if _runenv._RPC_TIME_OUT == 24.5: _runenv._RPC_TIME_OUT = oldt else: _asyncHotFix(srvId, hotfixpy, hotparams) rets[srvId] = 'async' return rets
def hotFix(hotfixpy, serverIds, isWait, hotparams): assert (isinstance(serverIds, (list, tuple))) assert (isinstance(hotfixpy, (str, unicode))) assert (hotfixpy.startswith('file://') or hotfixpy.startswith('code://')) assert (isinstance(isWait, int)) allSrvIds = gdata.allServersMap().keys() for srvId in serverIds: assert (srvId in allSrvIds) rets = {} for srvId in serverIds: if isWait: oldt = _runenv._RPC_TIME_OUT # 对于目前配置更新超时,临时调整超时时间 _runenv._RPC_TIME_OUT = 24.5 rets[srvId] = _syncHotFix(srvId, hotfixpy, hotparams) if _runenv._RPC_TIME_OUT == 24.5: _runenv._RPC_TIME_OUT = oldt else: _asyncHotFix(srvId, hotfixpy, hotparams) rets[srvId] = 'async' return rets
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 __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 _main(): ftlog.info('zzzzzz', gdata.serverId()) # GT0044001_999 ftlog.info( 'zzzzzz', gdata.srvIdRoomIdListMap() ) # {'GT0044001_999': [441011001, 441021001, 441031001, 441041001, 443011001, 443021001, 444021001, 444031001, 444041001, 444051001, 444111001, 444121001, 444141001, 444151001, 444991001, 445011001, 446011001]} ftlog.info('zzzzzz', gdata.allServersMap())