1
0
mirror of synced 2025-01-22 11:23:40 +01:00

randomizer

This commit is contained in:
Shiroi Kitsu 2023-02-20 11:20:46 +07:00
parent 9395f1f4f0
commit 035410daec
2 changed files with 67 additions and 33 deletions

View File

@ -138,18 +138,37 @@ export async function getOpponentsTarget(carId: number, registeredargetAvailable
// Randomize pick
let random: number = 1;
let randomArray: number[] = [];
let maxNumber = 5;
if(opponentTargetCount < 5)
{
maxNumber = opponentTargetCount;
}
// Randomize it 5 times
for(let i=0; i<5; i++)
{
random = Math.floor(Math.random() * opponentTargetCount);
while(randomArray.length < 5)
{
// Pick random car Id
random = Math.floor(Math.random() * opponentTargetCount + 0.9);
// Try randomize it again if it's 0, and fix if more than car length
if(random < 1 || random >= opponentTargetCount)
{
random = Math.floor(Math.random() * opponentTargetCount + 0.9);
}
// Random Number not yet selected
if(randomArray.indexOf(random) === -1)
{
// Push current number to array
randomArray.push(random);
}
}
// Try randomize it again if it's 1
if(random === 1)
{
random = Math.floor(Math.random() * opponentTargetCount);
}
// Pick the array number
let pickRandom = Math.floor(Math.random() * randomArray.length + 0.9);
random = randomArray[pickRandom];
// Check opponents target
let opponentTarget = await prisma.carStampTarget.findMany({
@ -157,9 +176,14 @@ export async function getOpponentsTarget(carId: number, registeredargetAvailable
stampTargetCarId: carId,
recommended: true,
},
orderBy:{
locked: 'desc'
},
orderBy: [
{
id: 'asc'
},
{
recommended: 'desc'
}
],
skip: random,
take: 1,
});
@ -205,6 +229,12 @@ export async function getOpponentsTarget(carId: number, registeredargetAvailable
result = Math.abs(carChallengers.result);
}
// Error handling if regionId is below 1 or above 47
if(carTarget!.regionId < 1 || carTarget!.regionId > 47)
{
carTarget!.regionId = Math.floor(Math.random() * 10) + 10;
}
// Push the data
challenger = wmproto.wm.protobuf.ChallengerCar.create({
car: carTarget,
@ -322,26 +352,33 @@ export async function createCar(body: wm.protobuf.CreateCarRequest)
}
}
// Randomize regionId
let regionId: number = 18;
// Randomize pick
let random: number = 1;
let randomArray: number[] = [];
// Randomize it 5 times
for(let i=0; i<5; i++)
{
regionId = Math.floor(Math.random() * 47) + 1;
while(randomArray.length < 5)
{
// Pick random car Id
random = Math.floor(Math.random() * 47 + 0.9) + 1;
// Try randomize it again if it's 0, and fix if more than car length
if(random < 1 || random > 47)
{
random = Math.floor(Math.random() * 47 + 0.9) + 1;
}
// Random Number not yet selected
if(randomArray.indexOf(random) === -1)
{
// Push current number to array
randomArray.push(random);
}
}
// Try randomize it again if it's 1
if(regionId === 1)
{
regionId = Math.floor(Math.random() * 47) + 1;
}
// Error handling if regionId is below 1 or above 47
if(regionId < 1 || regionId > 47)
{
regionId = Math.floor(Math.random() * 10) + 10;
}
// Pick the array number
let pickRandom = Math.floor(Math.random() * randomArray.length + 0.9);
random = randomArray[pickRandom];
// Default car values
let carInsert = {
@ -358,7 +395,7 @@ export async function createCar(body: wm.protobuf.CreateCarRequest)
carSettingsDbId: settings.dbId,
carStateDbId: state.dbId,
carGTWingDbId: gtWing.dbId,
regionId: regionId,
regionId: random,
lastPlayedAt: date,
lastPlayedPlaceId: 1, // Server Default
};

View File

@ -195,7 +195,6 @@ export async function saveOCMGhostHistory(body: wm.protobuf.SaveGameResultReques
where:{
carId: saveExGhostHistory.carId,
ocmMainDraw: saveExGhostHistory.ocmMainDraw,
area: saveExGhostHistory.area,
competitionId: ocmEventDate!.competitionId,
periodId: 0
}
@ -207,7 +206,7 @@ export async function saveOCMGhostHistory(body: wm.protobuf.SaveGameResultReques
if(countGBR)
{
// Check if the newest advantage distance is bigger than the older advantage distance
if(countGBR!.result < saveExGhostHistory.result)
if(countGBR.result < saveExGhostHistory.result)
{
console.log('OCM Ghost Tally found');
@ -217,7 +216,6 @@ export async function saveOCMGhostHistory(body: wm.protobuf.SaveGameResultReques
// Get OCM Period ID
let OCM_periodId = await prisma.oCMPeriod.findFirst({
where:{
competitionDbId: ocmEventDate!.dbId,
competitionId: ocmEventDate!.competitionId,
startAt:
{
@ -248,7 +246,6 @@ export async function saveOCMGhostHistory(body: wm.protobuf.SaveGameResultReques
let getGBR = await prisma.oCMGhostBattleRecord.findFirst({
where:{
carId: saveExGhostHistory.carId,
area: saveExGhostHistory.area,
competitionId: ocmEventDate!.competitionId,
}
});
@ -323,7 +320,7 @@ export async function saveOCMGhostHistory(body: wm.protobuf.SaveGameResultReques
else
{
console.log('OCM Ghost Battle Record not found');
console.log('Creating new OOCM Ghost Battle Record entry');
console.log('Creating new OCM Ghost Battle Record entry');
// Current date is OCM Main Draw
if(ocmEventDate!.competitionStartAt < date && ocmEventDate!.competitionCloseAt > date)