-
-
Save alexandre-bruffa/c2f337f6c43b1e535f6dd38ac57d2811 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import boto3 | |
multiplayerFleetID = "fleet-00000a00-0000-000a-0a00-0a0aaaaaa00a" | |
maximumPlayersBySession = 2 | |
def lambda_handler(event, context): | |
client = boto3.client('gamelift') | |
try: | |
# Find any sessions that have available players | |
response = client.search_game_sessions( | |
FleetId=multiplayerFleetID, | |
FilterExpression="hasAvailablePlayerSessions=true" | |
) | |
game_sessions = response["GameSessions"] | |
# If there are no sessions, then we need to create a game session | |
if len(game_sessions) == 0: | |
# No game session detected, creating a new one | |
response = client.create_game_session( | |
MaximumPlayerSessionCount=maximumPlayersBySession, # only two players allowed per game | |
FleetId=multiplayerFleetID | |
) | |
selected_game_session = response["GameSession"] | |
else: | |
# We grab the first session we find and join it | |
selected_game_session = game_sessions[0] | |
# There isn't a logical way selected_game_session could be null at this point | |
# but it's worth checking for in case other logic is added | |
if selected_game_session: | |
# Now we have a game session one way or the other, create a session for this player | |
response = client.create_player_session( | |
GameSessionId=selected_game_session["GameSessionId"], | |
PlayerId=context.aws_request_id | |
) | |
response = response["PlayerSession"] | |
# datetime is not JSON serializable, so we cast it manually | |
response["CreationTime"] = str(response["CreationTime"]) | |
return response | |
else: | |
return { | |
"statusCode": 500, | |
"body": "Unable to find game session, check GameLift API status" | |
} | |
except Exception as e: | |
return { | |
"statusCode": 500, | |
"body": str(e) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment