1
0
mirror of synced 2024-12-11 22:46:03 +01:00

Merge pull request #42 from ghkkk090/master

API for ocm ranking (still alpha), FIX ghost play count ocm, unlimited ghost stamp bug, give meter reward bug, etc
This commit is contained in:
Luna 2022-09-14 12:58:25 +01:00 committed by GitHub
commit 97db501f3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 420 additions and 81 deletions

236
src/api.ts Normal file
View File

@ -0,0 +1,236 @@
import express, { Application } from "express";
import { prisma } from ".";
import { Module } from "./module";
export default class ApiModule extends Module {
register(app: Application): void {
app.use(express.urlencoded({
type: '*/*',
extended: true
}));
app.use(express.json({
type: '*/*'
}));
// API Get Requests
// Get Current Bayshore Version
app.get('/api/bayshore_version', async (req, res) => {
let message: any = {
error: null,
version: null
};
let myJSON = '{'+
'"version": "v1.0.0",'+
'"log":'+
'['+
'"• Fix ghost play count when retiring ocm",'+
'"• API for ocm ranking",'+
'"• Fix unlimited ghost stamp return (hopefully no more of this)",'+
'"• Fix give meter reward bug if playCount still 0",'+
'"• Hopefully fix ocm HoF bug"'+
'"• Fix duplicate id in carOrder"'+
'"• Fix OCM HoF wrong shopName"'+
']'+
'}';
message.version = JSON.parse(myJSON);
// Send the response to the client
res.send(message);
})
// Post Login
app.post('/api/login', async (req, res) => {
// Get the request body
let query = req.query;
// Message Response
let message: any = {
error: null,
user: null
};
// Get the user from the database
let user = await prisma.user.findFirst({
where: {
chipId: {
startsWith: query.cardChipId?.toString()
},
accessCode: query.accessCode?.toString()
},
include: {
cars: {
select: {
state: true,
gtWing: true,
lastPlayedPlace: true,
carId: true,
name: true,
defaultColor: true,
visualModel: true,
level: true,
title: true,
regionId: true,
}
}
}
});
if(user)
{
message.user = user;
}
else
{
message.error = 404
}
// Send the response to the client
res.send(message);
})
// Get Current Competition Id
app.get('/api/get_competition_id', async (req, res) => {
// Get current date
let date = Math.floor(new Date().getTime() / 1000);
// Message Response
let message: any = {
error: null,
competitionId: 1 // default
};
// 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',
},
],
select:{
competitionId: true
}
});
if(ocmEventDate)
{
message.competitionId = ocmEventDate.competitionId;
}
else{
ocmEventDate = await prisma.oCMEvent.findFirst({
orderBy: {
dbId: 'desc'
},
select:{
competitionId: true
}
});
message.competitionId = ocmEventDate!.competitionId;
}
// Send the response to the client
res.send(message);
});
// Get Current Competition Id
app.get('/api/get_hof_competition_id', async (req, res) => {
// Message Response
let message: any = {
error: null,
competitionId: 1 // default
};
// Get current / previous active OCM Event
let ocmEventDate = await prisma.oCMTally.findFirst({
where:{
periodId: 999999999
},
orderBy: {
competitionId: 'desc'
},
select:{
competitionId: true
}
});
if(ocmEventDate)
{
message.competitionId = ocmEventDate.competitionId;
}
// Send the response to the client
res.send(message);
});
// Get Competition Ranking
app.get('/api/get_competition_ranking', async (req, res) => {
// Get url query
let competitionId = Number(req.query.competitionId);
// Message Response
let message: any = {
error: null,
cars: [],
lastPlayedPlace: 'Bayshore'
};
// Get all of the cars matching the query
message.cars = await prisma.oCMTally.findMany({
where:{
competitionId: competitionId
},
orderBy: {
result: 'desc'
},
include:{
car: {
select:{
carId: true,
name: true,
defaultColor: true,
visualModel: true,
level: true,
title: true,
regionId: true,
}
},
}
});
let getLastPlayedPlace = await prisma.oCMGhostBattleRecord.findFirst({
where:{
carId: message.cars[0].carId,
competitionId: competitionId
}
})
message.lastPlayedPlace = getLastPlayedPlace?.playedShopName;
// Send the response to the client
res.send(message);
});
}
}

