From c11197c2af6a5ae54d1ba8250868698f52082062 Mon Sep 17 00:00:00 2001 From: lahirumenik <115687865+lahirumenik@users.noreply.github.com> Date: Wed, 24 Jan 2024 21:01:28 +0530 Subject: [PATCH 1/2] join game functions --- backend/serverless/Lambda/joingame.js | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 backend/serverless/Lambda/joingame.js diff --git a/backend/serverless/Lambda/joingame.js b/backend/serverless/Lambda/joingame.js new file mode 100644 index 0000000..09fb99b --- /dev/null +++ b/backend/serverless/Lambda/joingame.js @@ -0,0 +1,55 @@ + +const { DynamoDBClient, GetItemCommand } = require("@aws-sdk/client-dynamodb"); +const client = new DynamoDBClient() + + +exports.handler = async (event, context, callback) => { + try { + + + const requestBody = JSON.parse(event.body); + const email = requestBody.email; + + const anchorid = requestBody.anchorid; + + + // Add user to DynamoDB arcore + const dynamoDBParams = { + "TableName": 'Arcombat-arcore', + "Key": { + "email": {"S": email}, + + } + }; + const dynamoDBParamsEnv = { + "TableName": 'Arcombat-env', + "Key": { + "email": {"S": email}, + + } + }; + + const commandEnv = new GetItemCommand(dynamoDBParamsEnv); + const responseEnv = await client.send(commandEnv); + + const command = new GetItemCommand(dynamoDBParams); + const response = await client.send(command); + + + // Return a response + return { + statusCode: 200, + body: JSON.stringify('arcore anchor stored success.') + }; + + + + //error handleing + } catch (error) { + console.error('Error:', error); + return { + statusCode: 500, + body: {"msg":JSON.stringify('Internal Server Error')} + }; + } +}; From 459b867ad6d894ab6242007f60124098f963ac22 Mon Sep 17 00:00:00 2001 From: lahirumenik <115687865+lahirumenik@users.noreply.github.com> Date: Wed, 24 Jan 2024 21:47:00 +0530 Subject: [PATCH 2/2] add join game auth logic --- backend/serverless/Lambda/joingame.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/backend/serverless/Lambda/joingame.js b/backend/serverless/Lambda/joingame.js index 09fb99b..f9a4f71 100644 --- a/backend/serverless/Lambda/joingame.js +++ b/backend/serverless/Lambda/joingame.js @@ -32,18 +32,37 @@ exports.handler = async (event, context, callback) => { const commandEnv = new GetItemCommand(dynamoDBParamsEnv); const responseEnv = await client.send(commandEnv); + const users = responseEnv.Item.users.L; + var found = false + for (let i = 0; i < users.length; i++) { + let value = users[i]["S"]; + if (value == email) { + found = true; + break; + } + + } + if (found == false) { + return { + statusCode: 500, + body: JSON.stringify('user not in game') + }; + } + else{ + const command = new GetItemCommand(dynamoDBParams); const response = await client.send(command); + const anchorid = response.Item.anchorid.S; // Return a response return { statusCode: 200, - body: JSON.stringify('arcore anchor stored success.') + body: anchorid }; - + } //error handleing } catch (error) { console.error('Error:', error);