add missing files

This commit is contained in:
Aleksander Wilczyński 2024-08-16 12:58:53 +02:00
parent fec84ae109
commit 873a38768e
Signed by untrusted user: alekswilc
GPG Key ID: D4464A248E5F27FE
5 changed files with 297 additions and 0 deletions

10
scripts/clear-redis.js Normal file
View File

@ -0,0 +1,10 @@
import { createClient } from 'redis';
createClient({url: process.env.REDIS_URI}).connect().then(async (x) => {
await x.json.del('stations_occupied');
await x.json.del('stations');
await x.json.del('trains');
await x.json.del('trains_occupied');
console.log('done')
process.exit(1);
})

47
src/modules/trains.ts Normal file
View File

@ -0,0 +1,47 @@
import { Server, Station, Train } from '@simrail/types';
import { MLog } from '../mongo/logs.js';
import { IPlayer } from '../types/player.js';
import { SimrailClientEvents } from '../util/SimrailClient.js';
import { v4 } from 'uuid';
import { getVehicle } from '../util/contants.js';
import { MProfile } from '../mongo/profile.js';
import { MTrainLog } from '../mongo/trainLogs.js';
export class TrainsModule {
public static load() {
client.on(SimrailClientEvents.TrainLeft, async (server: Server, train: Train, player: IPlayer, joinedAt: number, leftAt: number, points: number, distance: number, vehicle: string) => {
if (distance) {
const userProfile = await MProfile.findOne({ steam: player.steamid }) ?? await MProfile.create({ steam: player.steamid, id: v4() });
const vehicleName = getVehicle(vehicle) ?? vehicle;
if (!userProfile.trainStats) userProfile.trainStats = {};
if (userProfile.trainStats[vehicleName]) {
userProfile.trainStats[vehicleName].distance = userProfile.trainStats[vehicleName].distance + distance;
userProfile.trainStats[vehicleName].score = userProfile.trainStats[vehicleName].score + points;
} else {
userProfile.trainStats[vehicleName] = {
distance, score: points
}
}
await MProfile.findOneAndUpdate({ id: userProfile.id }, { trainStats: userProfile.trainStats })
}
MTrainLog.create({
id: v4(),
userSteamId: player.steamid,
userAvatar: player.avatarfull,
userUsername: player.personaname,
joinedDate: joinedAt,
leftDate: leftAt,
trainNumber: train.TrainNoLocal,
server: server.ServerCode,
distance, points
});
})
}
}

46
src/mongo/profile.ts Normal file
View File

@ -0,0 +1,46 @@
import { Model, model, Schema } from 'mongoose';
export const raw_schema = {
id: {
type: String,
required: true
},
steam: {
type: String,
required: true
},
trainStats: {
type: Object,
required: false,
default: {}
},
dispatcherStats: {
type: Object,
required: false,
default: {}
},
}
const schema = new Schema<IProfile>(raw_schema);
export type TMProfile = Model<IProfile>
export const MProfile = model<IProfile>('profile', schema);
export interface IProfile {
id: string
steam: string
trainStats: {
[trainName: string]: {
score: number,
distance: number
}
}
dispatcherStats: {
[name: string]: {
time: number
}
}
}

67
src/mongo/trainLogs.ts Normal file
View File

@ -0,0 +1,67 @@
import { Model, model, Schema } from 'mongoose';
export const raw_schema = {
id: {
type: String,
required: true
},
trainNumber: {
type: String,
required: true
},
userSteamId: {
type: String,
required: true
},
userUsername: {
type: String,
required: true
},
userAvatar: {
type: String,
required: true
},
joinedDate: {
type: Number,
required: false,
default: undefined
},
leftDate: {
type: Number,
required: true
},
distance: {
type: Number,
required: false,
default: 0
},
points: {
type: Number,
required: false,
default: 0
},
server: {
type: String,
required: true
},
}
const schema = new Schema<ITrainLog>(raw_schema);
export type TMTrainLog = Model<ITrainLog>
export const MTrainLog = model<ITrainLog>('train_logs', schema);
export interface ITrainLog {
id: string
userSteamId: string
userUsername: string
userAvatar: string
joinedDate?: number
leftDate: number
trainNumber: string
distance: number
points: number
server: string
}

