From 0d194478dc951f67197c09b537d2a130a0f9710b Mon Sep 17 00:00:00 2001 From: Rin Date: Sat, 16 Jul 2022 22:46:40 +0100 Subject: [PATCH] Time attack records, no I have not tested this --- .../20220716203307_timeattack/migration.sql | 20 ++++ .../20220716203851_timeattack2/migration.sql | 8 ++ .../20220716210527_timeattack3/migration.sql | 16 +++ prisma/schema.prisma | 24 +++- src/modules/game.ts | 104 +++++++++++++++++- 5 files changed, 166 insertions(+), 6 deletions(-) create mode 100644 prisma/migrations/20220716203307_timeattack/migration.sql create mode 100644 prisma/migrations/20220716203851_timeattack2/migration.sql create mode 100644 prisma/migrations/20220716210527_timeattack3/migration.sql diff --git a/prisma/migrations/20220716203307_timeattack/migration.sql b/prisma/migrations/20220716203307_timeattack/migration.sql new file mode 100644 index 0000000..b1d8ad4 --- /dev/null +++ b/prisma/migrations/20220716203307_timeattack/migration.sql @@ -0,0 +1,20 @@ +-- CreateTable +CREATE TABLE "TimeAttackRecord" ( + "dbId" SERIAL NOT NULL, + "userId" INTEGER NOT NULL, + "model" INTEGER NOT NULL, + "time" INTEGER NOT NULL, + "course" INTEGER NOT NULL, + "section1Time" INTEGER NOT NULL, + "section2Time" INTEGER NOT NULL, + "section3Time" INTEGER NOT NULL, + "section4Time" INTEGER NOT NULL, + "section5Time" INTEGER, + "section6Time" INTEGER, + "section7Time" INTEGER, + + CONSTRAINT "TimeAttackRecord_pkey" PRIMARY KEY ("dbId") +); + +-- AddForeignKey +ALTER TABLE "TimeAttackRecord" ADD CONSTRAINT "TimeAttackRecord_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/migrations/20220716203851_timeattack2/migration.sql b/prisma/migrations/20220716203851_timeattack2/migration.sql new file mode 100644 index 0000000..56aadfd --- /dev/null +++ b/prisma/migrations/20220716203851_timeattack2/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - Added the required column `isMorning` to the `TimeAttackRecord` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "TimeAttackRecord" ADD COLUMN "isMorning" BOOLEAN NOT NULL; diff --git a/prisma/migrations/20220716210527_timeattack3/migration.sql b/prisma/migrations/20220716210527_timeattack3/migration.sql new file mode 100644 index 0000000..7204123 --- /dev/null +++ b/prisma/migrations/20220716210527_timeattack3/migration.sql @@ -0,0 +1,16 @@ +/* + Warnings: + + - You are about to drop the column `userId` on the `TimeAttackRecord` table. All the data in the column will be lost. + - Added the required column `carId` to the `TimeAttackRecord` table without a default value. This is not possible if the table is not empty. + +*/ +-- DropForeignKey +ALTER TABLE "TimeAttackRecord" DROP CONSTRAINT "TimeAttackRecord_userId_fkey"; + +-- AlterTable +ALTER TABLE "TimeAttackRecord" DROP COLUMN "userId", +ADD COLUMN "carId" INTEGER NOT NULL; + +-- AddForeignKey +ALTER TABLE "TimeAttackRecord" ADD CONSTRAINT "TimeAttackRecord_carId_fkey" FOREIGN KEY ("carId") REFERENCES "Car"("carId") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index fa34f4a..5d6feb3 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -98,8 +98,9 @@ model Car { items CarItem[] - carStateDbId Int @unique - state CarState @relation(fields: [carStateDbId], references: [dbId]) + carStateDbId Int @unique + state CarState @relation(fields: [carStateDbId], references: [dbId]) + TimeAttackRecord TimeAttackRecord[] } model CarItem { @@ -136,3 +137,22 @@ model CarState { transferred Boolean @default(false) toBeDeleted Boolean @default(false) } + +model TimeAttackRecord { + dbId Int @id @default(autoincrement()) + + car Car @relation(fields: [carId], references: [carId]) + carId Int + + model Int // Car model, literally just the `model` field from Car + time Int + course Int + isMorning Boolean + section1Time Int @map("section1Time") + section2Time Int @map("section2Time") + section3Time Int @map("section3Time") + section4Time Int @map("section4Time") + section5Time Int? @map("section5Time") + section6Time Int? @map("section6Time") + section7Time Int? @map("section7Time") +} diff --git a/src/modules/game.ts b/src/modules/game.ts index d83967c..11b7047 100644 --- a/src/modules/game.ts +++ b/src/modules/game.ts @@ -44,6 +44,51 @@ export default class GameModule extends Module { }) break; } + case wm.wm.protobuf.GameMode.MODE_TIME_ATTACK: + { + if (!body.retired && !body.timeup) { + let currentRecord = await prisma.timeAttackRecord.findFirst({ + where: { + carId: body.carId, + model: body.car!.model!, + } + }); + + // Make sure we don't save a worse record! + if (currentRecord && body.taResult!.time > currentRecord.time) + break; + + await prisma.timeAttackRecord.upsert({ + create: { + carId: body.carId, + model: body.car!.model!, + section1Time: body!.taResult!.section_1Time, + section2Time: body!.taResult!.section_2Time, + section3Time: body!.taResult!.section_3Time, + section4Time: body!.taResult!.section_4Time, + section5Time: body!.taResult!.section_5Time, + section6Time: body!.taResult!.section_6Time, + section7Time: body!.taResult!.section_7Time, + ...body!.taResult! + }, + update: { + section1Time: body!.taResult!.section_1Time, + section2Time: body!.taResult!.section_2Time, + section3Time: body!.taResult!.section_3Time, + section4Time: body!.taResult!.section_4Time, + section5Time: body!.taResult!.section_5Time, + section6Time: body!.taResult!.section_6Time, + section7Time: body!.taResult!.section_7Time, + ...body!.taResult! + }, + where: { + // Could be null - if it is null, this will insert. + dbId: currentRecord?.dbId + } + }); + } + break; + } } await prisma.carSettings.update({ where: { @@ -244,13 +289,64 @@ export default class GameModule extends Module { r.send(Buffer.from(end)); }) - app.post('/method/load_time_attack_record', (req, res) => { - console.log('load TA records'); + app.post('/method/load_time_attack_record', async (req, res) => { let body = wm.wm.protobuf.LoadTimeAttackRecordRequest.decode(req.body); - let ping = { + let taRecordsForModel = await prisma.timeAttackRecord.findMany({ + take: 100, + where: { + model: body.model, + course: body.course + }, + orderBy: { + time: 'desc' + } + }); + let taRecordsOverall = await prisma.timeAttackRecord.findMany({ + take: 100, + where: { + course: body.course + }, + orderBy: { + time: 'desc' + } + }); + let taRecordPb = await prisma.timeAttackRecord.findFirst({ + where: { + carId: body.carId, + course: body.course + }, + orderBy: { + time: 'desc' + } + }); + if (!taRecordPb) { + let msg = { + error: wm.wm.protobuf.ErrorCode.ERR_SUCCESS, + }; + let resp = wm.wm.protobuf.LoadTimeAttackRecordResponse.encode(msg); + let end = resp.finish(); + let r = res + .header('Server', 'v388 wangan') + .header('Content-Type', 'application/x-protobuf; revision=8053') + .header('Content-Length', end.length.toString()) + .status(200); + r.send(Buffer.from(end)); + return; + } + let msg = { error: wm.wm.protobuf.ErrorCode.ERR_SUCCESS, + wholeRanking: taRecordsOverall.map(a => a.time), + modelRanking: taRecordsForModel.map(a => a.time), + personalBestTime: taRecordPb.time, + pbSection1Time: taRecordPb.section1Time, + pbSection2Time: taRecordPb.section2Time, + pbSection3Time: taRecordPb.section3Time, + pbSection4Time: taRecordPb.section4Time, + pbSection5Time: taRecordPb.section5Time, + pbSection6Time: taRecordPb.section6Time, + pbSection7Time: taRecordPb.section7Time, }; - let resp = wm.wm.protobuf.LoadTimeAttackRecordResponse.encode(ping); + let resp = wm.wm.protobuf.LoadTimeAttackRecordResponse.encode(msg); let end = resp.finish(); let r = res .header('Server', 'v388 wangan')