-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.py
135 lines (117 loc) · 4.08 KB
/
GameManager.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
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
import threading
import time
import requests
class GameManager(threading.Thread):
api = 'https://rom.centrale-assos.fr/cac/'
username = ''
userkey = ''
partie = ''
card = ''
cards = []
running = True
action = None
callback = None
lastgamecall = 0
ACTION_PING = 0
ACTION_LOGIN = 1
ACTION_LOBBY = 2
ACTION_JOIN = 3
ACTION_CREATE = 4
ACTION_GAME = 5
ACTION_PLAY = 6
ACTION_VOTE = 7
def run(self):
print("GM running")
while self.running:
self.perform_calls()
time.sleep(0.1)
def request(self, uri='', payload=None):
try:
if payload != None:
print(payload)
r = requests.post(self.api + '?' + uri, data=payload, headers={'X-UK': self.userkey})
else:
r = requests.get(self.api+'?'+uri, headers={'X-UK': self.userkey})
if r.status_code != 200:
print(r.text)
return False
else:
print(r.text)
return r.json()
except:
return False
def perform_calls(self):
actiondebut = self.action
if self.action == self.ACTION_PING:
self.callback(self.request())
elif self.action == self.ACTION_LOGIN:
result = self.request('login', {'username': self.username})
if result != False:
if result['ok']:
self.userkey = result['userkey']
self.callback(True)
else:
self.callback(False, result['message'])
else:
self.callback(False, 'Echec de la communication avec le serveur.')
elif self.action == self.ACTION_LOBBY:
result = self.request('lobby')
if result != False:
self.callback(True, result['games'])
else:
self.callback(False)
elif self.action == self.ACTION_CREATE or self.action == self.ACTION_JOIN:
if self.action == self.ACTION_CREATE: act = 'create'
else: act = 'join'
result = self.request(act, {'name': self.partie})
if result != False:
if result['ok']:
self.callback(True)
else:
self.callback(False, result['message'])
else:
self.callback(False)
elif self.action in [self.ACTION_GAME, self.ACTION_PLAY, self.ACTION_VOTE]:
if self.action == self.ACTION_GAME and time.time() - self.lastgamecall < 2:
return
if self.action == self.ACTION_GAME:
result = self.request('game')
else:
result = self.request('game', {'card': self.card})
self.action = self.ACTION_GAME
if result != False:
self.callback(True, result)
else:
self.callback(False)
self.lastgamecall = time.time()
if actiondebut == self.action and self.action != self.ACTION_GAME:
self.action = None
def ping(self, callback):
self.callback = callback
self.action = self.ACTION_PING
def login(self, callback, username):
self.username = username
self.callback = callback
self.action = self.ACTION_LOGIN
def lobby(self, callback):
self.callback = callback
self.action = self.ACTION_LOBBY
def createGame(self, callback, name):
self.callback = callback
self.partie = name
self.action = self.ACTION_CREATE
def joinGame(self, callback, name):
self.callback = callback
self.partie = name
self.action = self.ACTION_JOIN
def play(self, callback, card):
self.callback = callback
self.card = card
self.action = self.ACTION_PLAY
def vote(self, callback, card):
self.callback = callback
self.card = card
self.action = self.ACTION_VOTE
def game(self, callback):
self.callback = callback
self.action = self.ACTION_GAME