/
player.py
383 lines (276 loc) · 10.2 KB
/
player.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
from CardHolder import *
from communication import *
import json
import string
addr = 'njaal.net'
#addr = 'localhost'
port = 9898
my_port = random.randint(10000, 15000)
class Players():
'''
Class for keeping track of the players in play
'''
def __init__(self, players, nick):
print players
self.players = players
self.out = []
self.index = 0
self.nick = nick
#optimize in the future !!!! can find this faster
for player in self.players:
if player[-1] == nick:
self.index = self.players.index(player)
def getMyData(self):
return self.players[self.index]
def getNextPlayer(self):
return self.players[(self.index + 1) % len(self.players)]
def getMyIndex(self):
return self.index
def update(self, playersData):
# If this is the case, it means that the game is over and we have a looser
if len(playersData["in"]) <= 1:
return -1
#If not we need to resort the list of players and who we are to connect to and such
remove = []
for player in playersData["out"]:
for tuples in self.players:
if tuples[-1] == player:
remove.append(tuples)
self.players = [x for x in self.players if x not in remove]
index = 0
for player in self.players:
if player[-1] == self.nick:
self.index = index
break
index += 1
return 0
class Player:
'''
Main class to do the old maid
'''
def __init__(self, nick):
self.nick = nick
self.state = "Startphase"
self.hand = CardHolder()
self.client = Client()
def main(self):
gameData = self.joinTheTable()
while(self.state != "GameOver"):
if self.state == "Startphase":
print "State : Startphase"
if gameData["result"] == "ok":
#Start by connecting to the other players
self.players = Players(gameData["players"], self.nick)
me = self.players.getMyData()
self.server = Server('0.0.0.0', me[0][1])
self.nextPlayer = self.players.getNextPlayer()
#Nasty hack because the server sucks BIG TIME, could write a better one in my sleep
self.client.connect(self.nextPlayer[0][0].split(':')[-1], self.nextPlayer[0][1])
self.server.connect()
#The first player start drawing card from the table
if self.players.getMyIndex() == 0:
self.state = "DrawCardFromTable"
#The rest wait for their turn
else:
self.state = "WaitForTurn"
else:
print "The table returned error, can't play the game ", gameData
return
elif self.state == "DrawCardFromTable":
print "State : DrawCard"
cardData = self.drawCardFromTable()
if cardData["result"] != "error":
#Place the card inside your hand if not a pair
cardtmp = cardData["card"]
card = Card(cardtmp[0], cardtmp[1])
if self.hand.equalCards(card):
discardList = self.hand.discardCardPair(card)
self.discardCards(discardList, False)
else:
self.hand.insertCard(card)
#If ok the turn is passed to the next player
if cardData["result"] == "ok":
self.sendTurnToNextOpponent()
self.state = "WaitForTurn"
#If last card all the cards has been drawn and i should start offering my hand
elif cardData["result"] == "last_card":
self.state = "OfferHand"
else:
print "Didn't get to draw a card ", cardData
return
elif self.state == "WaitForTurn":
print "State : waiting"
#Do an action based on the cmd recived from opponent
datafromOponent = json.loads(self.server.recive(1000))
if datafromOponent["cmd"] == "your_turn":
self.server.send(json.dumps({"result": "ok"}))
self.state = "DrawCardFromTable"
elif datafromOponent["cmd"] == "offer":
self.server.send(json.dumps({"result": "ok"}))
self.state = "PickCard"
else:
print "Got something i didn't expect from opponent ", datafromOponent
return
elif self.state == "OfferHand":
print "State : OfferHand"
self.offerHand()
elif self.state == "PickCard":
print "State : PickCard"
total =int(datafromOponent["num_cards"])
print "number of cards got from opponent " , total
if (total <= 0):
print "Player to the left is done"
res = self.players.update(self.getStatus())
if (res == -1):
print "I Lost the game, Game over"
return
#To handle the special case where the prev player is out i need to offer my hand to the next player
self.offerHand()
#in case if i won the game on the last offer hand
if self.state != "WaitForEndOfGame":
#Then wait for a new connection beacuse the previous is out
self.server.connect()
continue
num = random.randint(0,int(datafromOponent["num_cards"] ))
sendPickData = json.dumps({'cmd' : 'pick', 'card_num' : num})
try:
self.server.send(sendPickData)
responseJson = self.server.recive(1000)
response = json.loads(responseJson)
except Exception as e:
print "Pick Card failed: ", e.args
return
if response["result"] != "error":
#Place the card inside your hand if not a pair
cardtmp = response["card"]
card = Card(cardtmp[0], cardtmp[1])
if self.hand.equalCards(card):
discardList = self.hand.discardCardPair(card)
self.discardCards(discardList)
if len(self.hand) == 0:
print "I won the game, by picking the last card i needed"
self.sendOut()
print "Sending offer hand to", self.nextPlayer[1]
self.sendOfferHand()
self.state = "WaitForEndOfGame"
continue
else:
self.hand.insertCard(card)
self.state = "OfferHand"
self.offerHand()
#If i took the last card from the other player
if total == 1:
#nasty hack, to simply allow tha other client to be out
time.sleep(1)
status = self.getStatus()
res = self.players.update(status)
if (res == -1):
print "I Lost the game, Picked the last card from the opponent"
return
self.server.connect()
else:
print "Didn't get to pick a card ", response
return
elif self.state == "WaitForEndOfGame":
print "Won the game"
try:
responseJson = self.server.recive(1000, 1)
response = json.loads(responseJson)
if response["cmd"] == "offer":
data = json.dumps({"result":"out"})
self.server.send(data)
except Exception as e:
status = self.getStatus()
print status
if self.players.update(status) == -1:
print "Game Over"
return
time.sleep(1)
else:
print "unknown state ", self.state
return
def offerHand(self):
print self.hand
self.hand.shuffle()
print "Sending offer hand to palyer: ", self.nextPlayer[1]
#The player i offered my hand to is out, so need to update the players
if (self.sendOfferHand() == -1):
res = self.players.update(self.getStatus())
if res == -1:
print "I Lost the game, Game over"
sys.exit()
print "Previous player is done, nick:", self.nextPlayer[1]
nextPlayer = self.players.getNextPlayer()
print "Connecting to the next player and sending offer hand, nick:" , nextPlayer[1]
self.nextPlayer = nextPlayer
self.client.connect(self.nextPlayer[0][0], self.nextPlayer[0][1])
self.sendOfferHand()
datafromOponent = json.loads(self.client.recive(1000))
if datafromOponent["cmd"] == "pick":
data = json.dumps({'result': 'ok', 'card': self.hand.pickCard(int(datafromOponent["card_num"]) - 1)})
self.client.send(data)
if len(self.hand) == 0:
print "I won the game"
self.sendOut()
self.state = "WaitForEndOfGame"
return
self.state = "WaitForTurn"
else:
print "Strange command recived instead of pick: ", datafromOponent
def sendOut(self):
data = json.dumps({"cmd" : "out_of_cards"})
response = self.sendToTable(data);
if response["result"] != "ok":
print "didn't get to end the game", response
sys.exit()
def sendOfferHand(self):
sendOfferHandData = json.dumps({'cmd' : 'offer', 'num_cards' : (len(self.hand))})
try:
self.client.send(sendOfferHandData)
responseJson = self.client.recive(1000)
response = json.loads(responseJson)
except Exception as e:
print "Offer hand failed: ", e.args
sys.exit()
if response["result"] != "ok":
return -1
return 0
def sendTurnToNextOpponent(self):
sendTurnData = json.dumps({'cmd' : 'your_turn'})
try:
self.client.send(sendTurnData)
response = json.loads(self.client.recive(1000))
except Exception as e:
print "Could't send next turn to socket ", e.args
sys.exit()
if response["result"] != "ok":
print "next player did not accept the next turn ", response
def getStatus(self):
data = json.dumps({'cmd': 'status'})
return self.sendToTable(data)
def joinTheTable(self):
#example ('{"result":"ok", "players":{"player1":[["1.1", 11], "asv009"], "player2":[["2.2", 22], "nick"]}}')
self.tableClient = Client()
self.tableClient.connect(addr, port)
joinTableData = json.dumps({'cmd': 'join', 'nick': self.nick, 'port' : my_port})
gameData = self.sendToTable(joinTableData)
return gameData
def drawCardFromTable(self):
# message from table can be --> {result:ok/last_card/error, card: [3, spades]}
drawCardData = json.dumps({'cmd': 'draw'})
cardData = self.sendToTable(drawCardData)
return cardData
def discardCards(self, cards, test = True):
# send {cmd:discard, cards: [[3, spades], [3, clubs]], last_cards:true/false}
# Returns {result: ok/error, message:ok/error_message}
discardCardData = json.dumps({"cmd": "discard", "cards": cards, "last_cards": (len(self.hand) == 0) & test})
print discardCardData
response = self.sendToTable(discardCardData)
if response["result"] != "ok":
print "Something went wrong with discard ", response
def sendToTable(self, data):
self.tableClient.send(data)
return json.loads(self.tableClient.recive(1000))
if __name__ == "__main__":
player = Player(sys.argv[1])
player.main()