forked from Tapin42/cfbr_orders
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorders.py
439 lines (408 loc) · 14 KB
/
orders.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
from cfbr_db import Db
from uuid import uuid4
from functools import reduce
###############################################################
#
# Functions to handle order logic
#
###############################################################
class Orders:
@staticmethod
def get_next_offers(hoy_d, hoy_m, num_orders=1):
# This is sorted by tier and then least-filled within the tier.
round_orders = Orders.get_orders(hoy_d, hoy_m)
rv = []
# Now all we need to do is find the first 'x' sets of orders that aren't already complete, if they exist...
for candidate in round_orders:
if candidate['pct_complete'] < 1:
rv.append(candidate['territory'])
if len(rv) >= num_orders:
return rv
# ...and if they need more orders than we've already pulled, we'll just tell them to go with the Tier 1 targets with the
# lowest percentage completion (which will already be over 100%, since we got here in the first place)
for candidate in round_orders:
territory = candidate['territory']
if not territory in rv:
rv.append(territory)
if len(rv) >= num_orders:
return rv
# It's theoretically possible that we have less possible total orders than are requested; if we made it this far,
# return whatever we've got
return rv
@staticmethod
def get_orders(hoy_d, hoy_m):
query = '''
SELECT
name,
tier,
quota,
assigned,
pct_complete
FROM (
SELECT
t.name,
p.season,
p.day,
p.tier,
p.quota,
0 as assigned,
0 as pct_complete
FROM plans p
INNER JOIN territory t ON p.territory=t.id
WHERE
NOT EXISTS (SELECT * FROM orders o
WHERE p.territory = o.territory
AND p.season=o.season
AND p.day=o.day
AND o.accepted=TRUE)
UNION ALL
SELECT
t.name,
p.season,
p.day,
p.tier,
p.quota,
SUM(o.stars) AS assigned,
SUM(o.stars) / CAST(p.quota AS REAL) AS pct_complete
FROM plans p
INNER JOIN territory t ON p.territory=t.id
LEFT JOIN orders o ON (
p.territory = o.territory
AND p.season = o.season
AND p.day = o.day
)
WHERE
o.accepted=TRUE
GROUP BY
p.territory, p.season, p.day, p.tier
)
WHERE
season = ?
AND day = ?
ORDER BY
tier ASC,
pct_complete ASC;
'''
res = Db.get_db().execute(query, (hoy_m, hoy_d))
orders = []
for row in res:
territory, tier, quota, assigned, pct_complete = row
orders.append({
'territory': territory,
'tier': tier,
'quota': quota,
'assigned': assigned,
'pct_complete': pct_complete
})
res.close()
return orders
@staticmethod
def get_assigned_orders(hoy_d, hoy_m):
query = '''
SELECT
t.name,
SUM(o.stars) as stars
FROM orders o
INNER JOIN territory t ON o.territory=t.id
WHERE
season=?
AND day=?
AND accepted=TRUE
GROUP BY t.name
ORDER BY stars DESC
'''
res = Db.get_db().execute(query, (hoy_m, hoy_d))
territory_moves = dict(res.fetchall())
res.close()
return territory_moves
@staticmethod
def user_already_moved(username, hoy_d, hoy_m):
query = '''
SELECT
t.name
FROM orders o
INNER JOIN territory t ON o.territory=t.id
WHERE
user=?
AND season=?
AND day=?
AND accepted=TRUE
LIMIT 1
'''
res = Db.get_db().execute(query, (username, hoy_m, hoy_d))
cmove = res.fetchone()
res.close()
# The return value here will either be None or the territory name that the user acted on
return None if cmove is None else cmove[0]
@staticmethod
def user_already_offered(username, hoy_d, hoy_m):
query = '''
SELECT
t.name,
o.uuid
FROM offers o
INNER JOIN territory t ON o.territory=t.id
WHERE
user=?
AND season=?
AND day=?
ORDER BY
rank ASC
'''
res = Db.get_db().execute(query, (username, hoy_m, hoy_d))
cmove = res.fetchall()
res.close()
# The return value here will either be None or an array of tuples of the form (territory, order_uuid)
return cmove
@staticmethod
def get_foreign_order(team, hoy_d, hoy_m):
query = '''
SELECT
t.name,
FROM enemy_plans e
INNER JOIN territory t ON e.territory=t.id
WHERE
team=?
AND season=?
AND day=?
GROUP BY t.name
ORDER BY stars DESC
'''
res = Db.get_db().execute(query, (team, hoy_m, hoy_d))
fmove = res.fetchone()
res.close()
# If all else fails, default to the most primal hate
return "Columbus" if fmove is None else fmove[0]
@staticmethod
def write_new_offer(username, order, hoy_d, hoy_m, current_stars, rank):
query = '''
INSERT INTO offers (season, day, user, territory, stars, rank, uuid)
VALUES (?, ?, ?,
(SELECT id FROM territory WHERE name=?),
?, ?, ?)
'''
new_uuid = str(uuid4())
db = Db.get_db()
db.execute(query, (hoy_m, hoy_d, username, order, current_stars, rank, new_uuid))
db.commit()
return new_uuid
@staticmethod
def confirm_offer(username, hoy_d, hoy_m, uuid):
query = '''
INSERT INTO orders (
season,
day,
user,
territory,
stars,
accepted,
uuid
)
SELECT
season,
day,
user,
territory,
stars,
TRUE,
uuid
FROM
offers
WHERE
user=?
AND season=?
AND day=?
AND uuid=?
'''
db = Db.get_db()
cur = db.cursor()
cur.execute(query, (username, hoy_m, hoy_d, uuid))
nrows = cur.rowcount
db.commit()
if nrows > 0:
# Now retrieve the territory name of the order so we can return it
assigned_row = Orders.user_already_moved(username, hoy_d, hoy_m)
# Returning "None" from here would mean something went seriously askew, since
# we literally just assigned a row. Which doesn't mean it won't happen!
return None if assigned_row is None else assigned_row
return None
@staticmethod
def get_day_and_tier_totals(hoy_d, hoy_m, tier):
# For the record: We could do this in a single query, but that makes the logic much more
# difficult to read and to maintain. So I'm splitting it up so that you don't have to
# be an SQL expert to understand it. Note that this is absolutely going to be a slower
# overall process than doing it in a single query, but we're not exactly working with
# BiG DaTa here.
# First: Get the quota total for the tier
query = '''
SELECT
SUM(quota) as quota
FROM
plans
WHERE
season=?
AND day=?
AND tier=?
'''
res = Db.get_db().execute(query, (hoy_m, hoy_d, tier))
# We'll always get a tuple back, but if it's (None,) we need to coerce that to zero
quota = res.fetchone()[0] or 0
res.close()
# Next, get the assigned stars total
query = '''
SELECT
SUM(stars) AS stars
FROM
orders o
WHERE
season=?
AND day=?
AND accepted=TRUE
AND territory IN (
SELECT territory
FROM plans p
WHERE
p.season=o.season
AND p.day=o.day
AND p.tier=?
)
'''
res = Db.get_db().execute(query, (hoy_m, hoy_d, tier))
# We'll always get a tuple back, but if it's (None,) we need to coerce that to zero
assigned = res.fetchone()[0] or 0
res.close()
return (quota, assigned)
@staticmethod
def get_day_totals(hoy_d, hoy_m):
# See the note on get_day_and_tier_totals for the reason why this is done as two separate
# queries.
# First: Get the quota total for the day. This would look nicer if SUM(MAX(x)) worked.
query = '''
SELECT
SUM(quota)
FROM (
SELECT
MAX(quota) as quota
FROM
plans
WHERE
season=?
AND day=?
GROUP BY
territory
)
'''
res = Db.get_db().execute(query, (hoy_m, hoy_d))
row = res.fetchone()
if row:
quota = row[0]
else:
quota = 0
res.close()
if quota:
# We have some territories in the plans, so let's see a) how many and b) how many are complete
query = '''
SELECT
SUM(o.stars) / CAST(p.quota AS REAL) AS pct
FROM
plans p LEFT JOIN orders o ON (
p.season=o.season
AND p.day=o.day
AND p.territory=o.territory
AND o.accepted=TRUE
)
WHERE
p.season=?
AND p.day=?
AND p.tier in (
SELECT MAX(c.tier)
FROM plans c
WHERE c.territory=p.territory
AND c.season=p.season
AND c.day=p.day
)
GROUP BY
p.territory;
'''
res = Db.get_db().execute(query, (hoy_m, hoy_d))
all_pcts = res.fetchall()
res.close()
nterritories = len(all_pcts) or 0
if nterritories:
ncompleted = len(list(filter(lambda x: (x[0] or 0) >= 1.0, all_pcts)))
else:
ncompleted = 0
else:
nterritories = ncompleted = 0
# Finally, get the assigned stars total and number of players
query = '''
SELECT
SUM(stars) AS stars,
COUNT(DISTINCT user) AS players
FROM
orders o
WHERE
season=?
AND day=?
AND accepted=TRUE
AND territory IN (
SELECT territory
FROM plans p
WHERE
p.season=o.season
AND p.day=o.day
)
'''
res = Db.get_db().execute(query, (hoy_m, hoy_d))
row = res.fetchone()
assigned = row[0] or 0
nplayers = row[1] or 0
res.close()
return {
"nplayers": nplayers,
"quota": quota,
"assigned": assigned,
"nterritories": nterritories,
"ncompleted": ncompleted
}
@staticmethod
def get_tier_territory_summary(hoy_d, hoy_m, tier):
query = '''
SELECT
t.name,
p.quota,
SUM(o.stars) AS assigned,
SUM(o.stars) / CAST(p.quota AS REAL) AS pct,
COUNT(DISTINCT o.user) AS players
FROM
plans p LEFT JOIN orders o ON (
p.season=o.season
AND p.day=o.day
AND p.territory=o.territory
AND o.accepted=TRUE
) INNER JOIN territory t ON (
p.territory=t.id
)
WHERE
p.season=?
AND p.day=?
AND p.tier=?
GROUP BY
t.name
ORDER BY
pct ASC;
'''
res = Db.get_db().execute(query, (hoy_m, hoy_d, tier))
tary = res.fetchall()
res.close()
nterritories = len(tary) or 0
# In cases where the LEFT JOIN produces nothing in orders (eg, territory has no assigned orders)
# we need to cast the percentage to an int. Since NoneType isn't directly castable, we need
# to resort to `int(x[3] or 0)`.
ncompleted = len(list(filter(lambda x: int(x[3] or 0) >= 1, tary))) or 0
nplayers = reduce(lambda a, b: a + b[4], tary, 0)
return {
"nterritories": nterritories,
"ncompleted": ncompleted,
"nplayers": nplayers
}