Ejemplo n.º 1
0
 def __init__(self, name):
     self.name = name
     self.elo = 0.0
     self.games = []
     self.wins = 0
     self.losses = 0
     self.goalsFor = 0
     self.goalsAgainst = 0
     self.skillBuffer = CircularSkillBuffer(10)
     self.gamesAsRed = 0
     self.highestSkill = {"time": 0, "skill": 0}
     self.lowestSkill = {"time": 0, "skill": 0}
     self.achievements = {}
Ejemplo n.º 2
0
 def __init__(self, name):
     self.name = name
     self.elo = 0.0
     self.games = []
     self.wins = 0
     self.losses = 0
     self.goalsFor = 0
     self.goalsAgainst = 0
     self.skillBuffer = CircularSkillBuffer(10)
     self.gamesAsRed = 0
     self.highestSkill = {"time": 0, "skill": 0}
     self.lowestSkill = {"time": 0, "skill": 0}
     self.achievements = {}
Ejemplo n.º 3
0
 def __init__(self, name):
     self.name = name
     self.elo = 0.0
     self.games = []
     self.wins = 0
     self.losses = 0
     self.goalsFor = 0
     self.goalsAgainst = 0
     self.skillBuffer = CircularSkillBuffer(10)
     self.gamesAsRed = 0
     self.highestSkill = {"time": 0, "skill": 0}
     self.lowestSkill = {"time": 0, "skill": 0}
     self.mostSignificantGame = None
     self.gamesToday = 0
Ejemplo n.º 4
0
class Player(object):

    # Number of days inactivity after which players are considered inactive
    DAYS_INACTIVE = 60

    def __init__(self, name):
        self.name = name
        self.elo = 0.0
        self.games = []
        self.wins = 0
        self.losses = 0
        self.goalsFor = 0
        self.goalsAgainst = 0
        self.skillBuffer = CircularSkillBuffer(10)
        self.gamesAsRed = 0
        self.highestSkill = {"time": 0, "skill": 0}
        self.lowestSkill = {"time": 0, "skill": 0}
        self.mostSignificantGame = None
        self.gamesToday = 0

    def game(self, game):
        if self.name == game.redPlayer:
            delta = -game.skillChangeToBlue
            opponent = game.bluePlayer
            self.gamesAsRed += 1
            if game.redScore > game.blueScore:
                self.wins += 1
            elif game.redScore < game.blueScore:
                self.losses += 1
            self.goalsFor += game.redScore
            self.goalsAgainst += game.blueScore
        elif self.name == game.bluePlayer:
            delta = game.skillChangeToBlue
            opponent = game.redPlayer
            if game.redScore < game.blueScore:
                self.wins += 1
            elif game.redScore > game.blueScore:
                self.losses += 1
            self.goalsFor += game.blueScore
            self.goalsAgainst += game.redScore
        else:
            return
        self.skillBuffer.put({'oldskill': self.elo, 'skill': self.elo + delta, 'played': opponent})
        self.elo += delta

        if (self.elo > self.highestSkill["skill"]):
            self.highestSkill = {"time": game.time, "skill": self.elo}

        if (self.elo < self.lowestSkill["skill"]):
            self.lowestSkill = {"time": game.time, "skill": self.elo}

        if self.mostSignificantGame is None or abs(delta) > abs(self.mostSignificantGame.skillChangeToBlue):
            self.mostSignificantGame = game

        if date.fromtimestamp(game.time) == date.today():
            self.gamesToday += 1

        self.games.append(game)

    def isActive(self, atTime=time.time()):
        #  Using date.* classes is too slow here
        return (not exclusions.contains(self.name)) and len(self.games) > 0 and (self.games[-1].time > atTime - (60 * 60 * 24 * self.DAYS_INACTIVE))

    def overrated(self):
        lastSkill = self.skillBuffer.lastSkill()
        if self.skillBuffer.isFull:
            return lastSkill - self.skillBuffer.avg()
        return 0

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        return self.name + ":" + str(self.elo)

    def wonGame(self, game):
        return (game.redPlayer == self.name and game.redScore > game.blueScore) or (game.bluePlayer == self.name and game.blueScore > game.redScore)

    def lostGame(self, game):
        return (game.redPlayer == self.name and game.redScore < game.blueScore) or (game.bluePlayer == self.name and game.blueScore < game.redScore)

    def getStreaks(self):
        winStreak = Streak()

        loseStreak = Streak()

        currentStreak = Streak()

        lastWon = False
        lastLost = False

        for game in self.games:
            wonGame = self.wonGame(game)
            lostGame = self.lostGame(game)

            if (wonGame != lastWon) or (lostGame != lastLost):
                # end of streak
                if lastWon:
                    if currentStreak.count > winStreak.count:
                        winStreak = currentStreak
                if lastLost:
                    if currentStreak.count > loseStreak.count:
                        loseStreak = currentStreak
                currentStreak = Streak()
                currentStreak.fromDate = game.time
                currentStreak.toDate = game.time if (wonGame or lostGame) else 0
                currentStreak.count = 1 if (wonGame or lostGame) else 0

            if (wonGame and lastWon) or (lostGame and lastLost):
                currentStreak.toDate = game.time
                currentStreak.count += 1

            lastWon = wonGame
            lastLost = lostGame

        currentStreakType = "wins" if lastWon else "losses" if lastLost else "(last game was a draw)"

        return {'win': winStreak, 'lose': loseStreak, 'current': currentStreak, 'currentType': currentStreakType}
