1
0
mirror of synced 2024-09-24 03:08:26 +02:00

Time attack records, no I have not tested this

This commit is contained in:
Rin 2022-07-16 22:46:40 +01:00
parent 46d3c58ee3
commit 0d194478dc
5 changed files with 166 additions and 6 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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")
}

View File

@ -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')