-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## How does this PR impact the user? <!-- Add "before" and "after" screenshots or screen recordings; we like loom for screen recordings https://www.loom.com/ --> ## Description Updated Prisma scheme: - Added `food_eaten` field in GameStats - Changed some fileds name - Changed `player_id` field type `Int` -> `String` Updated end game functuion: - Separated end game code and created new function `endGame` - Added function that writes game stats to the db ## Limitations <!-- Anything related to this PR that wasn't "done" in this PR --> ## Checklist - [x] my PR is focused and contains one wholistic change - [ ] I have added screenshots or screen recordings to show the changes
- Loading branch information
1 parent
a547c19
commit 5e1deb5
Showing
10 changed files
with
174 additions
and
21 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
migrations/20240908164726_update_game_fields_types/migration.sql
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,21 @@ | ||
/* | ||
Warnings: | ||
- You are about to alter the column `end_time` on the `Game` table. The data in that column could be lost. The data in that column will be cast from `String` to `DateTime`. | ||
- You are about to alter the column `start_time` on the `Game` table. The data in that column could be lost. The data in that column will be cast from `String` to `DateTime`. | ||
*/ | ||
-- RedefineTables | ||
PRAGMA defer_foreign_keys=ON; | ||
PRAGMA foreign_keys=OFF; | ||
CREATE TABLE "new_Game" ( | ||
"game_id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
"start_time" DATETIME NOT NULL, | ||
"end_time" DATETIME NOT NULL, | ||
"end_reason" TEXT NOT NULL | ||
); | ||
INSERT INTO "new_Game" ("end_reason", "end_time", "game_id", "start_time") SELECT "end_reason", "end_time", "game_id", "start_time" FROM "Game"; | ||
DROP TABLE "Game"; | ||
ALTER TABLE "new_Game" RENAME TO "Game"; | ||
PRAGMA foreign_keys=ON; | ||
PRAGMA defer_foreign_keys=OFF; |
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,30 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the column `num_ate` on the `GameStats` table. All the data in the column will be lost. | ||
- You are about to drop the column `num_eaten` on the `GameStats` table. All the data in the column will be lost. | ||
- You are about to drop the column `player_id` on the `GameStats` table. All the data in the column will be lost. | ||
- Added the required column `deaths` to the `GameStats` table without a default value. This is not possible if the table is not empty. | ||
- Added the required column `food_eaten` to the `GameStats` table without a default value. This is not possible if the table is not empty. | ||
- Added the required column `kills` to the `GameStats` table without a default value. This is not possible if the table is not empty. | ||
- Added the required column `user_id` to the `GameStats` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- RedefineTables | ||
PRAGMA defer_foreign_keys=ON; | ||
PRAGMA foreign_keys=OFF; | ||
CREATE TABLE "new_GameStats" ( | ||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
"game_id" INTEGER NOT NULL, | ||
"user_id" INTEGER NOT NULL, | ||
"size" REAL NOT NULL, | ||
"food_eaten" INTEGER NOT NULL, | ||
"kills" INTEGER NOT NULL, | ||
"deaths" INTEGER NOT NULL, | ||
CONSTRAINT "GameStats_game_id_fkey" FOREIGN KEY ("game_id") REFERENCES "Game" ("game_id") ON DELETE RESTRICT ON UPDATE CASCADE | ||
); | ||
INSERT INTO "new_GameStats" ("game_id", "id", "size") SELECT "game_id", "id", "size" FROM "GameStats"; | ||
DROP TABLE "GameStats"; | ||
ALTER TABLE "new_GameStats" RENAME TO "GameStats"; | ||
PRAGMA foreign_keys=ON; | ||
PRAGMA defer_foreign_keys=OFF; |
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,19 @@ | ||
-- RedefineTables | ||
PRAGMA defer_foreign_keys=ON; | ||
PRAGMA foreign_keys=OFF; | ||
CREATE TABLE "new_GameStats" ( | ||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
"game_id" INTEGER NOT NULL, | ||
"user_id" INTEGER NOT NULL, | ||
"size" REAL NOT NULL, | ||
"food_eaten" INTEGER NOT NULL, | ||
"kills" INTEGER NOT NULL, | ||
"deaths" INTEGER NOT NULL, | ||
CONSTRAINT "GameStats_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE, | ||
CONSTRAINT "GameStats_game_id_fkey" FOREIGN KEY ("game_id") REFERENCES "Game" ("game_id") ON DELETE RESTRICT ON UPDATE CASCADE | ||
); | ||
INSERT INTO "new_GameStats" ("deaths", "food_eaten", "game_id", "id", "kills", "size", "user_id") SELECT "deaths", "food_eaten", "game_id", "id", "kills", "size", "user_id" FROM "GameStats"; | ||
DROP TABLE "GameStats"; | ||
ALTER TABLE "new_GameStats" RENAME TO "GameStats"; | ||
PRAGMA foreign_keys=ON; | ||
PRAGMA defer_foreign_keys=OFF; |
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 |
---|---|---|
@@ -1,32 +1,35 @@ | ||
datasource db { | ||
provider = "sqlite" | ||
url = env("DATABASE_URL") | ||
} | ||
} | ||
|
||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
model User { | ||
id Int @id @default(autoincrement()) | ||
password String | ||
username String @unique | ||
id Int @id @default(autoincrement()) | ||
password String | ||
username String @unique | ||
game_stats GameStats[] | ||
} | ||
|
||
model Game { | ||
game_id Int @id @default(autoincrement()) | ||
start_time String | ||
end_time String | ||
game_id Int @id @default(autoincrement()) | ||
start_time DateTime | ||
end_time DateTime | ||
end_reason String | ||
game_stats GameStats[] | ||
} | ||
|
||
model GameStats { | ||
id Int @id @default(autoincrement()) | ||
id Int @id @default(autoincrement()) | ||
game_id Int | ||
player_id Int | ||
user_id Int | ||
size Float | ||
num_eaten Int | ||
num_ate Int | ||
food_eaten Int | ||
kills Int | ||
deaths Int | ||
user User @relation(fields: [user_id], references: [id]) | ||
games Game @relation(fields: [game_id], references: [game_id]) | ||
} | ||
} |
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,45 @@ | ||
import prisma from "~/utils/db"; | ||
|
||
export type GameStat = { | ||
userId: number; | ||
size: number; | ||
foodEaten: number; | ||
kills: number; | ||
deaths: number; | ||
}; | ||
|
||
type RecordGameArgs = { | ||
startTime: Date; | ||
endTime: Date; | ||
endReason: string; | ||
stats: GameStat[]; | ||
}; | ||
|
||
export async function recordGameEnd({ startTime, endTime, endReason, stats }: RecordGameArgs) { | ||
try { | ||
// Создание записи о завершении игры | ||
const game = await prisma.game.create({ | ||
data: { | ||
start_time: startTime, | ||
end_time: endTime, | ||
end_reason: endReason, | ||
game_stats: { | ||
create: stats.map(stat => ({ | ||
user_id: stat.userId, | ||
size: stat.size, | ||
food_eaten: stat.foodEaten, | ||
kills: stat.kills, | ||
deaths: stat.deaths, | ||
})), | ||
}, | ||
}, | ||
include: { | ||
game_stats: true, | ||
}, | ||
}); | ||
|
||
console.log("Game record added:", game); | ||
} catch (error) { | ||
console.error("Error recording game end:", error); | ||
} | ||
} |
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