Ejemplo n.º 5
0
class Player(object):

    # Number of days inactivity after which players are considered inactive
    DAYS_INACTIVE = 60

    def __init__(self, name):
        self.name = name
        self.elo = 0.0
        self.games = []
        self.withinActive = 0
        self.wins = 0
        self.losses = 0
        self.goalsFor = 0
        self.goalsAgainst = 0
        self.skillBuffer = CircularSkillBuffer(10)
        self.gamesAsRed = 0
        self.highestSkill = {"time": 0, "skill": 0}
        self.lowestSkill = {"time": 0, "skill": 0}
        self.achievements = {}

    def game(self, game):
        if self.name == game.redPlayer:
            delta = -game.skillChangeToBlue
            self.gamesAsRed += 1
            if game.redScore > game.blueScore:
                self.wins += 1
            elif game.redScore < game.blueScore:
                self.losses += 1
            self.goalsFor += game.redScore
            self.goalsAgainst += game.blueScore
        elif self.name == game.bluePlayer:
            delta = game.skillChangeToBlue
            if game.redScore < game.blueScore:
                self.wins += 1
            elif game.redScore > game.blueScore:
                self.losses += 1
            self.goalsFor += game.blueScore
            self.goalsAgainst += game.redScore
        else:
            return
        self.elo += delta
        self.skillBuffer.put(self.elo)

        if (self.elo > self.highestSkill["skill"]):
            self.highestSkill = {"time": game.time, "skill": self.elo}

        if (self.elo < self.lowestSkill["skill"]):
            self.lowestSkill = {"time": game.time, "skill": self.elo}

        self.games.append(game)
        self.withinActive = game.time + (60 * 60 * 24 * self.DAYS_INACTIVE)

    def getSkillBounds(self):
        return {"highest": self.highestSkill, "lowest": self.lowestSkill}

    def mostSignificantGame(self):
        mostSignificantGame = None
        for game in self.games:
            if self.name == game.redPlayer:
                delta = -game.skillChangeToBlue
            else:
                delta = game.skillChangeToBlue
            if mostSignificantGame is None or abs(delta) > abs(mostSignificantGame.skillChangeToBlue):
                mostSignificantGame = game
        return mostSignificantGame

    @property
    def gamesToday(self):
        today = date.today()
        return self.gamesOn(today)

    def gamesOn(self, date):
        return len([g for g in self.games if g.timeAsDatetime().date() == date])

    def skillChangeToday(self):
        today = date.today()
        skillChange = 0
        for game in [g for g in self.games if g.timeAsDatetime().date() == today]:
            skillChange += game.skillChangeToBlue if game.bluePlayer == self.name else -game.skillChangeToBlue
        return skillChange

    def rankChangeToday(self):
        today = date.today()
        change = 0
        for game in [g for g in self.games if g.timeAsDatetime().date() == today]:
            change += game.bluePosChange if game.bluePlayer == self.name else game.redPosChange
        return change

    def achieve(self, achievements, game):
        for achievement in achievements:
            if achievement in self.achievements.keys():
                self.achievements[achievement].append(game)
            else:
                self.achievements[achievement] = [game]

    def overrated(self):
        if self.skillBuffer.isFull:
            lastSkill = self.skillBuffer.lastSkill()
            return lastSkill - self.skillBuffer.avg()
        return 0

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        return self.name + ":" + str(self.elo)

    def wonGame(self, game):
        return (game.redPlayer == self.name and game.redScore > game.blueScore) or (game.bluePlayer == self.name and game.blueScore > game.redScore)

    def lostGame(self, game):
        return (game.redPlayer == self.name and game.redScore < game.blueScore) or (game.bluePlayer == self.name and game.blueScore < game.redScore)

    def getAllStreaks(self, games):
        streaks = []
        currentStreak = Streak()

        for game in games:
            wonGame = self.wonGame(game)
            lostGame = self.lostGame(game)
            if (wonGame and currentStreak.win) or (lostGame and not currentStreak.win):
                currentStreak.gameTimes.append(game.time)
            else:
                # end of streak
                if currentStreak.count >= 1:
                    streaks.append(currentStreak)
                currentStreak = Streak()
                if wonGame or lostGame:
                    currentStreak.gameTimes.append(game.time)
                currentStreak.win = wonGame
        return {'past': streaks, 'current': currentStreak}

    def getStreaks(self):
        streaks = self.getAllStreaks(self.games)
        winStreaks = sorted([s for s in streaks['past'] if s.win], key=lambda s:s.count, reverse=True)
        loseStreaks = sorted([s for s in streaks['past'] if not s.win], key=lambda s:s.count, reverse=True)
        currentStreakType = "(last game was a draw)" if streaks['current'].count == 0 else "wins" if streaks['current'].win else "losses"
        return {
            'win': winStreaks[0] if len(winStreaks) > 0 else Streak(),
            'lose': loseStreaks[0] if len(loseStreaks) > 0 else Streak(),
            'current': streaks['current'],
            'currentType': currentStreakType
        }
