Skip to content

Commit

Permalink
Add columns to separate out p1 and p2 scores.
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Dick committed Oct 12, 2023
1 parent cfb0edb commit 06e7609
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
35 changes: 35 additions & 0 deletions admin/db_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def run_updates(league_name):
if current_version == '4':
_update_from_4_to_5(league_name)
current_version = '5'
if current_version == '5':
_update_from_5_to_6(league_name)
current_version = '6'


# Adds the ordering index to players
Expand Down Expand Up @@ -113,3 +116,35 @@ def _update_from_4_to_5(league_name):
conn.close()

db.set_config(league_name, configs.LEAGUE_VERSION, '5')


def _update_from_5_to_6(league_name):
current_version = db.get_config(league_name, configs.LEAGUE_VERSION)
if not current_version or current_version != '5':
return False

conn = db.get_connection(league_name)
c = conn.cursor()

c.execute("ALTER TABLE match ADD player_1_score INT")
c.execute("ALTER TABLE match ADD player_2_score INT")
c.execute("ALTER TABLE match ADD tie_score INT")
c.execute("ALTER TABLE match ADD play_all_sets INT DEFAULT 0")
conn.commit()
conn.close()

all_matches = db.get_matches(league_name)
conn = db.get_connection(league_name)
c = conn.cursor()

for match in all_matches:
if match.winner_id is None:
continue
if match.winner_id == match.player_1_id:
c.execute("UPDATE match SET player_1_score=?, player_2_score=?, tie_score=0 WHERE rowid=?", (match.sets_needed, match.sets-match.sets_needed, match.id))
else:
c.execute("UPDATE match SET player_2_score=?, player_1_score=?, tie_score=0 WHERE rowid=?", (match.sets_needed, match.sets-match.sets_needed, match.id))

conn.commit()
conn.close()
db.set_config(league_name, configs.LEAGUE_VERSION, '6')
14 changes: 11 additions & 3 deletions backend/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import partial
from backend import configs

LATEST_VERSION = 5
LATEST_VERSION = 6


def path(league_name):
Expand Down Expand Up @@ -39,6 +39,10 @@ def initialize(league_name):
'date_played DATE, '
'message_sent INT DEFAULT 0, '
'forfeit INT DEFAULT 0, '
'player_1_score INT, '
'player_2_score INT, '
'tie_score INT, '
'play_all_sets DEFAULT 0, '
'FOREIGN KEY (player_1) REFERENCES player, '
'FOREIGN KEY (player_2) REFERENCES player, '
'FOREIGN KEY (winner) REFERENCES player)')
Expand Down Expand Up @@ -289,7 +293,7 @@ def add_match(league_name, player_1, player_2, week_date, grouping, season, sets


class Match:
def __init__(self, id, p1_id, p2_id, winner_id, week, grouping, season, sets, sets_needed, date_played, message_sent, forfeit):
def __init__(self, id, p1_id, p2_id, winner_id, week, grouping, season, sets, sets_needed, date_played, message_sent, forfeit, p1_score, p2_score, tie_score, play_all_sets):
self.id = id
self.player_1_id = p1_id
self.player_2_id = p2_id
Expand All @@ -302,10 +306,14 @@ def __init__(self, id, p1_id, p2_id, winner_id, week, grouping, season, sets, se
self.date_played = date_played
self.message_sent = message_sent
self.forfeit = forfeit
self.player_1_score = p1_score
self.player_2_score = p2_score
self.tie_score = tie_score
self.play_all_sets = play_all_sets

@classmethod
def from_db(cls, row):
return Match(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11])
return Match(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15])


def get_matches(league_name):
Expand Down

0 comments on commit 06e7609

Please sign in to comment.