Exemplo n.º 1
0
    def testRead(self):
        try:
            game1 = ("tlr", 0, "cjm", 10, 1445443858)
            filePath = "temp.txt"
            with open(filePath, 'w') as temp:
                temp.write("\n%s %s %s %s %.0f" % game1)

            sut = GameStore(filePath)
            result = sut.getGames()
            self.assertEqual(len(result), 1)
            self._assertGame(result[0], game1)
        finally:
            os.remove(filePath)
Exemplo n.º 2
0
    def testRead(self):
        try:
            game1 = ("tlr", 0, "cjm", 10, 1445443858)
            filePath = "temp.txt"
            with open(filePath, 'w') as temp:
                temp.write("\n%s %s %s %s %.0f" % game1)

            sut = GameStore(filePath)
            result = sut.getGames()
            self.assertEqual(len(result), 1)
            self._assertGame(result[0], game1)
        finally:
            os.remove(filePath)
Exemplo n.º 3
0
class CachingGameStore(object):
    _cacheFilePath = "cache"

    def __init__(self, ladderFilePath, useCache):
        self._gameStore = GameStore(ladderFilePath)
        self._usingCache = useCache

    def loadGames(self, ladder, ladderTime):
        loaded = False
        if ladderTime['now']:
            loaded = self._loadFromCache(ladder)
        if not loaded:
            self._loadFromStore(ladder, ladderTime)
            if ladderTime['now']:
                self._writeToCache(ladder)

    def writeGame(self, game):
        self._deleteCache()
        self._gameStore.appendGame(game)

    def deleteGame(self, gameTime, deletedBy):
        self._deleteCache()
        return self._gameStore.deleteGame(gameTime, deletedBy)

    def _loadFromStore(self, ladder, ladderTime):
        loadedGames = self._gameStore.getGames()
        if not ladderTime['now']:
            loadedGames = [g for g in loadedGames if ladderTime['range'][0] <= g.time and g.time <= ladderTime['range'][1]]
        for loadedGame in loadedGames:
            ladder.addGame(loadedGame)

    def _loadFromCache(self, ladder):
        if os.path.exists(self._cacheFilePath) and self._usingCache:
            ladder.games = pickle.load(open(self._cacheFilePath, 'rb'))
            for game in [g for g in ladder.games if not g.isDeleted()]:
                red = ladder.getPlayer(game.redPlayer)
                blue = ladder.getPlayer(game.bluePlayer)
                red.game(game)
                blue.game(game)
                red.achieve(game.redAchievements, game)
                blue.achieve(game.blueAchievements, game)
            return True
        return False

    def _writeToCache(self, ladder):
        if self._usingCache:
            pickle.dump(ladder.games, open(self._cacheFilePath, 'wb'), pickle.HIGHEST_PROTOCOL)

    def _deleteCache(self):
        if os.path.exists(self._cacheFilePath) and self._usingCache:
            os.remove(self._cacheFilePath)
Exemplo n.º 4
0
    def testDelete(self):
        try:
            game1 = ("tlr", 0, "cjm", 10, 1445443858)
            filePath = "temp.txt"
            with open(filePath, 'w') as temp:
                temp.write("\n%s %s %s %s %.0f" % game1)

            sut = GameStore(filePath)
            sut.deleteGame(1445443858, "foo", 1445443861)

            result = sut.getGames()
            self.assertEqual(len(result), 1)
            self._assertGame(result[0], game1 + ("foo", 1445443861))
            with open(filePath, 'r') as temp:
                self.assertMultiLineEqual(temp.read(), "\ntlr 0 cjm 10 1445443858 foo 1445443861")
        finally:
            os.remove(filePath)
Exemplo n.º 5
0
    def testDelete(self):
        try:
            game1 = ("tlr", 0, "cjm", 10, 1445443858)
            filePath = "temp.txt"
            with open(filePath, 'w') as temp:
                temp.write("\n%s %s %s %s %.0f" % game1)

            sut = GameStore(filePath)
            sut.deleteGame(1445443858, "foo", 1445443861)

            result = sut.getGames()
            self.assertEqual(len(result), 1)
            self._assertGame(result[0], game1 + ("foo", 1445443861))
            with open(filePath, 'r') as temp:
                self.assertMultiLineEqual(
                    temp.read(), "\ntlr 0 cjm 10 1445443858 foo 1445443861")
        finally:
            os.remove(filePath)
Exemplo n.º 6
0
    def testAppend(self):
        try:
            game1 = ("tlr", 0, "cjm", 10, 1445443858)
            game2 = ("jma", 5, "tmm", 5, 1445443859)
            filePath = "temp.txt"
            with open(filePath, 'w') as temp:
                temp.write("\n%s %s %s %s %.0f" % game1)

            sut = GameStore(filePath)
            sut.appendGame(self._createGame(game2))
            result = sut.getGames()
            self.assertEqual(len(result), 2)
            self._assertGame(result[0], game1)
            self._assertGame(result[1], game2)
            with open(filePath, 'r') as temp:
                self.assertMultiLineEqual(temp.read(), "\ntlr 0 cjm 10 1445443858\njma 5 tmm 5 1445443859")
        finally:
            os.remove(filePath)
