-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed7256f
commit 53f2a92
Showing
18 changed files
with
936 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { util, Context, AppSyncIdentityCognito } from "@aws-appsync/utils"; | ||
import type { DynamoDBGetItemRequest } from "@aws-appsync/utils/lib/resolver-return-types"; | ||
import type { Game, JoinGameInput } from "../../../appsync/graphql"; | ||
|
||
export function request( | ||
context: Context<{ input: JoinGameInput }>, | ||
): DynamoDBGetItemRequest { | ||
if (!context.identity) { | ||
util.error("Unauthorized: Identity information is missing." as string); | ||
} | ||
|
||
const identity = context.identity as AppSyncIdentityCognito; | ||
if (!identity.sub) { | ||
util.error("Unauthorized: User ID is missing." as string); | ||
} | ||
|
||
const id = context.arguments.input.gameId; | ||
const key = { | ||
PK: "GAME#" + id, | ||
SK: "GAME", | ||
}; | ||
|
||
return { | ||
operation: "GetItem", | ||
key: util.dynamodb.toMapValues(key), | ||
}; | ||
} | ||
|
||
export function response( | ||
context: Context<{ input: JoinGameInput }>, | ||
): Game | undefined { | ||
if (context.error) { | ||
util.error(context.error.message, context.error.type, context.result); | ||
} | ||
|
||
if ( | ||
context.result === null || | ||
context.result.joinToken !== context.arguments.input.joinToken | ||
) { | ||
util.error( | ||
"Game not found or invalid token" as string, | ||
"AccessDeniedException", | ||
); | ||
} | ||
|
||
const identity = context.identity as AppSyncIdentityCognito; | ||
if (identity.sub === context.result.fireflyUserId) { | ||
util.error("You cannot join your own game" as string, "Conflict"); | ||
} | ||
|
||
if (context.result.players?.includes(identity.sub)) { | ||
util.error("You are already a player in this game" as string, "Conflict"); | ||
} | ||
|
||
return context.result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { util, Context, AppSyncIdentityCognito } from "@aws-appsync/utils"; | ||
import type { PutItemInputAttributeMap } from "@aws-appsync/utils/lib/resolver-return-types"; | ||
import environment from "../../environment.json"; | ||
import type { Game, JoinGameInput } from "../../../appsync/graphql"; | ||
|
||
export function request(context: Context<{ input: JoinGameInput }>): unknown { | ||
if (!context.identity) { | ||
util.error("Unauthorized: Identity information is missing." as string); | ||
} | ||
|
||
const identity = context.identity as AppSyncIdentityCognito; | ||
if (!identity.sub) { | ||
util.error("Unauthorized: User ID is missing." as string); | ||
} | ||
|
||
const id = context.arguments.input.gameId; | ||
const timestamp = util.time.nowISO8601(); | ||
|
||
const gameItem = { | ||
operation: "UpdateItem", | ||
table: "Wildsea-" + environment.name, | ||
key: util.dynamodb.toMapValues({ | ||
PK: "GAME#" + id, | ||
SK: "GAME", | ||
}), | ||
update: { | ||
expression: "ADD #players :player SET #updatedAt = :updatedAt", | ||
expressionNames: { | ||
"#players": "players", | ||
"#updatedAt": "updatedAt", | ||
}, | ||
expressionValues: { | ||
":player": { SS: [identity.sub] }, | ||
":updatedAt": { S: timestamp }, | ||
}, | ||
}, | ||
} as PutItemInputAttributeMap; | ||
|
||
const playerItem = { | ||
operation: "PutItem", | ||
table: "Wildsea-" + environment.name, | ||
key: util.dynamodb.toMapValues({ | ||
PK: "GAME#" + id, | ||
SK: "PLAYER#PC#" + identity.sub, | ||
}), | ||
attributeValues: util.dynamodb.toMapValues({ | ||
gameId: id, | ||
userId: identity.sub, | ||
GSI1PK: "USER#" + identity.sub, | ||
gameName: context.prev.result.gameName, | ||
gameDescription: context.prev.result.gameDescription, | ||
characterName: "Unnamed Character", | ||
type: "CHARACTER", | ||
createdAt: timestamp, | ||
updatedAt: timestamp, | ||
}), | ||
} as PutItemInputAttributeMap; | ||
|
||
return { | ||
operation: "TransactWriteItems", | ||
transactItems: [gameItem, playerItem], | ||
}; | ||
} | ||
|
||
export function response( | ||
context: Context<{ input: JoinGameInput }>, | ||
): Game | undefined { | ||
if (context.error) { | ||
util.error(context.error.message, context.error.type, context.result); | ||
} | ||
|
||
context.prev.result.joinToken = null; | ||
return context.prev.result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.