def quit_view(request): client_id = request.POST.get('client_id') client = clients.get(client_id) if client is None: return INVALID_CLIENT_ID if client.game is None: return INVALID_GAME_ID game = games[client.game.id] game.remove_player(client) game.end('%s quit the game' % client.name) client_names.remove(client.name) del clients[client_id] return {}
def chat_view(request): client_id = request.POST.get('client_id') message = request.POST.get('message') client = clients.get(client_id) if client is None: return INVALID_CLIENT_ID if client.game is None: return INVALID_GAME_ID game = games[client.game.id] game.chat(client, message) return { }
def move_view(request): client_id = request.POST.get('client_id') position = int(request.POST['position']) client = clients.get(client_id) if client is None: return INVALID_CLIENT_ID if client.game is None: return INVALID_GAME_ID game = games[client.game.id] if not game.check_move(client, position): return INVALID_MOVE game.move(position) return { }
def play_view(request): client_id = request.POST.get('client_id') name = request.POST.get('name') resume = request.POST.get('resume', False) client = clients.get(client_id) if resume: return RESUME_ERROR # if client: # # preserve previous name if we are resuming a game # name = client.name # # game = games.get(client.game_id) # if game is None or game.complete(): # return RESUME_ERROR # else: # return RESUME_ERROR # initialize the client if client: # log the client out, killing their current games quit_view(request) if name: client.name = name else: client_id = client_id or create_client_id() name = name or create_name() client = Client(client_id, name) # new client, increase unique count global unique_client_num unique_client_num += 1 # avoid multiple clients with the same name if name in client_names: return INVALID_CLIENT_NAME # add the client to a game and possibly begin if len(pending_games) > 0: game = pending_games.popleft() game.add_player(client) if game.is_ready(): game.begin() else: pending_games.appendleft() else: game_id = create_game_id() game = TicTacToe(game_id) game.add_player(client) pending_games.append(game) games[game_id] = game # remember the client client.game = game clients[client.id] = client client_names.add(client.name) return { 'client_id': client.id, 'name': client.name, 'game_id': game.id, }