예제 #1
0
 def _copyOthelloGameToForm(self, game):
         game_logic = load_game_logic(game.gamelogic)
         p1, p2 = game_logic._getScore()
         player1 = game.userKeys[0].parent().get().name
         if game_logic.game_mode == TWO_PLAYER_MODE:
             player2 = game.userKeys[1].parent().get().name
         else:
             player2 = None
         return OthelloGameForm(
                 game_id=game.game_id,
                 player_one=player1,
                 player_two=player2,
                 score_p1=p1,
                 score_p2=p2,
                 game_status=game.status,
                 board=game.gamelogic.board,
                 game_mode=game.gamelogic.game_mode)
예제 #2
0
    def _make_move(self, request):
        """ Controller for movements. If game is SINGLE_PLAYER,
        CPU will take turn after player submits valid move.
        Otherwise, CPU waits for valid move or yield from player"""
        game = get_by_game_id(request.game_id)
        if not game:
            return False, "Game not found"
        print "Game status", game.status, type(game.status)
        if not game.status == "ACTIVE":
            return False, "This game is not active"

        if ndb.Key(OthelloPlayer, request.user_name,
                   parent=ndb.Key(User, request.user_name))\
                not in game.userKeys:
            return False, "Player {0} is not registered for this game".\
                    format(request.user_name)
        parent_keys = [p.parent() for p in game.userKeys]
        players = ndb.get_multi(parent_keys)
        print "Players in this game", players

        # load game logic object
        game_logic = load_game_logic(game.gamelogic)

        player_turn = 1 if players[0].name == request.user_name else 2
        isvalid, message = game_logic._make_move(player_turn, request.move)
        print message

        # load an update game history in memory
        game_history = OthelloGameHistory.query(ancestor=game.key).get()
        if not game_history.moves:
            moves = []
        else:
            moves = json.loads(game_history.moves)
        # append moves from user if move is valid
        if isvalid:
            moves.append([request.user_name, request.move])
            if game_logic.game_mode == TWO_PLAYER_MODE:
                # PUSH TASK QUEUE to notify opponent that move has been made
                taskqueue.add(params={'email': players[0].email if
                              player_turn == 2 else players[1].email,
                              'score': game_logic._getScore()},
                              url='/tasks/send_gameturn_notification_email')

        print "Whose turn is it?", game_logic.CP
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        # CPU MOVE

        if isvalid and game_logic.game_mode == SINGLE_PLAYER_MODE:
            print "Calling CPU for move..."
            isvalid, cpu_move = game_logic._cpu_move()
            moves.append(['cpu', cpu_move])
            print message
            message = "Your move was valid. {0}".format(cpu_move)

        # updates game logic with values from OthelloLogic instance
        update_game_logic(game_logic, game.gamelogic)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        # GAME OVER CONDITIONS

        if game_logic._isBoardFull():
            game.status = "ENDED_COMPLETE"
            message = "Game has ended. The board is full!"
        if request.move == '0':
            game.status = "ENDED_INCOMPLETE"
            # record last move 
            moves.append([players[0].name if player_turn == 1 else 
                players[1].name, request.move])
            message = "The user abandoned the game. "
            "Score will be accounted for"

        if not game.status == "ACTIVE":
            # get score
            score_player1, score_player2 = game_logic._getScore()
            score_diff = abs(score_player1-score_player2)
            player1_entry = OthelloScoreBoardEntry.query(
                    ancestor=game.userKeys[0]).get()
            # CHECK END OF TWO PLAYER GAME
            if game_logic.game_mode == TWO_PLAYER_MODE:
                player2_entry = OthelloScoreBoardEntry.query(
                        ancestor=game.userKeys[1]).get()
                if score_player1 > score_player2:
                    # PLAYER 1 WINS
                    message = "Player {0} wins!".format(
                            game.userKeys[0].parent().get().name)
                    player1_entry.points += score_player1
                    player1_entry.wins += 1
                    player1_entry.winning_streak += 1
                    # update score difference average - player 1
                    update_score_difference_ave(
                            player1_entry.score_difference_average,
                            score_diff)
                    player2_entry.winning_streak = 0
                elif score_player1 < score_player2:
                    # PLAYER 2 WINS
                    message = "Player {0} wins!".format(
                            game.userKeys[1].parent().get().name)
                    player2_entry.points += score_player2
                    player2_entry.wins += 1
                    player2_entry.winning_streak += 1
                    # update score difference average - player 2
                    update_score_difference_ave(
                            player2_entry.score_difference_average,
                            score_diff)
                    player1_entry.winning_streak = 0
                else:
                    # tie game
                    message = "This game is tied! No changes in scoreboard"
                # update players scoreboard entries in datastore
                ndb.put_multi([player1_entry, player2_entry])
            # CHECK END FOR SINGLE PLAYER GAME
            if game_logic.game_mode == SINGLE_PLAYER_MODE:
                if score_player1 > score_player2:
                    # PLAYER 1 WINS
                    message = "Player {0} wins!".format(
                            game.userKeys[0].parent().get().name)
                    player1_entry.points += score_player1
                    player1_entry.wins += 1
                    update_score_difference_ave(
                            player1_entry.score_difference_average,
                            score_diff)
                    player1_entry.winning_streak += 1
                elif score_player1 < score_player2:
                    message = "CPU wins."
                    player1_entry.winning_streak = 0
                player1_entry.put()

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        # UPDATE GAME AND HISTORY DATASTORE
        game.put()
        # UPDATE GAME HISTORY
        game_history.moves = json.dumps(moves)
        game_history.put()

        return True, message