diff --git a/readme.md b/readme.md index 00c3acf7..42f6f6a9 100644 --- a/readme.md +++ b/readme.md @@ -11,7 +11,7 @@ https://simrail.alekswilc.dev/ - Statystyki ## Dalszy rozwój -- Obsługa pociagów, a nie tylko posterunków ([#8](https://git.alekswilc.dev/alekswilc/simrail-logs/issues/8)) +- Nowy wygląd aplikacji # Zgłaszanie błedów Jak zgłosić błąd? diff --git a/src/http/routes/leaderboard.ts b/src/http/routes/leaderboard.ts new file mode 100644 index 00000000..8e791848 --- /dev/null +++ b/src/http/routes/leaderboard.ts @@ -0,0 +1,81 @@ +import { Router } from 'express'; +import dayjs from 'dayjs'; +import { PlayerUtil } from '../../util/PlayerUtil.js'; +import { msToTime } from '../../util/time.js'; + +import { PipelineStage } from 'mongoose'; +import { MProfile, raw_schema } from '../../mongo/profile.js'; + +const generateSearch = (regex: RegExp) => [ + { + steam: { $regex: regex }, + }, + { + steamName: { $regex: regex }, + }, +] + +export class LeaderboardRoute { + static load() { + const app = Router(); + + app.get('/train', async (req, res) => { + const s = req.query.q?.toString().split(',').map(x => new RegExp(x, "i")); + + const filter: PipelineStage[] = []; + + + s && filter.push({ + $match: { + $and: [ + ...s.map(x => ({ $or: generateSearch(x) })) + ] + } + }) + + + const records = await MProfile.aggregate(filter) + .sort({ trainPoints: -1 }) + .limit(10) + res.render('leaderboard/index.ejs', { + records, + dayjs, + msToTime, + type: 'train', + q: req.query.q, + + }); + }) + + + app.get('/station', async (req, res) => { + const s = req.query.q?.toString().split(',').map(x => new RegExp(x, "i")); + + const filter: PipelineStage[] = []; + + + s && filter.push({ + $match: { + $and: [ + ...s.map(x => ({ $or: generateSearch(x) })) + ] + } + }) + + + const records = await MProfile.aggregate(filter) + .sort({ dispatcherTime: -1 }) + .limit(10) + res.render('leaderboard/index.ejs', { + records, + dayjs, + msToTime, + type: 'station', + q: req.query.q, + + }); + }) + + return app; + } +} \ No newline at end of file diff --git a/src/http/routes/profile.ts b/src/http/routes/profile.ts new file mode 100644 index 00000000..840a69ee --- /dev/null +++ b/src/http/routes/profile.ts @@ -0,0 +1,32 @@ +import { Router } from 'express'; +import dayjs from 'dayjs'; +import { PlayerUtil } from '../../util/PlayerUtil.js'; +import { msToTime } from '../../util/time.js'; +import { MProfile } from '../../mongo/profile.js'; +import { MBlacklist } from '../../mongo/blacklist.js'; + + + +export class ProfilesRoute { + static load() { + const app = Router(); + + + app.get('/:id', async (req, res) => { + if (!req.params.id) return res.redirect('/'); + const player = await MProfile.findOne({ steam: req.params.id }); + if (!player) return res.render('profiles/private.ejs'); + const blacklist = await MBlacklist.findOne({ steam: req.params.id! }); + if (blacklist && blacklist.status) return res.render('profiles/private.ejs'); + const steam = await PlayerUtil.getPlayer(player?.steam!); + const steamStats = await PlayerUtil.getPlayerStats(player?.steam!); + + res.render('profiles/index.ejs', { + player, steam, steamStats: steamStats, + msToTime + }); + }) + + return app; + } +} \ No newline at end of file diff --git a/src/http/routes/stations.ts b/src/http/routes/stations.ts index 6c98ae13..01b53f56 100644 --- a/src/http/routes/stations.ts +++ b/src/http/routes/stations.ts @@ -4,6 +4,7 @@ import dayjs from 'dayjs'; import { PlayerUtil } from '../../util/PlayerUtil.js'; import { msToTime } from '../../util/time.js'; import { PipelineStage } from 'mongoose'; +import { MBlacklist } from '../../mongo/blacklist.js'; const generateSearch = (regex: RegExp) => [ { @@ -55,6 +56,8 @@ export class StationsRoute { app.get('/details/:id', async (req, res) => { if (!req.params.id) return res.redirect('/stations/'); const record = await MLog.findOne({ id: req.params.id }); + const blacklist = await MBlacklist.findOne({ steam: record?.userSteamId! }); + if (blacklist && blacklist.status) return res.redirect('/stations/'); const player = await PlayerUtil.getPlayer(record?.userSteamId!); res.render('stations/details.ejs', { @@ -65,45 +68,7 @@ export class StationsRoute { }); }) - app.get('/leaderboard/', async (req, res) => { - const s = req.query.q?.toString().split(',').map(x => new RegExp(x, "i")); - - const data = Object.keys(raw_schema) - .reduce((o, key) => ({ ...o, [key]: `$${key}` }), {}); - - const filter: PipelineStage[] = [ - { - $project: { - // record.leftDate - record.joinedDate - result: { $subtract: ['$leftDate', '$joinedDate'] }, - ...data - } - }, - ]; - - s && filter.unshift( - { - $match: { - $and: [ - ...s.map(x => ({ $or: generateSearch(x) })) - ] - } - } - ) - - - const records = await MLog.aggregate(filter) - .sort({ result: -1 }) - .limit(30) - res.render('stations/leaderboard.ejs', { - records, - dayjs, - q: req.query.q, - msToTime - }); - }) - - + // API ENDPOINTS // CREATE AN ISSUE IF YOU NEED API ACCESS: https://git.alekswilc.dev/alekswilc/simrail-logs/issues /* diff --git a/src/http/routes/trains.ts b/src/http/routes/trains.ts new file mode 100644 index 00000000..91345626 --- /dev/null +++ b/src/http/routes/trains.ts @@ -0,0 +1,126 @@ +import { Router } from 'express'; +import dayjs from 'dayjs'; +import { PlayerUtil } from '../../util/PlayerUtil.js'; +import { msToTime } from '../../util/time.js'; +import { PipelineStage } from 'mongoose'; +import { MTrainLog, raw_schema } from '../../mongo/trainLogs.js'; +import { MBlacklist } from '../../mongo/blacklist.js'; + +const generateSearch = (regex: RegExp) => [ + { + trainNumber: { $regex: regex }, + }, + { + userSteamId: { $regex: regex }, + }, + { + server: { $regex: regex }, + } +] + +export class TrainsRoute { + static load() { + const app = Router(); + + app.get('/', async (req, res) => { + const s = req.query.q?.toString().split(',').map(x => new RegExp(x, "i")); + + const filter: PipelineStage[] = []; + + + s && filter.push({ + $match: { + $and: [ + ...s.map(x => ({ $or: generateSearch(x) })) + ] + } + }) + + const records = await MTrainLog.aggregate(filter) + .sort({ leftDate: -1 }) + .limit(30) + res.render('trains/index.ejs', { + records, + dayjs, + q: req.query.q, + msToTime + }); + }) + + app.get('/details/:id', async (req, res) => { + if (!req.params.id) return res.redirect('/trains/'); + const record = await MTrainLog.findOne({ id: req.params.id }); + const player = await PlayerUtil.getPlayer(record?.userSteamId!); + const blacklist = await MBlacklist.findOne({ steam: record?.userSteamId! }); + if (blacklist && blacklist.status) return res.redirect('/trains/'); + + res.render('trains/details.ejs', { + record, + dayjs, + player, + msToTime + }); + }) + + app.get('/leaderboard/', async (req, res) => { + const s = req.query.q?.toString().split(',').map(x => new RegExp(x, "i")); + + const data = Object.keys(raw_schema) + .reduce((o, key) => ({ ...o, [key]: `$${key}` }), {}); + + const filter: PipelineStage[] = [ + { + $project: { + // record.leftDate - record.joinedDate + result: { $subtract: ['$leftDate', '$joinedDate'] }, + ...data + } + }, + ]; + + s && filter.unshift( + { + $match: { + $and: [ + ...s.map(x => ({ $or: generateSearch(x) })) + ] + } + } + ) + + + const records = await MTrainLog.aggregate(filter) + .sort({ result: -1 }) + .limit(30) + res.render('trains/leaderboard.ejs', { + records, + dayjs, + q: req.query.q, + msToTime + }); + }) + + + // API ENDPOINTS + // CREATE AN ISSUE IF YOU NEED API ACCESS: https://git.alekswilc.dev/alekswilc/simrail-logs/issues + /* + app.get('/api/last', async (req, res) => { + const records = await MLog.find() + .sort({ leftDate: -1 }) + .limit(30) + res.json({ code: 200, records }); + }) + + app.get('/api/search', async (req, res) => { + if (!req.query.q) return res.send('invalid'); + const records = await MLog.find({ $text: { $search: req.query.q as string } }) + .sort({ leftDate: -1 }) + .limit(30) + + res.json({ code: 200, records }); + })*/ + + + return app; + } +} \ No newline at end of file diff --git a/src/http/server.ts b/src/http/server.ts index 33ace508..b4552d65 100644 --- a/src/http/server.ts +++ b/src/http/server.ts @@ -2,6 +2,10 @@ import express from 'express'; import { fileURLToPath } from 'node:url'; import path from 'node:path'; import { StationsRoute } from './routes/stations.js'; +import { TrainsRoute } from './routes/trains.js'; +import { ProfilesRoute } from './routes/profile.js'; +import { LeaderboardRoute } from './routes/leaderboard.js'; +import { MProfile } from '../mongo/profile.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); @@ -20,6 +24,28 @@ export class ApiModule { app.get('/details/:id', (req, res) => res.redirect('/stations/details/'+req.params.id)); app.use('/stations/', StationsRoute.load()); + app.use('/trains/', TrainsRoute.load()); + app.use('/profiles/', ProfilesRoute.load()); + app.use('/leaderboard/', LeaderboardRoute.load()) + + + app.get('/migrate', async () => { + const _ = await MProfile.find({}); + + _.forEach(async(x) => { + x.trainPoints = Object.values(x.trainStats).length === 1 ? + Object.values(x.trainStats)[0].score : + Object.values(x.trainStats).reduce((acc, curr) => acc + curr.score, 0) + + x.trainDistance = Object.values(x.trainStats).length === 1 ? + Object.values(x.trainStats)[0].distance : + Object.values(x.trainStats).reduce((acc, curr) => acc + curr.distance, 0) + + await MProfile.updateOne({ id: x.id }, { trainPoints: x.trainPoints, trainDistance: x.trainDistance }) + }) + + + }) app.listen(2005); } diff --git a/src/http/views/_modules/header.ejs b/src/http/views/_modules/header.ejs index 78ed9f9d..f5799800 100644 --- a/src/http/views/_modules/header.ejs +++ b/src/http/views/_modules/header.ejs @@ -1,19 +1,11 @@

