def get_board(request): resp = {} user = token_to_user(request) #for unlogged users hasGameID = "gameid" in request.COOKIES setGameID = False if user is not None: game = user.currentGame if game is None: game = newGame(user) user.currentGame = game user.save() elif hasGameID: game = cookie_to_game(request) if game is None: game = newGame(user) setGameID = True else: game = newGame(None) setGameID = True if game.gameover: game = newGame(user) setGameID = True if user is not None: user.currentGame = game user.save() resp["board"] = move_logic.deserialize_board(game.lastMove.board) resp["moveNumber"] = game.lastMove.moveNumber response = HttpResponse(json.dumps(resp), content_type='application/json') if setGameID: response.set_cookie("gameid", value=game.gameid, httponly=False) return response
def check_validity(move): # isValid, randomNumber clientSecret = move.clientSecret clientSecretHashed = move.clientSecretHashed valid = (sha256(clientSecret) == clientSecretHashed) if valid: serverSecret = move.serverSecret randomNumber = xorHex(serverSecret, clientSecret) else: serverSecret = "X" randomNumber = rand256hex() allempty = move_logic.deserialize_board(move.allempty) emptyNo = len(allempty) board = move_logic.deserialize_board(move.board) position = allempty[int(randomNumber, 16)%emptyNo] board[position[0]][position[1]] = move_logic.new_value if move.moveNumber != sum(map(sum, board)): raise Error("moveNumber != sum(board), database corruption, we're all going to die.") gameover = False result = move.moveNumber game = move.belongs_to if emptyNo == 1: gameover = not move_logic.has_move(board) if gameover: result = move.moveNumber + 1 game.result = result #person = game.belongs_to #if result > person.bestResult: # person.bestResult = result move.board = move_logic.serialize_board(board) game.gameover = gameover game.save() move.save() return { "valid": valid, "serverSecret": serverSecret, "randomNumber": randomNumber, "position": position, "value": move_logic.new_value, "gameover": gameover, "moveNumber": result }
def prepareCommitment(game, booleans, clientCommitment): previous_move = game.lastMove previous_board = move_logic.deserialize_board(previous_move.board) full_board = move_logic.next_board(previous_board, *booleans) board = full_board["newboard"] allempty = full_board["allempty"] changed = full_board["changed"] if changed: try: current_move = negotiate_first(game, board, allempty, clientCommitment) except UnfinishedMove: return HttpResponse("Pacta sunt servanda. You are obliged to finish " + "your previous move by exchanging secrets. To " + "give up the negotiation and accept server's" + "choice of the pseudorandom number send" + '"surrender="True""', status=452) else: return HttpResponseForbidden("not changed!") full_board["serverSecretHashed"] = current_move.serverSecretHashed; full_board["moveNumber"] = current_move.moveNumber return HttpResponse(json.dumps(full_board), content_type='application/json')