-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
64 lines (58 loc) · 2.36 KB
/
server.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
#------------------------------------------------------------------------------
# server.py - server for simple zero-sum game
#------------------------------------------------------------------------------
import zmq
import json
import utils
GAME_CONTENT = utils.getGameContent()
PORT = utils.defaultServerPort()
HOST = utils.defaultHost()
def start():
"""
Start the game server and wait for other players to join game
"""
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://%s:%s" % (HOST, PORT))
print "server started.."
while True:
# Wait for next request from client
gameData = json.loads(socket.recv())
print "data recieved %s"%gameData
score = gameData.get('score')
try:
computerData = getComputerChoice(gameData)
except utils.ServerException, e:
errorData = {"Error":e.message}
socket.send(json.dumps(errorData))
continue
score[computerData['computerColor']] += computerData['points']
computerData['score'] = score
socket.send(json.dumps(computerData))
print "responding with %s"%computerData
def getComputerChoice(data):
"""
Based on other players choice make your move
:param data: Dictionary object of data passed in from client, other players choice
:return: Dictionary object with the best possible choice to win the game.
"""
playerColor = data.get('playerColor')
playerChoice = data.get('playerChoice')
playerOptions = getattr(utils, '%sOptions'%playerColor.lower())()
if not playerChoice in playerOptions:
raise utils.ServerException("%s player picked an invalid option."%playerColor.title())
if playerColor == 'blue':
computerColor = 'red'
computerChoices = GAME_CONTENT[computerColor].get(playerChoice)
compPicked = max(computerChoices, key=lambda k: computerChoices[k])
points = computerChoices[compPicked]
else:
computerColor = 'blue'
computerChoices = GAME_CONTENT[computerColor]
compPicked = max(computerChoices, key=lambda k: computerChoices[k].get(str(playerChoice)))
points = computerChoices[compPicked].get(str(playerChoice))
return {'computerChoice': compPicked,
'computerColor': computerColor,
'points': points}
if __name__ == "__main__":
start()