SimRail Logs

- - <% if (section==='stations' ) { %> - - - <% } else {%> + Logi posterunków / - Logi pociągów - <% } %> + Logi pociągów / + Tablica pociągow / + Tablica posterunków +

Hej! Podoba Ci się projekt? Polajkuj post na forum, a będzie diff --git a/src/http/views/home.ejs b/src/http/views/home.ejs index c4530e80..ae6470a9 100644 --- a/src/http/views/home.ejs +++ b/src/http/views/home.ejs @@ -39,7 +39,7 @@

Planowane funkcjonalności

diff --git a/src/http/views/leaderboard/index.ejs b/src/http/views/leaderboard/index.ejs new file mode 100644 index 00000000..00e1de91 --- /dev/null +++ b/src/http/views/leaderboard/index.ejs @@ -0,0 +1,77 @@ + + + + + + + simrail.alekswilc.dev + + + + + + + + + + + + + + <%- include('../_modules/header.ejs', { section: 'leaderboard' }) %> + +

Tablica wyników

+
+ + + + + +

Użyj przecinka, aby wyszukać wiele wartości: pl2,Łazy

+ +
+
+ + + <% if (!records.length) { %> +

Nie znaleziono wyników dla twojego zapytania.

+ <% } %> + + + +
+ +

Dane do rankingu zbierane są od dnia 19.08.2024.

