Quiz

Форк
0
/
models.py 
84 строки · 2.2 Кб
1
from django.db import models
2
from questions.models import Question, Category
3

4
TYPES = (
5
        ('normal', 'Обычная'),
6
        ('theme', 'Тематическая')
7
    )
8

9

10
class Queue(models.Model):
11

12
    types = TYPES
13

14
    type = models.CharField(max_length=16, choices=types, default=types[0])
15
    lowest_level = models.SmallIntegerField()
16
    highest_level = models.SmallIntegerField()
17

18
    @property
19
    def lobbies(self):
20
        return self.lobbies.select_related()
21

22
    @property
23
    def players_count(self):
24
        return sum([lobby.players_count for lobby in self.lobbies.all()])
25

26

27
class Lobby(models.Model):
28

29
    objects = None
30
    types = TYPES
31

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)
34

35
    @property
36
    def players(self):
37
        return self.players.select_related()
38

39
    @property
40
    def players_count(self):
41
        players = self.players
42
        total_players = 0
43
        for _ in players.all():
44
            total_players += 1
45

46
        return total_players
47

48
    @property
49
    def get_average_level(self):
50
        players = self.players
51
        total_level = 0
52
        total_players = 0
53
        for player in players.all():
54
            total_level += player.level
55
            total_players += 1
56

57
        return int(total_level / total_players)
58

59

60
class Game(models.Model):
61

62
    types = TYPES
63

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)
73

74
    @property
75
    def players(self):
76
        return list(self.results.keys())
77

78
    @property
79
    def display_type(self):
80
        return eval(self.type)[1]
81

82
    @property
83
    def get_time(self):
84
        return self.started.strftime("%Y-%m-%d/%H:%M")
85

86

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.