-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
284 lines (208 loc) · 6.88 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#from flask_socketio import SocketIO, emit, join_room, leave_room, \
# close_room, rooms, disconnect
from random import randint
import random
UNASSIGNED, LIBERAL, FACIST, HITLER = 'Unassigned', 'Liberal', 'Facist' , 'Hitler'
class Player():
def __init__(self, sid, socketio, name):
self.sid = sid
self.socketio = socketio
self.name = name
self.role = UNASSIGNED
self.eligable = True
def reply(self, event, data):
self.socketio.emit(event, data, room=self.sid, namespace='/test')
def assignRole(self, role):
self.role = role
class SecretHitler():
CMD_START = '0'
STATE_LOBBY, STATE_START, STATE_SLEEP, STATE_ELECT, STATE_LEGISLATIVE, STATE_EXECUTIVE, STATE_END = 0, 1, 2, 3, 4 ,5, 6
MIN_PLAYERS = 5
MAX_PLAYERS = 10
FACIST_POLICIES = 11
LIBERAL_POLICIES = 6
def __init__(self, socketio):
self.socketio = socketio
self.players = {}#SID = player
self.state = SecretHitler.STATE_LOBBY
self.num_facists = 0
self.facists = []
self.num_liberals = 0
self.liberals = []
self.hitler = None
self.fac_pol_en = 0
self.lib_pol_en = 0
self.president_index = 0
self.president = None
self.chancellor = None
self.votes = {}
#5 -6 six players
#Close your eyes
#F and H open eyes and acknowledge each other
#Close eyes
#Everyone open eyes
#7 - 10
#F know H but H not know F
#Rounds
#Election
#Legislative
#Executive action
def addPlayer(self, sid, message):
new_player = Player(sid, self.socketio, message['name'])
if self.state == SecretHitler.STATE_LOBBY:
self.players[sid] = new_player
for player in self.players.values():
player.reply('my_response', {'data': 'Player '+new_player.name+' connected', 'count': 0})
else:
new_player.reply('my_response', {'data': 'Game has already started sorry!!'})
def removePlayer(self, sid):
name = self.players[sid].name
del self.players[sid]
for player in self.players.values():
player.reply('my_response', {'data': 'Player '+name+' disconnected', 'count': 0})
def processPlayerMessage(self, sid, message):
# pass
print message['data']
command = message['data']
if command == SecretHitler.CMD_START:
print 'starting'
if self.state == SecretHitler.STATE_LOBBY:
self.state = SecretHitler.STATE_START
def messagePlayers(self, message):
for player in self.players.values():
player.reply('my_response', message)
def messageLiberals(self, message):
for libearl_id in self.liberals:
self.players[libearl_id].reply('my_response', message)
def messageFacists(self, message, msg_hitler=True):
for facist_id in self.facists:
self.players[facist_id].reply('my_response', message)
if msg_hitler:
self.players[self.hitler].reply('my_response', message)
def run(self):
self.lobbyState()
end_game = False
while end_game != True:
self.socketio.sleep(0.1)
if self.state == SecretHitler.STATE_START:
self.startState()
elif self.state == SecretHitler.STATE_SLEEP:
self.sleepState()
elif self.state == SecretHitler.STATE_ELECT:
self.electState()
elif self.state == SecretHitler.STATE_END:
self.endState()
end_game = True
def lobbyState(self):
sent_ready_msg = False
while self.state == SecretHitler.STATE_LOBBY:
ready = len(self.players) > SecretHitler.MIN_PLAYERS
self.socketio.sleep(0.1)
if ready and not sent_ready_msg:
self.messagePlayers({'data': 'Ready to start!'})
sent_ready_msg = True
elif not ready and sent_ready_msg:
self.messagePlayers({'data': 'Not ready to start!'})
sent_ready_msg = False
self.messagePlayers({'data':'Starting!'})
def startState(self):
num_players = len(self.players)
if num_players == 5:
self.num_liberals = 3
self.num_facists = 1
if num_players == 6:
self.num_liberals = 4
self.num_facists = 1
if num_players == 7:
self.num_liberals = 4
self.num_facists = 2
if num_players == 8:
self.num_liberals = 5
self.num_facists = 2
if num_players == 9:
self.num_liberals = 5
self.num_facists = 3
if num_players == 10:
self.num_liberals = 6
self.num_facists = 3
keys = self.players.keys()
#Assign hitler and the facists
self.hitler = random.choice(keys)
keys.remove(self.hitler)
self.players[self.hitler].assignRole(HITLER)
for i in range(0, self.num_facists):
facist = random.choice(keys)
keys.remove(facist)
self.facists.append(facist)
#Assign liberals
for key in keys:
self.liberals.append(key)
#notify everyone of their role
self.players[self.hitler].reply('my_response', {'data':'You are a Hitler, kill yourself'})
for facist in self.facists:
self.players[facist].reply('my_response', {'data':'You are a facist scum'})
for liberal in self.liberals:
self.players[liberal].reply('my_response', {'data':'You are a liberal scum'})
self.socketio.sleep(3)
self.state = SecretHitler.STATE_SLEEP
def sleepState(self):
self.messagePlayers({'data': 'Go to sleep!'})
self.socketio.sleep(5)
self.messageFacists({'data': 'Facists wake up!'}, msg_hitler=False)
self.socketio.sleep(10)
self.players[self.hitler].reply('my_response', {'data': 'Hitler stick your thumb out!'})
self.socketio.sleep(10)
self.messageFacists({'data': 'Facists go to sleep!'}, msg_hitler=True)
self.socketio.sleep(10)
self.messagePlayers({'data': 'Everyone wake up!'})
self.socketio.sleep(5)
self.state = SecretHitler.STATE_ELECT
def electState(self):
if self.president == None:
self.president = random.choice(self.players.keys())
for index, player_key in enumerate(self.players.keys()):
if self.president == player_key:
self.president_index = index
break
else:
self.president_index += 1
if self.president_index == len(self.players):
self.president_index = 0
self.president = self.players.keys()[self.president_index]
self.players[self.president].reply('my_response', {'data':'You are the president'})
self.socketio.sleep(5)
self.players[self.president].reply('my_response', {'data':'Select a chancellor'})
self.chancellor = None
while self.chancellor == None:
self.socketio.sleep(0.1)
nominee = self.players[self.chancellor]
self.players[self.chancellor].reply('my_response', {'data': nominee.name+' has been nominated for chancellor'})
messagePlayers('my_response', {'data':'Vote for '+nominee.name+' to be chancellor'})
self.votes = {}
while len(self.votes) < len(self.players):
self.socketio.sleep(0.1)
votes_yes = 0
votes_no = 0
msg = ''
for player_id, vote in self.votes.iteritems():
msg += self.players[player_id].name + " voted: " + vote +"\n"
if vote == 'yes':
votes_yes += 1
else:
votes_no += 1
messagePlayers('my_response', {'data': msg})
if votes_yes > votes_no:
#voted in
pass
elif votes_yes == votes_no:
#tie
pass
else:
#fail
pass
def legislativeState(self):
pass
def exectutiveState(self):
pass
def endState(self):
pass