forked from jordillull/elopingpong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_match.py
executable file
·149 lines (59 loc) · 2.52 KB
/
add_match.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python
import sqlite3
import sys
import elo
import datetime
DEFAULT_RATING = 1500
def get_rating(con, player):
cur = con.cursor()
rating = cur.execute("""
SELECT rating
FROM player
WHERE short_name = '{0}'""".format(player)).fetchone()
return float(rating[0])
def log_match_info(player_a, score_a, player_b, score_b, rating_a, rating_b, new_rating_a, new_rating_b):
print "{0} won the Match! {1} - {2}".format(player_a if score_a > score_b else player_b, score_a, score_b)
print
print "{0} {1} {2:.2f} points. His new rating is {3:.2f}".format(player_a, 'won' if new_rating_a > rating_a else 'lost', abs(new_rating_a-rating_a), new_rating_a)
print
print "{0} {1} {2:.2f} points. His new rating is {3:.2f}".format(player_b, 'won' if new_rating_b > rating_b else 'lost', abs(new_rating_b-rating_b), new_rating_b)
def add_match(player_a, score_a, player_b, score_b):
con = sqlite3.connect('pingpong.db')
cur = con.cursor()
rating_a = get_rating(con, player_a)
rating_b = get_rating(con, player_b)
if not rating_a:
print "Error: Rating for player {0} was not found".format(player_a)
return
if not rating_b:
print "Error: Rating for player {0} was not found".format(player_b)
return
new_rating_a, new_rating_b = elo.compute_new_ratings(score_a, rating_a, score_b, rating_b)
query = """
INSERT INTO match
(player_a, player_b, score_a, score_b, elo_won_a, elo_won_b, date)
VALUES ('{0}','{1}','{2}','{3}','{4}','{5}', '{6}')
""".format(
player_a, player_b,
score_a, score_b,
new_rating_a - rating_a,
new_rating_b - rating_b,
str(datetime.datetime.now())
)
cur.execute(query)
query = """
UPDATE player
SET rating = {0}
WHERE short_name = '{1}'"""
cur.execute(query.format(new_rating_a, player_a))
cur.execute(query.format(new_rating_b, player_b))
log_match_info(player_a, score_a, player_b, score_b, rating_a, rating_b, new_rating_a, new_rating_b)
con.commit()
cur.close()
con.close()
if __name__ == "__main__":
if len(sys.argv) != 5:
print "Incorrect number of parameters."
print " Usage: ./add_match player_a score_a player_b score_b"
else:
add_match(sys.argv[1], int(sys.argv[2]), sys.argv[3], int(sys.argv[4]))