Skip to content

Commit

Permalink
refactor game
Browse files Browse the repository at this point in the history
  • Loading branch information
zheplusplus committed Feb 13, 2012
1 parent 6419029 commit bf9db49
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 49 deletions.
106 changes: 61 additions & 45 deletions gateway/wsgi/game.py
Expand Up @@ -15,48 +15,57 @@
class GameRoom:
def __init__(self):
self.players_tokens = []
self.response = self.before_game_start
self.host = ''
self.handlers = {
'/ctrl/add': self.add_player,
'/ctrl/start': self.start,
'/info/status': self.game_status,
'/info/events': self.get_events,
'/info/hint': self.get_hint,
'/act': self.player_act,
}
self.game_started = False

def game_status(self, path, request_body):
if path == '/info/status':
def response(self, path, request_body):
if not path in self.handlers:
return { 'code': 404 }
try:
return self.handlers[path](request_body)
except ValueError, e:
return {
'code': ret_code.OK,
'players': len(self.players_tokens),
'started': 0 if self.response == self.before_game_start
else 1,
'code': ret_code.BAD_REQUEST,
'reason': e.message,
}
except KeyError, e:
return {
'code': ret_code.BAD_REQUEST,
'reason': ret_code.BR_MISSING_ARG % e.message,
}
return { 'code': 404 }

def before_game_start(self, path, request_body):
if path == '/ctrl/start':
return self.start(request_body)
if path == '/ctrl/add':
return self.add_player(time.time(), request_body)
return self.game_status(path, request_body)

def after_game_start(self, path, request_body):
try:
if path == '/act':
return self.player_act(eval(request_body, dict(), dict()))
if path == '/info/events':
return {
'code': 200,
'events': self.get_events(eval(request_body,
dict(), dict()))
}
if path == '/info/hint':
hint = self.gc.hint()
hint['code'] = 200
return hint
return self.game_status(path, request_body)
except (NameError, SyntaxError), e:
return {
'code': ret_code.BAD_REQUEST,
'reason': 'syntax error',
'reason': e.message,
}

def add_player(self, time, name):
token = sha256(name + str(time)).hexdigest()
def _check_game_not_started(self):
if self.game_started:
raise ValueError('Game started')

def _check_game_started(self):
if not self.game_started:
raise ValueError('Game not started')

def game_status(self, request_body):
return {
'code': ret_code.OK,
'players': len(self.players_tokens),
'started': 1 if self.game_started else 0,
'host': 1 if self.host == request_body else 0,
}

def add_player(self, request_body):
self._check_game_not_started()
token = sha256(request_body + str(time.time())).hexdigest()
log.i('Add player:token=' + token)
if len(self.players_tokens) == 0:
log.i('Be host:token=' + token)
Expand All @@ -68,6 +77,7 @@ def add_player(self, time, name):
}

def start(self, token):
self._check_game_not_started()
if len(self.players_tokens) < 2:
return {
'code': ret_code.BAD_REQUEST,
Expand All @@ -85,19 +95,25 @@ def start(self, token):
pc, ActionStack())
for token in self.players_tokens: pc.add_player(Player(token, 4))
self.gc.start()
self.response = self.after_game_start
self.game_started = True
return { 'code': ret_code.OK }

def get_events(self, args):
try:
return self.gc.get_events(args['token'], args['previous event id'])
except KeyError, e:
return {
'code': ret_code.BAD_REQUEST,
'reason': ret_code.BR_MISSING_ARG % e.message,
}
def get_hint(self, req):
self._check_game_started()
hint = self.gc.hint()
hint['code'] = 200
return hint

def get_events(self, request_body):
self._check_game_started()
args = eval(request_body, dict(), dict())
return {
'code': 200,
'events': self.gc.get_events(args['token'],
args['previous event id']),
}

def player_act(self, args):
return self.gc.player_act(args)
def player_act(self, request_body):
return self.gc.player_act(eval(request_body, dict(), dict()))

game_room = GameRoom()
9 changes: 5 additions & 4 deletions gateway/wsgi/static/index.html
Expand Up @@ -84,18 +84,19 @@
});
}

function updateRoomPlayers(count) {
function updateRoomPlayers(count, is_host) {
document.getElementById('players_count').innerHTML = count;
document.getElementById('start_button').disabled = (0 == is_host);
}

function enter() {
joinPane.style.visibility = 'hidden';
startPane.style.visibility = 'visible';

post_act('', '/info/status', function(req) {
post_act(token, '/info/status', function(req) {
var result = eval('(' + req.responseText + ')');
if (result['started'] == 0) {
updateRoomPlayers(result['players']);
updateRoomPlayers(result['players'], result['host']);
setTimeout(enter, BEAT_INTERVAL);
} else {
started();
Expand Down Expand Up @@ -176,7 +177,7 @@
<body onload='init()'>
<p><button onclick='joinGame()'>Join</button></p>
<p>
<button onclick='startGame()'>Start</button>
<button id='start_button' onclick='startGame()'>Start</button>
<span id='players_count'></span> player(s) in the room now.
</p>
<table border=1></table>
Expand Down

0 comments on commit bf9db49

Please sign in to comment.