-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreturnDurString.js
More file actions
212 lines (188 loc) · 6.41 KB
/
returnDurString.js
File metadata and controls
212 lines (188 loc) · 6.41 KB
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
// imported from cc_framework
(function($)
{
var ms = new messageSrv();
var globalQueue = [];
var heartBeatQueue = $({});
var updateInterval = 60000;
var globalCountdownFunc = function()
{
if (globalQueue.length < 1)
{
return;
}
$.each(globalQueue,function(k, v)
{
heartBeatQueue.queue('hb', function(next)
{
v();
globalQueue.shift();
if (typeof(next) == "function")
{
next();
}
});
});
heartBeatQueue.dequeue('hb');
};
var ditFunc = function(data)
{
var count = this.count;
globalQueue.push(function()
{
if ($('#down_in_timer').length)
{
var durText = ((data.duration/1000) <= 10 ? 'Just a few seconds.' : getDuration((data.duration/1000)));
$('#down_in_timer').text(durText);
}
});
};
$(window).ready(function()
{
ms.init();
new countdown(null,(1000*60*60*24*365),null,null,globalCountdownFunc).start();
});
function messageSrv()
{
this.cdTimerObj = null;
this.previousData = null;
this.init = function()
{
var url = localurl+"backend/getmessage";
$.ajax(
{
url: url,
type: 'POST',
success: function(data)
{
if((data.advert || typeof(data.down_in) != "undefined" || data.down_in > 0) && data.userid >= data.aduidrange['min'] && data.userid < data.aduidrange['max'])
{
var defaultTimerText = ($('#down_in_timer').length ? $('#down_in_timer').text() : "");
$('#above-game-message').html(data.advert);
if (ms.cdTimerObj != null && typeof(ms.cdTimerObj.stop) != "undefined")
{
this.cdTimerObj.stop();
}
if (typeof(data.down_in) != "undefined" || data.down_in > 0) {
$('#above-game-message').append('<br />Scheduled downtime in: <span id="down_in_timer">' + defaultTimerText + '</span>');
if ((data.down_in * 1000) > updateInterval) {
ms.cdTimerObj = new countdown(null, (data.down_in * 1000), updateInterval, null, ditFunc, ms.init).start();
}
else {
ms.cdTimerObj = new countdown(null, (updateInterval - (data.down_in * 1000)), updateInterval, null, ditFunc, ms.init).start();
}
} else {
setTimeout(ms.init,updateInterval);
}
ms.showMATGBlastPopup(data, ms.previousData);
ms.previousData = data;
}
else
{
$('#above-game-message').html('');
setTimeout(ms.init,updateInterval);
}
},
dataType: 'json',
data: fbdata
});
};
this.showMATGBlastPopup = function(currentData, oldData) {
if(oldData != null) {
var msg = "";
if(oldData.ingame_advert != currentData.ingame_advert && (typeof(oldData.down_in) == "undefined" && typeof(currentData.down_in) != "undefined")) {
var defaultTimerText = getDuration(currentData.down_in);
msg = currentData.advert + "<br /> " + 'Scheduled downtime in: <span id="down_in_timer">'+defaultTimerText+'</span>';
} else if(oldData.ingame_advert != currentData.ingame_advert) {
msg = currentData.ingame_advert;
} else if(typeof(oldData.down_in) == "undefined" && typeof(currentData.down_in) != "undefined") {
var defaultTimerText = getDuration(currentData.down_in);
msg = 'Scheduled downtime in: <span id="down_in_timer">'+defaultTimerText+'</span>';
}
var swf = document.getElementById("gameswf");
if (msg != "" && (swf != null || swf != undefined)) {
var tmp = document.createElement("div");
tmp.innerHTML = msg;
var textMsg = tmp.textContent || tmp.innerText;
//console.log(textMsg);
swf.showAlertPopup(textMsg);
}
}
};
this.MATGCallback = function(data) {
console.log(data);
};
}
function countdown(started,duration,halt,inc,func,cb)
{
this.timer = null;
this.startTs = new Date().getTime()-((started === null) ? 0 : started);
this.endTs = this.startTs+duration;
this.func = func;
this.cb = cb;
this.inc = inc;
this.start = function()
{
this.stop();
var self = this;
this.timer = setInterval(function(){doCountdown(self);},(this.inc !== null ? this.inc : 100));
};
this.stop = function()
{
clearInterval(this.timer);
this.timer = null;
};
this.halt = halt;
this.count = 0;
return this;
}
function doCountdown(obj)
{
var currentTs = new Date().getTime();
obj.func(
{
duration: (obj.endTs-currentTs),
percent: (((currentTs-obj.startTs)/(obj.endTs-obj.startTs))*100)
});
if (obj.halt)
{
if (currentTs >= (obj.startTs+obj.halt))
{
obj.stop();
if (typeof(obj.cb) != "function") return;
obj.cb();
return;
}
}
if (currentTs < obj.endTs) return;
obj.stop();
if (typeof(obj.cb) != "function") return;
obj.cb();
}
function getDuration(seconds,remSigFig)
{
remSigFig = (remSigFig === null) ? false : remSigFig;
seconds = Math.ceil(seconds);
if (seconds == '0')
{
return '0 secs';
}
var days = Math.floor(seconds / (24*60*60))+'';
var hours = Math.floor((seconds - (days * (24*60*60))) / (60*60))+'';
var minutes = Math.floor((seconds - (days * (24*60*60)) - (hours * (60*60))) / (60))+'';
seconds = Math.floor((seconds - (days * (24*60*60)) - (hours * (60*60)) - (minutes * (60))))+'';
var noDays = (days == '0') ? true : false;
var noHours = (hours == '0') ? true : false;
var noMins = (minutes == '0') ? true : false;
var noSecs = (seconds == '0') ? true : false;
pdays = (days == 0) ? 'day' : 'days';
phours = (hours == 0) ? 'hr' : 'hrs';
pminutes = (minutes == 0) ? 'min' : 'mins';
pseconds = (seconds == 0) ? 'sec' : 'secs';
var returnDurString = (noDays) ? '' : days+pdays+' ';
returnDurString += (noHours && noDays) ? '' : (remSigFig === false && noHours) ? '' : hours+phours+' ';
returnDurString += (remSigFig === true && noMins) ? '' : minutes+pminutes+' ';
returnDurString += (remSigFig === true && noSecs) ? '' : seconds+pseconds;
return returnDurString;
}
})(jQuery);