-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodels.py
More file actions
476 lines (373 loc) · 15.9 KB
/
models.py
File metadata and controls
476 lines (373 loc) · 15.9 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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import uuid
import google.cloud.tasks_v2 as tasks_v2
import structlog
from django.apps import apps
from django.conf import settings
from django.db import models, transaction
from django.urls import reverse
from django.utils import timezone
from sortedm2m.fields import SortedManyToManyField
from siarnaq import bracket
from siarnaq.api.episodes.managers import EpisodeQuerySet, TournamentQuerySet
from siarnaq.gcloud import tasks
logger = structlog.get_logger(__name__)
class Language(models.TextChoices):
"""
An immutable type enumerating the available languages.
"""
JAVA_8 = "java8"
JAVA_21 = "java21"
PYTHON_3 = "py3"
class EligibilityCriterion(models.Model):
"""
A database model for an eligibility criterion for entering into a tournament.
"""
title = models.CharField(max_length=128)
"""The title of this criterion."""
description = models.TextField()
"""The description of this criterion."""
icon = models.CharField(max_length=8)
"""An icon to display for teams that satisfy this criterion."""
is_private = models.BooleanField(default=True)
"""Whether this criterion can only be assigned by an admin."""
def __str__(self):
return self.title
class Episode(models.Model):
"""
A database model for the information regarding an episode of Battlecode.
"""
name_short = models.SlugField(max_length=16, primary_key=True)
"""Short unique identifying tag for the episode."""
name_long = models.CharField(max_length=128)
"""Full-form name for the episode."""
blurb = models.TextField(blank=True)
"""A longer description of the episode."""
registration = models.DateTimeField()
"""
The time at which registration for the episode begins.
Before this time, the episode is only visible to staff users.
"""
game_release = models.DateTimeField()
"""
The time at which the game specs are released.
Before this time, the game specs are only visible to staff users.
"""
game_archive = models.DateTimeField()
"""
The time at which the episode is archived.
After this time, no more ranked matches can be played.
"""
submission_frozen = models.BooleanField(default=True)
"""
Whether submissions are frozen.
If true, only teams with staff privileges can make submisssions.
"""
autoscrim_schedule = models.CharField(max_length=64, null=True, blank=True)
"""A cron specification for the autoscrim schedule, or null if disabled."""
language = models.CharField(max_length=8, choices=Language.choices)
"""The implementation language supported for this episode."""
scaffold = models.URLField(blank=True)
"""The URL of the git repository where the scaffold can be obtained."""
artifact_name = models.CharField(max_length=32, blank=True)
"""The name of the artifact generated by deploy systems."""
release_version_client = models.CharField(max_length=32, blank=True)
"""The code release version for the client."""
release_version_public = models.CharField(max_length=32, blank=True)
"""The code release available for public use."""
release_version_saturn = models.CharField(max_length=32, blank=True)
"""The code release used by Saturn, which may differ from the public version."""
eligibility_criteria = models.ManyToManyField(
EligibilityCriterion, related_name="episodes", blank=True
)
"""The eligibility criteria active in this episode."""
is_allowed_ranked_scrimmage = models.BooleanField(default=True)
"""Whether ranked scrimmages are allowed in this episode."""
ranked_scrimmage_hourly_limit = models.PositiveSmallIntegerField(default=10)
"""The maximum number of ranked scrimmage requests a team can create in an hour."""
unranked_scrimmage_hourly_limit = models.PositiveSmallIntegerField(default=10)
"""
The maximum number of unranked scrimmage requests a team can create in an hour.
"""
objects = EpisodeQuerySet.as_manager()
def __str__(self):
return self.name_short
def frozen(self):
"""Return whether the episode is currently frozen to submissions."""
now = timezone.now()
if self.submission_frozen or now < self.game_release:
return True
def autoscrim(self, best_of, override_freeze=False):
"""
Trigger a round of automatically-generated ranked scrimmages for all teams in
this episode with an accepted submission, unless the episode is archived or
frozen.
Parameters
----------
best_of : int
The number of maps to be played in each match, must be no greater than the
number of maps available for the episode.
"""
log = logger.bind(episode=self.pk)
if self.frozen() and not override_freeze:
log.warn("autoscrim_frozen", message="Refusing to autoscrim: frozen.")
return
if timezone.now() > self.game_archive:
log.warn("autoscrim_archived", message="Refusing to autoscrim: archived.")
apps.get_model("teams", "Team").objects.autoscrim(episode=self, best_of=best_of)
def for_saturn(self):
"""Return the representation of this object as expected by Saturn."""
return {
"name": self.name_short,
"language": self.language,
"scaffold": self.scaffold,
}
class Map(models.Model):
"""
A database model for the information regarding a game map in an episode.
"""
episode = models.ForeignKey(Episode, on_delete=models.CASCADE, related_name="maps")
"""The episode to which this map belongs."""
name = models.SlugField(max_length=24)
"""The name of the map."""
is_public = models.BooleanField(default=False)
"""
Whether the map is publicly accessible.
If false, only teams with staff privileges can use the map.
"""
class Meta:
constraints = [
models.UniqueConstraint(
fields=["episode", "name"],
name="map-unique-episode-name",
)
]
def __str__(self):
return f"{self.name} ({self.episode})"
class TournamentStyle(models.TextChoices):
"""
An immutable type enumerating the available styles of tournament.
"""
SINGLE_ELIMINATION = "SE"
DOUBLE_ELIMINATION = "DE"
class Tournament(models.Model):
"""
A database model for the information regarding a tournament in an episode.
"""
name_short = models.SlugField(max_length=32, primary_key=True)
"""Short unique identifying tag for the tournament."""
name_long = models.CharField(max_length=128)
"""Full-form name for the tournament."""
blurb = models.TextField(blank=True)
"""A longer description of the tournament."""
episode = models.ForeignKey(
Episode,
on_delete=models.PROTECT,
related_name="tournaments",
)
"""The episode to which this tournament belongs."""
style = models.CharField(max_length=2, choices=TournamentStyle.choices)
"""The style of this tournament."""
eligibility_includes = models.ManyToManyField(
EligibilityCriterion,
related_name="include_tournaments",
blank=True,
)
"""
The eligibility criteria that must be satisfied for a team to enter the tournament.
"""
eligibility_excludes = models.ManyToManyField(
EligibilityCriterion,
related_name="exclude_tournaments",
blank=True,
)
"""
The eligibility criteria that must not be satisfied for a team to enter the
tournament.
"""
require_resume = models.BooleanField()
"""Whether teams must have submitted resumes in order to enter the tournament."""
is_public = models.BooleanField()
"""Whether this tournament is included in the public index."""
display_date = models.DateField()
"""
The official date of the tournament; that is, when it will be streamed.
"""
submission_freeze = models.DateTimeField()
"""
The time at which submissions are frozen for the tournament.
Between this time and the unfreeze time, only staff members are able to submit.
"""
submission_unfreeze = models.DateTimeField()
"""
The time at which submissions are unfrozen for the tournament.
Between the freeze time and this time, only staff members are able to submit.
The submissions to enter into this tournament are the latest ones accepted by this
time.
"""
external_id_private = models.SlugField(max_length=128, blank=True)
"""The bracket service's ID of the associated private bracket."""
external_id_public = models.SlugField(max_length=128, blank=True)
"""The bracket service's ID of the associated public bracket."""
objects = TournamentQuerySet.as_manager()
def __str__(self):
return self.name_short
def get_eligible_teams(self):
"""Returns the list of teams that would be entered in this tournament,
if it were to start right now."""
return (
apps.get_model("teams", "Team")
.objects.filter_eligible(self)
# Run `with_active_submission` last,
# so that the annotation still is included.
# (With hack set strat, annotations go away)
.with_active_submission()
.order_by("-profile__rating__value")
)
def initialize(self):
"""
Seed the tournament with eligible teams in order of decreasing rating,
populate the brackets in the bracket service, and create TournamentRounds.
"""
# For security by obfuscation, and to allow easy regeneration of bracket,
# create a random string to append to private bracket.
key = str(uuid.uuid4())
self.external_id_public = f"{self.name_short}"
self.external_id_private = f"private_{self.external_id_public}_{key}"
teams = self.get_eligible_teams()
for is_private in [True, False]:
bracket.create_tournament(self, is_private=is_private)
bracket.bulk_add_teams(self, teams, is_private=is_private)
bracket.start_tournament(self, is_private=is_private)
# Create TournamentRound objects
round_objects = bracket.create_round_objects(self)
with transaction.atomic():
TournamentRound.objects.bulk_create(round_objects)
self.save(update_fields=["external_id_private", "external_id_public"])
class ReleaseStatus(models.IntegerChoices):
"""
An immutable type enumerating the degree to which the results of a tournament match
are released. Greater values indicate greater visibility.
"""
HIDDEN = 0
PARTICIPANTS = 1
RESULTS = 2
class TournamentRound(models.Model):
"""
A database model for the information regarding a round of a tournament. A round is
defined as a parallel set of matches; for example, "Round 1", or the semi-finals.
"""
tournament = models.ForeignKey(
Tournament,
on_delete=models.PROTECT,
related_name="rounds",
)
"""The tournament to which this round belongs."""
external_id = models.SmallIntegerField(null=True, blank=True)
"""
The bracket service's internal ID of this round.
"""
name = models.CharField(max_length=64)
"""The name of this round in human-readable form, such as "Round 1"."""
display_order = models.PositiveSmallIntegerField()
"""
The order (within the tournament) to display this round in user-facing interfaces.
"""
maps = SortedManyToManyField(Map, related_name="tournament_rounds", blank=True)
"""The maps to be used in this round."""
release_status = models.IntegerField(
choices=ReleaseStatus.choices, default=ReleaseStatus.HIDDEN
)
"""THe degree to which matches in this round are released."""
in_progress = models.BooleanField(default=False)
"""Whether the round is currently being run on the Saturn compute cluster."""
class Meta:
constraints = [
models.UniqueConstraint(
fields=["tournament", "external_id"],
name="round-unique-tournament-bracket",
)
]
def __str__(self):
return f"{self.tournament} ({self.name})"
def enqueue(self):
"""Creates and enqueues all matches for this round.
Fails if this round is already in progress."""
if self.in_progress:
raise RuntimeError("The round's matches are already running in Saturn.")
num_maps = self.maps.count()
# Game runner allows for even number of maps.
# But we can't allow this for tournaments, since this would result in ties.
if num_maps % 2 == 0:
raise RuntimeError("The round does not have an odd number of maps.")
(
match_objects,
match_participant_objects,
) = bracket.get_match_and_participant_objects_for_round(self)
Match = apps.get_model("compete", "Match")
with transaction.atomic():
matches = Match.objects.bulk_create(match_objects)
# Can only create these objects after matches are saved,
# because beforehand, matches will not have a pk.
maps_for_match_objects = [
Match.maps.through(match_id=match.pk, map_id=map.pk)
for match in matches
for map in self.maps.all()
]
Match.maps.through.objects.bulk_create(maps_for_match_objects)
apps.get_model("compete", "MatchParticipant").objects.bulk_create(
match_participant_objects
)
# IMPORTANT: bulk create does not respect the map ordering so we reset it here
tournament_round_maps = self.maps.all()
map_order = {map.id: index for index, map in enumerate(tournament_round_maps)}
# Prepare bulk update data
through_model = matches[0].maps.through
bulk_updates = []
with transaction.atomic():
for match in matches:
match_maps = match.maps.all()
for map in match_maps:
if map.id in map_order:
bulk_updates.append(
through_model(
match_id=match.id,
map_id=map.id,
sort_value=map_order[map.id],
)
)
# Delete existing relationships
through_model.objects.filter(match__in=matches).delete()
# Bulk create new relationships with correct order
through_model.objects.bulk_create(bulk_updates)
# Enqueue the matches
Match.objects.filter(pk__in=[match.pk for match in matches]).enqueue()
self.in_progress = True
self.save(update_fields=["in_progress"])
def request_publish_to_bracket(self, *, is_public):
"""Request that the match results are published asynchronously."""
tasks_client = tasks.get_task_client()
parent = tasks_client.queue_path(
settings.GCLOUD_PROJECT,
settings.GCLOUD_LOCATION,
settings.GCLOUD_BRACKET_QUEUE,
)
for match in self.matches.all():
url = "https://{}{}".format(
settings.ALLOWED_HOSTS[0],
reverse(
"match-publish-public-bracket",
kwargs={"pk": match.pk, "episode_id": match.episode.pk},
),
)
task = tasks_v2.Task(
http_request=tasks_v2.HttpRequest(
http_method=tasks_v2.HttpMethod.POST,
url=url,
oidc_token=tasks_v2.OidcToken(
service_account_email=settings.GCLOUD_SERVICE_EMAIL,
),
),
)
tasks_client.create_task(request=dict(parent=parent, task=task))
self.release_status = ReleaseStatus.RESULTS
self.save(update_fields=["release_status"])