async function joingame()

in backend/Functions/WebSockets/livegameplayer/app.js [33:95]


async function joingame(incoming, connectionId, domainName, stage) {
  const gameData = {};
  gameData.Key = { pk: incoming.playerName, sk: incoming.gameId };
  gameData.TableName = playerInventoryTableName;
  let activeGameResponse;
  try {
    activeGameResponse = await ddb.get(gameData).promise();
    if (!('Item' in activeGameResponse)) {
      console.error(`invalid game info ${JSON.stringify(activeGameResponse)}`);
      return { statusCode: 500, body: JSON.stringify({ data: 'Invalid game' }) };
    }
  } catch (e) {
    console.error(`${JSON.stringify(gameData)}`);
    console.error(`error getting game data ${JSON.stringify(e)}`);
    return { statusCode: 500, body: 'Error getting game data' };
  }

  const item = {};
/*  item.gameId = incoming.gameId;
  item.connectionId = connectionId;
  item.userName = incoming.username;
  item.role = 'PLAYER';*/
  const pk = incoming.gameId + "#" + incoming.hostname; 
  item.pk = pk;
  item.sk = 'PLAYER=' + connectionId;
  item.playerName = incoming.username;
  item.connectionId = connectionId;
  item.gameId = incoming.gameId;
  const parms = {};
  parms.TableName = playersTableName;
  parms.Item = item;
  try {
    await ddb.put(parms, (err, data) => {
      if (err) {
        console.error(`Error ${JSON.stringify(err.stack)}`);
      } else {
        // may want to log some metrics here about dynamodb usage
      }
    }).promise;
    // send SNS message that the player has joined
    const message = {};
    message.action = 'liveplayer';
    message.domainName = domainName;
    message.stage = stage;
    message.playerName = incoming.username;
    message.subaction = 'joined';
    message.pk = pk;
    message.gameId = incoming.gameId;
    message.hostname = incoming.hostname;
    await WSSend.SendChat(message);
    const data = {};
    const msg = {};
    msg.channel = 'joinedlive';
    data.gameId = activeGameResponse.Item.sk + "#" + activeGameResponse.Item.pk;
    data.quizName = activeGameResponse.Item.quizName;
    data.hostname = incoming.hostname;
    msg.message = data;
    return { statusCode: 200, body: JSON.stringify(msg) };
  } catch (e) {
    console.error(`failed to add player to game table ${JSON.stringify(e)}`);
    return { statusCode: 500, body: 'Error adding player to game' };
  }
}