Ejemplo n.º 1
0
    def test(self):
        sut = Comrades()
        player = Player("foo")
        opponent = Player("bar")
        for i in range(0, 99):
            game = Game(player.name, 5, opponent.name, 5, 0)
            player.game(game)
            opponent.game(game)
            result = sut.applies(player, game, opponent, None)
            self.assertFalse(result)
            result = sut.applies(opponent, game, player, None)
            self.assertFalse(result)

        game = Game(player.name, 5, opponent.name, 5, 0)
        player.game(game)
        opponent.game(game)
        result = sut.applies(player, game, opponent, None)
        self.assertTrue(result)
        result = sut.applies(opponent, game, player, None)
        self.assertTrue(result)

        game = Game(player.name, 5, opponent.name, 5, 0)
        player.game(game)
        opponent.game(game)
        result = sut.applies(player, game, opponent, None)
        self.assertFalse(result)
        result = sut.applies(opponent, game, player, None)
        self.assertFalse(result)
Ejemplo n.º 2
0
    def testTwice(self):
        sut = Consistency()
        player = Player("foo")
        opponent = Player("bar")
        for i in range(0, 4):
            game = Game(player.name, 2, opponent.name, 8, i)
            player.game(game)
            opponent.game(game)
            result = sut.applies(player, game, opponent, None)
            self.assertFalse(result)
        game = Game(player.name, 2, opponent.name, 8, 4)
        player.game(game)
        opponent.game(game)
        result = sut.applies(player, game, opponent, None)
        self.assertTrue(result)

        for i in range(5, 9):
            game = Game(player.name, 2, opponent.name, 8, i)
            player.game(game)
            opponent.game(game)
            result = sut.applies(player, game, opponent, None)
            self.assertFalse(result)
        game = Game(player.name, 2, opponent.name, 8, 9)
        player.game(game)
        opponent.game(game)
        result = sut.applies(player, game, opponent, None)
        self.assertTrue(result)
Ejemplo n.º 3
0
    def test(self):
        games = [Game('red', 6, 'blue', 4, 1), Game('red', 1, 'blue', 9, 1)]

        games = eloTransform.do(games)

        self.assertAlmostEqual(-2.5, games[0].skillChangeToBlue)
        self.assertAlmostEqual(10.400, games[1].skillChangeToBlue, 3)
Ejemplo n.º 4
0
 def _createGame(self, args):
     game = None
     if len(args) == 5 or len(args) == 7:
         game = Game(args[0], args[1], args[2], args[3], args[4])
         if len(args) == 7:
             game.deletedBy = args[5]
             game.deletedAt = args[6]
     return game
Ejemplo n.º 5
0
 def _createGame(self, args):
     game = None
     if len(args) == 5 or len(args) == 7:
         game = Game(args[0], args[1], args[2], args[3], args[4])
         if len(args) == 7:
             game.deletedBy = args[5]
             game.deletedAt = args[6]
     return game
Ejemplo n.º 6
0
    def testInterrupted(self):
        sut = TheDominator()
        player = Player("foo")
        opponent = Player("bar")
        for i in range(0, 9):
            game = Game(player.name, 10, opponent.name, 0, i)
            game.skillChangeToBlue = -1
            player.game(game)
            opponent.game(game)
            result = sut.applies(player, game, opponent, None)
            self.assertFalse(result)

        baz = Player("baz")
        game = Game(player.name, 10, baz.name, 0, 9)
        game.skillChangeToBlue = -1
        player.game(game)
        result = sut.applies(player, game, baz, None)
        self.assertFalse(result)

        game = Game(player.name, 0, opponent.name, 10, 10)
        game.skillChangeToBlue = -1
        player.game(game)
        opponent.game(game)
        result = sut.applies(player, game, opponent, None)
        self.assertFalse(result)
        result = sut.applies(opponent, game, player, None)
        self.assertFalse(result)
Ejemplo n.º 7
0
    def testUnique(self):
        games = [
            Game('red', 6, 'blue', 4, 1),
            Game('red', 6, 'blue', 4, 86401),
        ]

        games = achievementTransform.do(games)

        self.assertIn('Early Bird', [a.name for a in games[0].redAchievements])
        self.assertNotIn('Early Bird',
                         [a.name for a in games[1].redAchievements])
