generated from Dennis-Rosenbaum/MMM-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
154 lines (129 loc) · 5.72 KB
/
node_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const NodeHelper = require("node_helper");
const fs = require("fs");
module.exports = NodeHelper.create({
start: function () {
console.log("[MMM-Biathlon] NodeHelper started.");
this.config = {};
},
socketNotificationReceived: function (notification, payload) {
if (notification === "GET_BIATHLON_EVENTS") {
this.getBiathlonEvents(payload.seasonid, payload.eventClassificationIds);
}
},
getBiathlonEvents: async function (seasonid, eventClassificationIds) {
console.log("[MMM-Biathlon] Retrieving events for the season:", seasonid);
this.config = this.config || {};
this.config.EventClassificationId = eventClassificationIds;
const url = `https://biathlonresults.com/modules/sportapi/api/Events?SeasonId=${seasonid}&Level=ALL`;
try {
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
const events = await response.json();
const userEventIds = this.config.EventClassificationId || [];
const eventsByClassification = new Map();
events.forEach((event) => {
if (userEventIds.includes(event.EventClassificationId)) {
const classificationId = event.EventClassificationId;
const eventStartDate = new Date(event.StartDate);
const now = new Date();
if (!eventsByClassification.has(classificationId)) {
eventsByClassification.set(classificationId, { nextEvent: null, lastEvent: null });
}
const classificationEvents = eventsByClassification.get(classificationId);
if (eventStartDate > now) {
if (!classificationEvents.nextEvent || eventStartDate < new Date(classificationEvents.nextEvent.StartDate)) {
classificationEvents.nextEvent = event;
}
} else {
if (!classificationEvents.lastEvent || eventStartDate > new Date(classificationEvents.lastEvent.StartDate)) {
classificationEvents.lastEvent = event;
}
}
}
});
const nextEvents = [];
eventsByClassification.forEach((events) => {
if (events.nextEvent) {
nextEvents.push(events.nextEvent);
}
});
console.log("[MMM-Biathlon] Next events:\n" +
nextEvents.map(event => `${event.EventId} - ${event.Description}`).join("\n")
);
for (const [classificationId, events] of eventsByClassification.entries()) {
if (events.nextEvent) {
this.sendSocketNotification("BIATHLON_NEXT_EVENT", { nextevent: events.nextEvent });
}
if (events.lastEvent) {
console.log("[MMM-Biathlon] Directly retrieving competitions for the last event:\n" +
`${events.lastEvent.EventId} - ${events.lastEvent.Description}`
);
await this.getCompetitions(events.lastEvent.EventId);
} else {
console.log("[MMM-Biathlon] No last event found for EventClassificationId", classificationId);
}
}
} catch (error) {
console.error("[MMM-Biathlon] Error retrieving events:", error);
this.sendSocketNotification("BIATHLON_NEXT_EVENT", { nextevent: null });
}
},
getCompetitions: async function (eventId) {
const url = `https://biathlonresults.com/modules/sportapi/api/Competitions?EventId=${eventId}`;
try {
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
const competitions = await response.json();
if (competitions && competitions.length > 0) {
await this.getResultsForCompetitions(competitions);
} else {
console.log("[MMM-Biathlon] No competitions found for this event.");
}
} catch (error) {
console.error("[MMM-Biathlon] Error retrieving competitions:", error);
}
},
getResultsForCompetitions: async function (competitions) {
try {
const resultsPromises = competitions.map(async (competition) => {
const raceId = competition.RaceId;
const url = `https://biathlonresults.com/modules/sportapi/api/Results?RaceId=${raceId}`;
try {
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
const raceData = await response.json();
const inforace = {
StartTime: raceData.Competition.StartTime,
ShortDescription: raceData.Competition.ShortDescription,
Organizer: raceData.SportEvt.Organizer,
Description: raceData.SportEvt.Description
};
const resultrace = raceData.Results.map((result) => ({
Rank: result.Rank,
Name: result.Name,
Nat: result.Nat,
TotalTime: result.TotalTime,
Shootings: result.Shootings
}));
const resultracerelay = [];
if (['MXRL', 'MXSR', 'SWRL', 'SMRL', 'YXSR', 'YXRL', 'JXRL', 'JXSR', 'YWRL', 'YMRL', 'JWRL', 'JMRL'].some(suffix => raceId.endsWith(suffix))) {
const uniqueResults = new Map();
resultrace.forEach((result) => {
if (!uniqueResults.has(result.Rank)) {
uniqueResults.set(result.Rank, result);
resultracerelay.push(result);
}
});
}
this.sendSocketNotification("BIATHLON_RACE_RESULTS", { inforace, resultrace, resultracerelay });
} catch (error) {
console.error("[MMM-Biathlon] Error retrieving race results:", raceId, error);
}
});
await Promise.all(resultsPromises);
console.log("[MMM-Biathlon] All race results have been retrieved.");
} catch (error) {
console.error("[MMM-Biathlon] Error retrieving race results:", error);
}
},
});