Skip to content

Commit

Permalink
Fixed not being able to reuse ports and fixed crash
Browse files Browse the repository at this point in the history
  • Loading branch information
FenixFeather committed Dec 6, 2013
1 parent 2c478f4 commit 1b5d51b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
20 changes: 11 additions & 9 deletions cards-client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env python

#An outline of an engine for concept-to-description/category matching card game.

import random
import time
import pickle
Expand All @@ -27,11 +25,15 @@ def __repr__(self):
def myTurn(self):
try:
answer = self.server.request_info((self.number,'myturn'))
# print(answer)
return answer
except socket.error:
return False
except EOFError:
return False
print("socket error")
return (False, None)
except EOFError as e:
# print("EOFError")
# print(e.args)
return (False, None)

def __str__(self):
return self.name
Expand Down Expand Up @@ -156,7 +158,7 @@ def update(s):
while True:
try:
myTurn, slowPlayer = me.myTurn()
update("Waiting for {0}...".format(slowPlayer))
update("Waiting for {0}...".format(slowPlayer if slowPlayer else "others"))
if myTurn:
print("Your turn!")
me.updateTurnInfo()
Expand All @@ -177,17 +179,17 @@ def update(s):
winner, scores, pool = game.scores
print("{0} won this round!".format(winner))
for key in pool.keys():
print("{0} submitted '{1}'".format(pool[key],key))
print("{0} submitted '{1}'".format(pool[key],key.strip('\n')))
print("==Scores==")
for key in scores.keys():
print("{0}: {1}".format(key,scores[key]))
print("\n----------------\n----------------\n")
break
time.sleep(0.5)
update("Waiting for judgement.. ")
update("Waiting for judgement.. ")
time.sleep(0.5)
time.sleep(0.5)
update("Waiting for {0}.. ".format(slowPlayer))
update("Waiting for {0}.. ".format(slowPlayer if slowPlayer else "others"))
time.sleep(0.5)
except KeyboardInterrupt:
sys.exit(0)
Expand Down
4 changes: 2 additions & 2 deletions cards-server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
#TODO:who are we waiting for?
#An outline of an engine for concept-to-description/category matching card game.

import random
import socket
Expand Down Expand Up @@ -125,6 +124,7 @@ def __init__(self, Deck):
self.serv = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

##bind our socket to the address
self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.serv.bind((ADDR)) #the double parens are to create a tuple with one element
self.initPlayers(self.numPlayers)
print [player.name for player in self.players]
Expand Down Expand Up @@ -190,7 +190,7 @@ def newRound(self):
conn,addr = self.serv.accept()
data = pickle.loads(conn.recv(self.BUFSIZE))
if str(data[1]) == 'myturn':
conn.send(pickle.dumps((hash(player) == data[0],player.name)))
conn.send(pickle.dumps((hash(self.judge) == data[0],self.judge.name)))
conn.close()
elif str(data[1]) == 'roundEnd':
conn.send(pickle.dumps(False))
Expand Down

0 comments on commit 1b5d51b

Please sign in to comment.