Ejemplo n.º 8
0
 def testUnder50(self):
     ach = AgainstTheOdds()
     player = Player("foo")
     player.elo = 0
     opponent = Player("bar")
     opponent.elo = 49
     game = Game(player.name, 10, opponent.name, 0, 0)
     game.skillChangeToBlue = -50
     player.game(game)
     opponent.game(game)
     result = ach.applies(player, game, opponent, None)
     self.assertFalse(result)
Ejemplo n.º 9
0
 def testOver50_2(self):
     ach = AgainstTheOdds()
     player = Player("foo")
     player.elo = 0
     opponent = Player("baz")
     opponent.elo = 50
     game = Game(opponent.name, 0, player.name, 10, 0)
     game.skillChangeToBlue = 10
     player.game(game)
     opponent.game(game)
     result = ach.applies(player, game, opponent, None)
     self.assertTrue(result)
Ejemplo n.º 10
0
 def testOver100(self):
     ach = AgainstAllOdds()
     player = Player("foo")
     player.elo = 0
     opponent = Player("bar")
     opponent.elo = 100
     game = Game(player.name, 10, opponent.name, 0, 0)
     game.skillChangeToBlue = -1
     player.game(game)
     opponent.game(game)
     result = ach.applies(player, game, opponent, None)
     self.assertTrue(result)
Ejemplo n.º 11
0
 def getGames(self):
     games = []
     with open(self._ladderFilePath, 'r') as ladder:
         for line in ladder.readlines():
             gameLine = line.split()
             # Red player, red score, blue player, blue score, time[, deletedBy, deletedAt]
             if len(gameLine) == 5 or len(gameLine) == 7:
                 game = Game(gameLine[0], gameLine[1], gameLine[2], gameLine[3], int(gameLine[4]))
                 games.append(game)
                 if len(gameLine) == 7:
                     game.deletedBy = gameLine[5]
                     game.deletedAt = int(gameLine[6])
     return games
Ejemplo n.º 12
0
    def testTransfer(self):
        games = [
            Game('red', 6, 'blue', 4, 0),
            Game('red', 5, 'blue', 5, 1),
            Game('red', 6, 'blue', 4, 2),
            Game('red', 1, 'blue', 9, 3),
        ]

        games = beltTransform.do(games)
        self._assert(games[0], True, 'red')
        self._assert(games[1], False, None)
        self._assert(games[2], False, None)
        self._assert(games[3], False, 'blue')
Ejemplo n.º 13
0
    def testFreeBelt(self):
        timeout = 60 * 60 * 24 * constants.DAYS_INACTIVE
        games = [
            Game('red', 6, 'blue', 4, 0),
            Game('red', 5, 'blue', 5, 1),
            Game('red', 6, 'blue', 4, 2),
            Game('foo', 1, 'blue', 9, timeout + 3),
        ]

        games = beltTransform.do(games)
        self._assert(games[0], True, 'red')
        self._assert(games[1], False, None)
        self._assert(games[2], False, None)
        self._assert(games[3], True, 'blue')
Ejemplo n.º 14
0
 def getGames(self):
     games = []
     ladder = urlopen(self._ladderFilePath)
     for line in ladder:
         gameLine = line.decode('utf-8').split()
         numParts = len(gameLine)
         # Red player, red score, blue player, blue score, time[, deletedBy, deletedAt]
         if numParts == 5 or numParts == 7:
             game = Game(gameLine[0], gameLine[1], gameLine[2], gameLine[3],
                         gameLine[4])
             games.append(game)
             if numParts == 7:
                 game.deletedBy = gameLine[5]
                 game.deletedAt = int(gameLine[6])
     return games
Ejemplo n.º 15
0
 def test(self):
     sut = Dedication()
     player = Player("foo")
     opponent = Player("bar")
     timeBetweenGames = 60*60*24* 59
     for i in range(0, 7):
         game = Game(player.name, 5, opponent.name, 5, i * timeBetweenGames)
         player.game(game)
         opponent.game(game)
         result = sut.applies(player, game, opponent, None)
         self.assertFalse(result)
     game = Game(player.name, 5, opponent.name, 5, 7 * timeBetweenGames)
     player.game(game)
     opponent.game(game)
     result = sut.applies(player, game, opponent, None)
     self.assertTrue(result)
