add update game endpoint
This commit is contained in:
		@ -17,7 +17,7 @@ export const POST: RequestHandler = async ({ locals }): Promise<Response> => {
 | 
				
			|||||||
	const { username, sub } = user.payload;
 | 
						const { username, sub } = user.payload;
 | 
				
			||||||
	const game = new Game();
 | 
						const game = new Game();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	game.addPlayer(sub, username);
 | 
						game.addPlayer({ id: sub, username, isReady: false });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const newListing = createNewListing(game);
 | 
						const newListing = createNewListing(game);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,9 +1,16 @@
 | 
				
			|||||||
import type { GameData } from "$lib/GameData";
 | 
					import { isGameData, type GameData } from "$lib/GameData";
 | 
				
			||||||
import { isListing } from "$lib/Listing";
 | 
					import { isListing } from "$lib/Listing";
 | 
				
			||||||
import { readListingById, ServerCollections } from "$lib/server/mongo";
 | 
					import { Game } from "$lib/server/Game";
 | 
				
			||||||
import { getParam, ResourceId } from "$lib/server/requestTools";
 | 
					import { updateListing } from "$lib/server/modifyListing";
 | 
				
			||||||
 | 
					import {
 | 
				
			||||||
 | 
						readListingById,
 | 
				
			||||||
 | 
						ServerCollections,
 | 
				
			||||||
 | 
						writeUpdatedListing,
 | 
				
			||||||
 | 
					} from "$lib/server/mongo";
 | 
				
			||||||
 | 
					import { getBody, getParam, ResourceId } from "$lib/server/requestTools";
 | 
				
			||||||
import {
 | 
					import {
 | 
				
			||||||
	badRequestResponse,
 | 
						badRequestResponse,
 | 
				
			||||||
 | 
						conflictResponse,
 | 
				
			||||||
	notFoundResponse,
 | 
						notFoundResponse,
 | 
				
			||||||
	singleResponse,
 | 
						singleResponse,
 | 
				
			||||||
} from "$lib/server/responseBodies";
 | 
					} from "$lib/server/responseBodies";
 | 
				
			||||||
@ -24,3 +31,40 @@ export const GET: RequestHandler = async ({ params }): Promise<Response> => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	return singleResponse(game);
 | 
						return singleResponse(game);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const PUT: RequestHandler = async ({ request, params }): Promise<Response> => {
 | 
				
			||||||
 | 
						const id = getParam(params, ResourceId.Game);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!id) {
 | 
				
			||||||
 | 
							return badRequestResponse("missing gameid parameter");
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const gameListing = await readListingById(
 | 
				
			||||||
 | 
							ServerCollections.Games,
 | 
				
			||||||
 | 
							id,
 | 
				
			||||||
 | 
							isListing<GameData>,
 | 
				
			||||||
 | 
						);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!gameListing) {
 | 
				
			||||||
 | 
							return notFoundResponse();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const game = Game.from(gameListing.data);
 | 
				
			||||||
 | 
						const body = await getBody(request, isGameData);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!body) {
 | 
				
			||||||
 | 
							return badRequestResponse("missing game data in body");
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!game.isStarted) {
 | 
				
			||||||
 | 
							if (body.isStarted) {
 | 
				
			||||||
 | 
								game.start();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else if (game.isStarted !== body.isStarted) {
 | 
				
			||||||
 | 
							return conflictResponse();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						await writeUpdatedListing(ServerCollections.Games, updateListing(gameListing, game));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return singleResponse(gameListing);
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -47,13 +47,6 @@ export const PUT: RequestHandler = async ({ params, request }): Promise<Response
 | 
				
			|||||||
	const game = Game.from(listing.data);
 | 
						const game = Game.from(listing.data);
 | 
				
			||||||
	if (game.setPlayerReady(player) === null) return notFoundResponse();
 | 
						if (game.setPlayerReady(player) === null) return notFoundResponse();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// TODO: there's a potential race condition here where some player is unreadying as this
 | 
					 | 
				
			||||||
	//       function is running. This should do some kind of check in MongoDB to make sure
 | 
					 | 
				
			||||||
	//       all players are ready if it's starting the game. For now: good enough.
 | 
					 | 
				
			||||||
	if (game.players.every(({ isReady }) => isReady)) {
 | 
					 | 
				
			||||||
		game.start();
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	await writeUpdatedListing(ServerCollections.Games, updateListing(listing, game));
 | 
						await writeUpdatedListing(ServerCollections.Games, updateListing(listing, game));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return singleResponse(game);
 | 
						return singleResponse(game);
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user