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
|
const fs = require('fs')
const path = require("path");
let config_loc = __filename+".json"
const { PermissionsBitField, EmbedBuilder, AttachmentBuilder,ActionRowBuilder,ButtonBuilder,ButtonStyle } = require('discord.js');
let config = JSON.parse(fs.readFileSync(config_loc))
const util = require("../../src/util.js")
const sharp = require("sharp");
const llog = require("../../src/logg")
const db = require("../../src/db.js")
module.exports = {
name : "battleship",
command: ["battleship"],
mod_only: true,
config:config,
config_loc:config_loc,
async main (client,Discord,message,args){
//let uid2 = args[1];
//if(uid2[0]=='<') uid2 = (args[1].substring(2,args[1].length-1))
this.exec(client,{action:args[0],message:message,id:args[1]})
},
async board_prev(args){
const move_y = 82
const move_x = 66
let pos = {x:0,rx:32,y:0,ry:14,rotated:false}
let max = {x:8,y:7}
let embed = new EmbedBuilder()
.setTitle("battleship")
.setImage("attachment://battleship-board.png")
const left = new ButtonBuilder()
.setCustomId('left')
.setEmoji('⬅️')
.setStyle(ButtonStyle.Primary);
const up = new ButtonBuilder()
.setCustomId('up')
.setEmoji('⬆️')
.setStyle(ButtonStyle.Primary);
const down = new ButtonBuilder()
.setCustomId('down')
.setEmoji('⬇️')
.setStyle(ButtonStyle.Primary);
const right = new ButtonBuilder()
.setCustomId('right')
.setEmoji('➡️')
.setStyle(ButtonStyle.Primary);
const turn = new ButtonBuilder()
.setCustomId('turn')
.setEmoji('🔄')
.setStyle(ButtonStyle.Primary);
const row1 = new ActionRowBuilder()
.addComponents(left,up,down,right,turn);
const shoot = new ButtonBuilder()
.setCustomId('shoot')
.setEmoji('🎯')
.setStyle(ButtonStyle.Primary);
const row2 = new ActionRowBuilder()
.addComponents(shoot);
//! TODO: randomize file name
let to_add = [{
input: "./img/battleship-board-sel.png",
top: pos.rx,
left: pos.ry,
},]
if(args.len!=null){
for(let i = 1; i<args.len; i++){
to_add.push({
input: "./img/battleship-board-sel.png",
top: pos.rotated?pos.rx:pos.rx+(move_x*i),
left: !pos.rotated?pos.ry:pos.ry+(move_y*i),
})
}
}
args.len ??= 0;
await sharp("./img/battleship-board.png")
.composite(to_add).toFile("/tmp/battleship-board.png");
const file = new AttachmentBuilder('/tmp/battleship-board.png');
let mess = args.mm;
if(args.mm==null)
mess = await args.message.reply({embeds:[embed],files:[file],components:[row1,row2],ephemeral:true});
else mess = await args.mm.edit({embeds:[embed],files:[file],components:[row1,row2]});
args.mm = mess;
async function rec_edit(mess){
const collectorFilter = i => i.user.id === args.message.author.id;
try {
const confirmation = await mess.awaitMessageComponent({ filter: collectorFilter, time: 60000000 });
let oldpos = util.deepCopy(pos);
if(confirmation.customId == "down"){
pos.x--;
pos.rx+=move_x;
} else if(confirmation.customId == "up"){
pos.x++;
pos.rx-=move_x;
} else if(confirmation.customId == "right"){
pos.y++;
pos.ry+=move_y;
} else if(confirmation.customId == "left"){
pos.y--;
pos.ry-=move_y;
} else if(confirmation.customId == "shoot"){
//await mess.delete()
pos.mm = mess;
await confirmation.deferUpdate()
return pos;
} else if(confirmation.customId == "turn"){
pos.rotated = !pos.rotated
}
let outofbounds = false
if(!pos.rotated||args.len==0){
outofbounds ||= (args.len!=0&&-pos.x+args.len>max.x);
outofbounds ||= (args.len==0&&-pos.x>=max.x);
outofbounds ||= (pos.y>=max.y);
} else {
outofbounds ||= (args.len!=0&&pos.y+args.len>max.y);
//outofbounds ||= (args.len==0&&pos.y>max.y);
outofbounds ||= (-pos.x>=max.x);
}
outofbounds ||= (pos.x==1||pos.y==-1);
if(outofbounds){
pos = oldpos;
await confirmation.deferUpdate()
return rec_edit(mess)
}
let to_add = [{
input: "./img/battleship-board-sel.png",
top: pos.rx,
left: pos.ry,
},
]
if(args.len!=null&&args.len!=0){
for(let i = 1; i<args.len; i++){
to_add.push({
input: "./img/battleship-board-sel.png",
top: pos.rotated?pos.rx:pos.rx+(move_x*i),
left: !pos.rotated?pos.ry:pos.ry+(move_y*i),
})
}
}
await sharp("./img/battleship-board.png")
.composite(to_add).toFile("/tmp/battleship-board.png");
const file = new AttachmentBuilder('/tmp/battleship-board.png');
await mess.edit({embeds:[embed],files:[file], components: [row1,row2]});
await confirmation.deferUpdate()
return rec_edit(mess)
} catch (e) {
llog.error(e)
}
}
return await rec_edit(mess)
},
async exec(client,args){
switch(args.action){
case 'aim':
break;
case 'create':
case 'join':
let bttl_db;
if(args.action=='join'){
bttl_db = await db.BattleShip.findAll({where:{_id:args.id,p2_id:'null',status:"open"}})
if(bttl_db.length==0)
return args.message.reply({content:"id not found!",ephemeral:true})
}
let game_test = await db.BattleShip.findAll({where:{p1_id:args.message.author.id,status:"open"}})
if(game_test.length!=0)
return args.message.reply({content:"you are already in a game!",ephemeral:true})
let planned = [4,3,2,1]
let placements = []
let newboard = []
for(let i = 0; i!=8; i++){
newboard.push([])
for(let z = 0; z!=7; z++)
newboard[i].push('.');
}
args.rotated = false;
let temp;
for(let l of planned){
args.len = l
temp = await this.board_prev(args)
//console.log(temp)
temp.len = l;
placements.push(temp)
}
for(let p of placements){
if(p.rotated){
for(let i = 0; i!=p.len; i++)
newboard[-p.x][p.y+i] = 'b'
} else {
for(let i = 0; i!=p.len; i++)
newboard[-p.x+i][p.y] = 'b'
}
}
if(args.action=='create'){
let id = this.generate_id();
await temp.mm.edit({embeds:[],files:[],components:[],content:"your invite id is "+id+", send it to a friend to play!"})
db.BattleShip.create({
status:"open",
_id:id,
turn:0,
p1_id:args.message.author.id,
p2_id:'null',
p1_board:JSON.stringify(newboard),
p2_board:'null',
})
} else if(args.action=='join'){
await temp.mm.edit({embeds:[],files:[],components:[],content:"game created! you can now play, try `sns help battleship` for more info!"})
db.BattleShip.update({
p2_id:args.message.author.id,
p2_board:JSON.stringify(newboard)
},{where:{id:bttl_db[0].id}})
}
break;
}
},
generate_id(){
//taken from ./events/dm.js you should standardize it
var ticket = "";
var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 7; i++) {
ticket += characters.charAt(Math.floor(Math.random() * characters.length));
if (i == 4) ticket += "-";
}
return ticket
}
}
|