Exemplo n.º 7
0
    def testAppend(self):
        try:
            game1 = ("tlr", 0, "cjm", 10, 1445443858)
            game2 = ("jma", 5, "tmm", 5, 1445443859)
            filePath = "temp.txt"
            with open(filePath, 'w') as temp:
                temp.write("\n%s %s %s %s %.0f" % game1)

            sut = GameStore(filePath)
            sut.appendGame(self._createGame(game2))
            result = sut.getGames()
            self.assertEqual(len(result), 2)
            self._assertGame(result[0], game1)
            self._assertGame(result[1], game2)
            with open(filePath, 'r') as temp:
                self.assertMultiLineEqual(
                    temp.read(),
                    "\ntlr 0 cjm 10 1445443858\njma 5 tmm 5 1445443859")
        finally:
            os.remove(filePath)
Exemplo n.º 8
0
    def testGetAppendDelete(self):
        try:
            game1 = ("tlr", 0, "cjm", 10, 1445443858)
            game2 = ("foo", 2, "bar", 8, 1445443859)
            filePath = "temp.txt"
            with open(filePath, 'w') as temp:
                temp.write("\n%s %s %s %s %.0f" % game1)
                temp.write("\n%s %s %s %s %.0f" % game2)

            sut = GameStore(filePath)
            result = sut.getGames()
            self.assertEqual(len(result), 2)
            self._assertGame(result[0], game1)
            self._assertGame(result[1], game2)

            game3 = ("bar", 8, "baz", 2, 1445443860)
            game4 = ("bim", 8, "bob", 2, 1445443861)
            sut.appendGame(self._createGame(game3))
            sut.appendGame(self._createGame(game4))

            result = sut.getGames()
            self.assertEqual(len(result), 4)
            self._assertGame(result[0], game1)
            self._assertGame(result[1], game2)
            self._assertGame(result[2], game3)
            self._assertGame(result[3], game4)

            sut.deleteGame(1445443860, "baz", 1445443862)

            result = sut.getGames()
            self.assertEqual(len(result), 4)
            self._assertGame(result[0], game1)
            self._assertGame(result[1], game2)
            self._assertGame(result[2], game3 + ("baz", 1445443862))
            self._assertGame(result[3], game4)
            with open(filePath, 'r') as temp:
                self.assertMultiLineEqual(
                    temp.read(),
                    "\ntlr 0 cjm 10 1445443858\nfoo 2 bar 8 1445443859\nbar 8 baz 2 1445443860 baz 1445443862\nbim 8 bob 2 1445443861"
                )
        finally:
            os.remove(filePath)
Exemplo n.º 9
0
    def testGetAppendDelete(self):
        try:
            game1 = ("tlr", 0, "cjm", 10, 1445443858)
            game2 = ("foo", 2, "bar", 8, 1445443859)
            filePath = "temp.txt"
            with open(filePath, 'w') as temp:
                temp.write("\n%s %s %s %s %.0f" % game1)
                temp.write("\n%s %s %s %s %.0f" % game2)

            sut = GameStore(filePath)
            result = sut.getGames()
            self.assertEqual(len(result), 2)
            self._assertGame(result[0], game1)
            self._assertGame(result[1], game2)

            game3 = ("bar", 8, "baz", 2, 1445443860)
            game4 = ("bim", 8, "bob", 2, 1445443861)
            sut.appendGame(self._createGame(game3))
            sut.appendGame(self._createGame(game4))

            result = sut.getGames()
            self.assertEqual(len(result), 4)
            self._assertGame(result[0], game1)
            self._assertGame(result[1], game2)
            self._assertGame(result[2], game3)
            self._assertGame(result[3], game4)

            sut.deleteGame(1445443860, "baz", 1445443862)

            result = sut.getGames()
            self.assertEqual(len(result), 4)
            self._assertGame(result[0], game1)
            self._assertGame(result[1], game2)
            self._assertGame(result[2], game3 + ("baz", 1445443862))
            self._assertGame(result[3], game4)
            with open(filePath, 'r') as temp:
                self.assertMultiLineEqual(temp.read(), "\ntlr 0 cjm 10 1445443858\nfoo 2 bar 8 1445443859\nbar 8 baz 2 1445443860 baz 1445443862\nbim 8 bob 2 1445443861")
        finally:
            os.remove(filePath)
Exemplo n.º 10
0
 def __init__(self, ladderFilePath, useCache):
     self._gameStore = GameStore(ladderFilePath)
     self._usingCache = useCache