1
from django.db import models
2
from questions.models import Question, Category
6
('theme', 'Тематическая')
10
class Queue(models.Model):
14
type = models.CharField(max_length=16, choices=types, default=types[0])
15
lowest_level = models.SmallIntegerField()
16
highest_level = models.SmallIntegerField()
20
return self.lobbies.select_related()
23
def players_count(self):
24
return sum([lobby.players_count for lobby in self.lobbies.all()])
27
class Lobby(models.Model):
32
type = models.CharField(max_length=16, choices=types, default=types[0])
33
queue = models.ForeignKey(Queue, on_delete=models.SET_NULL, related_name='lobbies', null=True, blank=True)
37
return self.players.select_related()
40
def players_count(self):
41
players = self.players
43
for _ in players.all():
49
def get_average_level(self):
50
players = self.players
53
for player in players.all():
54
total_level += player.level
57
return int(total_level / total_players)
60
class Game(models.Model):
64
type = models.CharField(max_length=16, choices=types, default=types[0])
65
lowest_level = models.SmallIntegerField()
66
highest_level = models.SmallIntegerField()
67
categories = models.ManyToManyField(Category, related_name='categories')
68
started = models.DateTimeField(auto_now=True)
69
is_finished = models.BooleanField(default=False)
70
current_question = models.ForeignKey(Question, on_delete=models.PROTECT, null=True, blank=True)
71
asked_questions = models.ManyToManyField(Question, related_name='asked_questions')
72
results = models.JSONField(default=dict)
76
return list(self.results.keys())
79
def display_type(self):
80
return eval(self.type)[1]
84
return self.started.strftime("%Y-%m-%d/%H:%M")