+ + <%- include('../_modules/footer.ejs', { thanks: false }) %> + + + \ No newline at end of file diff --git a/src/http/views/leaderboard/station.ejs b/src/http/views/leaderboard/station.ejs new file mode 100644 index 00000000..6038e1b3 --- /dev/null +++ b/src/http/views/leaderboard/station.ejs @@ -0,0 +1,9 @@ +
+ + <%- record.steamName %> + + <%- msToTime(record.dispatcherTime) %> + +

Spędzona liczba godzin: <%- msToTime(record.dispatcherTime) || 'Brak' %>

+ +
\ No newline at end of file diff --git a/src/http/views/leaderboard/train.ejs b/src/http/views/leaderboard/train.ejs new file mode 100644 index 00000000..7aa20264 --- /dev/null +++ b/src/http/views/leaderboard/train.ejs @@ -0,0 +1,11 @@ +
+ + <%- record.steamName %> + - + <%- record.trainPoints %> pkt. + +

Spędzona liczba godzin: <%- msToTime(record.trainTime) %>

+

Przejechane kilometry: <%- record.trainDistance / 1000 %>km

+

Zdobyte punkty: <%- record.trainPoints %>

+ +
\ No newline at end of file diff --git a/src/http/views/profiles/index.ejs b/src/http/views/profiles/index.ejs new file mode 100644 index 00000000..5cdc32f0 --- /dev/null +++ b/src/http/views/profiles/index.ejs @@ -0,0 +1,117 @@ + + + + + + + simrail.alekswilc.dev + + + + + + + + + + + + + + + + + <%- include('../_modules/header.ejs', { section: 'profiles' }) %> + +
+

