Passed
Push — main ( d12e93...0a6b7f )
by Yohann
01:14
created

ui.App.handle_event()   C

Complexity

Conditions 10

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 27
rs 5.9999
c 0
b 0
f 0
cc 10
nop 2

How to fix   Complexity   

Complexity

Complex classes like ui.App.handle_event() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import pygame
2
3
pygame.init()
4
pygame.key.set_repeat(500, 50)
5
6
7
class App:
8
9
    def __init__(self, client):
10
        self.client = client
11
        pygame.display.set_caption("PyChat v2.0")
12
13
        self.screen = pygame.display.set_mode((1280, 720))
14
15
        self.update = pygame.USEREVENT + 1
16
        pygame.time.set_timer(self.update, 1000)
17
18
        self.font = pygame.font.Font("oxygen.ttf", 12)
19
20
        self.message = ''
21
        self.history = []
22
23
        self.is_running = True
24
25
    def run(self):
26
        pygame.event.set_blocked(None)
27
        pygame.event.set_allowed(
28
            (self.update, pygame.KEYDOWN, pygame.TEXTINPUT, pygame.QUIT)
29
        )
30
31
        while self.is_running:
32
            self.draw()
33
            self.handle_event(pygame.event.wait())
34
35
    def draw(self):
36
        self.screen.fill(0)
37
38
        text = self.font.render(f">>>   {self.message}", True, (255, 255, 255))
39
        self.screen.blit(text, (20, 680))
40
41
        history = self.history[-15:]
42
        length_history = len(history)
43
44
        c_gain = 255 // (length_history + 1)
45
46
        for c, message in enumerate(history):
47
            color = ((c + 1) * c_gain, (c + 1) * c_gain, (c + 1) * c_gain)
48
49
            info = self.font.render(message['author'], True, color)
50
            self.screen.blit(info, (20, (c + 1) * 40))
51
52
            text = self.font.render(message['content'], True, color)
53
            self.screen.blit(text, (20, (c + 1) * 40 + 15))
54
55
        pygame.display.update()
56
57
    def handle_event(self, event):
58
        if event.type == pygame.QUIT:
59
            self.is_running = False
60
            return
61
62
        if event.type == self.update:
63
            new_messages = self.client.fetch_new_messages()
64
65
            if len(new_messages):
66
                for new_message in new_messages:
67
                    self.history.append(new_message)
68
69
            return
70
71
        if event.type == pygame.TEXTINPUT:
72
            self.message += event.text
73
            return
74
75
        if event.type == pygame.KEYDOWN:
76
            if event.key == pygame.K_BACKSPACE:
77
                self.message = self.message[:-1]
78
79
            if event.key == pygame.K_RETURN and len(self.message):
80
                self.client.send_message = self.message
81
82
                self.history.append({"author": "you", "content": self.message})
83
                self.message = ''
84