diff --git a/src/components/TrainsView/TrainStats.vue b/src/components/TrainsView/TrainStats.vue index b454664..7d2b44c 100644 --- a/src/components/TrainsView/TrainStats.vue +++ b/src/components/TrainsView/TrainStats.vue @@ -29,14 +29,14 @@
  • {{ $t('train-stats.avg-speed') }} - {{ avgSpeed.toFixed(1) }} km/h + {{ stats.avgSpeed.toFixed(1) }} km/h
  • {{ $t('train-stats.avg-timetable') }} - {{ avgDistance.toFixed(1) }} km + {{ stats.avgDistance.toFixed(1) }} km
  • @@ -46,33 +46,45 @@

    {{ $t('train-stats.top-categories') }}

    -
  • +
  • {{ top.name }} {{ top.count }}
  • + + + {{ $t('train-stats.no-timetables') }} +

    {{ $t('train-stats.top-vehicles') }}

    -
  • +
  • {{ top.name }} {{ top.count }}
  • + + + {{ $t('train-stats.no-vehicles') }} +

    {{ $t('train-stats.top-units') }}

    -
  • +
  • {{ top.name }} {{ top.count }}
  • + + + {{ $t('train-stats.no-units') }} +
    @@ -81,7 +93,7 @@
    - {{ $t('train-stats.none-stats') }} + {{ $t('train-stats.no-stats') }}
    @@ -98,6 +110,19 @@ interface ITop { count: number; } +interface IStats { + timetableCount: number; + avgSpeed: number; + avgDistance: number; + topCategories: ITop[]; + topVehicles: ITop[]; + topUnits: ITop[]; +} + +function compareTop(top1: ITop, top2: ITop) { + return Math.sign(top2.count - top1.count) || top1.name.localeCompare(top2.name, 'pl-PL'); +} + export default defineComponent({ data() { return { @@ -116,68 +141,62 @@ export default defineComponent({ return this.regionTrains.filter((train) => train.timetableData); }, - avgSpeed() { - if (this.regionTrains.length == 0) return 0; + stats() { + const stats = this.regionTrains.reduce( + (acc, train, i, arr) => { + // AVG SPEED + acc.avgSpeed += train.speed / arr.length; - return ( - this.regionTrains.reduce((acc, train) => (acc += train.speed), 0) / this.regionTrains.length - ); - }, - - avgDistance() { - if (this.regionTrainsWithTT.length == 0) return 0; - - return ( - this.regionTrainsWithTT.reduce((acc, train) => { - acc += train.timetableData!.routeDistance; - - return acc; - }, 0) / this.regionTrainsWithTT.length - ); - }, - - topCategories() { - return this.regionTrainsWithTT - .reduce((acc, train) => { - const topCategory = acc.find((top) => top.name == train.timetableData!.category); - - if (!topCategory) acc.push({ name: train.timetableData!.category, count: 1 }); - else topCategory.count++; - - return acc; - }, [] as ITop[]) - .sort((c1, c2) => Math.sign(c2.count - c1.count)) - .slice(0, 5); - }, - - topVehicles() { - return this.regionTrains - .reduce((acc, train) => { + // TOP VEHICLES const locoType = train.locoType.split('-')[0]; - const topVehicle = acc.find((top) => top.name == locoType); + const topVehicle = acc.topVehicles.find((top) => top.name == locoType); - if (!topVehicle) acc.push({ name: locoType, count: 1 }); + if (!topVehicle) acc.topVehicles.push({ name: locoType, count: 1 }); else topVehicle.count++; - return acc; - }, [] as ITop[]) - .sort((c1, c2) => Math.sign(c2.count - c1.count)) - .slice(0, 8); - }, + // TOP UNITS + const unitType = train.locoType; + const topUnit = acc.topUnits.find((top) => top.name == unitType); - topUnits() { - return this.regionTrains - .reduce((acc, train) => { - const locoType = train.locoType; - const topVehicle = acc.find((top) => top.name == locoType); + if (!topUnit) acc.topUnits.push({ name: unitType, count: 1 }); + else topUnit.count++; - if (!topVehicle) acc.push({ name: locoType, count: 1 }); - else topVehicle.count++; + if (train.timetableData !== undefined) { + acc.timetableCount++; + // AVG DISTANCE + acc.avgDistance += train.timetableData.routeDistance; + + // TOP CATEGORIES + const topCategory = acc.topCategories.find( + (top) => top.name == train.timetableData!.category + ); + + if (!topCategory) + acc.topCategories.push({ name: train.timetableData!.category, count: 1 }); + else topCategory.count++; + } + + if (i == arr.length - 1 && acc.timetableCount != 0) { + acc.avgDistance /= acc.timetableCount; + } return acc; - }, [] as ITop[]) - .sort((c1, c2) => Math.sign(c2.count - c1.count)) - .slice(0, 5); + }, + { + timetableCount: 0, + avgDistance: 0, + avgSpeed: 0, + topCategories: [], + topUnits: [], + topVehicles: [] + } as IStats + ); + + stats.topCategories.sort(compareTop); + stats.topUnits.sort(compareTop); + stats.topVehicles.sort(compareTop); + + return stats; } }, @@ -196,6 +215,7 @@ export default defineComponent({ diff --git a/src/locales/en.json b/src/locales/en.json index f7c16bc..ae5955d 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -303,9 +303,12 @@ "avg-timetable": "AVG TIMETABLE", "top-categories": "Timetable categories", "top-vehicles": "Vehicles online", - "top-units": "Units online", + "top-units": "Common units online", "stats-loading": "Loading...", - "stats-none": "No statistics available for the current region!" + "no-timetables": "No active timetables in this region!", + "no-vehicles": "No active vehicles in this region!", + "no-units": "No active units in this region!", + "no-stats": "No statistics available for the current region!" }, "journal": { "title": "DISPATCHER HISTORY", diff --git a/src/locales/pl.json b/src/locales/pl.json index fe95a0b..789ef97 100644 --- a/src/locales/pl.json +++ b/src/locales/pl.json @@ -286,9 +286,12 @@ "avg-timetable": "ŚREDNI RJ", "top-categories": "Kategorie RJ", "top-vehicles": "Pojazdy w grze", - "top-units": "Jednostki w grze", + "top-units": "Najczęstsze jednostki w grze", "stats-loading": "Ładowanie...", - "stats-none": "Brak statystyk online dla wybranego serwera!" + "no-timetables": "Brak aktywnych rozkładów jazdy na tym serwerze!", + "no-vehicles": "Brak aktywnych pojazdów na tym serwerze!", + "no-units": "Brak aktywnych jednostek na tym serwerze!", + "no-stats": "Brak statystyk online dla wybranego serwera!" }, "journal": { "title": "HISTORIA DYŻURÓW", diff --git a/src/styles/JournalSection.scss b/src/styles/JournalSection.scss index 77dcdd7..eadc5b7 100644 --- a/src/styles/JournalSection.scss +++ b/src/styles/JournalSection.scss @@ -12,7 +12,6 @@ .journal_wrapper { max-width: 1350px; width: 100%; - position: relative; margin: 0 auto; diff --git a/src/styles/dropdown.scss b/src/styles/dropdown.scss index 6085c9f..a349a70 100644 --- a/src/styles/dropdown.scss +++ b/src/styles/dropdown.scss @@ -27,6 +27,7 @@ .dropdown_wrapper { position: absolute; left: 0; + top: calc(100% + 0.5em); background-color: $bgCol; box-shadow: 0 5px 10px 2px #0f0f0f; diff --git a/src/styles/dropdown_filters.scss b/src/styles/dropdown_filters.scss index 2a5bc8b..1176a6c 100644 --- a/src/styles/dropdown_filters.scss +++ b/src/styles/dropdown_filters.scss @@ -8,6 +8,10 @@ margin-bottom: 0.5em; } +.filters-options { + position: relative; +} + h1.option-title { position: relative; font-size: 1.1em;