-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrain.js
235 lines (201 loc) · 8.3 KB
/
rain.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
var rainstatus = -2;
var thunderstatus = -2;
var rainTime = -2;
var thunderTime = -2;
var thunderMinTicks = 3600;
var thunderMaxTicks = 12000 + thunderMinTicks;
var thunderMinWaitTicks = 12000;
var rainMinTicks = 12000;
var rainMaxTicks = 12000 + rainMinTicks
var rainMinWaitTicks = 12000;
var updating = true;
var refreshingData = true;
var previousTimestamp = 0;
if (!/^isitraining\.(dev\.)?([0-9a-z]+\.)?wurstmineberg\.de$/.test(location.hostname)) {
$('#isitraining-caption').html('Is it raining on Wurstmineberg?');
}
setInterval(refreshTimer, 30000);
setInterval(tickTimer, 50);
fetchData();
function padNumber(num, size) {
var s = num + '';
while (s.length < size) s = "0" + s;
return s;
}
function ticks_to_text(ticks)
{
var seconds = Math.floor(ticks / 20);
var secondsSinceHour = seconds % 50;
var hours = Math.floor(seconds / 50);
var hoursSinceDay = hours % 24;
var days = Math.floor(hours / 24);
var daysSinceMonth = days % 8;
var months = Math.floor(days / 8);
var monthsSinceYear = months % 9 ;
var years = Math.floor(months / 9);
var text = padNumber(secondsSinceHour, 2) + 's';
if (hours > 0) {
text = hoursSinceDay + 'h ' + text;
};
if (days > 0) {
text = daysSinceMonth + 'd ' + text;
};
if (months > 0) {
text = monthsSinceYear + 'm ' + text;
};
if (years > 0) {
text = years + 'y ' + text;
};
return text;
}
function tickTimer() {
if (!updating) {
// The thunder status is known and it will now start thundering
if (thunderTime <= 0) {
if (thunderTime != -2) {
// Thunder status is unknown until next refresh
thunderTime = -2;
};
} else {
thunderTime--;
if (thunderTime <= 0) {
thunderstatus = thunderstatus == 1 ? 0 : 1;
};
};
// The rain status is known and it will now start thundering
if (rainTime <= 0) {
if (rainTime != -2) {
// Rain status is unknown until next refresh
rainTime = -2;
};
} else {
rainTime--;
if (rainTime <= 0) {
rainstatus = rainstatus == 1 ? 0 : 1;
};
};
displayWeatherStatus();
};
}
function refreshTimer() {
if (!refreshingData) {
fetchData();
};
}
function displayWeatherStatus() {
// Ok here is how it works. I looked at the source code!
// The only rain toggle is rainTime.
// When thunderTime gets to zero, it resets normally.
// Only when it also rains WHILE it is thundering, an actual thunderstorm is going on.
// When currently thundering it will stop when either thunderTime or rainTime gets to zero
var rainToggleTimeText = '';
if (rainstatus < 0) {
$('#rain-caption').text("???");
$('#rain-text').text("I have no idea. Seriously. Something is broken");
} else if (rainstatus == 0) {
$('#rain-caption').text("No!");
$('#rain-text').text("It's not raining, you can come out now.");
$('#wimg').attr('src', '/img/sun.png');
var minRainTimestamp = rainTime + rainMinTicks;
var maxRainTimestamp = rainTime + rainMaxTicks;
var minThunderTimestamp = thunderTime + thunderMinTicks;
var maxThunderTimestamp = thunderTime + thunderMaxTicks;
if (rainTime >= 0) {
if ((thunderstatus == 1 && thunderTime > rainTime) ||
(thunderTime < rainTime && thunderTime + thunderMinTicks > rainTime)) {
var timeText = ticks_to_text(rainTime);
$('#forecast-text').text("Weather forecast: WEATHER ALERT: There will be a (possibly short) thunderstorm in " + timeText + "!");
} else if (thunderstatus == 0 && thunderTime >= rainTime && thunderTime < maxRainTimestamp) {
if (thunderTime < rainTime + rainMinTicks) {
// Not thundering, not raining, but after the minimal time of rain it will have started to thunder.
var rainText = ticks_to_text(rainTime);
var thunderText = ticks_to_text(thunderTime);
$('#forecast-text').text("Weather forecast: It will be raining in " + rainText + ". ALERT: There will also be a thunderstorm in " + thunderText + "!");
} else {
// Probability that thunder will have started while rain is still going on.
var thunderTimeAfterRainMinTime = thunderTime - minRainTimestamp;
var thunderProbability = 1 - (thunderTimeAfterRainMinTime / (rainMaxTicks - rainMinTicks));
var timeText = ticks_to_text(rainTime);
$('#forecast-text').text("Weather forecast: It will be raining in " + timeText + " with a " + Math.floor(thunderProbability * 100) + "% chance of a thunderstorm!");
}
} else {
var timeText = ticks_to_text(rainTime);
$('#forecast-text').text("Weather forecast: It will be raining in " + timeText + ".");
};
};
} else if (thunderstatus == 1) {
$('#rain-caption').text("Yes! D:");
$('#rain-text').text("It's thundering even! Ermagehrd!");
$('#wimg').attr('src', '/img/thunder.png');
if (thunderTime >= 0 && rainTime >= 0) {
if (thunderTime < rainTime) {
var timeText = ticks_to_text(thunderTime);
$('#forecast-text').text("Weather forecast: It will stop thundering in " + timeText + ". Afterwards it will be raining.");
} else {
var timeText = ticks_to_text(rainTime);
$('#forecast-text').text("Weather forecast: There will be sunshine in " + timeText + ".");
};
};
} else {
$('#rain-caption').text("Yes! D:");
$('#rain-text').text("It's raining! Grab yo' helmets, hide yo' snowmen!");
$('#wimg').attr('src', '/img/rain.png');
if (thunderTime >= 0 && rainTime >= 0) {
if (thunderTime < rainTime) {
var timeText = ticks_to_text(thunderTime);
$('#forecast-text').text("Weather forecast: WEATHER ALERT: There will be a thunderstorm in " + timeText + "!");
} else {
var timeText = ticks_to_text(rainTime);
$('#forecast-text').text("Weather forecast: There will be sunshine in " + timeText + ".");
};
};
};
}
function fetchData() {
$.ajax('//api.wurstmineberg.de/server/level.json', {
dataType: 'json',
success: function(data) {
updating = true;
var tickOffset;
if ('Data' in data) {
if ('raining' in data['Data']) {
rainstatus = data['Data']['raining']
};
if ('thundering' in data['Data']) {
thunderstatus = data['Data']['thundering']
};
if ('rainTime' in data['Data']) {
rainTime = data['Data']['rainTime'];
};
if ('thunderTime' in data['Data']) {
thunderTime = data['Data']['thunderTime'];
};
};
if ('api-time-last-modified' in data &&
'api-time-result-fetched' in data) {
var secondOffset = data['api-time-result-fetched'] - data['api-time-last-modified'];
tickOffset = secondOffset * 20;
if (Math.floor(previousTimestamp) == Math.floor(data['api-time-last-modified'])) {
// No update so we don't need to update the data and replace it
updating = false;
refreshingData = false;
return;
};
previousTimestamp = data['api-time-last-modified'];
};
thunderTime -= tickOffset;
rainTime -= tickOffset;
if (thunderTime <= 0) {
thunderTime = -2;
thunderstatus = thunderstatus == 1 ? 0 : 1;
};
if (rainTime <= 0) {
rainTime = -2;
rainstatus = rainstatus == 1 ? 0 : 1;
};
updating = false;
refreshingData = false;
displayWeatherStatus();
}
});
};