2019-01-30 02:41:49 +00:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// ABOUT THIS NODE.JS SAMPLE: This sample is part of the SDK for JavaScript Developer Guide topic at
// https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/nodejs-write-lambda-function-example-full.html
// NOTE:
// Supporting topics for the above-mentioned topic also contain important information about the role policy when creating the IAM role.
// snippet-start:[lambda.JavaScript.writeLambdaFunction.complete]
// Configuring the AWS SDK
var AWS = require ( "aws-sdk" ) ;
AWS . config . update ( { region : "REGION" } ) ;
exports . handler = ( event , context , callback ) => {
const URL _BASE = "S3_BUCKET_URL" ;
// Define the object that will hold the data values returned
var slotResults = {
isWinner : false ,
leftWheelImage : { file : { S : "" } } ,
middleWheelImage : { file : { S : "" } } ,
rightWheelImage : { file : { S : "" } } ,
} ;
// define parameters JSON for retrieving slot pull data from the database
var thisPullParams = {
Key : { slotPosition : { N : "" } } ,
TableName : "slotWheels" ,
ProjectionExpression : "imageFile" ,
} ;
// create DynamoDB service object
var request = new AWS . DynamoDB ( {
region : "REGION" ,
apiVersion : "2012-08-10" ,
} ) ;
// set a random number 0-9 for the slot position
2019-08-22 11:16:11 -04:00
thisPullParams . Key . slotPosition . N = Math . floor ( Math . random ( ) * 15 ) . toString ( ) ;
2019-01-30 02:41:49 +00:00
// call DynamoDB to retrieve the image to use for the Left slot result
var myLeftPromise = request
. getItem ( thisPullParams )
. promise ( )
. then ( function ( data ) {
return URL _BASE + data . Item . imageFile . S ;
} ) ;
// set a random number 0-9 for the slot position
2019-08-22 11:16:11 -04:00
thisPullParams . Key . slotPosition . N = Math . floor ( Math . random ( ) * 15 ) . toString ( ) ;
2019-01-30 02:41:49 +00:00
// call DynamoDB to retrieve the image to use for the Left slot result
var myMiddlePromise = request
. getItem ( thisPullParams )
. promise ( )
. then ( function ( data ) {
return URL _BASE + data . Item . imageFile . S ;
} ) ;
// set a random number 0-9 for the slot position
2019-08-22 11:16:11 -04:00
thisPullParams . Key . slotPosition . N = Math . floor ( Math . random ( ) * 15 ) . toString ( ) ;
2019-01-30 02:41:49 +00:00
// call DynamoDB to retrieve the image to use for the Left slot result
var myRightPromise = request
. getItem ( thisPullParams )
. promise ( )
. then ( function ( data ) {
return URL _BASE + data . Item . imageFile . S ;
2024-01-16 10:41:11 -05:00
} ) ;
2019-01-30 02:41:49 +00:00
Promise . all ( [ myLeftPromise , myMiddlePromise , myRightPromise ] ) . then ( function (
values
) {
slotResults . leftWheelImage . file . S = values [ 0 ] ;
slotResults . middleWheelImage . file . S = values [ 1 ] ;
slotResults . rightWheelImage . file . S = values [ 2 ] ;
// if all three values are identical, the spin is a winner
if ( values [ 0 ] === values [ 1 ] && values [ 0 ] === values [ 2 ] ) {
slotResults . isWinner = true ;
}
// return the JSON result to the caller of the Lambda function
callback ( null , slotResults ) ;
} ) ;
2019-01-30 19:53:12 +00:00
} ;
2019-08-22 11:16:11 -04:00
// snippet-end:[lambda.JavaScript.writeLambdaFunction.complete]