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_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_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_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 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