<%- steam.personaname %>

+ + + <%if (steamStats.stats) {%> +
+ Statystyki Steam +

Zdobyte punkty: <%- steamStats.stats.find(x => x.name === 'SCORE')?.value ?? "0" %>

+

Przejechane kilometry: <%- (steamStats.stats.find(x => x.name === 'DISTANCE_M')?.value / 1000) ?? "0" %>

+

Czas spędzony jako dyżurny ruchu: <%- msToTime((steamStats.stats.find(x => x.name === 'DISPATCHER_TIME')?.value ?? 0)*1_000_000) || 'Nigdy nie wszedł w tryb dyżurnego ruchu.' %>

+
+

UWAGA: powyższe statystyki udostępnia platforma STEAM, mogą one być z łatwością manipulowane.

+ +
+ <%}%> + + +

Statystyki pociągów

+ <% if (player.trainTime) {%> +

Spędzony czas: <%- msToTime(player.trainTime) %>

+

Przejechane kilometry: <%- player.trainDistance / 1000 %>km

+

Zdobyte punkty: <%- player.trainPoints %>

+

Średnia prędkość: <%- ((player.trainDistance / (player.trainTime / 1000)) * 3.6).toFixed(2) %> km/h

+ <%}%> + <% if (player.trainStats && Object.keys(player.trainStats).length) {%> + + <%} else {%> +

Brak danych

+ <%}%> + +

Statystyki posterunków

+ <% if (player.dispatcherTime) {%> +

Spędzony czas: <%- msToTime(player.dispatcherTime) %>

+ <%}%> + <% if (player.dispatcherStats && Object.keys(player.dispatcherStats).length) {%> + + <%} else {%> +

Brak danych

+ <%}%> + +
+

+
+
+

Dane do rankingu zbierane są od dnia 19.08.2024.

+ <%- include('../_modules/footer.ejs', { thanks: false }) %> + + + + \ No newline at end of file diff --git a/src/http/views/profiles/private.ejs b/src/http/views/profiles/private.ejs new file mode 100644 index 00000000..0388b4e2 --- /dev/null +++ b/src/http/views/profiles/private.ejs @@ -0,0 +1,47 @@ + + + + + + + simrail.alekswilc.dev + + + + + + + + + + + + + + <%- include('../_modules/header.ejs', { section: 'profiles' }) %> + +
+

Profil gracza jest prywatny.

