def test_players(self):
        session = Session()
        p1 = Player(name='test_user')
        p1.spy_wins = 10
        session.add(p1)
        session.commit()

        g = Game(num_players=2,
                 date=datetime.datetime.utcnow(),
                 resistance_rounds=3,
                 spy_rounds=2)
        p2 = Player(name='test_user2')

        g.players.append(p1)
        g.players.append(p2)

        session.add(g)
        session.add(p2)
        session.commit()

        player_list = session.query(Player).filter_by(name='test_user').all()
        self.assertEqual(len(player_list), 1)
        self.assertIn(p1, player_list)

        player = player_list[0]
        self.assertEqual(len(player.games), 1)
        self.assertIn(g, player.games)
Beispiel #2
0
    def write_stats(self, spies_won):
        session = Session()

        game = Game(num_players=len(self.players), date=datetime.datetime.utcnow(), resistance_rounds=self.resistance_rounds, spy_rounds=self.spy_rounds)

        for player_name in self.players:
            player = Player.query.filter_by(name=player_name)
            if player is None:
                player = Player(name=player_name)

            if player in self.spies:
                if spies_won:
                    player.spy_wins = player.spy_wins + 1
                    player.total_wins = player.total_wins + 1
                else:
                    player.spy_losses = player.spy_losses + 1
            else:
                if not spies_won: 
                    player.resistance_wins = player.resistance_wins + 1
                    player.total_wins = player.total_wins + 1
                else:
                    player.resistance_losses = player.resistance_losses + 1

            player.total_games = player.total_games + 1
            player.win_percent = player.total_wins / player.total_games
            game.players.append(player)
            session.add(player)

        session.add(game)
        session.commit()
    def get_games(self):
        with open('games.csv') as csvfile:
			reader = csv.DictReader(csvfile)
			my_games=[]
			for row in reader:
				try:
					my_games.append(Game(row['Home/Neutral'], row['Visitor/Neutral'], int(row['PTSH']), int(row['PTSA'])))
				except ValueError:
					break
        return my_games
    def test_game_stats(self):
        session = Session()

        p1 = Player(name='test_user')
        p2 = Player(name='test_user2')

        games = [
            Game(num_players=2,
                 date=datetime.datetime.utcnow(),
                 resistance_rounds=3,
                 spy_rounds=2),
            Game(num_players=2,
                 date=datetime.datetime.utcnow(),
                 resistance_rounds=3,
                 spy_rounds=2),
            Game(num_players=2,
                 date=datetime.datetime.utcnow(),
                 resistance_rounds=3,
                 spy_rounds=2),
            Game(num_players=2,
                 date=datetime.datetime.utcnow(),
                 resistance_rounds=1,
                 spy_rounds=3)
        ]

        for game in games:
            game.players.append(p1)
            game.players.append(p2)
            session.add(game)

        session.add(p1)
        session.add(p2)
        session.commit()

        total_games = session.query(Game).count()
        resistance_wins = session.query(Game).filter(
            Game.resistance_rounds >= 3).count()
        spy_wins = session.query(Game).filter(Game.spy_rounds >= 3).count()

        self.assertEqual(resistance_wins / total_games, 3 / 4)
        self.assertEqual(spy_wins / total_games, 1 / 4)
Beispiel #5
0
 def get_games(self):
     format = "%a %b %d %Y"
     with open('games13.csv') as csvfile:
         reader = csv.DictReader(csvfile)
         my_games = []
         for row in reader:
             try:
                 my_games.append(
                     Game(row['Home/Neutral'], row['Visitor/Neutral'],
                          int(row['PTSH']), int(row['PTSA']),
                          datetime.datetime.strptime(row['Date'], format)))
             except ValueError:
                 break
     return my_games
    def generate_test_games(self, num, start_time, end_time):
        session = self.Session()
        cur_time = start_time
        total_delta = end_time - start_time
        delta = datetime.timedelta(seconds=total_delta.total_seconds() / num)
        for x in xrange(num):
            # pick a random group of players
            num_players = random.randint(4, len(self.players))
            cur_players = random.sample(self.players, num_players)
            spies = random.sample(cur_players, random.randint(2, 3))
            resistance = [p for p in cur_players if p not in spies]

            resistance_rounds = random.randint(0, 3)
            spy_rounds = min(3, random.randint(0, 5 - resistance_rounds))
            g = Game(num_players=num_players,
                     date=cur_time,
                     resistance_rounds=resistance_rounds,
                     spy_rounds=spy_rounds)

            cur_time = cur_time + delta

            for p in cur_players:
                g.players.append(p)

            if resistance_rounds >= 3:
                for spy in spies:
                    spy.spy_losses = spy.spy_losses + 1

                for res in resistance:
                    res.resistance_wins = res.resistance_wins + 1
                    res.total_wins = res.total_wins + 1
            else:
                for spy in spies:
                    spy.spy_wins = spy.spy_wins + 1
                    spy.total_wins = spy.total_wins + 1

                for res in resistance:
                    res.resistance_wins = res.resistance_wins + 1

            # Add the players to the session, and then commit.
            session.add(g)
            for p in cur_players:
                p.total_games = p.total_games + 1
                p.win_percent = float(p.total_wins) / p.total_games
                session.add(p)

        session.commit()
    def test_games(self):
        session = Session()
        p1 = Player(name='test_user')
        p2 = Player(name='test_user2')

        g = Game(num_players=2,
                 date=datetime.datetime.utcnow(),
                 resistance_rounds=3,
                 spy_rounds=2)

        g.players.append(p1)
        g.players.append(p2)

        session.add(g)
        session.add(p1)
        session.add(p2)
        session.commit()

        query_game = session.query(Game).first()
        self.assertEqual(query_game, g)

        self.assertEqual(len(query_game.players), 2)
        self.assertIn(p1, query_game.players)
Beispiel #8
0
    def retrieve_games(self):
        #returns list of Game instances with data gathered from sub-site of Basketball-Reference
        website = urlopen(
            "http://www.basketball-reference.com/leagues/NBA_2015_games.html"
        ).read()
        soup = BeautifulSoup(website)
        #uses CSS selectors to retrieve teams and scores of each game cell of table
        game_list = soup.select("#games tbody tr td")
        my_text = []
        #strips selected list of soups of their tags
        for game in game_list:
            my_text.append(game.get_text())

        #loads formatted Game instances into a new list to be returned
        my_games = []
        for i in range(0, len(my_text), 8):
            try:
                my_games.append(
                    Game(my_text[i + 4], my_text[i + 2], int(my_text[i + 5]),
                         int(my_text[i + 3])))
            except ValueError:
                break

        return my_games