forked from juma-tec/MALIK-MD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.js
263 lines (252 loc) · 8.52 KB
/
TicTacToe.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
/**
Copyright (C) 2022.
Licensed under the GPL-3.0 License;
You may not use this file except in compliance with the License.
It is supplied in the hope that it may be useful.
* @project_name : Secktor-Md
* @author : @samapndey001 <https://github.com/SamPandey001>
* @description : Secktor,A Multi-functional whatsapp bot.
* @version 0.0.6
**/
const { cmd, parseJid,getAdmin,tlang } = require("../lib/");
const eco = require('discord-mongoose-economy')
const ty = eco.connect(mongodb);
cmd({
pattern: "deltic",
desc: "deletes TicTacToe running session.",
filename: __filename,
category: "game",
},
async (Void,citel,text,{isCreator}) => {
if (!citel.isGroup) return citel.reply(tlang().group);
const groupMetadata = citel.isGroup ? await Void.groupMetadata(citel.chat).catch((e) => {}) : "";
const participants = citel.isGroup ? await groupMetadata.participants : "";
const groupAdmins = await getAdmin(Void, citel)
const isAdmins = citel.isGroup ? groupAdmins.includes(citel.sender) : false;
if(!isAdmins && !isCreator) return citel.reply('This command is only for Group Admin and my owner.')
this.game = this.game ? this.game : false
if (
Object.values(this.game).find(
(room) =>
room.id.startsWith("tictactoe")
)
) {
delete this.game
return citel.reply(`_Successfully Deleted running TicTacToe game._`);
} else {
return citel.reply(`there No TicTacToe game👾 is running.`)
}
})
cmd(
{
pattern: "tic",
desc: "Play TicTacToe",
filename: __filename,
category: "game",
},
async (Void,citel,text) => {
if (!citel.isGroup) return citel.reply(tlang().group);
let {prefix} = require('../lib')
{
let TicTacToe = require("../lib/ttt");
this.game = this.game ? this.game : {};
if (
Object.values(this.game).find(
(room) =>
room.id.startsWith("tictactoe") &&
[room.game.playerX, room.game.playerO].includes(citel.sender)
)
)
return citel.reply("_A game is already going on_");
let room = Object.values(this.game).find(
(room) =>
room.state === "WAITING" && (text ? room.name === text : true)
);
if (room) {
room.o = citel.chat;
room.game.playerO = citel.sender || citel.mentionedJid[0]
room.state = "PLAYING";
let arr = room.game.render().map((v) => {
return {
X: "❌",
O: "⭕",
1: "1️⃣",
2: "2️⃣",
3: "3️⃣",
4: "4️⃣",
5: "5️⃣",
6: "6️⃣",
7: "7️⃣",
8: "8️⃣",
9: "9️⃣",
}[v];
});
let str = `
Current turn: @${room.game.currentTurn.split("@")[0]}
Room ID: ${room.id}
${arr.slice(0, 3).join(" ")}
${arr.slice(3, 6).join(" ")}
${arr.slice(6).join(" ")}
`;
return await Void.sendMessage(citel.chat, {
text: str,
mentions: [room.game.currentTurn],
});
} else {
room = {
id: "tictactoe-" + +new Date(),
x: citel.chat,
o: "",
game: new TicTacToe(citel.sender, "o"),
state: "WAITING",
};
if (text) room.name = text;
citel.reply("_Waiting for player,use .ttt to join this game._ ");
this.game[room.id] = room;
}
}
}
);
cmd(
{
on: "text"
},
async (Void,citel,text) => {
if(!citel.isGroup) return
let {prefix} = require('../lib')
this.game = this.game ? this.game : {};
let room = Object.values(this.game).find(
(room) =>
room.id &&
room.game &&
room.state &&
room.id.startsWith("tictactoe") &&
[room.game.playerX, room.game.playerO].includes(citel.sender) &&
room.state == "PLAYING"
);
if (room) {
let ok;
let isWin = !1;
let isTie = !1;
let isSurrender = !1;
if (!/^([1-9]|(me)?give_up|surr?ender|off|skip)$/i.test(citel.text)) return;
isSurrender = !/^[1-9]$/.test(citel.text);
if (citel.sender !== room.game.currentTurn) {
if (!isSurrender) return !0;
}
if (
!isSurrender &&
1 >
(ok = room.game.turn(
citel.sender === room.game.playerO,
parseInt(citel.text) - 1
))
) {
citel.reply(
{
"-3": "The game is over.",
"-2": "Invalid",
"-1": "_Invalid Position_",
0: "_Invalid Position_",
}[ok]
);
return !0;
}
if (citel.sender === room.game.winner) isWin = true;
else if (room.game.board === 511) isTie = true;
let arr = room.game.render().map((v) => {
return {
X: "❌",
O: "⭕",
1: "1️⃣",
2: "2️⃣",
3: "3️⃣",
4: "4️⃣",
5: "5️⃣",
6: "6️⃣",
7: "7️⃣",
8: "8️⃣",
9: "9️⃣",
}[v];
});
if (isSurrender) {
room.game._currentTurn = citel.sender === room.game.playerX;
isWin = true;
}
let winner = isSurrender ? room.game.currentTurn : room.game.winner;
let str = `Room ID: ${room.id}
${arr.slice(0, 3).join(" ")}
${arr.slice(3, 6).join(" ")}
${arr.slice(6).join(" ")}
${
isWin
? `@${winner.split("@")[0]} Won ! and got 2000💎 in wallet🤑`
: isTie
? `Game Tied,well done to both of players.`
: `Current Turn ${["❌", "⭕"][1 * room.game._currentTurn]} @${
room.game.currentTurn.split("@")[0]
}`
}
⭕:- @${room.game.playerO.split("@")[0]}
❌:- @${room.game.playerX.split("@")[0]}`;
if ((room.game._currentTurn ^ isSurrender ? room.x : room.o) !== citel.chat)
room[room.game._currentTurn ^ isSurrender ? "x" : "o"] = citel.chat;
if(isWin){
await eco.give(citel.sender, "secktor", 2000);
}
if (isWin || isTie) {
await Void.sendMessage(citel.chat, {
text: str,
mentions: [room.game.playerO,room.game.playerX],
});
} else {
await Void.sendMessage(citel.chat, {
text: str,
mentions: [room.game.playerO,room.game.playerX],
});
}
if (isTie || isWin) {
delete this.game[room.id];
}
}
}
);
cmd({ pattern: "ship" , category: "fun" }, async(Void, citel, text) => {
const { tlang } = require('../lib')
if (!citel.isGroup) return citel.reply(tlang().group);
const groupMetadata = citel.isGroup ? await Void.groupMetadata(citel.chat).catch((e) => {}) : "";
const participants = citel.isGroup ? await groupMetadata.participants : "";
let members = participants.map(u => u.id)
const percentage = Math.floor(Math.random() * 100)
async function couple(percent) {
var text;
if (percent < 25) {
text = `\t\t\t\t\t*ShipCent : ${percentage}%* \n\t\tThere's still time to reconsider your choices 😂`
} else if (percent < 50) {
text = `\t\t\t\t\t*ShipCent : ${percentage}%* \n\t\t Good enough, I guess! 🤥`
} else if (percent < 75) {
text = `\t\t\t\t\t*ShipCent : ${percentage}%* \n\t\t\tStay together and you'll find a way ⭐️`
} else if (percent < 90) {
text = `\t\t\t\t\t*ShipCent : ${percentage}%* \n\tAmazing! You two will be a good couple 💖 `
} else {
text = `\t\t\t\t\t*ShipCent : ${percentage}%* \n\tYou two are fated to be together 💙`
}
return text
}
var user = citel.mentionedJid ? citel.mentionedJid[0] : citel.msg.contextInfo.participant || false;
var shiper;
if (user) {
shiper = user
} else {
shiper = members[Math.floor(Math.random() * members.length)]
}
let caption = `\t😇 *Matchmaking.*😇 \n`
caption += `\t\t✯✶⊶⊷⊶⊷❍⊶⊷⊶⊷✶✯\n`
caption += `@${citel.sender.split('@')[0]} x @${shiper.split('@')[0]}\n`
caption += `\t\t✯✶⊶⊷⊶⊷❍⊶⊷⊶⊷✶✯\n`
caption += await couple(percentage)
if(citel.sender.split('@')[0]===shiper.split('@')[0]) return citel.reply('```'+'Wait... do!,You wanna do matchmaking with yourself'+'```')
await Void.sendMessage(citel.chat,{text: caption,mentions: [citel.sender,shiper]},{quoted:citel})
}
)
// IDEA of Shipcent from => https://github.com/iamherok/WhatsApp-Botto-Ruka/blob/master/handler/message.js#L842