// Extract the required classes from the discord.js module
const Discord = require('discord.js');
const rp = require('request-promise');
const emojiCharacters = require('./emojiCharacters');
// Create an instance of a Discord client
const client = new Discord.Client();
const botName = 'M4ndU';
const devURL = 'https://mandu-mandu.tistory.com';
const yatchURL = 'http://127.0.0.1:3400';
const token = 'TOKEN_HERE';
const PREFIX = '?';
function showScore(game, i){
if (game.scorecard[i].locked === true) {
return game.scorecard[i].points;
} else {
var score = '0';
if (game.scorecard[i].points===null) {
score = 0;
}else {
score = game.scorecard[i].points;
}
return "0(+"+score+")";
}
}
function showScoreEmoji(game, i){
if (game.scorecard[i].locked === true) {
return ' ✅ ';
} else {
return ' ❌ ';
}
}
function showBonusScore(game){
if (game.scorecard[7].points===null) {
return `0(${game.scorecard[6].points}/63)(+35)`;
} else {
return `${game.scorecard[7].points===null?0:game.scorecard[7].points}(${game.scorecard[6].points===null?0:game.scorecard[6].points}/63)`;
}
}
function showBonusEmoji(game){
if (game.scorecard[7].points===null) {
return ' ❌ ';
} else if (game.scorecard[7].points=== 0) {
return ' ⛔️ ';
} else {
return ' ✅ ';
}
}
function embedScoreMsg(newGame, r, log, turn){
var dices = "";
for (var i = 0; i < 5; i++) {
dices += emojiCharacters[`${newGame.round.dice[i].value}`] +' ';
}
var gameBoardEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle(`야추! | 턴 ${turn}/13`)
.setDescription('입력시간제한 : 1분')
.setAuthor(botName, '', 'http://mandu-mandu.tistory.com/')
.addFields(
{ name: '주사위 🎲 ', value: `${dices}`, inline: true },
{ name: '남은 횟수 🤹 ', value: `${r}`, inline: true },
{ name: '점수 합계 🏆 ', value: `${newGame.scorecard[15].points===null?0:newGame.scorecard[15].points}`, inline: true },
{ name: '-----------', value: '점수판', inline: false },
{ name: `#1: Ones${showScoreEmoji(newGame, 0)}`, value: `점수: ${ showScore(newGame, 0)}`, inline: true },
{ name: `#2: Twos${showScoreEmoji(newGame, 1)}`, value: `점수: ${ showScore(newGame, 1)}`, inline: true },
{ name: `#3: Threes${showScoreEmoji(newGame, 2)}`, value: `점수: ${ showScore(newGame, 2)}`, inline: true },
{ name: `#4: Fours${showScoreEmoji(newGame, 3)}`, value: `점수: ${ showScore(newGame, 3)}`, inline: true },
{ name: `#5: Fives${showScoreEmoji(newGame, 4)}`, value: `점수: ${ showScore(newGame, 4)}`, inline: true },
{ name: `#6: Sixs${showScoreEmoji(newGame, 5)}`, value: `점수: ${ showScore(newGame, 5)}`, inline: true },
{ name: `1-6 보너스!${showBonusEmoji(newGame)}`, value: `점수:${ showBonusScore(newGame)}`, inline: false },
{ name: `#7: Three of a Kind${showScoreEmoji(newGame, 8)}`, value: `점수: ${ showScore(newGame, 8)}`, inline: true },
{ name: `#8: Four of a Kind${showScoreEmoji(newGame, 9)}`, value: `점수: ${ showScore(newGame, 9)}`, inline: true },
{ name: `#9: Full House${showScoreEmoji(newGame, 10)}`, value: `점수: ${ showScore(newGame, 10)}`, inline: true },
{ name: `#10: Small Straight${showScoreEmoji(newGame, 11)}`, value: `점수: ${ showScore(newGame, 11)}`, inline: true },
{ name: `#11: Large Straight${showScoreEmoji(newGame, 12)}`, value: `점수: ${ showScore(newGame, 12)}`, inline: true },
{ name: `#12: Chance${showScoreEmoji(newGame, 13)}`, value: `점수: ${ showScore(newGame, 13)}`, inline: true },
{ name: `#13: Yahtzee${showScoreEmoji(newGame, 14)}`, value: `점수: ${ showScore(newGame, 14)}`, inline: true },
{ name: '기록 📃', value: `${log}`, inline: false },
)
.setFooter('Made by M4ndU', '');
return gameBoardEmbed;
}
client.on('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', async msg => {
if (msg.content.startsWith(PREFIX)) {
const input = msg.content.slice(PREFIX.length).split(' ');
var command = input.shift();
var commandArgs = input;
if (command === '야추') {
//게임 시작
const newGame = await rp({uri:yatchURL + '/api/game'}).then(function (repos) {
return JSON.parse(repos);
});
var gameBoardEmbed = embedScoreMsg(newGame, 2,'.', 1);
var yachtmsg = await msg.channel.send(gameBoardEmbed).then(msg =>{
return msg;
});
Promise.all([
yachtmsg.react(emojiCharacters['1']),
yachtmsg.react(emojiCharacters['2']),
yachtmsg.react(emojiCharacters['3']),
yachtmsg.react(emojiCharacters['4']),
yachtmsg.react(emojiCharacters['5']),
yachtmsg.react('🔁'), //주사위 굴리기
yachtmsg.react('🔴'), //점수 바로 등록하기
]);
//init
var yachtGame = newGame;
const filter = (reaction, user) => {
return ['🔁','🔴'].includes(reaction.emoji.name) && user.id === msg.author.id;
};
var stopFlag = false;
var exit = false;
var options = {
method: 'POST',
uri:yatchURL + '/api/game/roll',
body: yachtGame,
json: true // Automatically stringifies the body to JSON
};
const filterm = response => {
return ['1','2','3','4','5','6','7','8','9','10','11','12','13'].includes(response.content) && response.author.id === msg.author.id;
};
var choose = '0';
var userReactions = new Object();
//init
//13턴 돌립니다.
for (var gameTurn = 1; gameTurn < 14; gameTurn++) {
//주사위 3라운드 굴리기
for (var g = 2; g > 0; g--) {
await yachtmsg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(async function(collected) {
const reaction = collected.first();
if (reaction.emoji.name === '🔁') {
for (var yrdl = 0; yrdl < 5; yrdl++) {
yachtGame.round.dice[yrdl].locked = yachtmsg.reactions.cache.get(emojiCharacters[`${yrdl+1}`]).users.cache.has(msg.author.id);
}
//roll
} else {
stopFlag = true;
g = 1;
}
}).catch(async function(collected) {
gameBoardEmbed = embedScoreMsg(yachtGame, 0,'시간초과.', gameTurn);
yachtmsg.edit(gameBoardEmbed);
stopFlag = true;
exit = true;
});
options = {
method: 'POST',
uri:yatchURL + '/api/game/roll',
body: yachtGame,
json: true // Automatically stringifies the body to JSON
};
if (exit) {
break;
}
if (!stopFlag) {
yachtGame = await rp(options).then(function (repos) {
return repos;
});
}
if (g === 1) {
gameBoardEmbed = embedScoreMsg(yachtGame, g-1,'점수로 등록할 번호를 입력하세요.', gameTurn);
await yachtmsg.edit(gameBoardEmbed);
break;
} else {
gameBoardEmbed = embedScoreMsg(yachtGame, g-1,'잠시 기다려주세요', gameTurn);
}
await yachtmsg.edit(gameBoardEmbed);
//2 react remove
await yachtmsg.reactions.resolve('🔁').users.remove(msg.author.id);
await yachtmsg.reactions.resolve('🔴').users.remove(msg.author.id);
gameBoardEmbed = embedScoreMsg(yachtGame, g-1,'아래 이모지를 클릭해주세요.', gameTurn);
await yachtmsg.edit(gameBoardEmbed);
}
if (exit) {
break;
}
//점수 등록과정
choose = '0';
stopFlag = false;
for (var rcc = 0; rcc < 5; rcc++) {
await msg.channel.awaitMessages(filterm, { max: 1, time: 60000, errors: ['time'] })
.then(async collected => {
var resmsg = collected.first();
if (isNaN(resmsg.content) || Number(resmsg.content) > 14 || Number(resmsg.content) < 1 || !Number.isInteger(Number(resmsg.content))) {
gameBoardEmbed = embedScoreMsg(yachtGame, 0,'1에서 14사이의 정수를 입력.');
yachtmsg.edit(gameBoardEmbed);
}else {
if (resmsg.content < 7) {
choose = Number(resmsg.content) -1;
} else {
choose = Number(resmsg.content) +1;
}
if (yachtGame.scorecard[Number(choose)].locked) {
gameBoardEmbed = embedScoreMsg(yachtGame, 0,'이미 등록된 번호. 다른번호로 입력.', gameTurn);
yachtmsg.edit(gameBoardEmbed);
}else {
options = {
method: 'PUT',
uri:yatchURL + '/api/game/select/' + yachtGame.scorecard[choose].id,
body: yachtGame,
json: true // Automatically stringifies the body to JSON
};
yachtGame = await rp(options).then(function (repos) {
return repos;
});
stopFlag = true;
}
}
resmsg.delete();
})
.catch(collected => {
gameBoardEmbed = embedScoreMsg(yachtGame, 0,'시간초과.', gameTurn);
yachtmsg.edit(gameBoardEmbed);
stopFlag = true;
exit = true;
});
if (stopFlag || exit) {
break;
}
}
stopFlag = false;
if (exit) {
break;
}
if (gameTurn === 13) {
gameBoardEmbed = embedScoreMsg(yachtGame, 0,'게임종료', gameTurn);
break;
}else {
gameBoardEmbed = embedScoreMsg(yachtGame, 2,'기다려주세요', gameTurn+1);
}
await yachtmsg.edit(gameBoardEmbed);
//all react remove
await yachtmsg.reactions.resolve(emojiCharacters['1']).users.remove(msg.author.id);
await yachtmsg.reactions.resolve(emojiCharacters['2']).users.remove(msg.author.id);
await yachtmsg.reactions.resolve(emojiCharacters['3']).users.remove(msg.author.id);
await yachtmsg.reactions.resolve(emojiCharacters['4']).users.remove(msg.author.id);
await yachtmsg.reactions.resolve(emojiCharacters['5']).users.remove(msg.author.id);
await yachtmsg.reactions.resolve('🔁').users.remove(msg.author.id);
await yachtmsg.reactions.resolve('🔴').users.remove(msg.author.id);
gameBoardEmbed = embedScoreMsg(yachtGame, 2,'아래 이모지를 클릭!', gameTurn+1);
await yachtmsg.edit(gameBoardEmbed);
}
}
}
});
client.login(token);