From 292675047424e222c2bf7c219f53898d31ee32f2 Mon Sep 17 00:00:00 2001 From: lahirumenik <115687865+lahirumenik@users.noreply.github.com> Date: Mon, 29 Jan 2024 00:42:15 +0530 Subject: [PATCH 1/3] fire socket function --- backend/serverless/Lambda/firesocket.js | 75 +++++++++++++++++++++++++ backend/serverless/Lambda/gunsocket.js | 10 ++-- 2 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 backend/serverless/Lambda/firesocket.js diff --git a/backend/serverless/Lambda/firesocket.js b/backend/serverless/Lambda/firesocket.js new file mode 100644 index 0000000..2b6c32f --- /dev/null +++ b/backend/serverless/Lambda/firesocket.js @@ -0,0 +1,75 @@ +const { ApiGatewayManagementApiClient, PostToConnectionCommand } = require("@aws-sdk/client-apigatewaymanagementapi"); +const { DynamoDBClient, GetItemCommand } = require("@aws-sdk/client-dynamodb"); +const dbclient = new DynamoDBClient() + +const ENDPOINT = 'https://9c0zh8p4oj.execute-api.ap-southeast-1.amazonaws.com/beta/'; + +const client = new ApiGatewayManagementApiClient({ endpoint: ENDPOINT }); + +exports.handler = async (event, context) => { + try { + const fireresult = event; + + + //horzontal cumulative roll -- need to be fixed + //const gyrox = gunresult.gyrox; + + const gyroy = fireresult.fire; + const gunid = fireresult.gunid; + + + const playerParams = { + "TableName": 'arcombat-gunid', + "Key": { + "gunid": {"S": gunid}, + + } + }; + + + const commandPlayer = new GetItemCommand(playerParams); + + const playerResponse = await dbclient.send(commandPlayer); + const email = playerResponse.Item.email.S; + // const email = "e19163@eng.pdn.ac.lk" + + const playerIdParams = { + "TableName": 'Arcombat-socket', + "Key": { + "email": {"S": email}, + + } + }; + + const commandplayerId = new GetItemCommand(playerIdParams); + const responseplayerId = await dbclient.send(commandplayerId); + const playerId = responseplayerId.Item.connectionid.S; + + + + + + // console.log(event); + const Msg = { + // Data : hitsocketmsg, + Data: "fire", + ConnectionId: playerId + }; + + + + + const commandSend = new PostToConnectionCommand(Msg); + await client.send(commandSend); + + + return "success" + } catch (error) { + console.error('Error:', error); + + return { + statusCode: 500, + body: JSON.stringify('Error starting connectio.') + }; + } +}; diff --git a/backend/serverless/Lambda/gunsocket.js b/backend/serverless/Lambda/gunsocket.js index 9176c3c..cc0c595 100644 --- a/backend/serverless/Lambda/gunsocket.js +++ b/backend/serverless/Lambda/gunsocket.js @@ -9,8 +9,11 @@ const client = new ApiGatewayManagementApiClient({ endpoint: ENDPOINT }); exports.handler = async (event, context) => { try { const gunresult = event; - const screenshot = gunresult.screenshot; - const gyrox = gunresult.gyrox; + + + //horzontal cumulative roll -- need to be fixed + //const gyrox = gunresult.gyrox; + const gyroy = gunresult.gyroy; const gunid = gunresult.gunid; console.log(event) @@ -23,9 +26,6 @@ exports.handler = async (event, context) => { } }; - - - const commandPlayer = new GetItemCommand(playerParams); From d9c7b89bf7a2fb5d02d6e3fbcaa3788c675df527 Mon Sep 17 00:00:00 2001 From: lahirumenik <115687865+lahirumenik@users.noreply.github.com> Date: Mon, 29 Jan 2024 01:06:32 +0530 Subject: [PATCH 2/3] health ammo socket --- backend/serverless/Lambda/firesocket.js | 3 - backend/serverless/Lambda/healthammo.js | 74 +++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 backend/serverless/Lambda/healthammo.js diff --git a/backend/serverless/Lambda/firesocket.js b/backend/serverless/Lambda/firesocket.js index 2b6c32f..2365243 100644 --- a/backend/serverless/Lambda/firesocket.js +++ b/backend/serverless/Lambda/firesocket.js @@ -9,10 +9,7 @@ const client = new ApiGatewayManagementApiClient({ endpoint: ENDPOINT }); exports.handler = async (event, context) => { try { const fireresult = event; - - //horzontal cumulative roll -- need to be fixed - //const gyrox = gunresult.gyrox; const gyroy = fireresult.fire; const gunid = fireresult.gunid; diff --git a/backend/serverless/Lambda/healthammo.js b/backend/serverless/Lambda/healthammo.js new file mode 100644 index 0000000..45aae75 --- /dev/null +++ b/backend/serverless/Lambda/healthammo.js @@ -0,0 +1,74 @@ +const { ApiGatewayManagementApiClient, PostToConnectionCommand } = require("@aws-sdk/client-apigatewaymanagementapi"); +const { DynamoDBClient, GetItemCommand } = require("@aws-sdk/client-dynamodb"); +const dbclient = new DynamoDBClient() + +const ENDPOINT = 'https://9c0zh8p4oj.execute-api.ap-southeast-1.amazonaws.com/beta/'; + +const client = new ApiGatewayManagementApiClient({ endpoint: ENDPOINT }); + +exports.handler = async (event, context) => { + try { + const result = event; + const ammo = result.ammo; + const health = result.health; + const gunid = fireresult.ssid; + + + const playerParams = { + "TableName": 'arcombat-gunid', + "Key": { + "gunid": {"S": gunid}, + + } + }; + + + const commandPlayer = new GetItemCommand(playerParams); + + const playerResponse = await dbclient.send(commandPlayer); + const email = playerResponse.Item.email.S; + // const email = "e19163@eng.pdn.ac.lk" + + const playerIdParams = { + "TableName": 'Arcombat-socket', + "Key": { + "email": {"S": email}, + + } + }; + + const commandplayerId = new GetItemCommand(playerIdParams); + const responseplayerId = await dbclient.send(commandplayerId); + const playerId = responseplayerId.Item.connectionid.S; + + + + const msg =JSON.stringify({ + "health":health, + 'ammo':ammo + }) + + // console.log(event); + const Msg = { + // Data : hitsocketmsg, + Data: msg, + ConnectionId: playerId + }; + + + + + const commandSend = new PostToConnectionCommand(Msg); + await client.send(commandSend); + + + return "success" + } catch (error) { + console.error('Error:', error); + + return { + statusCode: 500, + body: JSON.stringify('Error starting connectio.') + }; + } +}; From 234617d110f09572756d2e60b43d23e8fcc096f1 Mon Sep 17 00:00:00 2001 From: lahirumenik <115687865+lahirumenik@users.noreply.github.com> Date: Mon, 29 Jan 2024 01:17:29 +0530 Subject: [PATCH 3/3] add subscrber to gun --- backend/serverless/Lambda/healthammo.js | 27 ++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/backend/serverless/Lambda/healthammo.js b/backend/serverless/Lambda/healthammo.js index 45aae75..d15e9a7 100644 --- a/backend/serverless/Lambda/healthammo.js +++ b/backend/serverless/Lambda/healthammo.js @@ -1,6 +1,8 @@ const { ApiGatewayManagementApiClient, PostToConnectionCommand } = require("@aws-sdk/client-apigatewaymanagementapi"); const { DynamoDBClient, GetItemCommand } = require("@aws-sdk/client-dynamodb"); +const { IoTDataPlaneClient, PublishCommand } = require("@aws-sdk/client-iot-data-plane"); const dbclient = new DynamoDBClient() +const iotclient = new IoTDataPlaneClient(); const ENDPOINT = 'https://9c0zh8p4oj.execute-api.ap-southeast-1.amazonaws.com/beta/'; @@ -13,6 +15,25 @@ exports.handler = async (event, context) => { const health = result.health; const gunid = fireresult.ssid; + var numericPart = gunid[3]; + var topic = "gun/"+numericPart+"/healthammo"; + + + const msg =JSON.stringify({ + "health":health, + 'ammo':ammo + }) + + const input = { // PublishRequest + topic: topic, + + payload: msg, + + }; + + + const command = new PublishCommand(input); + const response = await client.send(command) const playerParams = { "TableName": 'arcombat-gunid', @@ -42,11 +63,7 @@ exports.handler = async (event, context) => { const playerId = responseplayerId.Item.connectionid.S; - - const msg =JSON.stringify({ - "health":health, - 'ammo':ammo - }) + // console.log(event); const Msg = {