def find_game_for_session(self): try: game = GameModel.objects.get(id=self.game_id).toGame() except GameModel.DoesNotExist: game = None # Make new GameModel and persist if game is None: game = Game() game.place_some_ships() instructions = game.get_player_instructions() send_message(to=self.player_1_num, text=instructions[Players.PLAYER_ONE]) send_message(to=self.player_2_num, text=instructions[Players.PLAYER_TWO]) game_model = GameModel.toModel(game) print('CREATING NEW GAME') game_model.save() print('Created new game_model', game_model.id) print(game_model) self.game_id = game.session_id = game_model.id self.save() else: print('GAME FOUND') print(game) return game
def index(request): # configVal = os.environ.get('A_CONFIG_VAR') send_message('447484292181', 'This is a test message.') # return HttpResponse('Hello from Python! =>' + configVal) return render(request, "index.html")
def player_2_quit(self): p1Message = 'Player 2 left' p2Message = 'Bye' send_message(to=self.player_1_num, text=p1Message) send_message(to=self.player_2_num, text=p2Message) self.player_2_state = PlayerState.QUIT self.session_state = SessionState.ENDED self.save() return {'p1': p1Message, 'p2': p2Message}
def player_2_rejected_invite(self): p1Message = self.player_2_num + ' has rejected your invite.' + ' reply ' + Keyword.INVITE.value + ' with a number to invite someone else' p2Message = 'Why so serious?' send_message(to=self.player_1_num, text=p1Message) send_message(to=self.player_2_num, text=p2Message) self.player_2_state = PlayerState.REJECTED self.session_state = SessionState.ENDED self.save() return {'p1': p1Message, 'p2': p2Message}
def player_2_accepted_invite(self): p1Message = self.player_2_num + ' has accepted your invite.' + ' reply ' + Keyword.QUIT.value + ' to quit' p2Message = 'You are now in a game with: ' + self.player_1_num + ' reply ' + Keyword.QUIT.value + ' to quit' send_message(to=self.player_1_num, text=p1Message) send_message(to=self.player_2_num, text=p2Message) self.player_1_state = PlayerState.IN_GAME self.player_2_state = PlayerState.IN_GAME self.session_state = SessionState.IN_GAME self.save() return {'p1': p1Message, 'p2': p2Message}
def invite_player_2(self): print("INVITING " + str(self.player_2_num)) self.session_state = SessionState.STARTING self.player_1_state = PlayerState.STARTING p1Message = self.player_2_num + ' has been invited' p2Message = self.player_1_num + ' has invited you to a game of Battleships - text ' + Keyword.ACCEPT.value + ' to join the game or ' + Keyword.QUIT.value + ' to refuse' send_message(to=self.player_1_num, text=p1Message) send_message(to=self.player_2_num, text=p2Message) self.player_2_state = PlayerState.INVITED self.save() return {'p1': p1Message, 'p2': p2Message}
def process_game_action(self, sent_by, first_word, remainder): game = self.find_game_for_session() # Do player turn playing_player = self.get_player(sent_by) game_response = process_action(game, first_word, playing_player, remainder) p1Message = game_response[Players.PLAYER_ONE] p2Message = game_response[Players.PLAYER_TWO] game_finished = game_response['GAME_OVER'] next_player = game.current_player # it has been updated :) if next_player == Players.PLAYER_TWO: send_message_with_board(to=self.player_1_num, text=p1Message, board=game_response['PLAYER_2_BOARD']) send_message(to=self.player_2_num, text=p2Message) else: send_message(to=self.player_1_num, text=p1Message) send_message_with_board(to=self.player_2_num, text=p2Message, board=game_response['PLAYER_1_BOARD']) random_event_messages = game.process_random_events() if random_event_messages: send_message(to=self.player_1_num, text=random_event_messages[Players.PLAYER_ONE]) send_message(to=self.player_2_num, text=random_event_messages[Players.PLAYER_TWO]) game_finished = random_event_messages['GAME_OVER'] # Persist GameModel game_model = GameModel.toModel(game) game_model.save() return game_finished