Skip to content

Commit

Permalink
Stopped fetching player ID separately from player data
Browse files Browse the repository at this point in the history
Update realtime position for randomTeleport shop item action
  • Loading branch information
hopperelec committed Feb 28, 2024
1 parent e602685 commit d0891d0
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 86 deletions.
13 changes: 3 additions & 10 deletions src/lib/server/get-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@ import prisma from "$lib/server/prisma";
import { error } from "@sveltejs/kit";
import chooseSpawnpoint from "$lib/server/choose-spawnpoint";

export async function getExistingPlayer(
export default async function getPlayer(
user: { id: number; schoolId: number },
gameId: number,
) {
return prisma.player.findFirst({
): Promise<{ id: number }> {
const player = await prisma.player.findFirst({
where: {
userId: user.id,
gameId: gameId,
},
select: { id: true },
});
}

export default async function getPlayer(
user: { id: number; schoolId: number },
gameId: number,
): Promise<{ id: number }> {
const player = await getExistingPlayer(user, gameId);
if (player) return player;
const game = await prisma.game.findFirst({
where: {
Expand Down
4 changes: 2 additions & 2 deletions src/routes/game/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import chooseSpawnpoint from "$lib/server/choose-spawnpoint";

export const GET = async ({ locals }) => {
const map = await getAnyMapFor(locals.user.schoolId);
const playerData = await prisma.player.create({
const player = await prisma.player.create({
data: {
user: {
connect: {
Expand All @@ -25,5 +25,5 @@ export const GET = async ({ locals }) => {
gameId: true,
},
});
redirect(302, "/game/" + playerData.gameId + "/");
redirect(302, "/game/" + player.gameId + "/");
};
30 changes: 15 additions & 15 deletions src/routes/game/[gameId=id]/answer/+server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { error, json } from "@sveltejs/kit";
import prisma from "$lib/server/prisma";
import { getExistingPlayer } from "$lib/server/get-player";
import ablyServer from "$lib/server/ably-server";

export const POST = async ({ request, params, locals }) => {
const player = await getExistingPlayer(locals.user, +params.gameId);
if (!player) error(400, "You are not in this game!");
const playerData = await prisma.player.findUnique({
const player = await prisma.player.findUnique({
where: {
id: player.id,
userId_gameId: {
userId: locals.user.id,
gameId: +params.gameId,
},
},
select: {
id: true,
currQuestion: {
select: {
answerRegex: true,
Expand All @@ -19,15 +20,16 @@ export const POST = async ({ request, params, locals }) => {
currMoveId: true,
},
});
if (!playerData?.currQuestion)
if (!player) error(400, "You are not in this game!");
if (!player.currQuestion)
error(400, "You currently have no question to answer!");
if (!playerData.currMoveId)
if (!player.currMoveId)
error(
500,
"An unexpected error occurred while trying to get the room you were trying to move to",
);
const correct = new RegExp(
"^" + playerData.currQuestion.answerRegex + "$",
"^" + player.currQuestion.answerRegex + "$",
"i",
).test(await request.text());
if (correct) {
Expand All @@ -40,16 +42,14 @@ export const POST = async ({ request, params, locals }) => {
points: {
increment: 1,
},
currRoomId: playerData.currMoveId,
currRoomId: player.currMoveId,
currMoveId: null,
},
});
await ablyServer.channels
.get("game:" + params.gameId)
.publish("move", {
userId: locals.user.id,
roomId: playerData.currMoveId,
});
await ablyServer.channels.get("game:" + params.gameId).publish("move", {
userId: locals.user.id,
roomId: player.currMoveId,
});
}
return json({ correct });
};
26 changes: 11 additions & 15 deletions src/routes/game/[gameId=id]/get-definition/+server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import prisma from "$lib/server/prisma";
import { error } from "@sveltejs/kit";
import { getExistingPlayer } from "$lib/server/get-player";

export const GET = async ({ url, params, locals }) => {
const player = await getExistingPlayer(locals.user, +params.gameId);
if (!player) error(400, "You are not in this game!");
const playerData = await prisma.player.findUnique({
const player = await prisma.player.findUnique({
where: {
id: player.id,
userId_gameId: {
userId: locals.user.id,
gameId: +params.gameId,
},
},
select: {
id: true,
currRoomId: true,
currQuestion: {
select: {
Expand All @@ -23,21 +24,16 @@ export const GET = async ({ url, params, locals }) => {
},
},
});
if (!playerData)
error(
500,
"An unexpected error occurred while trying to retrieve your player data",
);
if (playerData.currQuestion)
return new Response(playerData.currQuestion.definition);
if (!player) error(400, "You are not in this game!");
if (player.currQuestion) return new Response(player.currQuestion.definition);
const roomRequested = url.searchParams.get("room");
if (!roomRequested) error(400, "You must select a room you wish to move to");
const door = await prisma.door.findUnique({
where: {
mapId_svgRef1_svgRef2: {
mapId: playerData.game.mapId,
svgRef1: Math.min(playerData.currRoomId, +roomRequested),
svgRef2: Math.max(playerData.currRoomId, +roomRequested),
mapId: player.game.mapId,
svgRef1: Math.min(player.currRoomId, +roomRequested),
svgRef2: Math.max(player.currRoomId, +roomRequested),
},
},
select: { id: true },
Expand Down
109 changes: 65 additions & 44 deletions src/routes/game/[gameId=id]/shop/[itemId=id]/buy/+server.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,57 @@
import { error, json } from "@sveltejs/kit";
import prisma from "$lib/server/prisma";
import { getExistingPlayer } from "$lib/server/get-player";
import ablyServer from "$lib/server/ably-server";
import type { User } from "@prisma/client";

const ACTIONS: { [key: string]: (playerId: number) => Promise<boolean> } = {
randomTeleport: async (playerId) => {
const playerData = await prisma.player.findUnique({
where: {
id: playerId,
},
select: {
game: {
select: {
mapId: true,
type ActionDetails = {
playerId: number;
playerPoints: number;
user: User;
itemId: number;
itemCost: number;
};

const ACTIONS: { [key: string]: (details: ActionDetails) => Promise<boolean> } =
{
randomTeleport: async ({ playerId, user }) => {
const player = await prisma.player.findUnique({
where: {
id: playerId,
},
select: {
game: {
select: {
id: true,
},
},
},
},
});
if (!playerData)
error(
500,
"An unexpected error occurred while trying to retrieve your player data",
);
const room: { id: bigint }[] =
await prisma.$queryRaw`SELECT id FROM Room WHERE mapId = ${playerData.game.mapId} ORDER BY rand() LIMIT 1`;
if (room.length === 0) return false;
await prisma.player.update({
where: {
id: playerId,
},
data: {
currRoomId: Number(room[0].id),
},
});
return true;
},
};
});
if (!player)
error(
500,
"An unexpected error occurred while trying to retrieve your player data",
);
const room: { id: bigint }[] =
await prisma.$queryRaw`SELECT id FROM Room WHERE mapId = (SELECT mapId from Game WHERE id=${player.game.id}) ORDER BY rand() LIMIT 1`;
if (room.length === 0) return false;
const roomId = Number(room[0].id);
await prisma.player.update({
where: {
id: playerId,
},
data: {
currRoomId: roomId,
},
});
await ablyServer.channels.get("game:" + player.game.id).publish("move", {
userId: user.id,
roomId: roomId,
});
return true;
},
};

export const GET = async ({ params, locals }) => {
const player = await getExistingPlayer(locals.user, +params.gameId);
if (!player) error(400, "You are not in this game!");
const shopItem = await prisma.shopItem.findUnique({
where: {
id: +params.itemId,
Expand All @@ -49,31 +62,39 @@ export const GET = async ({ params, locals }) => {
},
});
if (!shopItem) error(404, "This item does not exist!");
const playerData = await prisma.player.findUnique({
const player = await prisma.player.findUnique({
where: {
id: player.id,
userId_gameId: {
userId: locals.user.id,
gameId: +params.gameId,
},
},
select: {
id: true,
currQuestionId: true,
points: true,
},
});
if (!playerData)
error(
500,
"An unexpected error occurred while trying to retrieve your player data",
);
if (playerData.currQuestionId)
if (!player) error(400, "You are not in this game!");
if (player.currQuestionId)
error(400, "You can not buy an item while answering a question!");
if (playerData.points < shopItem.cost)
if (player.points < shopItem.cost)
error(400, "You do not have enough points to buy this item");
const action = ACTIONS[shopItem.action];
if (!action)
error(
500,
"An unexpected error occurred trying to find find the reward for this item",
);
if (await action(player.id)) {
if (
await action({
playerId: player.id,
playerPoints: player.points,
user: locals.user,
itemId: +params.itemId,
itemCost: shopItem.cost,
})
) {
const newPlayerData = await prisma.player.update({
where: {
id: player.id,
Expand Down

0 comments on commit d0891d0

Please sign in to comment.