+ +
+
+ <%- include('../_modules/footer.ejs', { thanks: false }) %> + + + + \ No newline at end of file diff --git a/src/http/views/stations/details.ejs b/src/http/views/stations/details.ejs index 0bd588a3..c7300fd6 100644 --- a/src/http/views/stations/details.ejs +++ b/src/http/views/stations/details.ejs @@ -6,13 +6,13 @@ simrail.alekswilc.dev + content="<%- record.stationName %> | <%- record.userUsername %> | <%- dayjs(record.leftDate).format('hh:mm DD/MM/YYYY') %>"> - + + content="<%- record.stationName %> | <%- record.userUsername %> | <%- dayjs(record.leftDate).format('hh:mm DD/MM/YYYY') %>""> - + @@ -40,7 +40,7 @@ } function copylink() { - navigator.clipboard.writeText("https://simrail.alekswilc.dev/stations/details/<%= record.id %>/") + navigator.clipboard.writeText("https://simrail.alekswilc.dev/stations/details/<%- record.id %>/") } @@ -49,28 +49,28 @@
-

Użytkownik: - <%= record.userUsername %> +

Użytkownik: + <%- record.userUsername %>

-

Stacja: <%= record.stationName %> +

Stacja: <%- record.stationName %>

-

Serwer: <%= record.server.toUpperCase() %> +

Serwer: <%- record.server.toUpperCase() %>

-

Data wejścia: <%= record.joinedDate ? dayjs(record.joinedDate).format('HH:mm DD/MM/YYYY') : '--:-- --/--/--' - %> (<%= record.joinedDate ? dayjs(record.joinedDate).fromNow() : '--' %>)

-

