From 873a38768ec4e3fe28ff5c5b21476e7c7b95a945 Mon Sep 17 00:00:00 2001 From: alekswilc Date: Fri, 16 Aug 2024 12:58:53 +0200 Subject: [PATCH] add missing files --- scripts/clear-redis.js | 10 ++++ src/modules/trains.ts | 47 +++++++++++++++ src/mongo/profile.ts | 46 +++++++++++++++ src/mongo/trainLogs.ts | 67 ++++++++++++++++++++++ src/util/contants.ts | 127 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 297 insertions(+) create mode 100644 scripts/clear-redis.js create mode 100644 src/modules/trains.ts create mode 100644 src/mongo/profile.ts create mode 100644 src/mongo/trainLogs.ts create mode 100644 src/util/contants.ts diff --git a/scripts/clear-redis.js b/scripts/clear-redis.js new file mode 100644 index 0000000..695ab4f --- /dev/null +++ b/scripts/clear-redis.js @@ -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); +}) \ No newline at end of file diff --git a/src/modules/trains.ts b/src/modules/trains.ts new file mode 100644 index 0000000..1e6594d --- /dev/null +++ b/src/modules/trains.ts @@ -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 + }); + }) + } +} \ No newline at end of file diff --git a/src/mongo/profile.ts b/src/mongo/profile.ts new file mode 100644 index 0000000..5ec02e8 --- /dev/null +++ b/src/mongo/profile.ts @@ -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(raw_schema); + + +export type TMProfile = Model + +export const MProfile = model('profile', schema); + +export interface IProfile { + id: string + steam: string + trainStats: { + [trainName: string]: { + score: number, + distance: number + } + } + dispatcherStats: { + [name: string]: { + time: number + } + } +} \ No newline at end of file diff --git a/src/mongo/trainLogs.ts b/src/mongo/trainLogs.ts new file mode 100644 index 0000000..0362446 --- /dev/null +++ b/src/mongo/trainLogs.ts @@ -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(raw_schema); + +export type TMTrainLog = Model + +export const MTrainLog = model('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 +} \ No newline at end of file diff --git a/src/util/contants.ts b/src/util/contants.ts new file mode 100644 index 0000000..90efa29 --- /dev/null +++ b/src/util/contants.ts @@ -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; +}; \ No newline at end of file