127
src/util/contants.ts Normal file
View File

@ -0,0 +1,127 @@
import wcmatch from 'wildcard-match'
/*
E186_134 = "Traxx/E186-134",
E186_929 = "Traxx/E186-929",
E6ACTa_014 = "Dragon2/E6ACTa-014",
E6ACTa_016 = "Dragon2/E6ACTa-016",
E6ACTadb_027 = "Dragon2/E6ACTadb-027",
ED250_018 = "Pendolino/ED250-018 Variant",
EN57_009 = "EN57/EN57-009",
EN57_047 = "EN57/EN57-047",
EN57_1000 = "EN57/EN57-1000",
EN57_1003 = "EN57/EN57-1003",
EN57_1051 = "EN57/EN57-1051",
EN57_1219 = "EN57/EN57-1219",
EN57_1316 = "EN57/EN57-1316",
EN57_1458 = "EN57/EN57-1458",
EN57_1567 = "EN57/EN57-1567",
EN57_1571 = "EN57/EN57-1571",
EN57_1752 = "EN57/EN57-1752",
EN57_1755 = "EN57/EN57-1755",
EN57_1796 = "EN57/EN57-1796",
EN57_1821 = "EN57/EN57-1821",
EN57_614 = "EN57/EN57-614",
EN71_005 = "EN57/EN71-005",
EN71_011 = "EN57/EN71-011",
EN76_006 = "Elf/EN76-006",
EN76_022 = "Elf/EN76-022",
EN96_001 = "Elf/EN96-001",
EP07_135 = "4E/EP07-135",
EP07_174 = "4E/EP07-174",
EP08_001 = "4E/EP08-001",
EP08_013 = "4E/EP08-013",
ET22_1163 = "201E/ET22-1163", -- DLC required: 2868050
ET22_243 = "201E/ET22-243", -- DLC required: 2868050
ET22_256 = "201E/ET22-256", -- DLC required: 2868050
ET22_644 = "201E/ET22-644", -- DLC required: 2868050
ET22_836 = "201E/ET22-836", -- DLC required: 2868050
ET22_911 = "201E/ET22-911", -- DLC required: 2868050
ET25_002 = "Dragon2/ET25-002",
EU07_005 = "4E/EU07-005",
EU07_068 = "4E/EU07-068",
EU07_085 = "4E/EU07-085",
EU07_092 = "4E/EU07-092",
EU07_096 = "4E/EU07-096",
EU07_241 = "4E/EU07-241",
*/
export const trainsList = [
{
train: 'Traxx (E186)',
pattern: [
'Traxx/E186-*',
]
},
{
train: 'Dragon2 (E6ACTa, E6ACTadb)',
pattern: [
'Dragon2/E6ACTa-*',
'Dragon2/E6ACTadb-*'
]
},
{
train: 'Dragon2 (ET25)',
pattern: [
'Dragon2/ET25-*',
]
},
{
train: 'Pendolino (ED250)',
pattern: [
'Pendolino/ED250-*',
]
},
{
train: 'EN57',
pattern: [
'EN57/EN57-*',
]
},
{
train: 'EN71',
pattern: [
'EN57/EN71-*',
]
},
{
train: 'EN96',
pattern: [
'Elf/EN76-*',
]
},
{
train: 'EP07',
pattern: [
'4E/EP07-*',
]
},
{
train: 'EP08',
pattern: [
'4E/EP08-*',
]
},
{
train: 'ET22',
pattern: [
'201E/ET22-*',
]
},
{
train: 'EU07',
pattern: [
'4E/EU07-*',
]
}
]
export const getVehicle = (name: string) => {
return trainsList.find(x => wcmatch(x.pattern)(name))?.train;
};