def get(self): """Handles requests concerning all the rooms""" rep = self.get_query_argument('cmd') cmd = RoomRequest[rep] msg_ = {} if cmd == RoomRequest.GET_ROOMATES: user_id = self.get_query_argument('userid') room_id = self.get_query_argument('roomid') list_of_roomates = self.controller.get_all_roomates( user_id, room_id) msg_ = DiscardMessage(cmd=ClientRcvMessage.GET_ROOMATES_REP.value, data=list_of_roomates) elif cmd == RoomRequest.GET_ROOMS: list_of_rooms = self.controller.get_all_rooms() msg_ = DiscardMessage(cmd=ClientRcvMessage.GET_ROOMS_REP.value, data=list_of_rooms) elif cmd == RoomRequest.GET_CURRENT_PLAYER: room_id = self.get_query_argument('roomid') player = self.controller.get_current_player(room_id) print("Player: ", player) msg_ = DiscardMessage( cmd=ClientRcvMessage.GET_CURRENT_PLAYER_REP.value, prompt='Currently playing: ', data=player.nickname) self.write(DiscardMessage.to_json(msg_))
def gen_ping(self): """ This generates a ping message to send to the server to ask if roomates can actually start playing the game """ msg_ = DiscardMessage(cmd=RoomGameStatus.ARE_ROOMATES_IN_GAME.value, room_id=self.player.get_room_id(), user_id=self.player.get_user_id()) return DiscardMessage.to_json(msg_)
def broadcast_game_message(self, user_id, room_id, msg): """ Server broadcasts messages to all roomates :param user_id: Player identifier :param room_id: Room identifier :param msg: Message to be broadcasted """ if len(self.game_conns) > 0: for player_conns in self.game_conns: conn = player_conns.wssocket conn.write_message(DiscardMessage.to_json(msg))
def reply_on_game_conn(self, user_id, room_id, msg): """ Server sends a message to a particular Player :param user_id: User identifier :param room_id: Room identifier :param msg: Message to be sent to Player """ print("[[ In reply_on_game_conn ]]") for game_conn in self.game_conns: if all( (game_conn.user_id == user_id, game_conn.room_id == room_id)): game_conn.wssocket.write_message(DiscardMessage.to_json(msg)) break
def gen_test_message(self): """ This generates a user-generated message :param question: Question to be asked """ choice = None if self.has_initialised == True: choice = self.gen_ping() else: choice = self.get_str_input('Send a message: ') if choice == "End": msg_ = {} return json.dumps(msg_) else: msg_ = DiscardMessage(cmd=RoomGameStatus.GAME_MESSAGE.value, data=choice, room_id=self.player.get_room_id(), user_id=self.player.get_user_id()) return DiscardMessage.to_json(msg_)
def main_loop(self): """This is the main loop in the game""" choice = None msg_ = {} prompt, player = self.print_currently_playing() print(prompt, player) choice = self.get_enum_choice(self.get_str_input(self.menu())) while choice == None: print('Wrong option') choice = self.get_enum_choice(self.get_str_input(self.menu())) if choice == MainLoopChoices.PRETTY_HELP: #req = requests.get(self.doc_url) msg_ = DiscardMessage(cmd='DOCUMENTATION') elif choice == MainLoopChoices.CMD_RULES: msg_ = DiscardMessage(cmd='DOCUMENTATION') elif choice == MainLoopChoices.PLAY_ROUND: msg_ = self.player.play() elif choice == MainLoopChoices.LEAVE_GAME: msg_ = DiscardMessage(cmd='DOCUMENTATION') print("[[ Out of main_loop ]] ", msg_) return DiscardMessage.to_json(msg_)
def post(self): """Handles requests concerning a room""" recv_data = escape.json_decode(self.request.body) user_id = recv_data.get('userid') username = recv_data.get('username') query = self.get_query_argument('cmd') cmd = RoomRequest[query] msg_ = {} if cmd == RoomRequest.CREATE_A_ROOM: num_of_players = recv_data.get('num_of_players') room_name = recv_data.get('room_name') print('Num of players: ', num_of_players) if any((num_of_players < 2, num_of_players > 8)): raise web.HTTPError( status_code=400, log_message= "Argument num_of_players must be between 2 and 8") else: room_id = self.controller.create_room(num_of_players, room_name) self.controller.add_player_to_room(room_id, user_id, username) print('Room id: ', room_id) msg_ = DiscardMessage( cmd=ClientRcvMessage.CREATE_A_ROOM_REP.value, data=room_id) self.write(DiscardMessage.to_json(msg_)) elif cmd == RoomRequest.JOIN_ROOM: room_id = recv_data.get('roomid') if self.controller.can_join(room_id, user_id): self.controller.add_player_to_room(room_id, user_id, username) prompt_ = "You have been added to room " + str(room_id) msg_ = DiscardMessage(cmd=ClientRcvMessage.JOIN_ROOM_REP.value, prompt=prompt_) self.write(DiscardMessage.to_json(msg_)) else: raise web.HTTPError(status_code=500, log_message="Room is full") elif cmd == RoomRequest.START_GAME: room_id = recv_data.get('roomid') if self.controller.can_start_game(room_id, user_id): self.controller.start_game(room_id) msg_ = DiscardMessage( cmd=ClientRcvMessage.START_GAME_REP.value, prompt=ClientRcvMessage.GAME_HAS_STARTED_REP.value) else: msg_ = DiscardMessage( cmd=ClientRcvMessage.START_GAME_REP.value, prompt=ClientRcvMessage.GAME_HAS_ALREADY_STARTED_REP.value) self.write(DiscardMessage.to_json(msg_)) elif cmd == RoomRequest.SET_FIRST_PLAYER: room_id = recv_data.get('roomid') self.controller.set_initial_player(user_id, room_id) player = self.controller.get_initial_player(room_id) msg_ = DiscardMessage( cmd=ClientRcvMessage.SET_FIRST_PLAYER_REP.value, prompt='Player to start:', data=player.nickname) self.write(DiscardMessage.to_json(msg_)) elif cmd == RoomRequest.LEAVE_ROOM: pass else: pass