import { GameLiftClient, ListFleetsCommand, DescribeFleetAttributesCommand, DescribeGameSessionsCommand , CreateGameSessionCommand } from "@aws-sdk/client-gamelift";
const generateRandomName = () => {
return Math.random().toString(36).substring(2, 8).toUpperCase();
};
export const handler = async (event) => {
const gameLiftClient = new GameLiftClient({ region: process.env.REGION });
try {
// Step 1: Get Fleet IDs
const listFleetsCommand = new ListFleetsCommand({ Limit: 10 });
const listFleetsResponse = await gameLiftClient.send(listFleetsCommand);
const fleetIds = listFleetsResponse.FleetIds || [];
if (fleetIds.length === 0) {
throw new Error("No fleets found");
}
// Step 2: Get Fleet Attributes & Find an ACTIVE Fleet
const describeFleetAttributesCommand = new DescribeFleetAttributesCommand({
FleetIds: fleetIds,
Limit: 10
});
const describeFleetAttributesResponse = await gameLiftClient.send(describeFleetAttributesCommand);
const fleetAttributes = describeFleetAttributesResponse.FleetAttributes || [];
let fleetId;
for (const fleetAttribute of fleetAttributes) {
if (fleetAttribute.Status === "ACTIVE") {
fleetId = fleetAttribute.FleetId;
break;
}
}
if (!fleetId) {
throw new Error("No ACTIVE fleets found");
}
// Step 3: Get Active Game Sessions
const describeGameSessionCommand = new DescribeGameSessionsCommand({
FleetId: fleetId,
Limit: 10,
StatusFilter: "ACTIVE"
});
const describeGameSessionResponse = await gameLiftClient.send(describeGameSessionCommand);
let gameSessions = [];
// Step 4: Filter available game sessions
for (const session of describeGameSessionResponse.GameSessions || []) {
if (
session.CurrentPlayerSessionCount < session.MaximumPlayerSessionCount &&
session.Status === "ACTIVE" &&
session.PlayerSessionCreationPolicy === "ACCEPT_ALL"
) {
gameSessions.push(session);
}
}
// Step 5: If no available game sessions, create one
if (gameSessions.length === 0) {
console.log("No available game sessions found. Creating a new one...");
const createGameSessionCommand = new CreateGameSessionCommand({
FleetId: fleetId,
Name: generateRandomName(),
GameProperties: [{ Key: "player_type_available", Value: "tall" }],
MaximumPlayerSessionCount: 3,
Location: "custom-home-desk",
PlayerSessionCreationPolicy: "ACCEPT_ALL"
});
const createGameSessionResponse = await gameLiftClient.send(createGameSessionCommand);
if (!createGameSessionResponse.GameSession) {
throw new Error("Game session creation failed");
}
gameSessions.push(createGameSessionResponse.GameSession);
}
return {
GameSessions: gameSessions
};
} catch (e) {
console.error("Error:", e);
return { error: e.message };
}
};
import { GameLiftClient, CreatePlayerSessionCommand } from "@aws-sdk/client-gamelift";
export const handler = async (event) => {
try {
const gameLiftClient = new GameLiftClient({region: process.env.REGION});
const createPlayerSessionInput = {
GameSessionId: event.gameSessionId, // required
PlayerId: event.playerId, // required
PlayerData: event.playerData,
Location: "custom-home-desk" // remove for EC2
};
const createPlayerSessionCommand = new CreatePlayerSessionCommand(createPlayerSessionInput);
const createPlayerSessionresponse = await gameLiftClient.send(createPlayerSessionCommand);
return createPlayerSessionresponse.PlayerSession;
} catch (error) {
return error;
}
};