From 931016a9634e4647eb75a6574fa974f0e61b474c Mon Sep 17 00:00:00 2001 From: ghkkk090 Date: Mon, 24 Oct 2022 14:09:42 +0700 Subject: [PATCH] trying to fix register ocm ghost from terminal --- src/modules/cars.ts | 180 +++++++++------------------------------ src/modules/ghost_ocm.ts | 1 + src/modules/terminal.ts | 80 +++++++---------- 3 files changed, 73 insertions(+), 188 deletions(-) diff --git a/src/modules/cars.ts b/src/modules/cars.ts index 3859d44..94d5d0e 100644 --- a/src/modules/cars.ts +++ b/src/modules/cars.ts @@ -52,149 +52,49 @@ export default class CarModule extends Module { // Get current date let date = Math.floor(new Date().getTime() / 1000); - // Get current / previous active OCM Event - let ocmEventDate = await prisma.oCMEvent.findFirst({ - where: { - // qualifyingPeriodStartAt is less than current date - qualifyingPeriodStartAt: { lte: date }, - - // competitionEndAt is greater than current date - competitionEndAt: { gte: date }, - }, - orderBy: [ - { - dbId: 'desc' - }, - { - competitionEndAt: 'desc', - }, - ], - }); + // Get Registered Target + let getTarget = await prisma.ghostRegisteredFromTerminal.findFirst({ + where:{ + carId: body.carId + } + }); + let opponentGhost; + let opponentTrailId; + let opponentCompetitionId; - let pastEvent = 0; - if(!(ocmEventDate)) - { - ocmEventDate = await prisma.oCMEvent.findFirst({ - orderBy: [ - { - dbId: 'desc' - }, - { - competitionEndAt: 'desc', - }, - ], + if(getTarget) + { + + let getTargetTrail = await prisma.oCMTop1GhostTrail.findFirst({ + where:{ + carId: getTarget.opponentCarId, + competitionId: Number(getTarget.competitionId) + } }); - pastEvent = 1; - } - - // Current / previous OCM Event is found - let ghostCarsNo1; - let trailIdNo1: number = 0; - if(ocmEventDate) - { - let pastDay = date - ocmEventDate.competitionEndAt - - // Get Previous Top 1 OCM - if(pastDay < 604800 && pastEvent === 1) - { - let checkRegisteredGhost = await prisma.ghostRegisteredFromTerminal.findFirst({ + if(getTargetTrail) + { + let getTargetCar = await prisma.car.findFirst({ where:{ - carId: body.carId + carId: getTarget.opponentCarId } }); - if(checkRegisteredGhost) - { - console.log('OCM Ghost Registered From Terminal'); - - let getNo1OCM = await prisma.oCMTally.findFirst({ - where:{ - competitionId: ocmEventDate.competitionId, - periodId: 999999999 - }, - orderBy:{ - competitionId: 'desc' - } - }); - - if(getNo1OCM) - { - console.log('Getting registered car data'); - - // Get Car Data - let cars = await prisma.car.findFirst({ - where:{ - carId: getNo1OCM.carId - }, - include:{ - gtWing: true, - lastPlayedPlace: true - } - }); - - // Get Ghost Trail - let ghostTrailNo1 = await prisma.oCMTop1GhostTrail.findFirst({ - where:{ - carId: getNo1OCM.carId, - competitionId: ocmEventDate.competitionId - }, - orderBy:{ - dbId: 'asc' - } - }); - - if(ghostTrailNo1) - { - console.log('Getting registered ghost trail'); - - trailIdNo1 = ghostTrailNo1.dbId; - - ghostCarsNo1 = wm.wm.protobuf.GhostCar.create({ - car: { - ...cars!, - }, - area: ghostTrailNo1.area, - ramp: ghostTrailNo1.ramp, - path: ghostTrailNo1.path, - nonhuman: false, - type: wm.wm.protobuf.GhostType.GHOST_NORMAL, - trailId: trailIdNo1 - }); - } - else - { - ghostTrailNo1 = await prisma.oCMGhostTrail.findFirst({ - where:{ - carId: getNo1OCM.carId, - competitionId: ocmEventDate.competitionId - }, - orderBy:{ - playedAt: 'desc' - } - }); - - if(ghostTrailNo1) - { - console.log('Getting registered ghost trail from other table'); - - trailIdNo1 = ghostTrailNo1.dbId; - - ghostCarsNo1 = wm.wm.protobuf.GhostCar.create({ - car: { - ...cars!, - }, - area: ghostTrailNo1.area, - ramp: ghostTrailNo1.ramp, - path: ghostTrailNo1.path, - nonhuman: false, - type: wm.wm.protobuf.GhostType.GHOST_NORMAL, - trailId: trailIdNo1 - }); - } - } - } - } + opponentGhost = wm.wm.protobuf.GhostCar.create({ + car: { + ...getTargetCar!, + tunePower: getTargetTrail!.tunePower, + tuneHandling: getTargetTrail!.tuneHandling, + }, + area: getTargetTrail!.area, + ramp: getTargetTrail!.ramp, + path: getTargetTrail!.path, + nonhuman: false, + type: wm.wm.protobuf.GhostType.GHOST_NORMAL, + trailId: getTargetTrail!.dbId + }); + opponentTrailId = Number(getTargetTrail!.dbId); + opponentCompetitionId = Number(getTarget.competitionId); } } @@ -290,9 +190,9 @@ export default class CarModule extends Module { numOfChallengers: carsChallengers.length + 1 || null, // OCM Challenge Top 1 - opponentGhost: ghostCarsNo1 || null, - opponentTrailId: trailIdNo1 || null, - opponentCompetitionId: ocmEventDate?.competitionId || null + opponentGhost: opponentGhost || null, + opponentTrailId: opponentTrailId || null, + opponentCompetitionId: opponentCompetitionId || null }; // Generate the load car response message @@ -635,6 +535,8 @@ export default class CarModule extends Module { // Car update data data = { + name: common.sanitizeInput(cars.name), + visualModel: common.sanitizeInput(cars.visualModel), customColor: common.sanitizeInput(cars.customColor), wheel: common.sanitizeInput(cars.wheel), wheelColor: common.sanitizeInput(cars.wheelColor), diff --git a/src/modules/ghost_ocm.ts b/src/modules/ghost_ocm.ts index 8429bc8..1f77c7c 100644 --- a/src/modules/ghost_ocm.ts +++ b/src/modules/ghost_ocm.ts @@ -706,6 +706,7 @@ export default class GhostModule extends Module { let msg = { error: wm.wm.protobuf.ErrorCode.ERR_SUCCESS, competitionId: competition_id, + specialGhostId: competition_id, ghostCar: ghostCars!, trailId: ghostTrailId, updatedAt: date, diff --git a/src/modules/terminal.ts b/src/modules/terminal.ts index acf281e..53728fd 100644 --- a/src/modules/terminal.ts +++ b/src/modules/terminal.ts @@ -906,64 +906,46 @@ export default class TerminalModule extends Module { // Get the information from the request let body = wm.wm.protobuf.RegisterOpponentGhostRequest.decode(req.body); - let ocmEventDate = await prisma.oCMEvent.findFirst({ - orderBy: [ - { - dbId: 'desc' - }, - { - competitionEndAt: 'desc', - }, - ], + // Check if target is already registered + let checkOpponent = await prisma.ghostRegisteredFromTerminal.findFirst({ + where:{ + carId: body.carId, + } }); - if(ocmEventDate) + // Get Target Car ID + let ghostCompetitionTarget = await prisma.oCMTop1Ghost.findFirst({ + where:{ + competitionId: body.specialGhostId, + }, + orderBy:{ + periodId: 'desc' + } + }); + + // Target not yet registerted + if(!(checkOpponent)) { - let checkRegisteredGhost = await prisma.ghostRegisteredFromTerminal.findFirst({ - where:{ - carId: body.carId + await prisma.ghostRegisteredFromTerminal.create({ + data:{ + carId: body.carId, + competitionId: body.specialGhostId, + opponentCarId: ghostCompetitionTarget!.carId } }); - - let getNo1OCM = await prisma.oCMTally.findFirst({ + } + else + { + await prisma.ghostRegisteredFromTerminal.update({ where:{ - competitionId: ocmEventDate.competitionId, - periodId: 999999999 + dbId: checkOpponent.dbId }, - orderBy:{ - competitionId: 'desc' + data:{ + carId: body.carId, + competitionId: body.specialGhostId, + opponentCarId: ghostCompetitionTarget!.carId } }); - - if(!(checkRegisteredGhost)) - { - await prisma.ghostRegisteredFromTerminal.create({ - data:{ - carId: body.carId, - competitionId: ocmEventDate!.competitionId, - opponentCarId: getNo1OCM!.carId - } - }); - - console.log('Creating new Register Ghost Opponent entry') - } - else - { - await prisma.ghostRegisteredFromTerminal.update({ - where:{ - dbId: checkRegisteredGhost.dbId - }, - data:{ - carId: body.carId, - competitionId: ocmEventDate!.competitionId, - opponentCarId: getNo1OCM!.carId - } - }); - - console.log('Updating Register Ghost Opponent entry') - } - - } // Response data