Skip to content

Instantly share code, notes, and snippets.

@ipetrushin
Created April 21, 2016 15:44
Show Gist options
  • Save ipetrushin/1e1e53c9953a52860712f7bcffab49b7 to your computer and use it in GitHub Desktop.
Save ipetrushin/1e1e53c9953a52860712f7bcffab49b7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from BaseHTTPServer import HTTPServer
from BaseHTTPServer import BaseHTTPRequestHandler
import json
HOST = "194.176.114.21"
PORT = 8048
FILE_PREFIX = "."
players = {
"Alice" : 1,
"Bob": 2
}
games = [
{
"player2": 2,
"player1": 1,
"heaps": [1, 2, 3],
"turn": 1
}
]
player = 0
game_id = 0
class JSONRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "application/json")
self.wfile.write("\r\n")
try:
output = open(FILE_PREFIX + "/" + self.path[1:] + ".json", 'r').read()
except Exception:
output = "{'error': 'Could not find file " + self.path[1:] + ".json'" + "}"
self.wfile.write(output)
def do_POST(self):
self.send_response(200)
self.wfile.write('Content-Type: application/json\r\n')
self.wfile.write('Client: %s\r\n' % str(self.client_address))
self.wfile.write('User-agent: %s\r\n' % str(self.headers['user-agent']))
self.wfile.write('Path: %s\r\n' % self.path)
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
self.end_headers()
data = json.loads(self.data_string)
if 'action' in data:
print "action = " + data['action']
if data['action'] == 'register':
# register player
if 'nickname' in data:
if data['nickname'] in players:
print 'Nickname ' + data['nickname'] + ' is already registered'
self.wfile.write('"status" : "error", "error_text" : "user already registered"')
else:
global player
player += 1
print 'Nickname ' + data['nickname'] + ' is registered'
players[data['nickname']] = player
self.wfile.write(json.dumps(
{ "status": "ok", "player": str(player)}
))
if data['action'] == 'fetch_players':
self.wfile.write(json.dumps(players))
if data['action'] == 'join':
# join game
if ('player1' in data) and ('player2' in data):
# add check for player 1 or 2 already in games
global game_id
game_id += 1
games[game_id] = \
{
'player1' : data['player1'],
'player2' : data['player2'],
'heaps': [1, 2, 3],
'turn' : data['player1']
}
self.wfile.write(json.dumps(games[game_id]))
print 'Game # %d created' % game_id
else:
self.wfile.write(json.dumps(
{
'status' : 'error',
'error_message' : 'no players defined'
}
))
if data['action'] == 'get_status':
if ('game_id' in data) and (data['game_id'] <= len(games)):
self.wfile.write(json.dumps(games[game_id]))
if data['action'] == 'got_stone':
if ('game_id' in data) and (data['game_id'] <= len(games)):
if (('heap' in data) and ('player' in data) and ('stones' in data) and
(games[data['game_id']]['player1'] == data['player'] or games[data['game_id']]['player2'] == data['player'])
and (data['heap'] >= 0 and data['heap'] <= 3)
and (data['stones'] > 0 and data['stones'] <= games[data['game_id']]['heaps'][data['heap']])):
games[data['game_id']]['heaps'][data['heap']] -= data['stones']
self.wfile.write(json.dumps(games[data['game_id']]))
print 'Player %d got %d stone(s) from %d heap at game # %d' % \
(data['player'], data['stones'], data['heap'], data['game_id'])
server = HTTPServer((HOST, PORT), JSONRequestHandler)
server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment