-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
298 lines (262 loc) · 9.13 KB
/
index.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
const Discord= require('discord.js');
const path = require('path');
const fs = require('fs');
const randomInt = require('random-int');
var glob = require("glob");
require("dotenv").config()
const bot = new Discord.Client();
const imgfolder = "/home/vliaskovitis/Documents/rpg/BPG001_Shab-al-Hiri-Roach/";
//const imgfolder = "/home/vliaskovitis/Documents/rpg/CthulhuConfidential-Test/";
/*
createdeck suspects /home/vliaskovitis/Documents/rpg/ALICE/suspects 1
createdeck suspects /home/vliaskovitis/Documents/rpg/ALICE/suspects 0
createdeck locations /home/vliaskovitis/Documents/rpg/ALICE/locations 1
createdeck locations /home/vliaskovitis/Documents/rpg/ALICE/locations 0
createdeck search /home/vliaskovitis/Documents/rpg/ALICE/search 1
createdeck time /home/vliaskovitis/Documents/rpg/ALICE/time 1
createdeck characters /home/vliaskovitis/Documents/rpg/ALICE/characters 1
createdeck finalsuspects sfsdfds 1
createdeck finallocations sdfsdfsd 1
addcard /home/vliaskovitis/Documents/rpg/ALICE/suspects/teacher.png finalsuspects
*/
var drawn = []
var decks = []
var decksizes = []
var decknames = []
var matches;
var Jimp = require('jimp');
const token = '';
globaldeckid = 0;
function findFiles(startPath, filter, deckname, needreset){
reset = parseInt(needreset);
matches=0;
newdeck = 0;
deckid = findDeck(deckname);
if (deckid == -1) {
deckid = globaldeckid;
decks[deckid] = [];
deck = decks[deckid];
decknames[deckid] = deckname;
decksizes[deckid] = 0;
newdeck = 1;
}
if (deckid != -1 && reset) {
decks[deckid] = [];
deck = decks[deckid];
decksizes[deckid] = 0;
}
if (!fs.existsSync(startPath)){
console.log("no directory ",startPath);
//return;
} else {
files = fs.readdirSync(startPath);
for (var i=0;i<files.length;i++){
var filename=path.join(startPath,files[i]);
if (filename.indexOf(filter)>=0) {
console.log('-- found: ',filename);
drawn[matches]=0;
deck[matches]=filename;
matches++;
}
}
}
decksizes[deckid] += matches;
if (newdeck)
globaldeckid++;
console.log("Deck ", deckname, " has ", decksizes[deckid], " cards");
return matches;
}
function addCardToDeck(imgfolder, deckname, cardname){
var startPath = imgfolder;
var path = startPath.concat(cardname.trim());
path = path.concat(".png");
if (!fs.existsSync(path)){
console.log("File not found: ",path, " for card: ", cardname);
return;
} else {
deckid = findDeck(deckname);
deck = decks[deckid];
decksize = decksizes[deckid];
deck[decksize] = path;
console.log("before addcardtodeck ", path , " Deck ", deckname, " has ", decksizes[deckid], " cards");
decksizes[deckid]++;
console.log("after addcardtodeck ", path, " Deck ", deckname, " has ", decksizes[deckid], " cards");
}
}
function findDeck(deckname) {
for (var i=0; i<decknames.length; i++) {
if (!decknames[i].localeCompare(deckname)) {
console.log("Deck found", deckname);
return i;
}
}
console.log("Deck not found", deckname);
return -1;
}
function addRandomFromSet(imgfolder, deckname, string) {
deckid = findDeck(deckname);
deck = decks[deckid];
decksize = decksizes[deckid];
glob(imgfolder + '*' + string + '*png', {}, (err, files)=>{
cardid = randomInt(files.length - 1);
deck[decksize] = files[cardid];
console.log("adding ", files[cardid], "to deck: ", deckname);
decksizes[deckid]++;
})
}
function drawSpecificCard(msg, imgfolder, deckname, name){
deckid = findDeck(deckname);
deck = decks[deckid];
decksize = decksizes[deckid];
if(decksize == 0){
console.log("Deck is empty.");
}
else {
console.log("Card argument:", name);
var startPath = imgfolder;
var path = startPath.concat("/");
path = path.concat(name);
path = path.concat(".png");
//console.log("Draw card:", path);
if (!fs.existsSync(path)){
console.log("File not found");
return;
}
var cardid=-1;
for(var i=0; i<decksize-1; i++) {
if (!path.localeCompare(deck[i]))
cardid = i;
}
if (cardid == -1) {
console.log("Card not found");
return;
}
msg.author.send("You draw a card:", {files: [path]});
console.log("Draw card:", path, " id ", cardid);
for(var i=cardid; i<decksize-1; i++) {
deck[i]=deck[i+1];
}
decksizes[deckid]--;
}
}
function drawMatchingRegexCard(msg, imgfolder, deckname, match){
var foundmatch = 0;
deckid = findDeck(deckname);
deck = decks[deckid];
decksize = decksizes[deckid];
if(decksize == 0){
console.log("Deck is empty.");
}
else {
for (var i=0; i<= decksize - 1; i++) {
if(deck[i].includes(match)) {
console.log("Card found matching pattern:", deck[i]);
var path = deck[i];
if (!fs.existsSync(path)){
console.log("File not found");
return;
}
cardid=i;
msg.author.send("You draw a card:", {files: [path]});
console.log("Draw card:", path, " id ", cardid);
for(var j=cardid; j<decksize-1; j++) {
deck[j]=deck[j+1];
}
decksizes[deckid]--;
foundmatch = 1;
break;
}
}
}
if (foundmatch == 0) {
console.log("Cards containing pattern ", match, "not found");
return;
}
}
function drawCard(msg, hidden, rotate, deckname){
deckid = findDeck(deckname);
deck = decks[deckid];
decksize = decksizes[deckid];
if(decksize == 0){
console.log("Deck is empty.");
}
else {
var cardid=0;
cardid = randomInt(decksize-1);
var card = deck[cardid];
console.log('draws card! ', cardid, " ", card);
if (rotate) {
var rotated = "/mnt/devel/discord-deckmaster/tmp.png";
console.log('draws card! ', cardid, " ", card);
Jimp.read(card, (err, rotatedimage) => {
if (err) throw err;
rotatedimage
.rotate(180)
.write(rotated, () => {
console.log('written rotated card!');
rotatedimage_written = 1;
if (hidden == 1) {
msg.author.send("Same card rotated:", {files: [rotated]});
}
else {
msg.channel.send("Same card rotated:", {files: [rotated]});
}
}
); // save
});
}
if (hidden == 1) {
msg.author.send("You draw a card:", {files: [card]});
} else {
msg.channel.send("You draw a card:", {files: [card]});
}
for(var i=cardid; i<decksize-1; i++) {
deck[i]=deck[i+1];
}
decksizes[deckid]--;
}
}
bot.on('ready', () =>{
console.log('This bot is online!');
});
bot.on('message', msg=>{
if (msg.content.includes("createdeck")){
words = msg.content.split(/\s+/);
findFiles(words[2], '.png', words[1], words[3]);
console.log("Found: ",matches, " files");
}
if (msg.content.includes("drawrotate")){
words = msg.content.split(/\s+/);
drawCard(msg, 1, 1, words[1]);
}
else if (msg.content.includes("drawspecific")){
words = msg.content.split(/\s+/);
drawSpecificCard(msg, words[1], words[2], words[3]);
}
else if (msg.content.includes("drawcard")){
words = msg.content.split(/\s+/);
drawCard(msg, 1, 0, words[1]);
}
else if (msg.content.includes("drawtime")){
words = msg.content.split(/\s+/);
//drawSpecificCard(msg, "/home/vliaskovitis/Documents/rpg/ALICE/time", "time", words[1]);
drawMatchingRegexCard(msg, "/home/vliaskovitis/Documents/rpg/ALICE/time", "time", words[1]);
}
if (msg.content.includes("flip")){
words = msg.content.split(/\s+/);
drawCard(msg, 0, 0, words[1]);
}
if (msg.content.includes("addfinalsuspect")){
words = msg.content.split(/\s+/);
addCardToDeck("/home/vliaskovitis/Documents/rpg/ALICE/suspects/", "finalsuspects", words[1]);
}
if (msg.content.includes("addfinallocation")){
words = msg.content.split(/\s+/);
addCardToDeck("/home/vliaskovitis/Documents/rpg/ALICE/locations/", "finallocations", words[1]);
}
if (msg.content.includes("addtimefromset")){
words = msg.content.split(/\s+/);
addRandomFromSet("/home/vliaskovitis/Documents/rpg/ALICE/time/", "time", words[1]);
}
})
bot.login(process.env.BOT_TOKEN);