Data wyjścia: <%= dayjs(record.leftDate).format('HH:mm DD/MM/YYYY') %> (<%= dayjs(record.leftDate).fromNow() +

Data wejścia: <%- record.joinedDate ? dayjs(record.joinedDate).format('HH:mm DD/MM/YYYY') : '--:-- --/--/--' + %> (<%- record.joinedDate ? dayjs(record.joinedDate).fromNow() : '--' %>)

+

Data wyjścia: <%- dayjs(record.leftDate).format('HH:mm DD/MM/YYYY') %> (<%- dayjs(record.leftDate).fromNow() %>)

-

Spędzony czas: <%= record.joinedDate ? msToTime(record.leftDate - record.joinedDate) : '--' %> +

Spędzony czas: <%- record.joinedDate ? msToTime(record.leftDate - record.joinedDate) : '--' %>


- ;station: <%= record.stationName %> - ;steam: <%= record.userSteamId %> - ;server: <%= record.server %> - ;name: <%= record.userUsername %> - ;joined: <%=record.joinedDate ? dayjs(record.joinedDate).format() : 'no-data'%> - ;left: <%=dayjs(record.leftDate).format()%> - ;url: https://simrail.alekswilc.dev/stations/details/<%= record.id %>/ + ;station: <%- record.stationName %> + ;steam: <%- record.userSteamId %> + ;server: <%- record.server %> + ;name: <%- record.userUsername %> + ;joined: <%-record.joinedDate ? dayjs(record.joinedDate).format() : 'no-data'%> + ;left: <%-dayjs(record.leftDate).format()%> + ;url: https://simrail.alekswilc.dev/stations/details/<%- record.id %>/

diff --git a/src/http/views/stations/index.ejs b/src/http/views/stations/index.ejs index 4227f03f..6f657809 100644 --- a/src/http/views/stations/index.ejs +++ b/src/http/views/stations/index.ejs @@ -26,7 +26,7 @@

Wyszukaj posterunek, osobe lub serwer

- + @@ -38,25 +38,25 @@
  • [ - <%= record.server.toUpperCase() %> + <%- record.server.toUpperCase() %> ] - <%= record.stationName %> + <%- record.stationName %> - - <%= record.userUsername %> + <%- record.userUsername %>

    - <%= dayjs(record.leftDate).format('HH:mm DD/MM/YYYY') %> + <%- dayjs(record.leftDate).format('HH:mm DD/MM/YYYY') %>

    -

    Data dołączenia: <%= record.joinedDate ? dayjs(record.joinedDate).format('HH:mm DD/MM/YYYY') - : '--:-- --/--/--' %> (<%= record.joinedDate ? dayjs(record.joinedDate).fromNow() : '--' %>) +

    Data dołączenia: <%- record.joinedDate ? dayjs(record.joinedDate).format('HH:mm DD/MM/YYYY') + : '--:-- --/--/--' %> (<%- record.joinedDate ? dayjs(record.joinedDate).fromNow() : '--' %>)

    -

    Data wyjścia: <%= dayjs(record.leftDate).format('HH:mm DD/MM/YYYY') %> (<%= +

    Data wyjścia: <%- dayjs(record.leftDate).format('HH:mm DD/MM/YYYY') %> (<%- dayjs(record.leftDate).fromNow() %>)

    -

    Spędzony czas: <%= record.joinedDate ? msToTime(record.leftDate - record.joinedDate) : '--' %> +

    Spędzony czas: <%- record.joinedDate ? msToTime(record.leftDate - record.joinedDate) : '--' %>

    - +
    diff --git a/src/http/views/trains/details.ejs b/src/http/views/trains/details.ejs new file mode 100644 index 00000000..c6124981 --- /dev/null +++ b/src/http/views/trains/details.ejs @@ -0,0 +1,94 @@ + + + + + + + simrail.alekswilc.dev + + + + + + + + + + + + + + + + + <%- include('../_modules/header.ejs', { section: 'trains' }) %> + +
    + +

    Użytkownik: + <%- record.userUsername %> +

    +

    Stacja: <%- record.stationName %> +

    +

    Pociąg: <%- record.trainName %> <%- record.trainNumber %> +

    +

    Data wejścia: <%- record.joinedDate ? dayjs(record.joinedDate).format('HH:mm DD/MM/YYYY') : '--:-- --/--/--' + %> (<%- record.joinedDate ? dayjs(record.joinedDate).fromNow() : '--' %>)

    +

    Data wyjścia: <%- dayjs(record.leftDate).format('HH:mm DD/MM/YYYY') %> (<%- dayjs(record.leftDate).fromNow() + %>)

    +

    Spędzony czas: <%- record.joinedDate ? msToTime(record.leftDate - record.joinedDate) : '--' %> +

    + <% if (record.distance) { %> +

    Przejechane kilometry: <%- record.distance / 1000 %>

    +

    Zdobyte punkty: <%- record.points %>

    +

    Średnia prędkość: <%- ((record.distance / ((record.leftDate - record.joinedDate) / 1000)) * 3.6).toFixed(2) %> km/h

    + <% } %> +

    + +
    + ;train: <%- record.trainNumber %> + ;steam: <%- record.userSteamId %> + ;server: <%- record.server %> + ;name: <%- record.userUsername %> + ;joined: <%-record.joinedDate ? dayjs(record.joinedDate).format() : 'no-data'%> + ;left: <%-dayjs(record.leftDate).format()%><%if (record.distance) {%> + ;distance: <%- record.distance / 1000 %> + ;points: <%- record.points %><%}%> + ;url: https://simrail.alekswilc.dev/trains/details/<%- record.id %>/ + +
    +

    + + + +
    +
    + <%- include('../_modules/footer.ejs', { thanks: false }) %> + + + + \ No newline at end of file diff --git a/src/http/views/stations/leaderboard.ejs b/src/http/views/trains/index.ejs similarity index 61% rename from src/http/views/stations/leaderboard.ejs rename to src/http/views/trains/index.ejs index 04870120..169a36ed 100644 --- a/src/http/views/stations/leaderboard.ejs +++ b/src/http/views/trains/index.ejs @@ -7,7 +7,7 @@ simrail.alekswilc.dev - + @@ -18,46 +18,51 @@ margin: 1%; } - - <%- include('../_modules/header.ejs', { section: 'stations' }) %> + <%- include('../_modules/header.ejs', { section: 'trains' }) %> + +

    Wyszukaj pociąg, osobe lub serwer

    -

    Tablica godzin

    - - + + -

    Użyj przecinka, aby wyszukać wiele wartości: pl2,Łazy

    +

    Użyj przecinka, aby wyszukać wiele wartości: pl2,1413

    +
      <% records.forEach(record=> { %>
    • [ - <%= record.server.toUpperCase() %> + <%- record.server.toUpperCase() %> ] - <%= record.stationName %> + <%- record.trainName %> + - + <%- record.trainNumber %> - - <%= record.userUsername %> - - - <%= record.joinedDate ? msToTime(record.leftDate - record.joinedDate) : '--' %> + <%- record.userUsername %>

      - <%= dayjs(record.leftDate).format('HH:mm DD/MM/YYYY') %> + <%- dayjs(record.leftDate).format('HH:mm DD/MM/YYYY') %>

      -

      Data dołączenia: <%= record.joinedDate ? dayjs(record.joinedDate).format('HH:mm DD/MM/YYYY') - : '--:-- --/--/--' %> (<%= record.joinedDate ? dayjs(record.joinedDate).fromNow() : '--' %>) +

      Data dołączenia: <%- record.joinedDate ? dayjs(record.joinedDate).format('HH:mm DD/MM/YYYY') + : '--:-- --/--/--' %> (<%- record.joinedDate ? dayjs(record.joinedDate).fromNow() : '--' %>)

      -

      Data wyjścia: <%= dayjs(record.leftDate).format('HH:mm DD/MM/YYYY') %> (<%= +

      Data wyjścia: <%- dayjs(record.leftDate).format('HH:mm DD/MM/YYYY') %> (<%- dayjs(record.leftDate).fromNow() %>)

      -

      Spędzony czas: <%= record.joinedDate ? msToTime(record.leftDate - record.joinedDate) : '--' %> +

      Spędzony czas: <%- record.joinedDate ? msToTime(record.leftDate - record.joinedDate) : '--' %> + <% if (record.distance) { %> +

      Przejechane kilometry: <%- record.distance / 1000 %>

      +

      Zdobyte punkty: <%- record.points %>

      + <% } %>

      - +
      @@ -69,24 +74,24 @@

      Nie znaleziono wyników dla twojego zapytania.

      <% } %> +
      + <%- include('../_modules/footer.ejs', { thanks: false }) %> -
      - <%- include('../_modules/footer.ejs', { thanks: false }) %> + \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index a3feb354..d900def0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,6 +44,12 @@ import { TrainsModule } from './modules/trains.js'; StationsModule.load(); TrainsModule.load(); ApiModule.load(); + + + process.on('unhandledRejection', (reason, promise) => { + console.error(reason); + console.error(promise); + }) })(); diff --git a/src/modules/stations.ts b/src/modules/stations.ts index 2df9ef27..9e2ef9df 100644 --- a/src/modules/stations.ts +++ b/src/modules/stations.ts @@ -13,22 +13,26 @@ export class StationsModule { const stats = await PlayerUtil.getPlayerStats(player.steamid); const date = new Date(); if (stats) { - const time = date.getTime() - joinedAt; - - const userProfile = await MProfile.findOne({ steam: player.steamid }) ?? await MProfile.create({ steam: player.steamid, id: v4() }); + const time = (date.getTime() - joinedAt) ?? 0; + + const userProfile = await MProfile.findOne({ steam: player.steamid }) ?? await MProfile.create({ steam: player.steamid, id: v4(), steamName: player.personaname }); if (!userProfile.dispatcherStats) userProfile.dispatcherStats = {}; if (userProfile.dispatcherStats[station.Name]) { userProfile.dispatcherStats[station.Name].time = userProfile.dispatcherStats[station.Name].time + time; + } else { userProfile.dispatcherStats[station.Name] = { time } } + if (Number.isNaN(userProfile.dispatcherStats[station.Name].time)) userProfile.dispatcherStats[station.Name].time = 0; - console.log(userProfile.dispatcherStats); + if (!userProfile.dispatcherTime) userProfile.dispatcherTime = 0; - console.log(await MProfile.findOneAndUpdate({ id: userProfile.id }, { dispatcherStats: userProfile.dispatcherStats })) + userProfile.dispatcherTime = userProfile.dispatcherTime + time; + + await MProfile.findOneAndUpdate({ id: userProfile.id }, { dispatcherStats: userProfile.dispatcherStats, dispatcherTime: userProfile.dispatcherTime }) } MLog.create({ diff --git a/src/modules/trains.ts b/src/modules/trains.ts index 1e6594df..fdf67313 100644 --- a/src/modules/trains.ts +++ b/src/modules/trains.ts @@ -12,7 +12,8 @@ export class TrainsModule { 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 time = (leftAt - joinedAt) ?? 0; + const userProfile = await MProfile.findOne({ steam: player.steamid }) ?? await MProfile.create({ steam: player.steamid, id: v4(), steamName: player.personaname }); const vehicleName = getVehicle(vehicle) ?? vehicle; @@ -21,14 +22,26 @@ export class TrainsModule { if (userProfile.trainStats[vehicleName]) { userProfile.trainStats[vehicleName].distance = userProfile.trainStats[vehicleName].distance + distance; userProfile.trainStats[vehicleName].score = userProfile.trainStats[vehicleName].score + points; + userProfile.trainStats[vehicleName].time = userProfile.trainStats[vehicleName].time + time; } else { userProfile.trainStats[vehicleName] = { - distance, score: points + distance, score: points, time } } - await MProfile.findOneAndUpdate({ id: userProfile.id }, { trainStats: userProfile.trainStats }) + if (!userProfile.trainTime) userProfile.trainTime = 0; + userProfile.trainTime = userProfile.trainTime + time; + + if (!userProfile.trainPoints) userProfile.trainPoints = 0; + + userProfile.trainPoints = userProfile.trainPoints + points; + + if (!userProfile.trainDistance) userProfile.trainDistance = 0; + + userProfile.trainDistance = userProfile.trainDistance + distance; + + await MProfile.findOneAndUpdate({ id: userProfile.id }, { trainStats: userProfile.trainStats, trainTime: userProfile.trainTime, trainPoints: userProfile.trainPoints, trainDistance: userProfile.trainDistance }); } MTrainLog.create({ @@ -40,7 +53,8 @@ export class TrainsModule { leftDate: leftAt, trainNumber: train.TrainNoLocal, server: server.ServerCode, - distance, points + distance, points, + trainName: train.TrainName }); }) } diff --git a/src/mongo/blacklist.ts b/src/mongo/blacklist.ts new file mode 100644 index 00000000..2fb543d3 --- /dev/null +++ b/src/mongo/blacklist.ts @@ -0,0 +1,24 @@ +import { Model, model, Schema } from 'mongoose'; + + +export const raw_schema = { + steam: { + type: String, + required: true + }, + status: { + type: Boolean, + default: false + } +} + +const schema = new Schema(raw_schema); + +export type TMBlacklist = Model + +export const MBlacklist = model('blacklist', schema); + +export interface IBlacklist { + steam: string + status: boolean +} \ No newline at end of file diff --git a/src/mongo/profile.ts b/src/mongo/profile.ts index 5ec02e89..68adb9aa 100644 --- a/src/mongo/profile.ts +++ b/src/mongo/profile.ts @@ -10,6 +10,11 @@ export const raw_schema = { type: String, required: true }, + steamName: { + type: String, + required: true + }, + trainStats: { type: Object, required: false, @@ -20,6 +25,26 @@ export const raw_schema = { required: false, default: {} }, + trainTime: { + type: Number, + required: false, + default: 0 + }, + trainPoints: { + type: Number, + required: false, + default: 0 + }, + trainDistance: { + type: Number, + required: false, + default: 0 + }, + dispatcherTime: { + type: Number, + required: false, + default: 0 + }, } const schema = new Schema(raw_schema); @@ -36,6 +61,7 @@ export interface IProfile { [trainName: string]: { score: number, distance: number + time: number, } } dispatcherStats: { @@ -43,4 +69,10 @@ export interface IProfile { time: number } } + + dispatcherTime: number; + trainTime: number + trainPoints: number + steamName: string + trainDistance: number } \ No newline at end of file diff --git a/src/mongo/trainLogs.ts b/src/mongo/trainLogs.ts index 0362446a..08564a10 100644 --- a/src/mongo/trainLogs.ts +++ b/src/mongo/trainLogs.ts @@ -45,6 +45,10 @@ export const raw_schema = { type: String, required: true }, + trainName: { + type: String, + default: null + } } const schema = new Schema(raw_schema); @@ -61,6 +65,7 @@ export interface ITrainLog { joinedDate?: number leftDate: number trainNumber: string + trainName: string distance: number points: number server: string