当前位置: 首页>>代码示例>>Python>>正文


Python Mapper.get_townhalls_count方法代码示例

本文整理汇总了Python中mapper.Mapper.get_townhalls_count方法的典型用法代码示例。如果您正苦于以下问题:Python Mapper.get_townhalls_count方法的具体用法?Python Mapper.get_townhalls_count怎么用?Python Mapper.get_townhalls_count使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mapper.Mapper的用法示例。


在下文中一共展示了Mapper.get_townhalls_count方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Overseer

# 需要导入模块: from mapper import Mapper [as 别名]
# 或者: from mapper.Mapper import get_townhalls_count [as 别名]
class Overseer(object):
    def __init__(self, lcg, map_size):
        self.current_player_index = None
        self.game_started = False
        self._players = []
        self.lcg = lcg
        self.handlers = Handlers(self)
        self.mapper = Mapper(self, *map_size)

    def handle(self, environ, start_response):
        logger.info('Using overseer %s' % id(self))
        socket = environ["wsgi.websocket"]
        logger.debug(socket.__dict__)
        player = Player(socket, start_response)
        enter_teh_infiniteh_loopah = True
        if self.game_started:
            logger.info(
                '%s tried to connect, but game has already started' % player.id
            )
            logger.info('Delegating %s to new overseer...' % player.id)
            self.lcg.new_overseer()
            return self.lcg.redirect_to_overseer(environ, start_response)
        logger.info('%s connected' % player.id)
        while enter_teh_infiniteh_loopah:
            try:
                line = socket.receive()
            except socketerror:
                break
            if not line:
                break
            line = line.strip()
            if not line:
                break
            logger.debug('%s -> %s' % (player.id, line))
            try:
                parsed = json.loads(line)
            except ValueError:
                player.exception('What the hell are you sending to me?')
                continue
            try:
                self.delegate(player, parsed)
            except (GameError, ServerError) as e:
                player.exception(e)
                logger.info('%s raised %s' % (player.id, e))
                continue
        self.remove_player(player)
        try:
            socket.close()
        except socketerror:  # whatever, I no more care about this socket
            pass
        logger.info('%s disconnected' % player.id)

    def delegate(self, who, msg):
        if not ('type' in msg and 'message' in msg):
            raise ServerError('Not enough JSON fields')
        what = msg['type']
        message = msg['message']
        self.handlers.do(who, what, message)

    @property
    def players(self):
        return [p for p in self._players if p and p.name]

    @property
    def current_player(self):
        return self._players[self.current_player_index]

    @current_player.setter
    def current_player(self, player):
        self.current_player_index = self._players.index(player)

    def next_player(self):
        if self.current_player_index is None:
            self.current_player_index = 0
        else:
            while self.current_player:
                i = (self.current_player_index+1) % len(self._players)
                self.current_player_index = i
                valid = (
                    self.current_player and
                    self.current_player in self.mapper.remaining_players
                )
                if valid:
                    break
        townhall_count = self.mapper.get_townhalls_count(self.current_player)
        self.current_player.action_points = 10 + (townhall_count-1)*2
        for p in [p for p in self.players if p is not self.current_player]:
            p.send('nextPlayer', {'nickname': self.current_player.name})
        self.current_player.send('yourTurn', self.current_player.state)

    def remove_player(self, player):
        try:
            if not self.game_started:
                self._players.remove(player)
            else:
                i = self._players.index(player)
                self._players[i] = None
        except ValueError:  # lol
            pass
        # Notify others
#.........这里部分代码省略.........
开发者ID:Namek,项目名称:lets-code-game,代码行数:103,代码来源:overseer.py


注:本文中的mapper.Mapper.get_townhalls_count方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。