Ejemplo n.º 16
0
    def test(self):
        player = Player('foo')
        game = Game('foo', 4, 'red', 6, 1)
        game.skillChangeToBlue = -2
        player.games.append(game)

        actual = sut.perPlayerStatsToJson(sut.getPerPlayerStats(player))

        self.assertEqual(len(actual), 1)
        self.assertEqual(actual[0]['opponent'], 'red')
        self.assertEqual(actual[0]['skillChange'], 2)
        self.assertEqual(actual[0]['for'], 4)
        self.assertEqual(actual[0]['against'], 6)
        self.assertEqual(actual[0]['games'], 1)
        self.assertEqual(actual[0]['wins'], 0)
        self.assertEqual(actual[0]['losses'], 1)
Ejemplo n.º 17
0
    def testFirstDraw(self):
        games = [
            Game('red', 5, 'blue', 5, 0),
        ]

        games = beltTransform.do(games)
        self._assert(games[0], True, None)
Ejemplo n.º 18
0
    def testInactive(self):
        games = []
        game = Game('red', 6, 'blue', 4, 1)
        game.skillChangeToBlue = -2.5
        games.append(game)
        game = Game('red', 5, 'green', 5, 6000000)
        game.skillChangeToBlue = 0
        games.append(game)

        games = rankTransform.do(games)

        self.assertEqual(1, games[0].redPosAfter)
        self.assertEqual(2, games[0].bluePosAfter)
        self.assertEqual(1, games[1].redPosAfter)
        self.assertEqual(0, games[1].redPosChange)
        self.assertEqual(2, games[1].bluePosAfter)
        self.assertEqual(0, games[1].bluePosChange)
Ejemplo n.º 19
0
    def test(self):
        game = Game('red', 4, 'blue', 6, 1)
        game.skillChangeToBlue = 2
        game.redPosChange = -1
        game.redPosAfter = 2
        game.bluePosChange = 1
        game.bluePosAfter = 1

        actual = sut.gameToJson(game, '../')

        self.assertEqual(actual['red']['name'], 'red')
        self.assertEqual(actual['red']['href'], '../player/red/json')
        self.assertEqual(actual['red']['score'], 4)
        self.assertEqual(actual['red']['skillChange'], -2)
        self.assertEqual(actual['red']['rankChange'], -1)
        self.assertEqual(actual['red']['newRank'], 2)

        self.assertEqual(actual['blue']['name'], 'blue')
        self.assertEqual(actual['blue']['href'], '../player/blue/json')
        self.assertEqual(actual['blue']['score'], 6)
        self.assertEqual(actual['blue']['skillChange'], 2)
        self.assertEqual(actual['blue']['rankChange'], 1)
        self.assertEqual(actual['blue']['newRank'], 1)

        self.assertEqual(actual['positionSwap'], True)
        self.assertEqual(actual['date'], 1)
Ejemplo n.º 20
0
    def testFirstGameSince(self):
        player = Player("foo")
        opponent = Player("bar")
        game = Game(player.name, 5, opponent.name, 5, 10)
        player.games.append(game)
        opponent.games.append(game)
        opponent.games.append(Game(opponent.name, 5, "baz", 5, 11))
        retireTime = (60 * 60 * 24 * constants.DAYS_INACTIVE)
        game = Game(player.name, 5, opponent.name, 5, 11 + retireTime)
        player.games.append(game)
        opponent.games.append(game)

        sut = FirstGameSince()
        result = sut.getFact(player, player.games[-1], None)
        self.assertEqual(
            result, "That was foo's first game since retiring on 1970-02-12.")
        result = sut.getFact(opponent, opponent.games[-1], None)
        self.assertIsNone(result)
Ejemplo n.º 21
0
 def addAndWriteGame(self, redPlayer, redScore, bluePlayer, blueScore):
     game = None
     redScore = int(redScore)
     blueScore = int(blueScore)
     if redScore >= 0 and blueScore >= 0 and (redScore + blueScore) > 0:
         game = Game(redPlayer, redScore, bluePlayer, blueScore, int(time.time()))
         self.addGame(game)
         self._gameStore.writeGame(game)
     return game
