diff --git a/backend/serverless/Lambda/firesocket.js b/backend/serverless/Lambda/firesocket.js new file mode 100644 index 0000000..2365243 --- /dev/null +++ b/backend/serverless/Lambda/firesocket.js @@ -0,0 +1,72 @@ +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; + + + 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); diff --git a/backend/serverless/Lambda/healthammo.js b/backend/serverless/Lambda/healthammo.js new file mode 100644 index 0000000..d15e9a7 --- /dev/null +++ b/backend/serverless/Lambda/healthammo.js @@ -0,0 +1,91 @@ +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/'; + +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; + + 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', + "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: 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.') + }; + } +};