diff --git a/package.json b/package.json index 25f2ee1..0c0a21b 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@sentry/tracing": "^7.7.0", "@types/pem": "^1.9.6", "body-parser": "^1.20.0", + "dotenv": "^16.0.1", "express": "^4.18.1", "form-urlencoded": "^6.0.6", "iconv-lite": "^0.6.3", diff --git a/src/index.ts b/src/index.ts index 0cf66bf..970ff0d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,10 @@ import { Config } from './config'; import process from 'process'; import * as Sentry from '@sentry/node'; import * as Tracing from '@sentry/tracing'; + +import * as dotenv from "dotenv"; +dotenv.config({path: __dirname + '/.env'}); + globalAgent.options.keepAlive = true; // @ts-ignore diff --git a/src/modules/game.ts b/src/modules/game.ts index d63f038..b130cd4 100644 --- a/src/modules/game.ts +++ b/src/modules/game.ts @@ -85,20 +85,50 @@ export default class GameModule extends Module { } case wm.wm.protobuf.GameMode.MODE_TIME_ATTACK: { - if (!body.retired && !body.timeup) { + console.log(body); + + // If the game was not timed out / retired + if (!(body.retired || body.timeup)) { + + console.log('Game not retired / timed out, continuing ...') + + // Get the current time attack record for the car let currentRecord = await prisma.timeAttackRecord.findFirst({ - where: { - carId: body.carId, - model: body.car!.model!, + where: { + carId: body.carId, // , model: body.car!.model!, + course: body.taResult!.course } }); - // Make sure we don't save a worse record! - if (currentRecord && body.taResult!.time > currentRecord.time) - break; + // Record already exists + if (currentRecord) + { + // If the existing record is faster, do not continue + if (body.taResult!.time > currentRecord.time) break; - if (!currentRecord) { + console.log('Updating time attack record...') + + await prisma.timeAttackRecord.update({ + where: { + // Could be null - if it is null, this will insert. + dbId: currentRecord!.dbId + }, + data: { + time: body.taResult!.time, + 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, + } + }); + } + else // Creating a new record + { console.log('Creating new time attack record'); + await prisma.timeAttackRecord.create({ data: { carId: body.carId, @@ -117,24 +147,6 @@ export default class GameModule extends Module { }); break; } - - console.log('Updating time attack record...') - await prisma.timeAttackRecord.update({ - where: { - // Could be null - if it is null, this will insert. - dbId: currentRecord!.dbId - }, - data: { - time: body.taResult!.time, - 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, - } - }); } break; } diff --git a/yarn.lock b/yarn.lock index bddde5b..49ea285 100644 --- a/yarn.lock +++ b/yarn.lock @@ -496,6 +496,11 @@ diff@^4.0.1: resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d" integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A== +dotenv@^16.0.1: + version "16.0.1" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.1.tgz#8f8f9d94876c35dac989876a5d3a82a267fdce1d" + integrity sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ== + ee-first@1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"