def __init__(self, game): BaseGameMgr.__init__(self, game) self._svr = None self._keys = TimeMemCache(size=1000, default_timeout=5 * 60, name='player_mgr._keys') self.users = {} self.players = {} self.others = TimeMemCache(size=1000, default_timeout=(self._SAVE_TIME_ - 1), name='player_mgr.others') self._game.reg_obj(self) self._loop_task = None
def __init__(self): setattr(Game, self._rpc_name_, self) self.cache = TimeMemCache(default_timeout=600, name='rank_mgr.cache') #[[ret1,ret2...],{pid:ret索引},{con:rank}] self.player_level_data = None self.player_cbe_data = None #当天第一次的排名时间 self.first_time = None #深渊排名数据的保存 #{层数:[pid1,pid2]...} 此数据保存数据库 self.deep_floor_pids = {} #{pid1:层数,...} self.deep_pid_floor = {} #世界boss排名 #[{排名:1, 名字:xx, 等级:22, 总伤害血量}...} self.boss_rank_datas = [] #{pid1:(rank1, hurts),...} 此数据由世界boss广播而来 保存数据库 self.boss_pid_data = {} #是否将当天排名数据保存到log_rank(当前只有深渊是每天零点更新) self.is_save_rank = False import app app.sub(MSG_START, self.start)
def __init__(self): setattr(Game, self._rpc_name_, self) self.max_players = config.max_players self.logic_players = config.logic_players self._users = {} self._players = {} self._sub_mgrs = {} self.mgr2addrs = {} self.logons = TimeMemCache(size=10000, default_timeout=2, name='rpc_player_mgr.logons') # self.names = TimeMemCache(size=10000, default_timeout=1*30) #缓存保证不重名 self.name2pids = {} self.pid2names = {} self._name_lock = RLock() self._new_player_lock = RLock() self.names4ad = {} self.overload_time = 0 self._debug_ips = None self._debug_status = False self._area_url = None self._area_legal = None import app from game.base import msg_define app.sub(msg_define.MSG_START, self.start)
def __init__(self): self.arenas = TimeMemCache( size=CACHE_SIZE, default_timeout=CACHE_TIME) #玩家信息 {pid:PlayerArena} self.rivals = TimeMemCache( size=CACHE_SIZE, default_timeout=CACHE_TIME) # {pid:[name, rid]} self.rewards = HitMemCache(size=CACHE_SIZE) self.bots = {} self._lock = RLock() self.stoped = True self._reward_task = None self._auto_start_task = None self.clear() app.sub(MSG_START, self.start) self._active_mul = 1 self.sorts = {} #数据库中保存,方便修改 {sort:id}
def __init__(self): self.server_time = 0 self.servers = None self.rpc_caches = TimeMemCache(default_timeout=60)
class Game(object): instance = None inited = False if 0: #跨进程对象 instance = Game() from player.player_mgr import GPlayerMgr rpc_player_mgr = GPlayerMgr() from client import GameClient rpc_client = GameClient() from game.scene.scene_mgr import GSceneMgr rpc_scene_mgr = GSceneMgr() from game.store import GameStore, ResStore, PayStore rpc_store = GameStore() rpc_res_store = ResStore() rpc_pay_store = PayStore() from game.glog.logger import LoggerServer rpc_logger_svr = LoggerServer() from game.mgr.status import StatusMgr rpc_status_mgr = StatusMgr() from game.mgr.report import ReportMgr rpc_report_mgr = ReportMgr() from ally.ally_mgr import AllyMgr rpc_ally_mgr = AllyMgr() from game.mgr.arena import ArenaMgr rpc_arena_mgr = ArenaMgr() from game.mgr.boss import BossMgr rpc_boss_mgr = BossMgr() from game.player.vip import GVipMgr rpc_vip_mgr = GVipMgr() from game.mgr.rewardcode import ExchangeCodeMgr rpc_ex_code_mgr = ExchangeCodeMgr() from game.mgr.tbox import GTboxNewsMgr rpc_tboxnews_mgr = GTboxNewsMgr() from game.mgr.horn import HornMgr rpc_horn_mgr = HornMgr() from game.team.team_mgr import TeamMgr rpc_team_mgr = TeamMgr() from game.mgr.reward import RpcRewardMgr rpc_reward_mgr = RpcRewardMgr() from .mgr.chat import MyNotifyServer rpc_notify_svr = MyNotifyServer() from .mgr.rank import RankMgr rpc_rank_mgr = RankMgr() #本地全局对象 from game.res.res_mgr import ResMgr res_mgr = ResMgr() from game.glog.logger import GameLogger glog = GameLogger() from .base.setting import SettingMgr setting_mgr = SettingMgr() from game.item.item_mgr import ItemMgr item_mgr = ItemMgr() from game.item.reward import RewardMgr reward_mgr = RewardMgr() from game.mgr import chat chat_mgr = chat.ChatMgr() from game.player import mail, vip mail_mgr = mail.MailMgr() vip_mgr = vip.VipMgr() from game.achievement import achi_mgr achi_mgr = achi_mgr.AchievementMgr() from game.mgr import gem gem_mgr = gem.GemMgr() main_app = None app = None name = '' parent_stoped = False rpc_names = set() games = {} #其它逻辑进程中的game对象, {addr:(game, pid)} rpc_init_funcs = {} #缓存,缓存数量待优化 gcaches = TimeMemCache(size=1000, name='game.gcaches') lock_count = 9999 def __init__(self): Game.instance = self self.stoping = False self.stoped = True self.big_area = None #大区名 self.area = None #小区名 self.stop_lock = Semaphore(self.lock_count) self.stop_mgrs = [] @classmethod def _rpc_on_close(cls, proxy): """ 功能服关闭,等待重新链接 """ name = proxy._rpc_name addr, key = proxy.get_proxy_id() if os.environ.get('PARENT_STOPED', None): return if Game.app.stoped: return log.info('reconnect rpc(%s):%s', name, addr) while 1: new = get_obj(addr, key) if new is None or not new.valid(): sleep(0.1) continue cls.add_rpc_mgr(name, new) funcs = cls.rpc_init_funcs.get(name, []) for f in funcs: spawn(f, new) break @classmethod def iter_games(cls): addrs = Game.games.keys() for key in addrs: g = Game.games.get(key, None) if not g: continue yield g[0] @classmethod def one_game(cls): return Game.games.itervalues().next()[0] @classmethod def valid_games(cls): """ 返回有效的逻辑进程game对象 """ import psutil return [ game for game, pid in Game.games.values() if psutil.pid_exists(pid) ] @classmethod def reg_other_game(cls, rpc_game, process_id, _pickle=True, _no_result=True): """ 注册其它进程的game对象 """ log.debug('reg_other_game(%s)', rpc_game.get_addr()) Game.games[rpc_game.get_addr()] = (rpc_game, process_id) @classmethod def unreg_other_game(cls, game_addr, _no_result=True): """ 反注册其它进程的game对象 """ log.debug('unreg_other_game(%s)', game_addr) if isinstance(game_addr, list): game_addr = tuple(game_addr) Game.games.pop(game_addr, None) @classmethod def init_snss(cls, get_config=None, web_app=None, api_url=None, res_store=None): """ 初始化sns模块 """ from game.store import get_sns_params from webapi import init_snss, SNSClient if res_store is None: res_store = Game.rpc_res_store SNSClient.res_store = res_store if get_config is None: get_config = Game.rpc_res_store.get_config params = get_sns_params(get_config) return init_snss(params, web_app=web_app, api_url=api_url) @classmethod def sub_rpc_mgr_init(cls, rpc_mgr, func): funcs = Game.rpc_init_funcs.setdefault(rpc_mgr._rpc_name, set()) funcs.add(func) @classmethod def add_rpc_mgr(cls, name, mgr): if hasattr(mgr, 'sub_close'): #log.info('rpc_mgr sub_close:(%s, %s)', name, mgr) mgr.sub_close(cls._rpc_on_close) cls.rpc_names.add(name) mgr._rpc_name = name setattr(cls, name, mgr) @classmethod def init_res_mgr(cls): if getattr(cls, 'res_mgr', None): return from game.res.res_mgr import ResMgr Game.res_mgr = ResMgr() Game.res_mgr.load() @classmethod def init_logger(cls): if getattr(cls, 'glog', None): return from game.glog.logger import GameLogger Game.glog = GameLogger() @classmethod def init_mail_mgr(cls): if getattr(cls, 'mail_mgr', None): return from game.player.mail import MailMgr Game.mail_mgr = MailMgr() @classmethod def init_setting_mgr(cls): from .base.setting import SettingMgr Game.setting_mgr = SettingMgr() Game.setting_mgr.start() @classmethod def init_chat_mgr(cls): from game.mgr.chat import ChatMgr Game.chat_mgr = ChatMgr() Game.chat_mgr.start() @classmethod def init_item_mgr(cls): from .item.item_mgr import ItemMgr Game.item_mgr = ItemMgr() @classmethod def init_reward_mgr(cls): from .item.reward import RewardMgr Game.reward_mgr = RewardMgr() Game.reward_mgr.start() @classmethod def init_vip_mgr(cls): from .player.vip import VipMgr Game.vip_mgr = VipMgr() Game.vip_mgr.start() @classmethod def init_cls(cls): cls.init_res_mgr() cls.init_logger() cls.init_mail_mgr() cls.init_setting_mgr() cls.init_chat_mgr() cls.init_item_mgr() cls.init_reward_mgr() cls.init_vip_mgr() @classmethod def get_addr(cls): return Game.app.get_addr() def init_player_mgr(self): from player.player_mgr import SubPlayerMgr self.player_mgr = SubPlayerMgr(self) self.stop_mgrs.append(self.player_mgr) def init_scene_mgr(self): from scene.scene_mgr import SubSceneMgr self.scene_mgr = SubSceneMgr(self) self.stop_mgrs.append(self.scene_mgr) def init_task_mgr(self): """ 初始化任务管理器 """ from task.task_mgr import TaskMgr self.task_mgr = TaskMgr(self) def init_gm_mgr(self): from game.gm import GameMasterMgr self.gm_mgr = GameMasterMgr(self) def init_mining_mgr(self): from .mgr.mining import MiningMgr self.mining_mgr = MiningMgr(self) self.stop_mgrs.append(self.mining_mgr) def init(self): #定时器 from corelib.common import TimerSet self.timer_set = TimerSet(self) self.stop_mgrs.append(self.timer_set) #缓存,缓存数量待优化 self.caches = TimeMemCache(size=500, name='game.caches') self.init_snss() self.init_res_mgr() self.init_gm_mgr() self.init_player_mgr() self.init_scene_mgr() self.init_task_mgr() self.init_mining_mgr() from game.mgr.deep import DeepMgr self.deep_mgr = DeepMgr(self) self.stop_mgrs.append(self.deep_mgr) from game.mgr.fish import FishMgr self.fish_mgr = FishMgr(self) self.stop_mgrs.append(self.fish_mgr) from game.mgr.coinstree import CTreeMgr self.ctree_mgr = CTreeMgr(self) self.stop_mgrs.append(self.ctree_mgr) from game.mgr.tbox import TboxMgr self.tbox_mgr = TboxMgr(self) self.stop_mgrs.append(self.tbox_mgr) from game.mgr.hfate import HitFateMgr self.hfate_mgr = HitFateMgr(self) self.stop_mgrs.append(self.hfate_mgr) from game.mgr.bftask import BfTaskMgr self.bftask_mgr = BfTaskMgr(self) self.stop_mgrs.append(self.bftask_mgr) from game.mgr.fete import FeteMgr self.fete_mgr = FeteMgr(self) self.stop_mgrs.append(self.fete_mgr) #'神秘商人' from game.mgr.shop import ShopMgr self.shop_mgr = ShopMgr(self) self.stop_mgrs.append(self.shop_mgr) from game.mgr.social import SocialMgr self.social_mgr = SocialMgr(self) self.stop_mgrs.append(self.social_mgr) from game.achievement.achi_mgr import AchievementMgr self.achi_mgr = AchievementMgr(self) self.stop_mgrs.append(self.achi_mgr) from game.mgr.gem import GemMgr self.gem_mgr = GemMgr(self) self.stop_mgrs.append(self.gem_mgr) from game.mgr.day_lucky import DayLuckyMgr self.day_lucky_mgr = DayLuckyMgr(self) self.stop_mgrs.append(self.day_lucky_mgr) from game.mgr.day_sign import DaySignMgr self.day_sign_mgr = DaySignMgr(self) self.stop_mgrs.append(self.day_sign_mgr) #玩家vip属性管理 from game.mgr.vipattr import PlayerVipAttrMgr self.vip_attr_mgr = PlayerVipAttrMgr(self) self.stop_mgrs.append(self.vip_attr_mgr) #奖励奖励管理 from game.mgr.reward import RewardMgr self.reward_mgr2 = RewardMgr(self) self.stop_mgrs.append(self.reward_mgr2) def start(self): if not self.stoped: return self.stoped = False self.timer_set.start() #每天定时清理 self.cron_clean_data_zero(inited=True) #0点 for mgr in self.stop_mgrs: try: if not hasattr(mgr, 'start'): continue mgr.start() except StandardError as e: log.log_except('stop mgr(%s) error:%s', mgr, e) def _stop(self): pass def stop(self): """ 进程退出 """ if self.stoping: return self.stoping = True log.info(u'game模块停止') Game.parent_stoped = bool(os.environ.get('PARENT_STOPED', False)) def _stop_func(): try: self._stop() except StandardError: log.log_except() for mgr in self.stop_mgrs: try: mgr.stop() except StandardError as e: log.log_except('stop mgr(%s) error:%s', mgr, e) sleep(0.5) #允许其它线程切换 #等待其它完成 while self.stop_lock.wait() < self.lock_count: sleep(0.1) try: #保证在30分钟内处理完 with Timeout.start_new(60 * 30): _stop_func() except: log.log_except() self.stoped = True def cron_clean_data_zero(self, inited=False): """ 定在每天凌晨0点初始化的处理 """ now = datetime.datetime.now() refresh_time = custom_today(hour=0, minute=0) refresh_time += ONE_DAY_DELTA sleep_times = (refresh_time - now).seconds + 5 self.timer_set.call_later(sleep_times, self.cron_clean_data_zero) #执行游戏逻辑刷新 if not inited: log.info(u"每日0点定时刷新开始") spawn(self.on_cron_clean_data_zero) # for player in self.scene_mgr.players.itervalues(): # spawn(self.on_player_cron_clean_data_zero, player) def on_cron_clean_data_zero(self): """ channel,battle都需要的0点清理 """ pass def on_player_cron_clean_data_zero(self, player): """ channel,battle都需要的0点角色清理 """ pass def is_gm(self, uid): """ 根据用户名,检查是否gm """ from game.player.player import UserData return UserData.is_gm(uid) def sync_exec(self, func, args, _pickle=True): """ 由该进程执行func, func内独立使用各自的lock,保证同步执行 """ try: if args: return func(*args) return func() except: log.log_except() raise
def init(self): #定时器 from corelib.common import TimerSet self.timer_set = TimerSet(self) self.stop_mgrs.append(self.timer_set) #缓存,缓存数量待优化 self.caches = TimeMemCache(size=500, name='game.caches') self.init_snss() self.init_res_mgr() self.init_gm_mgr() self.init_player_mgr() self.init_scene_mgr() self.init_task_mgr() self.init_mining_mgr() from game.mgr.deep import DeepMgr self.deep_mgr = DeepMgr(self) self.stop_mgrs.append(self.deep_mgr) from game.mgr.fish import FishMgr self.fish_mgr = FishMgr(self) self.stop_mgrs.append(self.fish_mgr) from game.mgr.coinstree import CTreeMgr self.ctree_mgr = CTreeMgr(self) self.stop_mgrs.append(self.ctree_mgr) from game.mgr.tbox import TboxMgr self.tbox_mgr = TboxMgr(self) self.stop_mgrs.append(self.tbox_mgr) from game.mgr.hfate import HitFateMgr self.hfate_mgr = HitFateMgr(self) self.stop_mgrs.append(self.hfate_mgr) from game.mgr.bftask import BfTaskMgr self.bftask_mgr = BfTaskMgr(self) self.stop_mgrs.append(self.bftask_mgr) from game.mgr.fete import FeteMgr self.fete_mgr = FeteMgr(self) self.stop_mgrs.append(self.fete_mgr) #'神秘商人' from game.mgr.shop import ShopMgr self.shop_mgr = ShopMgr(self) self.stop_mgrs.append(self.shop_mgr) from game.mgr.social import SocialMgr self.social_mgr = SocialMgr(self) self.stop_mgrs.append(self.social_mgr) from game.achievement.achi_mgr import AchievementMgr self.achi_mgr = AchievementMgr(self) self.stop_mgrs.append(self.achi_mgr) from game.mgr.gem import GemMgr self.gem_mgr = GemMgr(self) self.stop_mgrs.append(self.gem_mgr) from game.mgr.day_lucky import DayLuckyMgr self.day_lucky_mgr = DayLuckyMgr(self) self.stop_mgrs.append(self.day_lucky_mgr) from game.mgr.day_sign import DaySignMgr self.day_sign_mgr = DaySignMgr(self) self.stop_mgrs.append(self.day_sign_mgr) #玩家vip属性管理 from game.mgr.vipattr import PlayerVipAttrMgr self.vip_attr_mgr = PlayerVipAttrMgr(self) self.stop_mgrs.append(self.vip_attr_mgr) #奖励奖励管理 from game.mgr.reward import RewardMgr self.reward_mgr2 = RewardMgr(self) self.stop_mgrs.append(self.reward_mgr2)
def __init__(self): setattr(Game, self._rpc_name_, self) self.cache = TimeMemCache(default_timeout=10, name='status_mgr.cache') self._slock = RLock()
def __init__(self): setattr(Game, self._rpc_name_, self) self.cache = TimeMemCache(default_timeout=10, name='report_mgr.cache') self.thread_pool = GlobalThreadPool(max_pool_size=5) self.report_base_dir = None self.report_base_url = None