def vote_finish_day(self, request, context): cur_user_id = request.user_id print(f"Received VOTE for DAY FINISH from {cur_user_id}") if cur_user_id in self._users: self._finish_day_votes[cur_user_id] = True for user_id in self._users: if user_id != cur_user_id: self._user_messages[user_id].append( mafia_pb2.CommunicationResponse( message=f'User {cur_user_id} voted to finish game day', author='Admin')) return mafia_pb2.Response(status=mafia_pb2.StatusCode.StatusCode_OK, message='Your vote has been counted') else: return mafia_pb2.Response(status=mafia_pb2.StatusCode.StatusCode_BAD_REQUEST, message='Wrong user id')
def wrapper(self, *args, **kwargs): if self._day_interval == config.DAY_INTERVAL.day: return func(self, *args, **kwargs) else: return mafia_pb2.Response( status=mafia_pb2.StatusCode.StatusCode_FORBIDDEN, message=f"You cannot perform that action during night")
def wrapper(self, *args, **kwargs): if not self._is_game_started: return func(self, *args, **kwargs) else: return mafia_pb2.Response( status=mafia_pb2.StatusCode.StatusCode_FORBIDDEN, message=f"You cannot perform that when game is already started")
def wrapper(self, *args, **kwargs): request = args[0] if self._roles[request.user_id] != config.ROLES.ghost: return func(self, *args, **kwargs) else: return mafia_pb2.Response( status=mafia_pb2.StatusCode.StatusCode_FORBIDDEN, message=f"You cannot perform that action, as you are ghost")
def accuse_user(self, request, context): if self._day_num > 1: accusing_user_id = request.accusing_user_id accused_user_id = request.accused_user_id if self._roles[accused_user_id] == config.ROLES.ghost: return mafia_pb2.Response( status=mafia_pb2.StatusCode.StatusCode_BAD_REQUEST, message=f'User {accused_user_id} is already a ghost') elif accusing_user_id in self._users and accused_user_id in self._users: self._accuse_votes[accusing_user_id] = accused_user_id return mafia_pb2.Response(status=mafia_pb2.StatusCode.StatusCode_OK, message='OK') else: return mafia_pb2.Response(status=mafia_pb2.StatusCode.StatusCode_BAD_REQUEST, message='Wrong user ids') else: return mafia_pb2.Response( status=mafia_pb2.StatusCode.StatusCode_BAD_REQUEST, message="You can't vote in the first day")
def delete_user(self, request, context): user_id = request.user_id if user_id in self._users: del self._users[user_id] return mafia_pb2.DeleteUserResponse(status=mafia_pb2.StatusCode.StatusCode_OK) else: return mafia_pb2.Response( status=mafia_pb2.StatusCode.StatusCode_NOT_FOUND, message=f"No user with id {user_id}")
def _handle_broadcast_message(self, request): cur_user_id = request.user_id message = request.message user_role = self._roles[cur_user_id] filter_role = lambda _: True if self._day_interval == config.DAY_INTERVAL.night: if user_role in [config.ROLES.mafia, config.ROLES.detective]: filter_role = lambda role: role == user_role else: return mafia_pb2.Response( status=mafia_pb2.StatusCode.StatusCode_FORBIDDEN, message=f"You cannot message during night") for user_id in self._users: if cur_user_id != user_id and filter_role(self._roles[user_id]): self._user_messages[user_id].append( mafia_pb2.CommunicationResponse(message=message, author=self._users[cur_user_id].name)) return mafia_pb2.CommunicationResponse()
def add_user(self, request, context): print(f'User messages: {self._user_messages}') cur_user_id = len(self._users) name = request.name while cur_user_id in self._users: cur_user_id += 1 for user_id in self._users: self._user_messages[user_id].append( mafia_pb2.CommunicationResponse(message=f'User {name} has just been added', author='Admin')) user = mafia_pb2.User(user_id=cur_user_id, name=name) self._users[cur_user_id] = user self._roles[cur_user_id] = config.ROLES.not_assigned self._user_messages[cur_user_id] = [] return mafia_pb2.Response(status=mafia_pb2.StatusCode.StatusCode_CREATED, data={"user_id": str(cur_user_id)})
def wrapper(self, *args, **kwargs): if not self._is_game_finished: return func(self, *args, **kwargs) else: return mafia_pb2.Response( status=mafia_pb2.StatusCode.StatusCode_FORBIDDEN, message=f"Game has already finished")
def get_users(self, request, context): users = '\n'.join([ f'{user.name}, user_id: {user.user_id}, is alive: {self._is_alive(user.user_id)}' for user in self._users.values() ]) return mafia_pb2.Response(status=mafia_pb2.StatusCode.StatusCode_OK, data={"users": users})