Exemplo n.º 1
0
 def test_get_slots(self):
     players = Players(
         [
             Player("Gary Gygax", "TSR", {"1A": "G", "2A": "G"}),
             Player("Dave Arneson", "TSR", {"1A": "P", "2A": "P"}),
             Player("Jane Bloggs", None, {"1B": "X"}),
         ]
     )
     self.assertEqual(["1A", "1B", "2A"], players.get_slots())
Exemplo n.º 2
0
def players_upload():
    f = request.files['players_file']
    if f.filename.endswith('.csv'):
        players = Players.from_csv(f)
    else:
        players = Players.from_xls(f.read())
    db.import_players(players, delete=True)
    flash("Players imported.")
    return redirect(url_for('players'))
Exemplo n.º 3
0
 def test_get_player(self):
     players = Players(
         [
             Player("Gary Gygax", "TSR", {"1A": "G", "2A": "G"}),
             Player("Dave Arneson", "TSR", {"1A": "P", "2A": "P"}),
             Player("Jane Bloggs", None, {"1B": "X"}),
         ]
     )
     self.assertEqual(None, players.get_player(None))
     self.assertEqual(None, players.get_player(""))
     self.assertEqual(list(players)[0], players.get_player("Gary Gygax"))
     self.assertRaises(ValueError, players.get_player, "foo")
Exemplo n.º 4
0
    def test_to_csv(self):
        players = Players(
            [
                Player("Gary Gygax", "TSR", {"1A": "G", "2A": "G"}),
                Player("Dave Arneson", "TSR", {"1A": "P", "2A": "P"}),
                Player("Jane Bloggs", None, {"1B": "X", "2B": "X"}),
            ]
        )

        players_csv = StringIO()
        players.to_csv(["1A", "1B", "2A", "2B"], players_csv)
        self.assertEqual(
            players_csv.getvalue(),
            "\r\n".join(
                ["name,team,1A,1B,2A,2B", "Gary Gygax,TSR,G,,G,", "Dave Arneson,TSR,P,,P,", "Jane Bloggs,,,X,,X", ""]
            ),
        )
Exemplo n.º 5
0
 def test_from_csv(self):
     players = Players.from_csv(
         StringIO(
             "\n".join(
                 ["name,team,1A,1B,2A,2B", "Gary Gygax,TSR,G,,G,", "Dave Arneson,TSR,P,,P,", "Jane Bloggs,,,X,,X"]
             )
         )
     )
     self.assertEqual(
         list(players),
         [
             Player("Gary Gygax", "TSR", {"1A": "G", "2A": "G"}),
             Player("Dave Arneson", "TSR", {"1A": "P", "2A": "P"}),
             Player("Jane Bloggs", None, {"1B": "X", "2B": "X"}),
         ],
     )
Exemplo n.º 6
0
def get_players():
    query = '''
    SELECT p.id AS id, name, team, slot, registration_type
    FROM players AS p
    LEFT JOIN player_registrations AS pr ON p.id=pr.player;
    '''
    rows = query_db(query)
    players = {}
    for row in rows:
        player_dict = players.setdefault(row['id'], {
            'name': row['name'],
            'team': row['team'],
            'slots': {},
        })
        if row['slot'] is not None:
            player_dict['slots'][row['slot']] = row['registration_type']
    return Players.from_dicts(players.values())
Exemplo n.º 7
0
 def test_from_imperfect_csv(self):
     players = Players.from_csv(
         StringIO(
             "\n".join(
                 [
                     "",
                     "Name,Team,First,Last,1a,1b,2a,2b",
                     "Gary Gygax,TSR,Gary,Gygax,G,,g,",
                     "Dave Arneson,TSR,Dave,Arneson,P,,p,",
                     "Jane Bloggs,,Jane,Bloggs,,X,,x",
                 ]
             )
         )
     )
     self.assertEqual(
         list(players),
         [
             Player("Gary Gygax", "TSR", {"1a": "G", "2a": "G"}),
             Player("Dave Arneson", "TSR", {"1a": "P", "2a": "P"}),
             Player("Jane Bloggs", None, {"1b": "X", "2b": "X"}),
         ],
     )
Exemplo n.º 8
0
def generate_players(slots, names):
    # TODO: Teams.
    players_by_slot = {}
    slots_by_timeslot = {}
    for slot in slots:
        slots_by_timeslot.setdefault(slot[0], []).append(slot)
        players_by_slot[slot] = {
            'G': random.randint(1, 3),
            'X': random.randint(0, 2),
            'P': random.randint(10, 18),
        }
    max_tables = max(len(v) for v in slots_by_timeslot.values()) * 4
    num_players = random.randint(max_tables * 7,
                                 (max_tables + len(slots) / 2) * 7)
    players = {}
    for name in random.sample(names, num_players):
        players[name] = {}
    gms = random.sample(players.keys(), max_tables)

    for timeslot, slotlist in slots_by_timeslot.iteritems():
        ts_gms = set(gms + random.sample(players, len(slotlist) * 3))
        ts_players = [p for p in players if p not in ts_gms]
        for slot in slotlist:
            for p in random.sample(ts_gms, players_by_slot[slot]['G']):
                players[p][slot] = 'G'
                ts_gms.remove(p)
            for p in random.sample(ts_gms, players_by_slot[slot]['X']):
                players[p][slot] = 'X'
                ts_gms.remove(p)
            for p in random.sample(ts_players, players_by_slot[slot]['P']):
                players[p][slot] = 'P'
                ts_players.remove(p)

    return Players.from_dicts([
        {'name': name, 'team': None, 'slots': pslots}
        for name, pslots in players.iteritems()])
Exemplo n.º 9
0
 def test_from_xls(self):
     xls_file = open(os.path.join(os.path.dirname(__file__), "players.xls"))
     players = Players.from_xls(xls_file.read())
     self.assertEqual(
         list(players), [Player("Gary Gygax", "TSR", {"1A": "G"}), Player("Dave Arneson", "TSR", {"1B": "G"})]
     )