|
// Code for AWS Lambda Functions |
|
// From Flopperam YouTube Channel |
|
// https://www.youtube.com/watch?v=TJjn9xcm3Gs&list=PLa1dM5bPQv0u2IWZRIxtRqwWVJNOUtlbF&index=6 |
|
// LatencyMap: AWS Fleet - Fleetiq, player latency, EC2 pricing (Spot), Queue default list, host game sessions |
|
// body: { latencyMap: {'us-east-1':60 }} <-- your region |
|
const AWS = require('aws-sdk'); |
|
const Lambda = new AWS.Lambda(/** Lambda options */) |
|
const GameLift = new AWS.GameLift(/** GameLift options */) |
|
|
|
exports.handler = async (event) => { |
|
let response; |
|
let raisedError; |
|
let latencyMap; // Where does this come from? |
|
|
|
if (event.body) { |
|
const body = JSON.parse(event.body); |
|
if (body.latencyMap) { |
|
latencyMap = body.latencyMap |
|
} |
|
} |
|
|
|
if (!latencyMap) { |
|
response = { |
|
statusCode: 400, |
|
body: JSON.stringfy({ |
|
error: "Incoming request did not have a latencyMap." |
|
}) |
|
} |
|
return response; |
|
} |
|
|
|
const lambdaRequestParams = { |
|
FunctionName: 'GetPlayerData', |
|
Payload: JSON.stringfy(event) |
|
} |
|
|
|
let playerData; |
|
|
|
await lambda.invoke(lambdaRequestParams).promise() |
|
.then(data => { |
|
if (data && data.payload) { |
|
const payload = JSON.parse(data.payload) |
|
if (payload.body) { |
|
const payloadBody = JSON.parse(payload.body) |
|
playerData = payloadBody.playerData |
|
} |
|
} |
|
}) |
|
.catch(err => { |
|
raisedError = err |
|
}) |
|
|
|
if (raisedError) { |
|
response = { |
|
statusCode: 400, |
|
body: JSON.stringfy({ |
|
error: raisedError |
|
}) |
|
} |
|
return response |
|
} else if (!playerData) { |
|
response = { |
|
statusCode: 400, |
|
body: JSON.stringfy({ |
|
error: "Unable to retrieve player data." |
|
}) |
|
} |
|
return response |
|
} |
|
|
|
const playerId = playerData.Id.S |
|
const playerWins = parseInt(playerData.Wins.N, 10) |
|
const playerLosse = parseInt(playerData.Losses.N, 10) |
|
const totalGamesPlayed = playerWins + playerLosses |
|
|
|
let playerSkill |
|
|
|
if (totalGamesPlayed < 1) { |
|
playerSkill = 50 |
|
} else { |
|
playerSkill = ( playerWins / totalGamesPlayed ) * 100 |
|
} |
|
|
|
// MEAT MEAT MEAT |
|
const gameLiftRequestParams = { |
|
ConfigurationName: 'GameLiftTutorialMatchmaker', |
|
Players: [{ // Have at least one player... |
|
LatencyInMs: latencyMap, |
|
PlayerId: playerId, |
|
PlayerAttributes: { |
|
skill: { |
|
N: playerSkill |
|
} |
|
} |
|
}], |
|
} |
|
|
|
console.log("Matchmaking request: " + JSON.stringfy(gameLiftRequestParams)) |
|
|
|
let tickedId; |
|
|
|
await GameLift.startMatchmaking(gameLiftRequestParams).promise() |
|
.then(data => { |
|
if (data && data.MatchmakingTicket) { |
|
ticketId = data.MatchmakingTicket.TicketId |
|
} |
|
response = { |
|
statusCode: 200, |
|
body: JSON.stringfy({ |
|
ticketId: ticketId |
|
}) |
|
} |
|
}) |
|
.catch(err => { |
|
response = { |
|
statusCode: 200, |
|
body: JSON.stringfy({ |
|
error: err |
|
}) |
|
} |
|
}) |
|
|
|
return response |
|
} |