View File

@ -9,6 +9,7 @@ import fs from 'fs';
import bodyParser from 'body-parser';
import AllnetModule from './allnet';
import MuchaModule from './mucha';
import ApiModule from './api';
import { Config } from './config';
import process from 'process';
import * as Sentry from '@sentry/node';
@ -30,8 +31,13 @@ const appRouter = Router();
const PORT_ALLNET = 80;
const PORT_MUCHA = 10082;
const PORT_BNGI = 9002;
const PORT_API = 9003;
const app = express();
const muchaApp = express();
const allnetApp = express();
const apiApp = express();
app.use(bodyParser.raw({
type: '*/*'
}));
@ -51,9 +57,6 @@ if (useSentry) {
});
}
const muchaApp = express();
const allnetApp = express();
// Get the current timestamp
let timestamp: string = common.getTimeStamp();
@ -77,6 +80,11 @@ allnetApp.use((req, res, next) => {
next()
});
/*apiApp.use((req, res, next) => {
console.log(timestamp+` [ API] ${req.method} ${req.url}`);
next()
});*/
// Get all of the files in the modules directory
let dirs = fs.readdirSync('dist/modules');
// Loop over the files
@ -108,11 +116,12 @@ app.all('*', (req, res) => {
// Register the ALL.NET / Mucha Server
new AllnetModule().register(allnetApp);
new MuchaModule().register(muchaApp);
new ApiModule().register(apiApp);
// Sentry is in use
if (useSentry)
{
// Use the sentry error handler
// Use the sentry error handler
app.use(Sentry.Handlers.errorHandler());
}
@ -141,3 +150,8 @@ https.createServer({key, cert}, muchaApp).listen(PORT_MUCHA, '0.0.0.0', 511, ()
https.createServer({key, cert}, app).listen(PORT_BNGI, '0.0.0.0', 511, () => {
console.log(`Game server listening on port ${PORT_BNGI}!`);
})
// Create the API server
http.createServer(apiApp).listen(PORT_API, '0.0.0.0', 4369, () => {
console.log(`API server listening on port ${PORT_API}!`);
})

View File

@ -37,10 +37,12 @@ export default class CarModule extends Module {
});
// Error handling if ghostLevel accidentally set to 0 or more than 10
if(car!.ghostLevel < 1){
if(car!.ghostLevel < 1)
{
car!.ghostLevel = 1;
}
if(car!.ghostLevel > 11){
if(car!.ghostLevel > 11)
{
car!.ghostLevel = 10;
}
@ -160,7 +162,37 @@ export default class CarModule extends Module {
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
});
}
}
}
}
}
@ -588,7 +620,6 @@ export default class CarModule extends Module {
// Get the request body for the update car request
let body = wm.wm.protobuf.UpdateCarRequest.decode(req.body);
console.log(body);
// Get the ghost result for the car
let cars = body?.car;

View File

@ -193,7 +193,7 @@ export default class GameModule extends Module {
let giveMeterReward = Config.getConfig().gameOptions.giveMeterReward;
// Check if this feature activated and check if user's play count is n*100 play
if(giveMeterReward === 1 && body.playCount % 100 === 0)
if(giveMeterReward === 1 && body.playCount % 100 === 0 && body.playCount !== 0)
{
// Calling give meter reward function (BASE_PATH/src/util/meter_reward.ts)
await meter_reward.giveMeterRewards(body);
@ -223,16 +223,15 @@ export default class GameModule extends Module {
// Get the index of the selected car
let index = carOrder.indexOf(body.carId);
// If the selected car is not first
if (index > 0)
// Only splice array when item is found
if (index > -1)
{
// Remove that index from the array
carOrder.slice(index);
// Add it back to the front
carOrder.unshift(body.carId);
carOrder.splice(index, 1); // 2nd parameter means remove one item only
}
// Add it back to the front
carOrder.unshift(body.carId);
// Otherwise, just ignore it
// Update the values

View File

@ -110,10 +110,29 @@ export default class GhostModule extends Module {
}
}
// Get all of the friend cars for the carId provided
// Get all of the challenger car for the carId provided except beated car
let checkBeatedCar = await prisma.carStampTarget.findMany({
where: {
stampTargetCarId: body.carId,
recommended: false
},
orderBy:{
carId: 'asc'
}
});
let arrChallengerCarId = [];
for(let i=0; i<checkBeatedCar.length; i++)
{
arrChallengerCarId.push(checkBeatedCar[i].carId);
}
let challengers = await prisma.carChallenger.findMany({
where: {
carId: body.carId
carId: body.carId,
NOT: {
challengerCarId: { in: arrChallengerCarId }, // Except beated challenger id
},
}
});

View File

@ -432,7 +432,7 @@ export default class GhostModule extends Module {
// Declare variable for Top 1 OCM Ghost
let ghostCars: wm.wm.protobuf.GhostCar;
let ghostTypes;
let cars: wm.wm.protobuf.ICar | (Car & { gtWing: CarGTWing; }) | null;
let cars: wm.wm.protobuf.ICar | null;
let playedPlace = wm.wm.protobuf.Place.create({
placeId: Config.getConfig().placeId,
regionId: Config.getConfig().regionId,
@ -607,70 +607,88 @@ export default class GhostModule extends Module {
carId: checkGhostTrail!.carId
},
include:{
gtWing: true,
lastPlayedPlace: true
lastPlayedPlace: true,
gtWing: true
}
});
// If regionId is 0 or not set, game will crash after defeating the ghost
if(cars!.regionId === 0)
if(cars)
{
let randomRegionId = Math.floor(Math.random() * 47) + 1;
cars!.regionId = randomRegionId;
}
// Set the tunePower used when playing ghost crown
cars!.tunePower = ocmTallyRecord!.tunePower;
// Set the tuneHandling used when playing ghost crown
cars!.tuneHandling = ocmTallyRecord!.tuneHandling;
// Set Ghost stuff Value
cars!.lastPlayedAt = checkGhostTrail.playedAt
ghostTrailId = checkGhostTrail.dbId!;
ghostTypes = wm.wm.protobuf.GhostType.GHOST_NORMAL;
let ocmEventDate = await prisma.oCMEvent.findFirst({
where: {
competitionId: competition_id
// If regionId is 0 or not set, game will crash after defeating the ghost
if(cars.regionId === 0)
{
let randomRegionId = Math.floor(Math.random() * 47) + 1;
cars.regionId = randomRegionId;
}
});
if(ocmEventDate)
{
// Creating GhostCompetitionSchedule
competitionSchedule = wm.wm.protobuf.GhostCompetitionSchedule.create({
// Set the tunePower used when playing ghost crown
cars.tunePower = ocmTallyRecord!.tunePower;
// OCM Competition ID (1 = C1 (Round 16), 4 = Nagoya (Round 19), 8 = Hiroshima (Round 21))
competitionId: ocmEventDate.competitionId,
// Set the tuneHandling used when playing ghost crown
cars.tuneHandling = ocmTallyRecord!.tuneHandling;
// OCM Qualifying Start Timestamp
qualifyingPeriodStartAt: ocmEventDate.qualifyingPeriodStartAt,
// Set Ghost stuff Value
cars.lastPlayedAt = checkGhostTrail.playedAt;
ghostTrailId = checkGhostTrail.dbId!;
ghostTypes = wm.wm.protobuf.GhostType.GHOST_NORMAL;
// OCM Qualifying Close Timestamp
qualifyingPeriodCloseAt: ocmEventDate.qualifyingPeriodCloseAt,
let checkShopName = await prisma.oCMGhostBattleRecord.findFirst({
where:{
carId: checkGhostTrail!.carId,
competitionId: competition_id
},
select:{
playedShopName: true
}
})
// OCM Competition (Main Draw) Start Timestamp
competitionStartAt: ocmEventDate.competitionStartAt,
if(checkShopName)
{
cars.lastPlayedPlace!.shopName = checkShopName.playedShopName;
}
// OCM Competition (Main Draw) Close Timestamp
competitionCloseAt: ocmEventDate.competitionCloseAt,
// OCM Competition (Main Draw) End Timestamp
competitionEndAt: ocmEventDate.competitionEndAt,
// idk what this is
lengthOfPeriod: ocmEventDate.lengthOfPeriod,
// idk what this is
lengthOfInterval: ocmEventDate.lengthOfInterval,
// Area for the event (GID_RUNAREA_*, 8 is GID_RUNAREA_NAGOYA)
area: ocmEventDate.area,
// idk what this is
minigamePatternId: ocmEventDate.minigamePatternId
let ocmEventDate = await prisma.oCMEvent.findFirst({
where: {
competitionId: competition_id
}
});
if(ocmEventDate)
{
// Creating GhostCompetitionSchedule
competitionSchedule = wm.wm.protobuf.GhostCompetitionSchedule.create({
// OCM Competition ID (1 = C1 (Round 16), 4 = Nagoya (Round 19), 8 = Hiroshima (Round 21))
competitionId: ocmEventDate.competitionId,
// OCM Qualifying Start Timestamp
qualifyingPeriodStartAt: ocmEventDate.qualifyingPeriodStartAt,
// OCM Qualifying Close Timestamp
qualifyingPeriodCloseAt: ocmEventDate.qualifyingPeriodCloseAt,
// OCM Competition (Main Draw) Start Timestamp
competitionStartAt: ocmEventDate.competitionStartAt,
// OCM Competition (Main Draw) Close Timestamp
competitionCloseAt: ocmEventDate.competitionCloseAt,
// OCM Competition (Main Draw) End Timestamp
competitionEndAt: ocmEventDate.competitionEndAt,
// idk what this is
lengthOfPeriod: ocmEventDate.lengthOfPeriod,
// idk what this is
lengthOfInterval: ocmEventDate.lengthOfInterval,
// Area for the event (GID_RUNAREA_*, 8 is GID_RUNAREA_NAGOYA)
area: ocmEventDate.area,
// idk what this is
minigamePatternId: ocmEventDate.minigamePatternId
});
}
}
}
}

View File

@ -990,7 +990,8 @@ export default class TerminalModule extends Module {
// Unlock all of the stamp targets for the car
await prisma.carStampTarget.updateMany({
where: {
stampTargetCarId: body.carId
stampTargetCarId: body.carId,
recommended: true
},
data: {
locked: false
@ -1007,7 +1008,8 @@ export default class TerminalModule extends Module {
await prisma.carStampTarget.updateMany({
where: {
carId: targetCar,
stampTargetCarId: body.carId
stampTargetCarId: body.carId,
recommended: true
},
data: {
locked: true

View File

@ -487,7 +487,8 @@ export default class UserModule extends Module {
{
let checkRegisteredGhost = await prisma.ghostRegisteredFromTerminal.findFirst({
where:{
carId: user.cars[i].carId
carId: user.cars[i].carId,
competitionId: ocmEventDate.competitionId
}
});
@ -495,6 +496,10 @@ export default class UserModule extends Module {
{
carStates[i].hasOpponentGhost = true;
}
else
{
carStates[i].hasOpponentGhost = false;
}
}
}
}

View File

@ -581,6 +581,21 @@ export async function saveGhostBattleResult(body: wm.protobuf.SaveGameResultRequ
}
}
}
// Ghost update data
let dataGhost = {
rgPlayCount: common.sanitizeInput(body.rgResult!.rgPlayCount),
}
// Update the car properties
await prisma.car.update({
where: {
carId: body.carId
},
data: {
...dataGhost
}
});
}
// Return the value to 'BASE_PATH/src/modules/game.ts'

View File

@ -300,11 +300,11 @@ export async function ocmTallying(body: wm.protobuf.LoadGhostCompetitionInfoRequ
// Get the Top 1 Advantage
if(top1advantage === null)
{
top1advantage = OCMTally[i].result;
top1advantage = OCMTally[0].result;
let getTrail = await prisma.oCMGhostTrail.findFirst({
where:{
carId: OCMTally[i].carId,
carId: OCMTally[0].carId,
competitionId: body.competitionId,
ocmMainDraw: true
}