This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
awards.py
313 lines (253 loc) · 11 KB
/
awards.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
'''isort:skip_file'''
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cindy.settings")
import numpy as np
import django
django.setup()
from django.utils import timezone
from django.db.models import Count, Max, Q
from sui_hei.models import *
BEST_OF_MONTH_GRANT_DAY = 15
snipe = (
(5, Award.objects.get_or_create(name_ja="千里眼")[0]),
(20, Award.objects.get_or_create(name_ja="★イーグルアイ")[0]),
(70, Award.objects.get_or_create(name_ja="★★サードアイ")[0]), )
sniped = (
(3, Award.objects.get_or_create(name_ja="かすり傷")[0]),
(15, Award.objects.get_or_create(name_ja="手負いの兵")[0]),
(50, Award.objects.get_or_create(name_ja="蜂の巣")[0]),
(100, Award.objects.get_or_create(name_ja="不死身")[0]),
(200, Award.objects.get_or_create(name_ja="射撃演習場の的")[0]), )
soup = (
(100, Award.objects.get_or_create(name_ja="100問出題")[0]),
(200, Award.objects.get_or_create(name_ja="200問出題")[0]),
(300, Award.objects.get_or_create(name_ja="300問出題")[0]),
(500, Award.objects.get_or_create(name_ja="500問出題")[0]),
(600, Award.objects.get_or_create(name_ja="600問出題")[0]),
(800, Award.objects.get_or_create(name_ja="出題王")[0]),
(1000, Award.objects.get_or_create(name_ja="出題皇帝")[0]),
(1500, Award.objects.get_or_create(name_ja="出題悪魔")[0]),
(2000, Award.objects.get_or_create(name_ja="出題魔王")[0]),
(3000, Award.objects.get_or_create(name_ja="出題魔神")[0]), )
shitumon = (
(1000, Award.objects.get_or_create(name_ja="1000回質問")[0]),
(2000, Award.objects.get_or_create(name_ja="2000回質問")[0]),
(3000, Award.objects.get_or_create(name_ja="3000回質問")[0]),
(5000, Award.objects.get_or_create(name_ja="5000回質問")[0]),
(6000, Award.objects.get_or_create(name_ja="6000回質問")[0]),
(8000, Award.objects.get_or_create(name_ja="質問王")[0]),
(10000, Award.objects.get_or_create(name_ja="質問皇帝")[0]),
(15000, Award.objects.get_or_create(name_ja="質問天使")[0]),
(20000, Award.objects.get_or_create(name_ja="質問大天使")[0]),
(30000, Award.objects.get_or_create(name_ja="質問神")[0]), )
seikai = (
(50, Award.objects.get_or_create(name_ja="探偵")[0]),
(100, Award.objects.get_or_create(name_ja="名探偵")[0]),
(200, Award.objects.get_or_create(name_ja="ホームズ(猫)")[0]),
(300, Award.objects.get_or_create(name_ja="新一")[0]),
(500, Award.objects.get_or_create(name_ja="小五郎")[0]),
(800, Award.objects.get_or_create(name_ja="耕助")[0]),
(1111, Award.objects.get_or_create(name_ja="★ポアロ")[0]),
(2222, Award.objects.get_or_create(name_ja="★★コロンボ")[0]),
(3333, Award.objects.get_or_create(name_ja="★★★ホームズ")[0]), )
good_ques = (
(100, Award.objects.get_or_create(name_ja="ピン")[0]),
(500, Award.objects.get_or_create(name_ja="矢印")[0]),
(1000, Award.objects.get_or_create(name_ja="印")[0]),
(2000, Award.objects.get_or_create(name_ja="しるべ")[0]),
(3000, Award.objects.get_or_create(name_ja="座標")[0]),
(5000, Award.objects.get_or_create(name_ja="羅針盤")[0]),
(7000, Award.objects.get_or_create(name_ja="コンパス")[0]),
(11111, Award.objects.get_or_create(name_ja="地図")[0]),
(22222, Award.objects.get_or_create(name_ja="地球儀")[0]),
(33333, Award.objects.get_or_create(name_ja="ポラリス")[0]), )
star = (
(5, Award.objects.get_or_create(name_ja="スターター", )[0]),
(100, Award.objects.get_or_create(name_ja="ポスター", )[0]),
(300, Award.objects.get_or_create(name_ja="キャスター", )[0]),
(500, Award.objects.get_or_create(name_ja="レジスター", )[0]),
(1000, Award.objects.get_or_create(name_ja="マイスター", )[0]),
(2000, Award.objects.get_or_create(name_ja="クラスター", )[0]),
(5000, Award.objects.get_or_create(name_ja="★", )[0]), )
best_of_month = (
(1, Award.objects.get_or_create(name_ja="★鶴")[0]),
(2, Award.objects.get_or_create(name_ja="★鶯")[0]),
(3, Award.objects.get_or_create(name_ja="★燕")[0]),
(4, Award.objects.get_or_create(name_ja="★蝶")[0]),
(5, Award.objects.get_or_create(name_ja="★鰹")[0]),
(6, Award.objects.get_or_create(name_ja="★蝸牛")[0]),
(7, Award.objects.get_or_create(name_ja="★蝉")[0]),
(8, Award.objects.get_or_create(name_ja="★鈴虫")[0]),
(9, Award.objects.get_or_create(name_ja="★蜻蛉")[0]),
(10, Award.objects.get_or_create(name_ja="★啄木鳥")[0]),
(11, Award.objects.get_or_create(name_ja="★鷹")[0]),
(12, Award.objects.get_or_create(name_ja="★狼")[0]), )
class SuiheiAwardJudger(object):
def __init__(self, judge=None):
'''
judge: function to decide which award to grant to a given user.
user as param, iterable with each object an instance of Award as output.
'''
if judge: self.judge = judge
self.message = ""
def _grant(self, user, award):
'''grant award to user'''
ua, status = UserAward.objects.get_or_create(
user_id=user, award_id=award)
if status:
ua.created = timezone.now()
ua.save()
print("Grant", award, "to", user)
self.message += "Grant " + str(award) + " to " + str(user) + "\n"
def execute(self, user):
if self.judge:
award = self.judge(user)
else:
award = []
for a in award:
self._grant(user, a)
return self.message
def execAll(self, users):
for user in users:
self.execute(user)
return self.message
class SuiheiAwardTimedJudger(SuiheiAwardJudger):
def __init__(self, day=None, weekday=None, month=None, *args, **kwargs):
self.day = day
self.weekday = weekday
self.month = month
super(SuiheiAwardTimedJudger, self).__init__(*args, **kwargs)
def _time_is_ok(self):
today = timezone.now()
if self.day and self.day != today.day:
return False
if self.weekday and self.weekday != today.weekday:
return False
if self.month and self.month != today.month:
return False
return True
def execute(self, *args, **kwargs):
if self._time_is_ok():
return super(SuiheiAwardTimedJudger, self).execute(*args, **kwargs)
else:
return ""
def execAll(self, *args, **kwargs):
if self._time_is_ok():
return super(SuiheiAwardTimedJudger, self).execAll(*args, **kwargs)
else:
return ""
def _award_or_none(count, awards):
for a in awards:
if count >= a[0]:
yield a[1]
def _soup_judge(user):
soup_count = Mondai.objects.filter(user_id=user).count()
return _award_or_none(soup_count, soup)
def _shitumon_judge(user):
shitumon_count = Shitumon.objects.filter(user_id=user).count()
return _award_or_none(shitumon_count, shitumon)
def _seikai_judge(user):
seikai_count = Shitumon.objects.filter(user_id=user, true=True).count()
return _award_or_none(seikai_count, seikai)
def _good_ques_judge(user):
good_ques_count = Shitumon.objects.filter(user_id=user, good=True).count()
return _award_or_none(good_ques_count, good_ques)
def _snipe_judge(user):
true_ques = Shitumon.objects.filter(user_id=user, true=True)
tested_soups = []
count = 0
for q in true_ques:
if q.mondai_id_id in tested_soups:
continue
else:
tested_soups.append(q.mondai_id_id)
soup = q.mondai_id
if not (soup.genre == 0 or soup.yami):
continue
if soup.yami:
user_first = soup.shitumon_set.filter(
user_id=user).order_by("id").first()
if user_first.true:
print("---", user, ':', soup)
count += 1
elif soup.genre == 0:
first_good = soup.shitumon_set.filter(
good=True).order_by("id").first()
user_first = soup.shitumon_set.filter(
user_id=user).order_by("id").first()
if (not first_good or first_good.id > q.id) and user_first.true:
print("---", user, ':', soup)
count += 1
user.snipe = count
user.save()
print("Update:", user, "snipe ->", count)
return []
def _sniped_judge(user):
soups = Mondai.objects.filter(
Q(genre=0) | Q(yami=True), user_id=user, status=1)
count = 0
for s in soups:
first_good_or_true = s.shitumon_set.filter(
Q(good=True) | Q(true=True)).order_by("id").first()
if (first_good_or_true and first_good_or_true.true):
count += 1
user.sniped = count
user.save()
print("Update:", user, "sniped ->", count)
return []
def _star_judge(user):
star_count = Star.objects.filter(user_id=user).count()
return _award_or_none(star_count, star)
judgers = {
"soup": SuiheiAwardJudger(judge=_soup_judge),
"shitumon": SuiheiAwardJudger(judge=_shitumon_judge),
"seikai": SuiheiAwardJudger(judge=_seikai_judge),
"good_ques": SuiheiAwardJudger(judge=_good_ques_judge),
"snipe": SuiheiAwardJudger(judge=_snipe_judge),
"sniped": SuiheiAwardJudger(judge=_sniped_judge),
"star": SuiheiAwardJudger(judge=_star_judge),
}
def best_of_month_granter():
# get current time
today = timezone.now()
# skip if is not the grant day
if today.day != BEST_OF_MONTH_GRANT_DAY:
return ""
# get year & month of the previous month
prevMonth = today.month - 1
prevYear = today.year
if prevMonth == 0:
prevMonth = 12
prevYear -= 1
# get the best soup of the last month
soupInPrevMonth = Mondai.objects.filter(
created__month=prevMonth,
created__year=prevYear).annotate(Count("star"))
star_count_max = soupInPrevMonth.aggregate(
Max("star__count"))["star__count__max"]
best_soups_of_last_month = soupInPrevMonth.filter(
star__count=star_count_max).all()
award_of_last_month = dict(best_of_month)[prevMonth]
# if no soup found, return
if not best_soups_of_last_month:
return ""
# grant award
message = ""
best_soup_index = int(
np.argmax([s.score for s in best_soups_of_last_month]))
best_soup_of_last_month = best_soups_of_last_month[best_soup_index]
ua, status = UserAward.objects.get_or_create(
user_id=best_soup_of_last_month.user_id, award_id=award_of_last_month)
if status:
ua.created = timezone.now()
ua.save()
message += "Grant [" + str(award_of_last_month) + ']'\
" to " + str(best_soup_of_last_month.user_id.nickname) + \
" for soup <" + str(best_soup_of_last_month.title) + '>'\
" got the most star count " + str(best_soup_of_last_month.star__count) + \
" in " + best_soup_of_last_month.created.date().strftime("%Y/%m/%d") + '\n'
print(message)
return message
granters = {
"best_of_month": best_of_month_granter,
}