-
Notifications
You must be signed in to change notification settings - Fork 2
/
gloomhaven.js
156 lines (132 loc) · 4.38 KB
/
gloomhaven.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
//var Gloomhaven = Gloomhaven || (function() {
var Gloomhaven = (function () {
var GLOOMHAVEN_COMMAND = "!GH";
var GLOOMHAVEN_COMMANDS = {};
function numify(x){
var xNum = x;
if (typeof(x) == typeof("")){
if (x.charAt(0) == "+"){ x = x.substring(1); }
xNum = parseFloat(x);
}
if ("" + xNum == "" + x){ return xNum; }
return x;
}
function getCleanImgsrc(imgsrc) {
var parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^?]*)(\?[^?]+)?$/);
if(parts) {
return parts[1]+"thumb"+parts[3]+(parts[4]?parts[4]:`?${Math.round(Math.random()*9999999)}`);
}
return;
}
function rawWrite(s, who, style, from) {
if (who) {
who = "/w " + who.split(" ", 1)[0] + " ";
}
sendChat(from, who + s.replace(/\n/g, "<br>"));
}
function write(s, who, style, from) {
return rawWrite(s.replace(/</g, "<").replace(/>/g, ">"), who, style, from);
}
// https://gist.github.com/kkragenbrink/5499147
function sprintf(f) {
var formatRegexp = /%[sdj%]/g;
var args = Array.prototype.slice.call(arguments, 0);
var argl = args.length;
if (typeof f !== 'string') {
var objects = [];
while (argl--) {
objects.unshift(args[i].toString());
}
return objects.join(' ');
}
var i = 1;
var str = String(f).replace(formatRegexp, function (x) {
if (x === '%%') {
return '%';
}
if (i >= args) {
return x;
}
switch (x) {
case '%s' : return String(args[i++]);
case '%d' : return Number(args[i++]);
case '%j' : return JSON.stringify(args[i++]);
default:
return x;
}
});
var x;
while (i++ < argl) {
x = args[i];
if (x === null || typeof x !== 'object') {
str = [str, x].join(' ')
} else {
str += [str, x.toString()].join();
}
}
return str;
}
function showHelp(who) {
var helpMsg = "";
helpMsg += "TODO: print out allowed commands";
write(helpMsg, who, "", "GH");
}
function handleGloomhavenMessage(tokens, msg) {
var command = tokens[1];
var args = tokens.slice(2);
write("Command: " + command, msg.who, "", "OP");
write("Args: " + args, msg.who, "", "OP");
if (command === undefined) {
showHelp(msg.who);
return;
}
if (GLOOMHAVEN_COMMANDS[command] === undefined) {
write("Unrecognized command '" + command + "'", msg.who, "", "OP");
showHelp(msg.who);
return;
}
GLOOMHAVEN_COMMANDS[command].run(msg, args);
return;
}
function handleChatMessage(msg) {
if ((msg.type !== "api") || msg.content.indexOf(GLOOMHAVEN_COMMAND) !== 0) {
return;
}
return handleGloomhavenMessage(msg.content.split(" "), msg);
}
function registerCommands() {
/* eslint-disable no-undef */
GLOOMHAVEN_COMMANDS["set-level"] = GloomhavenSetLevelCommand;
GLOOMHAVEN_COMMANDS["monster-card"] = GloomhavenMonsterCardCommand;
GLOOMHAVEN_COMMANDS["build-deck"] = GloomhavenDeckBuilderCommand;
/* eslint-enable no-undef */
if ((typeof(Shell) != "undefined") && (Shell) && (Shell.registerCommand)) {
Shell.registerCommand(GLOOMHAVEN_COMMAND, "!GH command", "Gloomhaven command",
handleGloomhavenMessage);
/* eslint-disable no-func-assign */
if (Shell.rawWrite) {
rawWrite = Shell.rawWrite;
}
if (Shell.write) {
write = Shell.write;
}
/* eslint-enable no-func-assign */
}
else{
on("chat:message", handleChatMessage);
}
}
var scenarioLevel = 1;
return {
scenarioLevel: scenarioLevel,
registerCommands: registerCommands,
numify: numify,
sprintf: sprintf,
write: write,
rawWrite: rawWrite,
getCleanImgsrc: getCleanImgsrc
};
}());
on("ready", function () {
Gloomhaven.registerCommands();
});