-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared-template.js
166 lines (149 loc) · 4.03 KB
/
shared-template.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
155
156
157
158
159
160
161
162
163
164
165
166
//
// WARNING: All edits here will be lost!!!
// Do not edit this file manually, because it will be overwritten on application start to keep the port in sync!
//
// We are using vanilla JS and jQuery here. For more information please watch this:
// https://www.youtube.com/watch?v=Uo3cL4nrGOk
//
// Define colors
var colorScsLight = "#d7d7d7";
var colorScsYellow = "#fdad01";
var colorRed = "#FF0000";
// Prepare polling vars
var pollingInterval;
var timer = 1000;
var retryCounter = 0;
// game data to be used
var data;
// some status variables
var isServiceConnected = true;
var isGameConnected = false;
var isGamePaused = false;
var onJob = false;
var train = false;
var ferry = false;
$(function () {
console.log("Document ready!");
setPollingInterval();
});
function setPollingInterval() {
clearInterval(pollingInterval);
pollingInterval = setInterval(function () {
execute();
}, timer);
}
// Slow down polling interval when connection is lost
function checkPollingInterval() {
if (retryCounter === 10 && timer === 1000) {
isServiceConnected = false;
console.log("Too many connection errors: changing interval to 10sec and hiding '.game-connected'...");
timer = 10000;
setPollingInterval();
$(".game-connected").css({
visibility: "hidden"
});
}
if (retryCounter === 19 && timer === 10000) {
console.log("Too many connection errors: changing interval to 30secs...");
timer = 30000;
setPollingInterval();
} else if (retryCounter === 0 && timer > 1000) {
isServiceConnected = true;
console.log("We are back to business! Changing interval to 1000ms...");
timer = 1000;
setPollingInterval();
}
}
// Run that creepy thing!
function execute() {
$.getJSON("http://localhost:{{port}}/", function (json) {
data = json;
})
.done(function () {
retryCounter = 0;
checkPollingInterval();
})
.fail(function () {
retryCounter++;
checkPollingInterval();
})
.always(function () {
updateStatus();
if (typeof executeOverlay === "function" && data !== undefined) {
// This function should be used inside the overlay html to be executed
// no idea if that makes sense to anybody...
executeOverlay();
}
});
}
// Some helper functions
function getReadableTime(time) {
var days = time.getUTCDate() - 1 > 0 ? time.getUTCDate() - 1 + "d:" : "";
var hours = time.getUTCHours() > 0 ? time.getUTCHours() + "h:" : "";
var minutes = time.getUTCMinutes() > 0 ? time.getUTCMinutes() + "m" : "";
return days + hours + minutes;
}
function getRealMinutes(absMinutes) {
//if (data.CommonValues.Scale > 0 && absMinutes < 10)
// return "(" + Math.round(absMinutes / data.CommonValues.Scale).toFixed() + "m)";
return "(" + Math.round(absMinutes / 19).toFixed() + "m)";
}
// Set some variables and manipulate styles
// Honestly! No clue how to do this in a proper way...
function updateStatus() {
$(".on-job").css({
visibility: "",
});
$(".train").css({
visibility: "",
});
$(".ferry").css({
visibility: "",
});
$(".game-paused").css({
visibility: "",
});
$(".game-connected").css({
visibility: "",
});
if (data !== undefined && data.SpecialEventsValues.OnJob) {
onJob = true;
} else {
$(".on-job").css({
visibility: "hidden",
});
onJob = false;
}
if (data !== undefined && data.SpecialEventsValues.Train) {
$(".train").css({
visibility: "hidden",
});
train = true;
} else {
train = false;
}
if (data !== undefined && data.SpecialEventsValues.Ferry) {
$(".ferry").css({
visibility: "hidden",
});
ferry = true;
} else {
ferry = false;
}
if (data !== undefined && data.Paused) {
$(".game-paused").css({
visibility: "hidden",
});
isGamePaused = true;
} else {
isGamePaused = false;
}
if (data !== undefined && data.SdkActive && isServiceConnected) {
isGameConnected = true;
} else {
$(".game-connected").css({
visibility: "hidden",
});
isGameConnected = false;
}
}