forked from brainfoolong/rwa-timedcommands
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backend.js
64 lines (59 loc) · 2.24 KB
/
backend.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
"use strict";
var Widget = require(__dirname + "/../../../src/widget");
var widget = new Widget();
/**
* On widget update cycle - Fired every 30 seconds for each server
* @param {RconServer} server
*/
widget.onUpdate = function (server) {
var commands = widget.storage.get(server, "commands") || {};
var now = Math.floor(new Date().getTime() / 1000 / 60);
var thisDay = new Date().getDay();
var thisHour = new Date().getHours();
var thisMinute = new Date().getMinutes();
for (var commandsIndex in commands) {
if (commands.hasOwnProperty(commandsIndex)) {
var row = commands[commandsIndex];
if (row.active !== "yes") continue;
if (!row.minutes.length || !row.hours.length || !row.weekdays.length) {
continue;
}
var lastExecution = row.lastExecution || 0;
if (lastExecution < now && row.minutes.indexOf(thisMinute.toString()) > -1 && row.hours.indexOf(thisHour.toString()) > -1 && row.weekdays.indexOf(thisDay.toString()) > -1) {
row.lastExecution = now;
commands[commandsIndex] = row;
server.cmd(row.command);
widget.storage.set(server, "commands", commands);
}
}
}
};
/**
* On frontend message
* @param {RconServer} server
* @param {WebSocketUser} user
* @param {string} action The action
* @param {*} messageData Any message data received from frontend
* @param {function} callback Pass an object as message data response for the frontend
*/
widget.onFrontendMessage = function (server, user, action, messageData, callback) {
var commands = widget.storage.get(server, "commands") || {};
switch (action) {
case "delete":
delete commands[messageData];
widget.storage.set(server, "commands", commands);
callback(this, null);
break;
case "get":
callback(this, commands);
break;
case "save":
if (messageData && messageData.name) {
commands[messageData.name] = messageData;
widget.storage.set(server, "commands", commands);
}
callback(this, null);
break;
}
};
module.exports = widget;