Bunco Simulator

Diff

Differences From Artifact [94a406]:

To Artifact [3c213c]:


33
34
35
36
37
38
39



40
41
42
43
44
45
46
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49







+
+
+







        self.current_roll = []
    
    def __repr__(self):
        return f"<Player {self.name}: " \
            + f"\tscores\t\t{self.round_scores}>" \
            + f"\troll counts\t{self.round_roll_counts}>"

    def __str__(self):
        return self.name

    def prep_new_round(self):
        self.round_scores.append(0)
        self.round_roll_counts.append(0)
        self.personal_roll_scores.append(0)
        self.turn_progress = 0
        self.current_streak = 0

103
104
105
106
107
108
109
110
111
112
113
114

115
116
117
118
119
120
121
122
123






124
125
126
127
128
129
130
106
107
108
109
110
111
112


113


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136







-
-

-
-
+









+
+
+
+
+
+







            # Finished reading the numbers -- good job, eyeballs and brain!
            result = self.score_last_roll()
            self.current_roll = []
            self.turn_progress = 0
        return result

class Table:
    table_count = 0

    def __init__(self):
        self.table_count += 1
        self.table_number = self.table_count
        self.table_number = 0
        self.team1_score = 0
        self.team2_score = 0
        self.players = []
        self.active_player = -1

    def __repr__(self):
        names = ", ".join([p.name for p in self.players])
        return f"<Table {self.table_number}: {names}>"

    def __str__(self):
        if self.table_number == 1:
            return "Head Table"
        else:
            return f"Table {self.table_number}"

    def update_teammate_score(self, score):
        teammate = self.players[teammate_lookup[self.active_player]]
        teammate.round_scores[Game.current_round() - 1] += score

    def tick(self):
        if self.active_player == -1:
            # First tick for the table this round
220
221
222
223
224
225
226

227
228
229
230
231
232
233
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240







+







        random.shuffle(player_list)
        tables = [Table() for _ in range(table_count)]

        for n in range(table_count):
            first = n * players_per_table
            stop_before = first + players_per_table
            tables[n].players = player_list[first:stop_before]
            tables[n].table_number = n + 1
        
        return tables

def load_players(filename):
    players = []

    with open(filename) as tsvfile:
282
283
284
285
286
287
288

289
290
291
292

293

294

295
296
297
298
299
300
301
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312







+




+

+

+







            print(f"== TABLE {n+1} == Team 1:{table.team1_score} pts, Team 2:{table.team2_score} pts")
            for player in table.players:
                print(f"    {player.name} {player.round_scores[Game.current_round() - 1]} points, streak {player.max_streak} buncos {player.bunco_count}")

    def prep_next_round(self):
        # losers from head table move to next table
        headtable_losers = self.tables[0].losers()
        log_table_move(headtable_losers, "lost", self.tables[0], self.tables[1])
        round_winners = self.tables[1].swap_for_winners(headtable_losers)

        # winners from other tables move to next table
        for n in range(2, len(self.tables)):
            log_table_move(round_winners, "won", self.tables[n-1], self.tables[n])
            round_winners = self.tables[n].swap_for_winners(round_winners)
        
        # last set of winners moves to head table
        log_table_move(round_winners, "won", self.tables[-1], self.tables[0])
        self.tables[0].swap_for_losers(round_winners)

        for table in self.tables:
            table.prep_new_round()

        self.increment_round()

314
315
316
317
318
319
320

321
322
323
324
325
326
327
328
329
330
331
332
333

334
335
336
337
338
339







340
341
342
343
344

325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

345
346
347
348
349
350

351
352
353
354
355
356
357
358
359
360
361

362







+












-
+





-
+
+
+
+
+
+
+




-
+
                    player.tick()
                    self.increment_tick()
        
        # Settle ties at each table by doing a roll-off as many times as needed
        for table in self.tables:
            while table.team1_score == table.team2_score:
                print(f"Roll off! {table}")
                log('all', f"{table} having a roll-off to resolve a tie")
                table.roll_off()

        self.print_status()
    
log_db = sqlite3.connect("bunco.sqlite")
log_dbc = log_db.cursor()

def run_query(*args):
    log_dbc.execute(*args)
    log_db.commit()

run_query('DROP TABLE IF EXISTS `bunco_log`')
run_query('CREATE TABLE `bunco_log` (id, tick_number, round, player_name, type, message)')
run_query('CREATE TABLE `bunco_log` (id PRIMARY KEY, tick_number, round, player_name, type, message)')

def log_roll(player, dice, score):
    msg = f"{player.name} comprehended that [their] roll of {dice} = {score} points"
    query = """INSERT INTO bunco_log(tick_number, round, player_name, type, message) 
                VALUES(?, ?, ?, ?, ?)"""
    run_query(query, (Game.current_tick(), Game.current_round(), player.name, 'roll', msg))
    run_query(query, (Game.current_tick(), Game.current_round(), str(player), 'roll', msg))

def log_table_move(players, reason, table_from, table_to):
    message = f"Having {reason} the last round, {players[0].name} and {players[1].name} move from {table_from} to {table_to}."
    query = """INSERT INTO bunco_log(tick_number, round, player_name, type, message) 
                VALUES(?, ?, ?, ?, ?)"""
    run_query(query, (Game.current_tick(), Game.current_round(), 'all', 'general', message))

def log(player, message):
    query = """INSERT INTO bunco_log(tick_number, round, player_name, type, message) 
                VALUES(?, ?, ?, ?, ?)"""
    run_query(query, (Game.current_tick(), Game.current_round(), player.name, 'general', message))
    run_query(query, (Game.current_tick(), Game.current_round(), str(player), 'general', message))