-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_readme.py
80 lines (67 loc) · 3.14 KB
/
update_readme.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import sqlite3
def connect_db(db_path='mtg-tournaments.db'):
"""Connect to the SQLite database."""
conn = sqlite3.connect(db_path)
return conn
def fetch_stats(conn):
"""Fetch general statistics and deck-opponent match stats from the database."""
cursor = conn.cursor()
# General stats
cursor.execute('SELECT COUNT(*) FROM Matches')
total_matches = cursor.fetchone()[0]
cursor.execute('''
SELECT SUM(CASE WHEN result LIKE '2-%' THEN 1 ELSE 0 END) AS matches_won,
SUM(CAST(SUBSTR(result, 1, 1) AS INTEGER)) AS total_games_won,
SUM(CAST(SUBSTR(result, 3, 1) AS INTEGER)) AS total_games_lost
FROM Matches
''')
matches_won, total_games_won, total_games_lost = cursor.fetchone()
matches_lost = total_matches - matches_won
# Decks Played with opponent match counts and win rates
cursor.execute('''
SELECT d.name AS player_deck_name, o.name AS opponent_deck_name,
COUNT(*) AS match_count,
SUM(CASE WHEN m.result LIKE '2-%' THEN 1 ELSE 0 END) AS wins
FROM Matches m
JOIN Decks d ON m.player_deck_id = d.id
JOIN Decks o ON m.opponent_deck_id = o.id
GROUP BY d.name, o.name
''')
matches_by_deck = cursor.fetchall()
return (total_matches, matches_won, matches_lost, total_games_won, total_games_lost, matches_by_deck)
def read_header(header_path='READMEHEADER.txt'):
"""Read the header text from a file."""
with open(header_path, 'r') as file:
return file.read()
def update_readme(header_text, stats):
"""Update the README file with statistics."""
(total_matches, matches_won, matches_lost, total_games_won, total_games_lost, matches_by_deck) = stats
readme_path = 'README.md'
with open(readme_path, 'w') as file:
file.write("# MTG-Tournament-Stats\n\n")
file.write(header_text + "\n\n## Stats Overview\n")
file.write(f"- Total matches played: {total_matches}\n")
file.write(f"- Matches won: {matches_won}\n")
file.write(f"- Matches lost: {matches_lost}\n")
file.write(f"- Total games won: {total_games_won}\n")
file.write(f"- Total games lost: {total_games_lost}\n\n")
current_deck = None
for player_deck_name, opponent_deck_name, match_count, wins in matches_by_deck:
# Start a new section when the player's deck changes
if player_deck_name != current_deck:
if current_deck is not None:
file.write("\n") # Add spacing between different player decks
file.write(f"### Deck: {player_deck_name}\n")
current_deck = player_deck_name
# Calculate win rate
win_rate = (wins / match_count * 100) if match_count > 0 else 0
file.write(f" - Opponent: {opponent_deck_name}, Matches: {match_count}, Win Rate: {win_rate:.2f}%\n")
def main():
conn = connect_db()
stats = fetch_stats(conn)
header_text = read_header()
update_readme(header_text, stats)
conn.close()
print("README.md updated with the latest stats.")
if __name__ == "__main__":
main()