Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions prisma/migrations/20250608120100_item_check/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:

- You are about to drop the column `type` on the `UserItem` table. All the data in the column will be lost.

*/
-- AlterTable
ALTER TABLE "CarItem" ADD COLUMN "earnedAt" INTEGER NOT NULL DEFAULT 0;

-- AlterTable
ALTER TABLE "UserItem" DROP COLUMN "type";
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ model UserItem {
itemId Int
User User @relation(fields: [userId], references: [id])
userId Int
type Int @default(0)
earnedAt Int @default(0)
}

Expand Down Expand Up @@ -174,6 +173,7 @@ model CarItem {
category Int
itemId Int
amount Int
earnedAt Int @default(0)
}

model CarSettings {
Expand Down
28 changes: 20 additions & 8 deletions src/modules/cars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,26 @@ export default class CarModule {
{
console.log('Car Item reward available, continuing ...');
for(let i=0; i<body.earnedItems.length; i++){
await prisma.carItem.create({
data: {
carId: body.carId,
category: body.earnedItems[i].category,
itemId: body.earnedItems[i].itemId,
amount: 1
}
});
let itemCheck = await prisma.carItem.findFirst({
where: {
carId: body.carId,
category: body.earnedItems[i].category,
itemId: body.earnedItems[i].itemId,
amount: 1
}
});

if (!itemCheck){
await prisma.carItem.create({
data: {
carId: body.carId,
category: body.earnedItems[i].category,
itemId: body.earnedItems[i].itemId,
amount: 1,
earnedAt: Math.floor(new Date().getTime() / 1000)
}
});
}
}
}
}
Expand Down
48 changes: 34 additions & 14 deletions src/modules/game/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,43 @@ export async function getItem(body: wm.protobuf.SaveGameResultRequest)
{
console.log('Car Item reward available, continuing ...');

for(let i=0; i<body.earnedItems.length; i++){
await prisma.carItem.create({
data: {
for (let i = 0; i < body.earnedItems.length; i++) {
let { category, itemId } = body.earnedItems[i];

// Find all matching items (not just first)
let existingItems = await prisma.carItem.findMany({
where: {
carId: body.carId,
category: body.earnedItems[i].category,
itemId: body.earnedItems[i].itemId,
amount: 1
category,
itemId
},
orderBy: {
dbId: 'asc' // Keep the oldest one
}
});

if (existingItems.length === 0) {
// Create new item if none exists
await prisma.carItem.create({
data: {
carId: body.carId,
category,
itemId,
amount: 1,
earnedAt: Math.floor(new Date().getTime() / 1000)
}
});
} else {
// If duplicates exist, delete all except the first
if (existingItems.length > 1) {
let duplicateIds = existingItems.slice(1).map(item => item.dbId);
await prisma.carItem.deleteMany({
where: {
dbId: { in: duplicateIds }
}
});
}
}
}
}

Expand All @@ -39,21 +67,13 @@ export async function getItem(body: wm.protobuf.SaveGameResultRequest)
console.log('User Item reward available, continuing ...');

for(let i=0; i<body.earnedUserItems.length; i++){
let type = 0;

if (body.earnedUserItems[i].category == 201) {
type = 1
} else {
type = 0
};

await prisma.userItem.create({
data: {
category: body.earnedUserItems[i].category,
itemId: body.earnedUserItems[i].itemId,
userId: body.car!.userId!,
earnedAt: 0,
type: type
}
});
}
Expand Down
97 changes: 49 additions & 48 deletions src/modules/ghost/ghost_history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,68 +350,69 @@ export async function saveOCMGhostHistory(body: wm.protobuf.SaveGameResultReques
let OCM_periodId = await prisma.oCMPeriod.findFirst({
where:{
competitionId: ocmEventDate!.competitionId,
},
orderBy:{
periodId: 'desc'
startAt:
{
lte: date - ocmEventDate!.lengthOfInterval, // competitionStartAt is less than current date
},
closeAt:
{
gte: date - ocmEventDate!.lengthOfInterval, // competitionCloseAt is greater than current date
}
},
select:{
periodId: true
}
});

// Period ID not found
if(!(OCM_periodId))
if (OCM_periodId)
{
OCM_periodId = await prisma.oCMPeriod.findFirst({
where:{
// Update ghost battle record
await prisma.oCMGhostBattleRecord.create({
data: {
...saveExGhostHistory,
competitionId: ocmEventDate!.competitionId,
startAt:
{
lte: date - ocmEventDate!.lengthOfInterval, // competitionStartAt is less than current date
},
closeAt:
{
gte: date - ocmEventDate!.lengthOfInterval, // competitionCloseAt is greater than current date
}
},
select:{
periodId: true
periodId: OCM_periodId!.periodId
}
});
}

// Update ghost battle record
await prisma.oCMGhostBattleRecord.create({
data: {
...saveExGhostHistory,
competitionId: ocmEventDate!.competitionId,
periodId: OCM_periodId!.periodId
}
});

let ocmTallyfind = await prisma.oCMTally.findFirst({
where:{
carId: saveExGhostHistory.carId,
competitionId: ocmEventDate!.competitionId,
},
});

if(ocmTallyfind)
{
console.log('Updating OCM Tally Record');

// Update the OCM Tally Record
await prisma.oCMTally.update({
let ocmTallyfind = await prisma.oCMTally.findFirst({
where:{
dbId: ocmTallyfind.dbId
carId: saveExGhostHistory.carId,
competitionId: ocmEventDate!.competitionId,
},
data: {
periodId: OCM_periodId!.periodId,
result: body.rgResult!.opponents![0].result,
tunePower: body.car?.tunePower!,
tuneHandling: body.car?.tuneHandling!
}
});

if(ocmTallyfind)
{
console.log('Updating OCM Tally Record');

// Update the OCM Tally Record
await prisma.oCMTally.update({
where:{
dbId: ocmTallyfind.dbId
},
data: {
periodId: OCM_periodId!.periodId,
result: body.rgResult!.opponents![0].result,
tunePower: body.car?.tunePower!,
tuneHandling: body.car?.tuneHandling!
}
});
}
else
{
// Create the OCM Tally Record
await prisma.oCMTally.create({
data: {
carId: body.car?.carId!,
competitionId: ocmEventDate!.competitionId,
periodId: OCM_periodId!.periodId,
result: body.rgResult!.opponents![0].result,
tunePower: body.car?.tunePower!,
tuneHandling: body.car?.tuneHandling!
}
});
}
}
}
// Current date is OCM Qualifying
Expand Down
Loading