async def login(conn): if CONN_MAP.__contains__(conn.userName): await conn.send(MessageUtils.err("用户已存在")) else: conn.conn.userName = conn.userName CONN_MAP[conn.userName] = conn await conn.send(MessageUtils.group_list(group_list()))
async def join(user_name, game_id): game = GROUP_MAP[game_id] conn = CONN_MAP[user_name] if not conn: return if not game: conn.send(MessageUtils.err("房间不存在")) return await game.join(conn) USER_GROUP_MAP[user_name] = game await game.send_all(MessageUtils.group(game)) await send_all(MessageUtils.group_list(group_list()))
async def join(self, conn): if self.status != GameStatus.WAIT: await conn.send(MessageUtils.err("游戏进行中")) return if self.memberSum >= 2: await conn.send(MessageUtils.err("房间已满")) return if not self._black: self._black = conn elif not self._white: self._white = conn self.userList.append(conn.userName) self.memberSum = len(self.userList)
async def create(userName): if userName not in CONN_MAP: return conn = CONN_MAP[userName] if not conn: return game = Game(conn) GROUP_MAP[game.id] = game USER_GROUP_MAP[userName] = game group = dict(game).copy() group['isHost'] = game.host == conn.userName await conn.send(MessageUtils.create_group(group)) await send_all(MessageUtils.group_list(group_list()))
async def ai_put(self): x, y = self.AI.findBestChess(self.map.map, self.player) self.map.click(x, y, self.player) await self.send_all(MessageUtils.pos(x, y, self.player)) if self.AI.isWin(self.map.map, self.player): await self.win(self.player) self.player = self.map.reverseTurn(self.player)
async def msg_group(msg, websocket): name = websocket.userName if name in USER_GROUP_MAP: group = USER_GROUP_MAP[name] else: return await group.send_all(MessageUtils.msg(name, msg))
async def put(self, x, y, playerType): if playerType != self.player: return self.map.click(x, y, playerType) await self.send_all(MessageUtils.pos(x, y, playerType)) if self.AI.isWin(self.map.map, playerType): await self.win(playerType) else: self.player = self.map.reverseTurn(playerType) if self.useAI: await self.ai_put()
async def win(self, playerType): self.status = GameStatus.WAIT self.winner = playerType win = None if playerType == MAP_ENTRY_TYPE.MAP_PLAYER_ONE: win = self._black elif playerType == MAP_ENTRY_TYPE.MAP_PLAYER_TWO: win = self._white if win: win_name = win.userName else: win_name = '机器人' await self.send_all(MessageUtils.show(win_name))
async def start(self): self.status = GameStatus.PLAYING if self.memberSum == 1: self.useAI = True self.blackUser = self.host self.player = MAP_ENTRY_TYPE.MAP_PLAYER_ONE else: self.useAI = False self.blackUser = self._black.userName if random.randint( 0, 1) == 1 else self._white.userName self.player = MAP_ENTRY_TYPE.MAP_PLAYER_TWO self.map = Map(CHESS_LEN, CHESS_LEN) await self.send_all(MessageUtils.start_game(self))
async def exit_group(user_name, game_id=None, group=None): if group: game = group else: if game_id in GROUP_MAP: game = GROUP_MAP[game_id] else: return if user_name in CONN_MAP: conn = CONN_MAP[user_name] else: return if not conn: return if not game: conn.send(MessageUtils.err("房间不存在")) return del USER_GROUP_MAP[user_name] game.exit_game(user_name) if game.isEmpty: del GROUP_MAP[game.id] else: await game.send_all(MessageUtils.group(game)) await send_all(MessageUtils.group_list(group_list()))
def group_change(self): group = self.__dict__.copy() self.send_all(MessageUtils.group(group))