Пример #1
0
    def test_handle_game_logic1(self):
        print "\n###################\nRunning Tests for game_logic.py now...\n"
        print "\nTest #1 game logic @ empty gs (player to place 5)"

        # test player first empty gs receives 5 cards
        xs = create_test_gs_vars()
        test_game_state = xs[0]
        test_game_id = xs[1]

        # disable stdout to avoid screen clutter
        f = open(os.devnull, 'w')
        sys.stdout = f

        # call game_logic function
        result = json.loads(
            game_logic.handle_game_logic(test_game_state, test_game_id))

        f.close()
        # re-enable stdout
        sys.stdout = sys.__stdout__

        # assert that 5 cards have been returned
        for potential_card in result:
            self.assertEqual(True, isCard(str(potential_card)))

        print "Passed!"
Пример #2
0
    def test_handle_game_logic4(self):
        print "\nTest #4 game logic @ game end"

        xs = create_test_gs_vars()
        test_game_state = xs[0]
        test_game_id = xs[1]
        test_db_state = xs[2]
        test_games = xs[3]

        test_db_state['deck'] = {}
        test_db_state['deck']['cards'] = possible_cards
        test_db_state['deck']['current-position'] = 25

        # initialise both players final game states (full boards)
        for i in range(1, 14):
            test_db_state['properties1']['cards']['items'][
                'position' + str(i)] = possible_cards[i - 1]
            test_db_state['properties2']['cards']['items'][
                'position' + str(i)] = possible_cards[i + 12]
        test_db_state['cardtoplace'] = possible_cards[12]  # stripped in scorer
        test_db_state['score'] = 0

        update = test_games.update({'_id': ObjectId(test_game_id)},
                                   {'$set': test_db_state})

        test_game_state = test_db_state

        # disable stdout temporarily to avoid cluttering screen
        f = open(os.devnull, 'w')
        sys.stdout = f

        # get result of game_logic function
        result = json.loads(
            game_logic.handle_game_logic(test_game_state, test_game_id))
        f.close()

        # re-enable stdout
        sys.stdout = sys.__stdout__

        # assert that a valid score array was returned
        self.assertEqual(type([]), type(result))
        self.assertEqual(5, len(result))
        for i in range(0, 3):
            for j in range(0, 3):
                self.assertEqual(type(1), type(result[i][j]))
        for x in result[3]:
            self.assertEqual(type(True), type(x))
        self.assertEqual(type(1), type(result[4][0]))

        print "Passed!"
Пример #3
0
    def ofc_backend(self, **params):
        ''' This page handles the OFC backend
        Loads game state and game id from POSTed params and passes to game_logic script
        return JSON dump of response to frontend
        '''
        try:
                game_state = json.loads(params['game-state']) # loads game state as dictionary
                game_id = params['game-id']
        except:
                ef = open('error_log.txt','a')
                ef.write( '\n{} invalid json: {}'.format(datetime.now(), params) )
                raise cherrypy.HTTPError(403, "There was an error loading the given game state.")

        response = game_logic.handle_game_logic(game_state, game_id)
        if response == 0:
            raise cherrypy.HTTPError(500, "Error in player's POSTed game state: inconsistency with stored state in database!")
        elif response == 1:
            raise cherrypy.HTTPError(500, "Error with AI's turn handling!")
        else:
            return response
Пример #4
0
    def test_handle_game_logic2(self):
        print "\nTest #2 game logic @ AIs first turn"

        # test player first AI's round 1 - should see updated db state
        xs = create_test_gs_vars()
        test_game_state = xs[0]
        test_game_id = xs[1]
        test_db_state = xs[2]
        test_games = xs[3]

        # set up test database to replicate a game state after a player's initial turn
        test_db_state['deck'] = {}
        test_db_state['deck'][
            'cards'] = possible_cards  # default order h 1-13, s 1-13, d 1-13, c 1-13
        test_db_state['deck']['current-position'] = 5
        test_db_state['first5cards'] = "h01h02h03h04h05"

        update = test_games.update({'_id': ObjectId(test_game_id)},
                                   {'$set': test_db_state})

        # simulate placement of deck's first 5 cards
        for i in range(1, 6):
            test_game_state['properties1']['cards']['items'][
                'position' + str(i)] = possible_cards[i - 1]

        # disable stdout temporarily to avoid cluttering screen
        f = open(os.devnull, 'w')
        sys.stdout = f

        # get result of game_logic function
        result = json.loads(
            game_logic.handle_game_logic(test_game_state, test_game_id))
        f.close()

        # re-enable stdout
        sys.stdout = sys.__stdout__

        if 'cardtoplace' not in result:
            raise KeyError()

        # ensure a valid card was returned for player's next turn
        self.assertEqual(True, isCard(str(result['cardtoplace'])))

        # check database was updated with appropriate cards
        p2CardCount = 0
        for i in range(1, 14):
            x = result['properties1']['cards']['items']['position' + str(i)]
            # we tested p1's cards placed in bottom row
            if i < 6:
                self.assertEqual(True, isCard(str(x)))
            else:
                self.assertEqual(None, x)

            # count AI's cards and validate type
            y = result['properties2']['cards']['items']['position' + str(i)]
            if y is not None:
                p2CardCount += 1
                self.assertEqual(True, isCard(str(y)))
        self.assertEqual(5, p2CardCount)

        print "Passed!"
Пример #5
0
    def test_handle_game_logic3(self):
        print "\nTest #3 game logic @ general case"

        xs = create_test_gs_vars()
        test_game_state = xs[0]
        test_game_id = xs[1]
        test_db_state = xs[2]
        test_games = xs[3]

        test_db_state['deck'] = {}
        test_db_state['deck']['cards'] = possible_cards
        test_db_state['deck']['current-position'] = 10

        # initialise both players first 5 cards
        for i in range(1, 6):
            test_db_state['properties1']['cards']['items'][
                'position' + str(i)] = possible_cards[i - 1]
            test_db_state['properties2']['cards']['items'][
                'position' + str(i)] = possible_cards[i + 4]

        update = test_games.update({'_id': ObjectId(test_game_id)},
                                   {'$set': test_db_state})

        # add player's placed card from this round
        test_game_state = test_db_state
        test_game_state['properties1']['cards']['items'][
            'position6'] = possible_cards[10]

        # disable stdout temporarily to avoid cluttering screen
        f = open(os.devnull, 'w')
        sys.stdout = f

        # get result of game_logic function
        result = json.loads(
            game_logic.handle_game_logic(test_game_state, test_game_id))
        f.close()

        # re-enable stdout
        sys.stdout = sys.__stdout__

        if 'cardtoplace' not in result:
            raise KeyError()

        # ensure a valid card was returned for player's next turn
        self.assertEqual(True, isCard(str(result['cardtoplace'])))

        # check database was updated with appropriate cards
        p2CardCount = 0
        for i in range(1, 14):
            x = result['properties1']['cards']['items']['position' + str(i)]
            if i < 7:
                self.assertEqual(True, isCard(str(x)))
            else:
                self.assertEqual(None, x)

            # count AI's cards and validate type
            y = result['properties2']['cards']['items']['position' + str(i)]
            if y is not None:
                p2CardCount += 1
                self.assertEqual(True, isCard(str(y)))
        self.assertEqual(6, p2CardCount)

        print "Passed!"