Ejemplo n.º 22
0
 def test(self):
     sut = PokeMaster()
     player = Player("foo")
     opponent = Player("bar")
     game = Game(player.name, 0, opponent.name, 10, 0)
     player.game(game)
     opponent.game(game)
     for i in range(0, 10):
         game = Game(player.name, i, opponent.name, 10 - i, i)
         player.game(game)
         opponent.game(game)
         result = sut.applies(player, game, opponent, None)
         self.assertFalse(result)
     game = Game(player.name, 10, opponent.name, 0, 10)
     player.game(game)
     opponent.game(game)
     result = sut.applies(player, game, opponent, None)
     self.assertTrue(result)
Ejemplo n.º 23
0
    def test(self):
        games = [Game('red', 6, 'blue', 4, 1)]

        games = achievementTransform.do(games)

        self.assertEqual(3, len(games[0].redAchievements))
        achievements = [a.name for a in games[0].redAchievements]
        self.assertIn('First Game', achievements)
        self.assertIn('Early Bird', achievements)
        self.assertIn('Night Owl', achievements)
Ejemplo n.º 24
0
 def testInterrupted(self):
     sut = Consistency()
     player = Player("foo")
     opponent = Player("bar")
     for i in range(0, 4):
         game = Game(player.name, 2, opponent.name, 8, i)
         player.game(game)
         opponent.game(game)
         result = sut.applies(player, game, opponent, None)
         self.assertFalse(result)
     game = Game(player.name, 3, opponent.name, 7, 4)
     player.game(game)
     opponent.game(game)
     result = sut.applies(player, game, opponent, None)
     self.assertFalse(result)
     game = Game(player.name, 2, opponent.name, 8, 5)
     player.game(game)
     opponent.game(game)
     result = sut.applies(player, game, opponent, None)
     self.assertFalse(result)
Ejemplo n.º 25
0
    def testWinsThenDraw(self):
        player = Player("foo")
        opponent = Player("bar")
        bulkAppend(player, 6, opponent, 4, 0, 10)
        player.games.append(Game(player.name, 5, opponent.name, 5, 10))

        sut = Wins()
        result = sut.getFact(player, player.games[9], None)
        self.assertEqual(result, "That was foo's 10th win.")
        result = sut.getFact(player, player.games[10], None)
        self.assertIsNone(result)
Ejemplo n.º 26
0
def deserialise(serialisedGames):
    game_parts = serialisedGames.split(',')
    games = []
    now = time.time()
    num_games = len(game_parts) // 4
    for i in range(0, num_games):
        g = Game(game_parts[4 * i].lower(), game_parts[4 * i + 1],
                 game_parts[4 * i + 3].lower(), game_parts[4 * i + 2],
                 now - (num_games - i))
        games.append(g)
    return games
Ejemplo n.º 27
0
    def testGoalsThenNone(self):
        player = Player("foo")
        opponent = Player("bar")
        bulkAppend(player, 5, opponent, 5, 0, 10)
        player.games.append(Game(player.name, 0, opponent.name, 10, 10))

        sut = Goals()
        result = sut.getFact(player, player.games[9], None)
        self.assertEqual(result, "That game featured foo's 50th goal.")
        result = sut.getFact(player, player.games[10], None)
        self.assertIsNone(result)
Ejemplo n.º 28
0
 def appendGame(self, redPlayer, redScore, bluePlayer, blueScore):
     game = None
     redScore = int(redScore)
     blueScore = int(blueScore)
     if redScore >= 0 and blueScore >= 0 and (redScore + blueScore) > 0:
         game = Game(redPlayer, redScore, bluePlayer, blueScore, int(time.time()))
         self._gameStore.appendGame(game)
         # Invalidate
         self.games = None
         self.players = None
         return game.time
     return None
Ejemplo n.º 29
0
    def testFirstGame(self):
        ladder = TableFootballLadder(os.path.join(__location__, "emptyLadder.txt"), False)
        player = Player("foo")
        opponent = Player("baz")
        game = Game(opponent.name, 0, player.name, 10, 0)
        ladder.addGame(game)

        sut = EarlyBird()
        result = sut.applies(player, game, opponent, ladder)
        self.assertTrue(result)
        result = sut.applies(opponent, game, player, ladder)
        self.assertFalse(result)
