Skip to content

Commit ed1904a

Browse files
committed
Add API scripts
1 parent 720a795 commit ed1904a

File tree

4 files changed

+232
-0
lines changed

4 files changed

+232
-0
lines changed

api/1-Utility.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Utility = function (obj) {
2+
var that = this;
3+
var _gms = [];
4+
var _cmds = {};
5+
6+
on('chat:message', function (msg) {
7+
if (msg.type != 'api')
8+
return;
9+
10+
var argv = msg.content.split(' ');
11+
var args = _.map(argv, function(e, i, a) { return _.rest(a, i).join(' '); });
12+
var cmd = argv[0].toLowerCase().substr(1).replace(/[^a-z0-9]/g, '');
13+
14+
if (_.has(_cmds, cmd)) {
15+
if (_cmds[cmd].restricted && !U.isGM(msg.playerid)) {
16+
U.chat('Server', msg.playerid, 'You do not have permission to use !' + cmd);
17+
return;
18+
}
19+
20+
var thisObj = {cmd: cmd, msg: msg, data: _cmds[cmd].data};
21+
_.bind(_cmds[cmd].callback, thisObj)(argv, args);
22+
} else {
23+
U.chat('Server', msg.playerid, 'Unknown command !' + cmd);
24+
}
25+
});
26+
27+
return {
28+
extractJSON: function (str) {
29+
var s = decodeURIComponent(str);
30+
s = s.replace(/^SINGLESPACED!!/, '');
31+
s = s.replace(/<\/?[^>]+>/g, '');
32+
s = s.replace(/^.*?###/, '');
33+
return JSON.parse(s);
34+
},
35+
36+
setGMs: function (gms) {
37+
if (_gms == []) {
38+
_gms = gms;
39+
}
40+
},
41+
42+
isGM: function (playerid) {
43+
for (var i in _gms) {
44+
if (_gms[i] == playerid)
45+
return true;
46+
}
47+
48+
return false;
49+
},
50+
51+
chat: function (from, to, msg) {
52+
var p = getObj('player', to);
53+
sendChat(from, '/w ' + p.get('_displayname') + ' ' + msg);
54+
},
55+
56+
registerCommand: function (cmd, restricted, data, callback) {
57+
var cmdobj = {};
58+
if (_.isFunction(callback)) {
59+
cmdobj.restricted = (restricted === true) ? true : false;
60+
cmdobj.data = data;
61+
cmdobj.callback = callback;
62+
} else if (_.isFunction(data)) {
63+
cmdobj.restricted = (restricted === true) ? true : false;
64+
cmdobj.data = null;
65+
cmdobj.callback = data;
66+
} else if (_.isFunction(restricted)) {
67+
cmdobj.restricted = false;
68+
cmdobj.data = null;
69+
cmdobj.callback = restricted;
70+
}
71+
72+
_cmds[cmd.toLowerCase()] = cmdobj;
73+
}
74+
};
75+
};
76+
77+
U = new Utility();

api/2-Config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//--------------------------
2+
// Configuration
3+
//--------------------------
4+
5+
// 1. GameMasters
6+
// Set this to an array of playerids for each GM in your campaign, one per line
7+
// The default value is my playerid just to give an example of what goes there,
8+
// you will almost certainly want to replace it with your own playerid.
9+
// Use the !playerid command to retrieve your player id if you do not know it
10+
// (!playerid is part of the GMCommands script)
11+
U.setGMs([
12+
'-J3ULS3Mv7bHztlCKpu7', // skizzerz
13+
]);

api/GMCommands.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Commands useful for GMs. A [GM] next to the command name indicates it can only be used by GMs.
3+
* To register yourself as a GM, insert your player id in the configuration script 2-Config.js
4+
* !playerid, Returns the player id of the current player
5+
* !eval [GM], Evaluates javascript code. Use the arguments array to access data passed in.
6+
* - arguments[0] is the message object
7+
* !state [GM], Returns a JSON representation of the state variable or portion thereof.
8+
* Example: !state players 0 attrs str to return the strength of player 0 if the state
9+
* variable were to look like {players: [{attrs: {str: 10}}]}
10+
*/
11+
(function() {
12+
U.registerCommand('eval', true, function (argv, args) {
13+
try {
14+
var f = new Function(args[1]);
15+
f(this.msg);
16+
} catch (e) {
17+
U.chat('Server', this.msg.playerid, e.name + ': ' + e.message);
18+
var lines = e.stack.split("\n");
19+
for (var i in lines) {
20+
log(lines[i]);
21+
}
22+
}
23+
});
24+
25+
U.registerCommand('state', true, function (argv) {
26+
var what = state;
27+
var a = _.rest(argv);
28+
for (var i in a) {
29+
if (_.has(what, a[i])) {
30+
what = what[a[i]];
31+
} else {
32+
U.chat('Server', this.msg.playerid, 'State variable not found');
33+
return;
34+
}
35+
}
36+
U.chat('Server', this.msg.playerid, JSON.stringify(state));
37+
});
38+
39+
U.registerCommand('playerid', function (argv) {
40+
U.chat('Server', this.msg.playerid, '-' + this.msg.playerid);
41+
});
42+
})();

api/Light.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Adds 3 commands related to lighting. A [GM] next to a command name means it is GM-only.
3+
* !light, to be used by players to set their token's light within defined boundaries
4+
* !flight [GM], to be used by GMs to change any token's light value arbitrarily
5+
* !todl [GM], to be used by GMs to move a token into the dynamic lighting layer so it
6+
* can still provide light but the token itself is invisible to players
7+
*/
8+
(function () {
9+
U.registerCommand('light', false, false, doLight);
10+
U.registerCommand('flight', true, true, doLight);
11+
12+
U.registerCommand('todl', true, function (argv) {
13+
var selected = this.msg.selected,
14+
playerid = this.msg.playerid;
15+
16+
_.each(selected, function(obj) {
17+
if (obj._type != 'graphic') return; // only move graphics
18+
var tok = getObj('graphic', obj._id);
19+
if (tok.get('subtype') != 'token') return; // don't try to move cards
20+
21+
// move to DL layer and make it so that all players can see light
22+
// can manage light level with !flight
23+
tok.set('layer', 'walls');
24+
tok.set('light_otherplayers', true);
25+
});
26+
27+
U.chat('!todl', playerid, 'Success');
28+
});
29+
30+
function doLight(argv) {
31+
var selected = this.msg.selected,
32+
playerid = this.msg.playerid,
33+
cmd = this.cmd,
34+
override = this.data;
35+
36+
var lightRadius = isNaN(Math.abs(argv[1])) ? false : Math.abs(argv[1]);
37+
var lightDimRad = isNaN(Math.abs(argv[2])) ? false : Math.abs(argv[2]);
38+
if (!selected || lightRadius === false) {
39+
U.chat(cmd, playerid, '<br><i>Usage: ' + cmd +
40+
' &' + 'lt;light radius&' + 'gt; [start of dim light]</i><br>' +
41+
'Use with the token you wish to modify selected');
42+
return;
43+
}
44+
45+
_.each(selected, function(obj) {
46+
var settings, tok, char;
47+
48+
if (obj._type != 'graphic') return; // only light graphics
49+
50+
tok = getObj('graphic', obj._id);
51+
if (tok.get('subtype') != 'token') return; // don't try to light cards
52+
53+
if (tok.get('represents') != '') {
54+
char = getObj('character', tok.get('represents'));
55+
settings = U.extractJSON(char.get('gmnotes')) || {};
56+
var owners = char.get('controlledby');
57+
if (!U.isGM(playerid) && owners.indexOf(playerid) == -1) {
58+
return; // don't own this object
59+
}
60+
} else if (!override) {
61+
return; // players can't light non-character-tokens with this
62+
}
63+
64+
// do limits if player (GM can do whatever)
65+
if (!override) {
66+
if (!settings.light_max && !settings.light_dim_max) {
67+
U.chat(cmd, playerid, 'You may not adjust light settings for this token');
68+
return;
69+
}
70+
71+
if (lightDimRad !== false) {
72+
if (lightDimRad > settings.light_max) {
73+
U.chat(cmd, playerid, 'You may not set the dim radius to above ' + settings.light_max);
74+
return;
75+
}
76+
77+
if (lightRadius > settings.light_dim_max) {
78+
U.chat(cmd, playerid, 'You may not set the light radius to above ' + settings.light_dim_max);
79+
return;
80+
}
81+
} else {
82+
if (lightRadius > settings.light_max) {
83+
U.chat(cmd, playerid,
84+
'You may not set the light radius to above ' + settings.light_max +
85+
' if you do not specify a dim radius');
86+
return;
87+
}
88+
}
89+
}
90+
91+
tok.set('light_radius', lightRadius);
92+
93+
if (lightDimRad !== false) {
94+
tok.set('light_dimradius', lightDimRad);
95+
} else {
96+
tok.set('light_dimradius', '');
97+
}
98+
});
99+
}
100+
})();

0 commit comments

Comments
 (0)