Ejemplo n.º 6
0
class Player(object):
    def __init__(self, name):
        self.name = name
        self.elo = 0.0
        self.games = []
        self.wins = 0
        self.losses = 0
        self.goalsFor = 0
        self.goalsAgainst = 0
        self.skillBuffer = CircularSkillBuffer(10)
        self.gamesAsRed = 0
        self.highestSkill = {"time": 0, "skill": 0}
        self.lowestSkill = {"time": 0, "skill": 0}
        self.achievements = {}

    def game(self, game):
        if self.name == game.redPlayer:
            delta = -game.skillChangeToBlue
            self.gamesAsRed += 1
            if game.redScore > game.blueScore:
                self.wins += 1
            elif game.redScore < game.blueScore:
                self.losses += 1
            self.goalsFor += game.redScore
            self.goalsAgainst += game.blueScore
        elif self.name == game.bluePlayer:
            delta = game.skillChangeToBlue
            if game.redScore < game.blueScore:
                self.wins += 1
            elif game.redScore > game.blueScore:
                self.losses += 1
            self.goalsFor += game.blueScore
            self.goalsAgainst += game.redScore
        else:
            return
        self.elo += delta
        self.skillBuffer.put(self.elo)

        if (self.elo > self.highestSkill["skill"]):
            self.highestSkill = {"time": game.time, "skill": self.elo}

        if (self.elo < self.lowestSkill["skill"]):
            self.lowestSkill = {"time": game.time, "skill": self.elo}

        self.games.append(game)

    def getSkillBounds(self):
        return {"highest": self.highestSkill, "lowest": self.lowestSkill}

    def mostSignificantGame(self):
        mostSignificantGame = None
        for game in self.games:
            if self.name == game.redPlayer:
                delta = -game.skillChangeToBlue
            else:
                delta = game.skillChangeToBlue
            if mostSignificantGame is None or abs(delta) > abs(
                    mostSignificantGame.skillChangeToBlue):
                mostSignificantGame = game
        return mostSignificantGame

    @property
    def gamesToday(self):
        today = date.today()
        return self.gamesOn(today)

    def gamesOn(self, date):
        return len(
            [g for g in self.games if g.timeAsDatetime().date() == date])

    def skillChangeToday(self):
        today = date.today()
        skillChange = 0
        for game in [
                g for g in self.games if g.timeAsDatetime().date() == today
        ]:
            skillChange += game.skillChangeToBlue if game.bluePlayer == self.name else -game.skillChangeToBlue
        return skillChange

    def rankChangeToday(self):
        today = date.today()
        change = 0
        for game in [
                g for g in self.games if g.timeAsDatetime().date() == today
        ]:
            change += game.bluePosChange if game.bluePlayer == self.name else game.redPosChange
        return change

    def achieve(self, achievements, game):
        for achievement in achievements:
            if achievement in self.achievements.keys():
                self.achievements[achievement].append(game)
            else:
                self.achievements[achievement] = [game]

    def overrated(self):
        if self.skillBuffer.isFull:
            lastSkill = self.skillBuffer.lastSkill()
            return lastSkill - self.skillBuffer.avg()
        return 0

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        return self.name + ":" + str(self.elo)

    def wonGame(self, game):
        return (game.redPlayer == self.name and game.redScore > game.blueScore
                ) or (game.bluePlayer == self.name
                      and game.blueScore > game.redScore)

    def lostGame(self, game):
        return (game.redPlayer == self.name and game.redScore < game.blueScore
                ) or (game.bluePlayer == self.name
                      and game.blueScore < game.redScore)

    def getAllStreaks(self, games):
        streaks = []
        currentStreak = Streak()

        for game in games:
            wonGame = self.wonGame(game)
            lostGame = self.lostGame(game)
            if (wonGame and currentStreak.win) or (lostGame
                                                   and not currentStreak.win):
                currentStreak.gameTimes.append(game.time)
            else:
                # end of streak
                if currentStreak.count >= 1:
                    streaks.append(currentStreak)
                currentStreak = Streak()
                if wonGame or lostGame:
                    currentStreak.gameTimes.append(game.time)
                currentStreak.win = wonGame
        return {'past': streaks, 'current': currentStreak}

    def getStreaks(self):
        streaks = self.getAllStreaks(self.games)
        winStreaks = sorted([s for s in streaks['past'] if s.win],
                            key=lambda s: s.count,
                            reverse=True)
        loseStreaks = sorted([s for s in streaks['past'] if not s.win],
                             key=lambda s: s.count,
                             reverse=True)
        currentStreakType = "(last game was a draw)" if streaks[
            'current'].count == 0 else "wins" if streaks[
                'current'].win else "losses"
        return {
            'win': winStreaks[0] if len(winStreaks) > 0 else Streak(),
            'lose': loseStreaks[0] if len(loseStreaks) > 0 else Streak(),
            'current': streaks['current'],
            'currentType': currentStreakType
        }