-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_access.py
More file actions
261 lines (224 loc) · 10.1 KB
/
data_access.py
File metadata and controls
261 lines (224 loc) · 10.1 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import math
from sqlalchemy import func
from sqlalchemy.sql.expression import desc
from sqlalchemy.sql.expression import or_
from sqlalchemy.sql.expression import text
import config
from Models import Database
from Models import Hero
from Models import History
from Models import Item
from Models import MatchHero
from Models import MatchHeroSummary
from Models import MatchItem
from Models import MatchItemSummary
from Models import MatchSummary
from Models import Player
from Models import initialise_database
LIMIT_DATA = 10 # limit all() query to 10 records
def _calculate_win_rate(player_win, matches):
return math.ceil(player_win / matches * 10000) / 100
class DataAccess:
def __init__(self, app):
if not app:
raise ValueError('Application is required!')
app.config['SQLALCHEMY_DATABASE_URI'] = config.mysql['CONNECT_STRING']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
Database.init_app(app)
def initialise_database(self):
initialise_database()
"""
Hero
"""
def add_hero(self, hero_id, hero_name, portrait_url):
new_hero = Hero(hero_id=hero_id,
hero_name=hero_name,
portrait_url=portrait_url)
Database.session.add(new_hero)
Database.session.commit()
return new_hero
def get_hero(self, hero_id):
if hero_id:
return Database.session.query(Hero).filter(Hero.hero_id == hero_id).first()
else:
raise ValueError('Hero id must be specified!')
def update_hero(self, hero):
if hero is None:
raise ValueError('Hero is required!')
Database.session.add(hero)
Database.session.commit()
return hero
"""
Item
"""
def add_item(self, item_id, item_name, image_url):
new_item = Item(item_id=item_id,
item_name=item_name,
image_url=image_url)
Database.session.add(new_item)
Database.session.commit()
return new_item
def get_item(self, item_id):
if item_id:
return Database.session.query(Item).filter(Item.item_id == item_id).first()
else:
raise ValueError('Item id must be specified!')
def update_item(self, item):
if item is None:
raise ValueError('Item is required!')
Database.session.add(item)
Database.session.commit()
return item
"""
Player
"""
def add_player(self, account_id, steam_id, profile_url, real_name=None, persona_name=None, avatar=None):
new_player = Player(account_id=account_id,
steam_id=steam_id,
real_name=real_name,
persona_name=persona_name,
avatar=avatar,
profile_url=profile_url)
Database.session.add(new_player)
Database.session.commit()
return new_player
def get_player(self, account_id=None, steam_id=None, real_name=None):
query = Database.session.query(Player)
if account_id:
return query.filter(Player.account_id == account_id).first()
elif steam_id:
return query.filter(Player.steam_id == steam_id).first()
elif real_name: # recommended to be optimized by full-text search.
return query.filter(or_(text('real_name like :real_name'), text('persona_name like :real_name'))).params(
real_name="%" + real_name + "%").limit(LIMIT_DATA).all()
else:
raise ValueError('Account id or Steam id or real name must be specified!')
def update_player(self, player):
if player is None:
raise ValueError('Player is required!')
Database.session.add(player)
Database.session.commit()
return player
"""
Match
"""
def get_match_summary_aggregate(self, match_id):
return Database.session.query(MatchHero.account_id,
func.sum(text('match_heroes.player_win')).label('player_win'),
func.count(MatchHero.player_win).label('matches')). \
filter(MatchHero.match_id >= match_id). \
group_by(MatchHero.account_id). \
all()
def add_match_hero(self, match_id, account_id, player_win, hero_id):
new_match_hero = MatchHero(match_id=match_id,
account_id=account_id,
player_win=player_win,
hero_id=hero_id)
Database.session.add(new_match_hero)
Database.session.commit()
return new_match_hero
def get_match_hero_summary_aggregate(self, match_id):
return Database.session.query(MatchHero.account_id,
MatchHero.hero_id,
func.sum(text('match_heroes.player_win')).label('player_win'),
func.count(MatchHero.player_win).label('matches')). \
filter(MatchHero.match_id >= match_id). \
group_by(MatchHero.account_id). \
group_by(MatchHero.hero_id). \
all()
def add_match_item(self, match_id, account_id, player_win, item_id):
new_match_item = MatchItem(match_id=match_id,
account_id=account_id,
player_win=player_win,
item_id=item_id)
Database.session.add(new_match_item)
Database.session.commit()
return new_match_item
def get_match_item_summary_aggregate(self, match_id):
return Database.session.query(MatchItem.account_id,
MatchItem.item_id,
func.sum(text('match_items.player_win')).label('player_win'),
func.count(MatchItem.player_win).label('matches')). \
filter(MatchItem.match_id >= match_id). \
group_by(MatchItem.account_id). \
group_by(MatchItem.item_id). \
all()
"""
Match Summary
"""
def get_top_player(self):
return Database.session.query(MatchSummary).join(MatchSummary.player). \
order_by(desc(MatchSummary.matches)). \
order_by(desc(MatchSummary.player_win)). \
limit(LIMIT_DATA).all()
def save_match_summary(self, account_id, player_win, matches):
match_summary = self.get_match_summary(account_id=account_id)
if match_summary:
match_summary.player_win += player_win
match_summary.matches += matches
else:
match_summary = MatchSummary(account_id=account_id,
player_win=player_win,
matches=matches,
win_rate=0)
match_summary.win_rate = _calculate_win_rate(match_summary.player_win, match_summary.matches)
Database.session.add(match_summary)
Database.session.commit()
def get_match_summary(self, account_id):
return Database.session.query(MatchSummary).filter(MatchSummary.account_id == account_id).first()
def save_match_hero_summary(self, account_id, hero_id, player_win, matches):
match_hero_summary = Database.session.query(MatchHeroSummary). \
filter(MatchHeroSummary.account_id == account_id,
MatchHeroSummary.hero_id == hero_id). \
first()
if match_hero_summary:
match_hero_summary.player_win += player_win
match_hero_summary.matches += matches
else:
match_hero_summary = MatchHeroSummary(account_id=account_id,
hero_id=hero_id,
player_win=player_win,
matches=matches,
win_rate=0)
match_hero_summary.win_rate = _calculate_win_rate(match_hero_summary.player_win, match_hero_summary.matches)
Database.session.add(match_hero_summary)
Database.session.commit()
def get_match_hero_summary(self, account_id):
return Database.session.query(MatchHeroSummary).join(MatchHeroSummary.hero). \
filter(MatchHeroSummary.account_id == account_id). \
order_by(desc(MatchHeroSummary.matches)). \
order_by(desc(MatchHeroSummary.player_win)). \
limit(LIMIT_DATA).all()
def save_match_item_summary(self, account_id, item_id, player_win, matches):
match_item_summary = Database.session.query(MatchItemSummary). \
filter(MatchItemSummary.account_id == account_id,
MatchItemSummary.item_id == item_id). \
first()
if match_item_summary:
match_item_summary.player_win += player_win
match_item_summary.matches += matches
else:
match_item_summary = MatchItemSummary(account_id=account_id,
item_id=item_id,
player_win=player_win,
matches=matches,
win_rate=0)
match_item_summary.win_rate = _calculate_win_rate(match_item_summary.player_win, match_item_summary.matches)
Database.session.add(match_item_summary)
Database.session.commit()
def get_match_item_summary(self, account_id):
return Database.session.query(MatchItemSummary).join(MatchItemSummary.item). \
filter(MatchItemSummary.account_id == account_id). \
order_by(desc(MatchItemSummary.matches)). \
order_by(desc(MatchItemSummary.player_win)). \
limit(LIMIT_DATA).all()
"""
History
"""
def add_history(self, last_match_id):
new_history = History(last_match_id=last_match_id)
Database.session.add(new_history)
Database.session.commit()
return new_history
def get_last_history(self):
return Database.session.query(History).order_by(desc(History.id)).first()