/
game.py
85 lines (67 loc) · 2.78 KB
/
game.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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
####################################################################
# This file is part of atts.
# Foobar is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Foobar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
#####################################################################
class Game:
def __init__(self):
self.players = {}
self.somme = 0
self.reset = 0
def update_game(self, data_ori):
"""data = {client1: {'client':data11},
client2: {'client':data2}}"""
# pour éviter dictionary changed size during iteration
data = data_ori.copy()
for k, v in data.items():
if v:
if 'reset' in v.values():
self.somme = 0
self.reset = 1
print("reset")
if self.reset == 0:
self.add_data(data)
else:
self.reset = 0
def add_data(self, data):
"""self.players =
{('192.168.1.12', 39464): 162,
('192.168.1.12', 39466): 162,
('192.168.1.12', 39468): 162,
('192.168.1.12', 39470): 162}
"""
# sinon ça pointe vers la même mémoire
players_old = self.players.copy()
for k, v in data.items():
# récup des nums
if k and v:
self.players[k] = v['client']
self.somme = sum(self.players.values())
if __name__ == "__main__":
data1 = {('192.168.1.12', 42514): {'client': '469'},
('192.168.1.12', 42512): {'client': '500'},
('192.168.1.12', 42518): {'client': '538'},
('192.168.1.12', 42516): {'client': '472'}}
data2 = {('192.168.1.12', 42514): {'client': '469'},
('192.168.1.12', 42512): {'client': '501'},
('192.168.1.12', 42518): {'client': '538'},
('192.168.1.12', 42516): {'client': '473'}}
data3 = {('192.168.1.12', 42514): {'client': '50'},
('192.168.1.12', 42512): {'client': '10'},
('192.168.1.12', 42518): {'client': '22'},
('192.168.1.12', 42516): {'client': '33'}}
game = Game()
game.update_game(data1)
game.update_game(data1)
game.update_game(data2)
game.update_game(data3)