1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-11-18 08:50:59 -05:00
Daggerbot-TS/src/models/MPServer.ts

120 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-12-24 10:21:40 -05:00
import DatabaseServer from '../components/DatabaseServer.js';
import {Model, DataTypes} from 'sequelize';
import CacheServer from '../components/CacheServer.js';
2023-02-26 18:45:56 -05:00
2023-12-24 10:21:40 -05:00
class MPServer extends Model {
declare public serverName: string;
declare public isActive: boolean;
declare public ip: string;
declare public code: string;
declare public playerData: number[];
}
2023-02-26 18:45:56 -05:00
2023-12-24 10:21:40 -05:00
export interface IServer {
serverName: string
isActive: boolean
ip: string
code: string
playerData: number[]
}
const cacheKey = 'MPServer';
export class MPServerSvc {
private model: typeof MPServer;
constructor() {
this.model = MPServer;
this.model.init({
serverName: {
type: DataTypes.STRING,
allowNull: false,
primaryKey: true,
},
isActive: {
type: DataTypes.BOOLEAN,
allowNull: false
},
ip: {
type: DataTypes.STRING,
allowNull: false,
},
code: {
type: DataTypes.STRING,
allowNull: false,
},
playerData: {
type: DataTypes.ARRAY(DataTypes.INTEGER),
allowNull: true,
2023-10-02 18:40:03 -04:00
}
2023-12-24 10:21:40 -05:00
}, {
tableName: 'mpserver',
createdAt: false,
updatedAt: false,
sequelize: DatabaseServer.seq,
})
this.model.sync();
}
2024-02-07 15:36:37 -05:00
query = async(pattern:string)=>await this.model.sequelize.query(pattern);
2023-12-24 10:21:40 -05:00
async fetchPlayerData(serverName:string) {
const findServerByName = await this.model.findOne({where: {serverName: serverName}});
if (findServerByName) return findServerByName.dataValues.playerData;
else return [];
}
async addServer(serverName:string, ip:string, code:string) {
const findServerByName = await this.model.findOne({where: {serverName: serverName}});
if (findServerByName) {
(await findServerByName.update({serverName: serverName, ip: ip, code: code})).save();
await CacheServer.delete(cacheKey).then(async()=>await this.findInCache());
2023-10-02 18:40:03 -04:00
} else {
2023-12-24 10:21:40 -05:00
await this.model.create({
serverName: serverName,
isActive: true,
ip: ip,
code: code,
playerData: []
});
await CacheServer.delete(cacheKey).then(async()=>await this.findInCache());
}
}
async removeServer(serverName:string) {
const findServerByName = await this.model.findOne({where: {serverName: serverName}});
if (findServerByName) {
await this.model.destroy({where: {serverName: serverName}});
await CacheServer.delete(cacheKey).then(async()=>await this.findInCache());
}
}
async toggleServerUsability(serverName:string, isActive:boolean) {
const findServerByName = await this.model.findOne({where: {serverName: serverName}});
if (findServerByName) {
this.model.update({isActive: isActive}, {where: {serverName: serverName}}).then(async flagUpdated=>{
if (flagUpdated) {
await CacheServer.delete(cacheKey).then(async()=>await this.findInCache());
return true;
}
});
} else return false;
}
async incrementPlayerCount(serverName:string, playerCount:number) {
const findServerByName = await this.model.findOne({where: {serverName: serverName}});
if (findServerByName) {
let PD = findServerByName.dataValues.playerData;
2023-12-28 10:43:04 -05:00
if (PD.length > 1920) PD = []; //Selfnote: 86400/45 = 1920, where 86400 is seconds in a day and 45 is the MPModule's refresh interval.
2023-12-24 10:21:40 -05:00
PD.push(playerCount);
const updatePD = await this.model.update({playerData: PD}, {where: {serverName: serverName}});
if (updatePD) true;
else return false;
} else return false;
}
async findInCache(): Promise<IServer[]> {
2023-12-26 17:46:08 -05:00
const cachedResult = await CacheServer.get(cacheKey, true);
2023-12-24 10:21:40 -05:00
let result;
if (cachedResult) result = cachedResult;
else {
result = await this.model.findAll();
2023-12-26 17:46:08 -05:00
CacheServer.set(cacheKey, result, true).then(()=>CacheServer.expiry(cacheKey, 1800));
2023-10-02 18:40:03 -04:00
}
return result;
}
}