def rest_tilebag_save_game(gameid): req_game=DataIf.getGameById(gameid) if req_game is not None: ret={'game' : req_game.serialize(forsave=True)} return jsonify(ret) else: abort(404) #no such game
def rest_tilebag_placetile(gameid): errno=404 errmsg="No Such Game" # Find the game they want to run req_game=DataIf.getGameById(gameid) if req_game is not None: # Make sure the caller is someone playing this game! pinfo=getCallingPlayerInfo(req_game) if pinfo is not None: # Get the tile they want to play if 'tile' in request.json: req_tile=request.json['tile'] # Pass the request into the game and see if it gets approved ret, errmsg=req_game.playTile(pinfo['id'], alpha=req_tile) if ret: DataIf.updateGame(req_game.id) return jsonify({'success':True}) else: errno=403 #forbidden, errmsg set by game engine else: errmsg="No tile specified" else: errmsg="Invalid user - not part of this game" errno=403 #forbidden else: pass # invalid game print("ERROR=({})-{}".format(errno, errmsg)) return jsonify({'message': errmsg}), errno
def rest_tilebag_placehotel(gameid): errno=404 errmsg="No Such Game" # Find the game they want to run req_game=DataIf.getGameById(gameid) if req_game is not None: # Make sure the caller is someone playing this game! pinfo=getCallingPlayerInfo(req_game) if pinfo is not None: if 'tile' in request.json and 'hotel' in request.json: req_hotel=request.json['hotel'] req_tile=request.json['tile'] # some json/python serialization fun, # "" will be equivalent to removing from the board if req_tile == "": req_tile=None ret, errmsg=req_game.placeHotel(pinfo['id'], req_hotel, req_tile) if ret: DataIf.updateGame(req_game.id) return jsonify({'success':True}) else: errno=403 #forbidden, errmsg set by game engine else: errmsg="No tile, or No hotel in hotel action" else: errmsg="Invalid user - not part of this game" errno=403 #forbidden print("ERROR=({})-{}".format(errno, errmsg)) return jsonify({'message': errmsg}), errno
def rest_tilebag_trigger_end(gameid): errno=404 errmsg="No Such Game" # Find the game they want to run req_game=DataIf.getGameById(gameid) if req_game is not None: # Make sure the caller is someone playing this game! pinfo=getCallingPlayerInfo(req_game) if pinfo is not None: # Get the tile they want to play if request.json and 'endgame' in request.json: ret, errmsg = req_game.requestEndGame(pinfo['id']) if ret: DataIf.updateGame(req_game.id) return jsonify({'success':True}) else: errno=403 #forbidden, errmsg set by game engine else: errmsg="Invalid Request: endgame key is not present" else: errmsg="Invalid user - not part of this game" errno=403 #forbidden print("ERROR=({})-{}".format(errno, errmsg)) return jsonify({'message': errmsg}), errno
def rest_tilebag_money(gameid): errno=404 errmsg="No Such Game" # Find the game they want to run req_game=DataIf.getGameById(gameid) if req_game is not None: # Make sure the caller is someone playing this game! pinfo=getCallingPlayerInfo(req_game) if pinfo is not None: # make sure we have the amount of the transaction if 'amount' in request.json: # all good, make the appropriate call to the game engine req_amount=request.json['amount'] ret, errmsg=req_game.moneyAction(pinfo['id'], req_amount) if ret: DataIf.updateGame(req_game.id) return jsonify({'success':True}) else: errno=403 #forbidden else: errmsg="no 'amount' in money request" else: errmsg="Invalid user - not part of this game" errno=403 #forbidden print("ERROR=({})-{}".format(errno, errmsg)) return jsonify({'message': errmsg}), errno
def rest_lobby_patch_game_state(gameid): errno = 404 errmsg = "No Such Game" # Find the game they want to alter req_game = DataIf.getGameById(gameid) if req_game is not None: errno = 400 if request.json and 'action' in request.json: action = request.json['action'] if action == "stop": if req_game.started: req_game.stop() DataIf.updateGame(gameid) return jsonify({'success': True}) else: errmsg = "Game not started, cannot stop it" elif action == "start" or action == "restart": # if it's a reset, do that, then do the normal start stuff if action == "restart": req_game.reset() if not req_game.started: # make sure there are enough players num_players, players = req_game.players if num_players >= req_game.minPlayers( ) and num_players <= req_game.maxPlayers(): # if we got this far, they must have asked us to start the game req_game.run() DataIf.updateGame(gameid) # return the URL for the running game (maybe tilebag/<id>?) return Response(request.host_url[:-1] + req_game.starturl() + '/' + str(gameid), status=201) else: errmsg = "Not enough players to play yet" # Or if they were on glue else: errmsg = "Game already started" else: errmsg = "Invalid action ({}) specified".format(action) else: errmsg = "no json, or no 'action' tag in that json" else: errmsg = "some dummy just tried to twiddle with a non existent game" print("ERROR=({})-{}".format(errno, errmsg)) return jsonify({'message': errmsg}), errno
def lobby_new_game(gameid): print("hi") # see if the game exists, and if not, create it req_game=DataIf.getGameById(gameid) if req_game is None or not req_game.started: print("trying to start a new game?") return render_template('newgame.html', gameid=gameid, lobbyrest=BASEURI) else: print("redirect to the actual, running game") return redirect(url_for('tilebag_blueprint.get_tilebag_clientif', gameid=gameid))
def rest_tilebag_get_game_info(gameid): req_game=DataIf.getGameById(gameid) if req_game is not None: ret={'game' : req_game.getPublicInformation()} # add private player info for the one that called in (if any) pinfo=getCallingPlayerInfo(req_game) if pinfo: ret['game']['you'] = pinfo return jsonify(ret) else: abort(404) #no such game
def get_tilebag_clientif(gameid): req_game = DataIf.getGameById(gameid) if req_game is not None: playerid = UserIf.getCallingPlayerId() # I was having difficulting with static files being cached between the # server and the caller, this makes sure if we change those files it'll # be okay CACHEFIX = int(getStaticMaxChangeTime()) print("CACHEFIX={}".format(CACHEFIX)) return render_template('tilebag.html', gameid=gameid, playerid=playerid, serverroot=request.host, debug=json.dumps(False), cachefix="?{}".format(CACHEFIX)) else: return redirect( url_for('lobby_blueprint.lobby_new_game', gameid=gameid))
def rest_lobby_make_game(): errno = 400 errmsg = "No game type specified" if request.json: if 'game' in request.json: print("Loading a Saved game from JSON") ginfo = request.json['game'] newGame = DataIf.createGame(ginfo['name'], ginfo['id']) newGame.loadFromSavedData(ginfo) return Response(request.base_url + '/' + str(newGame.id), status=201) elif 'gametype' in request.json: print("Creating a new game!") # See if the caller provided a custom game name req_gid = None if 'gameid' in request.json: req_gid = request.json['gameid'] if DataIf.getGameById(req_gid) is not None: errmsg = "that game already exists" errno = 409 req_gtype = request.json['gametype'] if getGameInfo(req_gtype) is None: errmsg = "asking for a game that doesn't exist" errno = 404 newGame = DataIf.createGame(req_gtype, req_gid) if newGame: return Response(request.base_url + '/' + str(newGame.id), status=201) else: errno = 404 errmsg = "Unable to create the game - Server Error" else: print("didn't specify the game type to create") print("ERROR=({})-{}".format(errno, errmsg)) return jsonify({'message': errmsg}), errno
def rest_lobby_join_game(gameid): errno = 404 errmsg = "No Such Game" # Get the user that wants to the join the game print(request.json) if request.json and 'name' in request.json: newPlayerName = request.json['name'] print("player name from json is {}".format(newPlayerName)) # Find the game they want to join req_game = DataIf.getGameById(gameid) if req_game is not None: # make sure there's room for one more at the table num_players, players = req_game.players if num_players >= req_game.maxPlayers(): errmsg = "whoa-la, already at the max for players" errno = 409 print("cool cool, try to add this player to the game!") newPlayerId = hashlib.sha256( gameid.encode('utf-8')).hexdigest()[:4] + str(num_players + 1) #newPlayerId=(gameid<<8)+(num_players+1) print("newPlayerId == " + str(newPlayerId)) if req_game.addPlayer( req_game.newPlayer(newPlayerId, name=newPlayerName)): DataIf.updateGame(gameid) return Response(request.base_url + '/' + str(newPlayerId), status=201) else: errmsg = "Internal error" errno = 500 #that's odd else: errmsg = "something wrong with the json" errno = 400 print("ERROR=({})-{}".format(errno, errmsg)) return jsonify({'message': errmsg}), errno
def rest_lobby_delete_game(gameid): req_game = DataIf.getGameById(gameid) if req_game is not None: if DataIf.deleteGame(gameid): return jsonify({'success': True}) abort(404) #no such game
def rest_lobby_get_game_info(gameid): req_game = DataIf.getGameById(gameid) if req_game is not None: return jsonify({'game': req_game.serialize(False)}) else: abort(404) #no such game