mirror of
https://github.com/shiroikitsu8/Bayshore_6r_legacy.git
synced 2024-11-28 09:20:54 +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:
commit
97db501f3f
236
src/api.ts
Normal file
236
src/api.ts
Normal 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);
|
||||
});
|
||||
}
|
||||
}
|
20
src/index.ts
20
src/index.ts
@ -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,6 +116,7 @@ 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)
|
||||
@ -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}!`);
|
||||
})
|
||||
|
@ -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;
|
||||
|
@ -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,15 +223,14 @@ 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);
|
||||
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
|
||||
|
||||
|
@ -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
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -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,29 +607,46 @@ export default class GhostModule extends Module {
|
||||
carId: checkGhostTrail!.carId
|
||||
},
|
||||
include:{
|
||||
gtWing: true,
|
||||
lastPlayedPlace: true
|
||||
lastPlayedPlace: true,
|
||||
gtWing: true
|
||||
}
|
||||
});
|
||||
|
||||
if(cars)
|
||||
{
|
||||
// If regionId is 0 or not set, game will crash after defeating the ghost
|
||||
if(cars!.regionId === 0)
|
||||
if(cars.regionId === 0)
|
||||
{
|
||||
let randomRegionId = Math.floor(Math.random() * 47) + 1;
|
||||
cars!.regionId = randomRegionId;
|
||||
cars.regionId = randomRegionId;
|
||||
}
|
||||
|
||||
// Set the tunePower used when playing ghost crown
|
||||
cars!.tunePower = ocmTallyRecord!.tunePower;
|
||||
cars.tunePower = ocmTallyRecord!.tunePower;
|
||||
|
||||
// Set the tuneHandling used when playing ghost crown
|
||||
cars!.tuneHandling = ocmTallyRecord!.tuneHandling;
|
||||
cars.tuneHandling = ocmTallyRecord!.tuneHandling;
|
||||
|
||||
// Set Ghost stuff Value
|
||||
cars!.lastPlayedAt = checkGhostTrail.playedAt
|
||||
cars.lastPlayedAt = checkGhostTrail.playedAt;
|
||||
ghostTrailId = checkGhostTrail.dbId!;
|
||||
ghostTypes = wm.wm.protobuf.GhostType.GHOST_NORMAL;
|
||||
|
||||
let checkShopName = await prisma.oCMGhostBattleRecord.findFirst({
|
||||
where:{
|
||||
carId: checkGhostTrail!.carId,
|
||||
competitionId: competition_id
|
||||
},
|
||||
select:{
|
||||
playedShopName: true
|
||||
}
|
||||
})
|
||||
|
||||
if(checkShopName)
|
||||
{
|
||||
cars.lastPlayedPlace!.shopName = checkShopName.playedShopName;
|
||||
}
|
||||
|
||||
let ocmEventDate = await prisma.oCMEvent.findFirst({
|
||||
where: {
|
||||
competitionId: competition_id
|
||||
@ -674,6 +691,7 @@ export default class GhostModule extends Module {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Push the Top 1 OCM ghost car data
|
||||
ghostCars = wm.wm.protobuf.GhostCar.create({
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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'
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user