Exemplo n.º 1
0
    async def event_kick_user(self, message: msg.UserId,
                              ws: WebSocketServerProtocol):
        user_id = message.user_id
        try:
            user = self.users[user_id]
            log.info(f'User {user.user.name} kicked by admin')
        except KeyError:
            await self.room.admin_send(rerr('no-such-user'), sock=ws)
            log.info(f'Wrong user {message.user_id} kick requested by admin')
            return
        del self.users[user_id]
        broadcast_msg = msg.UserId(user_id=user_id)
        await self.room.broadcast(rmsg('remove-user', broadcast_msg))
        await self.room.kick(user_id)

        if isinstance(self.state, HatFillState):
            if user_id in self.state.users:
                self.state.users.remove(user_id)
                await self._broadcast_game_state('remove-user')
        if (
            isinstance(self.state, RoundState) and
            (
                user_id == self.state.user_from.user.user_id or
                user_id == self.state.user_to.user.user_id
            )
        ):
            await self._stop_round(reason='kicked user')
Exemplo n.º 2
0
async def connect():
    uri = "ws://localhost:6613/ws/game/" + sys.argv[2]
    async with websockets.connect(uri) as websocket:
        await websocket.send(rmsg('user-auth', {'user_name': 'anus'}))
        while True:
            res = await(websocket.recv())
            print(res)
Exemplo n.º 3
0
async def create():
    uri = "ws://localhost:6613/ws/new_game/"
    async with websockets.connect(uri) as websocket:
        await websocket.send(
            rmsg('new-game', {'round_length': 30, 'hat_words_per_user': 5}))
        res = await(websocket.recv())
        print(res)
Exemplo n.º 4
0
 def _game_state_msg(self, reason, appendix) -> str:
     state_dict = dict(
         state_name=self.state.name,
         users=list(u.to_message() for u in self.users.values()),
         reason=reason,
         appendix=appendix,
         state_hat_fill=None,
         state_round=None,
         game_info=self.to_message(),
     )
     state_dict.update(self.state.to_message())
     return rmsg('game-state', msg.GameState(**state_dict))
Exemplo n.º 5
0
 async def _send_user_state(
     self,
     user: GameUser,
     sock: Optional[WebSocketServerProtocol] = None
 ):
     state_dict = dict(
         state_name=user.state.name,
         state_asking=None,
         state_answering=None,
     )
     state_dict.update(user.state.to_message())
     message = rmsg('user-state', msg.UserState(**state_dict))
     return await self.room.user_send(user.user.user_id, message, sock=sock)
Exemplo n.º 6
0
    async def event_join(self, data: Tuple[User, WebSocketServerProtocol]):
        user, sock = data
        if user.user_id not in self.users:
            # Create new game user
            game_user = GameUser(user)
            self.users[user.user_id] = game_user
            self._info(f'User {game_user} {user.user_id} joined')

            # Broadcast user joined game
            message = game_user.to_message()
            await self.room.broadcast(rmsg('new-user', message))
        else:
            game_user = self.users[user.user_id]
        # Send user and game state to user
        await self._send_user_state(game_user, sock=sock)
        await self._send_game_state(game_user, 'connect', sock=sock)
Exemplo n.º 7
0
async def create_game(ws: WebSocketServerProtocol, pr: persister.Persister):
    ip = remote_addr(ws)
    request: NewGameRequest = await recv_d(ws, NewGameRequest, 'new-game')

    game_id = new_game_id()

    game = Game(
        game_id=game_id,
        game_name=request.game_name,
        round_length=request.round_length,
        hat_words_per_user=request.hat_words_per_user,
        state_saver=make_state_saver(game_id=game_id, pr=pr),
    )
    rooms[game_id] = game.room
    log.info(f'Created game {game_id} named \"{request.game_name}\" for {ip}')
    await ws.send(rmsg('new-game-id', game_id))
Exemplo n.º 8
0
async def auth(websocket: WebSocketServerProtocol) -> Optional[User]:
    ip = remote_addr(websocket)
    try:
        tag, message = await recv(websocket, msg.AuthRequest)
    except ProtocolError as err:
        log.info(f'Remote socket {ip} failed to authenticate: {err}')
        return None
    user_name = message.user_name
    if user_name == 'admin':
        await websocket.send(rerr('auth-error', f'wrong name {user_name}'))
        return None

    user_id = adler32(user_name.encode())
    user = User(user_id, user_name)
    await websocket.send(rmsg('auth-ok', user.to_msg()))
    log.info(f'Connection {ip} authenticated as {user}')

    return user