-
Notifications
You must be signed in to change notification settings - Fork 0
/
tab1.py
executable file
·176 lines (159 loc) · 7.64 KB
/
tab1.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
from random import randint
import plotly.plotly as py
from plotly.graph_objs import *
import dash
import dash_table
from dash.dependencies import Input, Output, State, Event
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
from app import app, conn
from datetime import datetime
import elo_score
def tab1(init_player_df, elo_score_df, player_dict):
tab = html.Div(children=[
html.H1(children="Dart-Tally-Awesome",
style={'textAlign' : 'center'}),
html.Div(
style={'padding' : '40px',
'margin' : '20px',
'border-style' : 'solid',
'border-radius' : "10",
'border-width' : '1px',
'border-color' : 'gray',
'textAlign' : "center",
'box-shadow' : "5px 5px 6px grey",
'background-color' : '#d9d9d9'},
children=[
html.Div([html.Div("Player 1"),
dcc.Dropdown(options=[{'label' : i['player_name'],
'value' : i['player_id']}
for i in init_player_df.to_dict("rows")],
id="player_1_dropdown",
style={"textAlign" : "center",
'box-shadow' : "2px 2px 6px grey"}
),
html.Div("Player 1 Score"),
html.Div(dcc.Input(id='player_1_score',
value='',
type='number',
style={"textAlign" : "center",
"width" : "100px",
'box-shadow' : "2px 2px 6px grey",
}))],
className="four columns",
style={'textAlign' : 'center'}),
html.H3("-VS-",
className="four columns"),
html.Div([html.Div("Player 2"),
dcc.Dropdown(options=[{'label' : i['player_name'],
'value' : i['player_id']}
for i in init_player_df.to_dict("rows")],
id="player_2_dropdown",
style={"textAlign" : "center",
'box-shadow' : "2px 2px 6px grey"}
),
html.Div("Player 2 Score"),
html.Div(dcc.Input(id='player_2_score',
value='',
type='number',
style={"textAlign" : "center",
"width" : "100px",
'box-shadow' : "2px 2px 6px grey"}))],
className="four columns",
style={'textAlign' : 'center'}),
html.Button("Add Score",
id="add_score_button",
style={"textAlign" : 'center',
'box-shadow' : "2px 2px 6px grey",
"background-color" : "white"}
)],
className="row"),
dcc.Graph(id='timeseries',
config={'displayModeBar': False},
figure=go.Figure(
data = [go.Scatter(x=elo_score_df.index,
y=elo_score_df[i],
name=player_dict[i]['player_name'])
for i in list(elo_score_df)],
layout = go.Layout(hovermode='closest'))
),
html.Div(children=[
html.H3("The Elo Rating System"),
html.P("This web app is uses the Elo Rating System to rate players through time."\
" The previous rankings of the two players, and the outcome of the game all"\
" contribute to the new scores of each player. The Elo Rating System was"\
" originally divised by Arpad Elo for rating chess players. It has since"\
" been used for all sorts of zero-sum games. For more about this, head over"\
" to the Wikipedia Article:"),
html.A("The Elo Rating System - Wikipedia.org",
href="https://en.wikipedia.org/wiki/Elo_rating_system")
],
style={'padding' : '40px',
'margin' : '20px',
'border-style' : 'solid',
'border-radius' : "10",
'border-width' : '1px',
'border-color' : 'gray',
'textAlign' : "left",
'box-shadow' : "5px 5px 6px grey",
'background-color' : '#d9d9d9'},
)
])
return(tab)
@app.callback(
Output(component_id='timeseries', component_property='figure'),
[Input('add_score_button', 'n_clicks_timestamp')],
[State('player_1_dropdown', 'value'),
State('player_2_dropdown', 'value'),
State('player_1_score', 'value'),
State('player_2_score', 'value')])
def update_games(timestamp, p1_id, p2_id, p1_score, p2_score):
df = pd.read_sql(sql='SELECT * FROM game_log;', con=conn, parse_dates=['created_at'])
try:
p1_id = int(p1_id)
p2_id = int(p2_id)
p1_score = int(p1_score)
p2_score = int(p2_score)
except TypeError:
pass
else:
if p1_score >= p2_score:
winner_id = p1_id
winner_score = p1_score
looser_id = p2_id
looser_score=p2_score
else:
winner_id = p2_id
winner_score = p2_score
looser_id = p1_id
looser_score=p1_score
utc_stamp = str(datetime.utcfromtimestamp(int(timestamp / 1000)))
conn.execute("INSERT INTO game_log "\
"(winner_id, winner_score, looser_id, looser_score, created_at) "\
"VALUES({}, {}, {}, {}, '{}');".format(winner_id,
winner_score,
looser_id,
looser_score,
utc_stamp))
finally:
kfactor_list = pd.read_sql(sql='SELECT * FROM kfactor;', con=conn)['value'].tolist()
kfactor = kfactor_list.pop()
init_player_df = pd.read_sql(sql='SELECT * FROM player_ids;',
con=conn)
init_game_log_df = pd.read_sql(sql='SELECT * FROM game_log;',
con=conn,
parse_dates=['created_at'])
elo_score_df = elo_score.make_elo_scores_df(init_game_log_df,
init_player_df,
K=kfactor)
player_dict = init_player_df.set_index("player_id").to_dict('index')
figure = go.Figure(
data = [go.Scatter(x=elo_score_df.index,
y=elo_score_df[i],
name=player_dict[i]['player_name'])
for i in list(elo_score_df)],
layout = go.Layout(hovermode='closest'))
return(figure)