Skip to content

Commit

Permalink
Players can now disconnect safely from the game
Browse files Browse the repository at this point in the history
  • Loading branch information
FenixFeather committed Dec 7, 2013
1 parent 5347c62 commit ac583e2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
4 changes: 4 additions & 0 deletions cards_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def updatePool(self):

def register(self):
self.server.send_info((self.number,self.name))

def disconnect(self):
self.server.send_info((self.number,'disconnect'))

def displayCards(self,cards):
for i, card in enumerate(cards):
Expand Down Expand Up @@ -201,6 +204,7 @@ def update(s):
update("Waiting for {0}.. ".format(slowPlayer if slowPlayer else "others"))
time.sleep(0.5)
except KeyboardInterrupt:
me.disconnect()
sys.exit(0)
except socket.error:
print("Connection lost.")
Expand Down
7 changes: 6 additions & 1 deletion cards_gui_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ def updateWaiting(self, name):
self.statusBar().showMessage("Waiting for {0}{1}".format("others" if not name else name, "..." if self.even else ".."))
self.even = not self.even

def closeEvent(self, event):
self.spreading.me.disconnect()
event.accept()


class Cards(QtGui.QWidget):

def __init__(self):
Expand Down Expand Up @@ -259,7 +264,7 @@ def waitForRoundEnd(self):
winner, scores, pool = self.game.scores
text = ("{0} won this round!".format(winner)) + '\n'
for key in pool.keys():
text += "{0} submitted '{1}'".format(pool[key],key.strip('\n'))
text += "{0} submitted '{1}'".format(pool[key],key.strip('\n')) + '\n'
print(text)
self.timer.singleShot(1000,self.wait)
self.updateScores(scores)
Expand Down
61 changes: 51 additions & 10 deletions cards_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ def __init__(self, Deck):
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]
self.judge = random.choice(self.players)
self.judge = random.choice(self.players)
self.winnerName = "Nobody"
self.playerToRemove = None

def initPlayers(self,numPlayers):
while len(self.players) != numPlayers:
Expand Down Expand Up @@ -185,32 +187,68 @@ def handleRequests(self,player):
conn.send(pickle.dumps((self.winnerName,self.constructScoresDict(),self.constructPoolDict())))
conn.close()
return
elif data[1] == 'disconnect':
shouldReturn = False
for person in self.players:
shouldReturn = (person == player)
if data[0] == hash(person):
if person == self.judge:
self.judge = Player(-1,"Ghost")
self.judge.pool = person.pool
self.judge.dCard = person.dCard
print("{0} has disconnected!".format(person))
if self.players.index(person) > self.players.index(player):
self.players.remove(person)
else:
self.playerToRemove = person
conn.close()
if shouldReturn:
return

elif str(data[1]) == 'winner':
winnerHash = self.judge.judge(data[2])
conn.close()
for player in self.players:
if winnerHash == hash(player):
player.score += 1
self.winnerName = player.name
print("{0} won this round!".format(str(player)))
self.winnerName = "Nobody"
self.findWinner(winnerHash)
return

if self.roundEnd:
break


def findWinner(self, winnerHash):
for player in self.players:
if winnerHash == hash(player):
player.score += 1
self.winnerName = player.name
print("{0} won this round!".format(self.winnerName))
return

def newRound(self):
self.judge.pool = [] #reset judge's pool to empty
self.judge.dCard = None
self.roundEnd = False
self.judge = self.players[(self.players.index(self.judge) + 1)%len(self.players)] #rotate judge position
self.winnerName = "Nobody"
self.playerToRemove = None
try:
self.judge = self.players[(self.players.index(self.judge) + 1)%len(self.players)] #rotate judge position
except ValueError:
self.judge = random.choice(self.players)
print(str(self.judge) + " is now the judge!")
self.deck.dealDCard(self.judge)
print("The description for this round is: {0}\n".format(self.judge.dCard))
for player in self.players:
if player != self.judge:
self.handleRequests(player)

if self.playerToRemove:
self.players.remove(self.playerToRemove)
#Judging time
random.shuffle(self.judge.pool)
self.handleRequests(self.judge)
if self.judge.number != -1:
self.handleRequests(self.judge)
else:
winnerHash = self.judge.judge(0)
self.findWinner(winnerHash)

#Clean up the round and send everyone useful info!
start = int(time.time())
Expand All @@ -228,7 +266,10 @@ def constructPoolDict(self):
result = {}
playerIdDict = {player.number:player.name for player in self.players}
for card in self.judge.pool:
result[card.text] = playerIdDict[card.owner]
try:
result[card.text] = playerIdDict[card.owner]
except KeyError:
pass
# print(result)
return result

Expand Down

0 comments on commit ac583e2

Please sign in to comment.