Ejemplo n.º 30
0
    def test(self):
        player = Player('foo')
        player.achieve([FlawlessVictory().__class__],
                       Game('foo', 4, 'red', 6, 1))

        actual = sut.getPlayerAchievementsJson(player)

        self.assertEqual(len(actual), 25)
        self.assertEqual(len([a for a in actual if 'time' in a]), 1)
        self.assertIn('name', actual[0])
        self.assertIn('description', actual[0])
        self.assertIn('time', actual[0])
Ejemplo n.º 31
0
    def testReactive(self):
        games = []
        game = Game('red', 6, 'blue', 4, 1)
        game.skillChangeToBlue = -2.5
        games.append(game)
        game = Game('red', 5, 'green', 5, 6000000)
        game.skillChangeToBlue = 0
        games.append(game)
        game = Game('red', 1, 'blue', 9, 7000000)
        game.skillChangeToBlue = 10.4
        games.append(game)

        games = rankTransform.do(games)

        self.assertEqual(3, games[2].redPosAfter)
        self.assertEqual(-2, games[2].redPosChange)
        self.assertEqual(1, games[2].bluePosAfter)
        self.assertEqual(0, games[2].bluePosChange)
Ejemplo n.º 32
0
 def _create(self):
     player = Player("foo")
     game0 = Game(player.name, 10, "bar", 0, 1)
     game0.skillChangeToBlue = 2
     player.games.append(game0)
     game1 = Game(player.name, 10, "bar", 0, 2)
     game1.skillChangeToBlue = -3
     player.games.append(game1)
     game2 = Game(player.name, 10, "bar", 0, 3)
     game2.skillChangeToBlue = 4
     player.games.append(game2)
     game3 = Game(player.name, 10, "bar", 0, 4)
     game3.skillChangeToBlue = -1
     player.games.append(game3)
     return player
Ejemplo n.º 33
0
    def test(self):
        sut = Unstable()
        player = Player("foo")
        opponent = Player("bar")

        game = Game(player.name, 5, opponent.name, 5, 0)
        game.skillChangeToBlue = -5
        player.game(game)
        opponent.game(game)
        result = sut.applies(player, game, opponent, None)
        self.assertFalse(result)
        result = sut.applies(opponent, game, player, None)
        self.assertFalse(result)

        game = Game(player.name, 10, opponent.name, 0, 0)
        game.skillChangeToBlue = -5
        player.game(game)
        opponent.game(game)
        result = sut.applies(player, game, opponent, None)
        self.assertFalse(result)
        result = sut.applies(opponent, game, player, None)
        self.assertFalse(result)

        game = Game(player.name, 0, "bar", 10, 1)
        game.skillChangeToBlue = 5
        player.game(game)
        opponent.game(game)
        result = sut.applies(player, game, opponent, None)
        self.assertTrue(result)
        result = sut.applies(opponent, game, player, None)
        self.assertTrue(result)

        game = Game(player.name, 0, "bar", 10, 1)
        game.skillChangeToBlue = 5
        player.game(game)
        opponent.game(game)
        result = sut.applies(player, game, opponent, None)
        self.assertFalse(result)
        result = sut.applies(opponent, game, player, None)
        self.assertFalse(result)
Ejemplo n.º 34
0
    def test(self):
        games = []
        game = Game('red', 6, 'blue', 4, 1)
        game.skillChangeToBlue = -2.5
        games.append(game)
        game = Game('red', 1, 'blue', 9, 1)
        game.skillChangeToBlue = 10.4
        games.append(game)

        games = rankTransform.do(games)

        self.assertEqual(1, games[0].redPosAfter)
        self.assertEqual(2, games[0].bluePosAfter)
        self.assertEqual(2, games[1].redPosAfter)
        self.assertEqual(-1, games[1].redPosChange)
        self.assertEqual(1, games[1].bluePosAfter)
        self.assertEqual(1, games[1].bluePosChange)
Ejemplo n.º 35
0
def addGame(red, redScore, blue, blueScore, time, skillChangeToBlue=0):
    game = Game(red.name, redScore, blue.name, blueScore, time)
    game.skillChangeToBlue = skillChangeToBlue
    red.game(game)
    blue.game(game)
    return game