EC2 Default User

Set conditions for defeat

Showing 1 changed file with 231 additions and 18 deletions
......@@ -16,12 +16,13 @@ const STDICT_KEY = '14AA284251F616FF2151DBE4CD7B3D4C'
const bodyParser = require('body-parser');
const { json } = require('body-parser');
const { start } = require('repl');
const { exit } = require('process');
const { callbackify } = require('util');
var app = express();
app.use(bodyParser.json());
app.post('/hook', function (req, res) {
var eventObj = req.body.events[0];
console.log(eventObj);
var source = eventObj.source;
var message = eventObj.message;
......@@ -39,16 +40,223 @@ app.post('/hook', function (req, res) {
});
var playingId = [];
var words = [];
function getIdindex(userId) {
return playingId.indexOf(userId);
}
function exit_(userId) {
var index = getIdindex(userId);
playingId.splice(index, 1);
words.splice(index, 1);
}
function isCommand(message)
{
return message == '/시작' || message == '/종료' ? true : false;
}
function all_check(replyToken, userId, word, index) {
if(words[index] != 0) {
if(!check_endtoend(word, index)) {
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":replyToken,
"messages":[
{
"type":"text",
"text":"끝음절로 시작해야합니다."
},
{
"type":"text",
"text":"패배했습니다."
}
]
}
},(error, response, body) => {
console.log(body);
});
exit_(userId);
return false;
} else if(!check_length(word)) {
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":replyToken,
"messages":[
{
"type":"text",
"text":"한 글자 단어입니다."
},
{
"type":"text",
"text":"패배했습니다."
}
]
}
},(error, response, body) => {
console.log(body);
});
exit_(userId);
return false;
} else {
var uncondition = 0;
if(uncondition == 0) {
request.post(
{
url: STDICT_URL,
headers: {
'Referer': 'https://stdict.korean.go.kr/api/search.do',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${word}&` + 'req_type=json&' + 'advanced=y&' + 'method=exact&' + 'type1=word'
},(error, response, body) => {
if(!error && response.statusCode == 200) {
try {console.log(body);
const parsedData = JSON.parse(body);
num = parsedData.channel.total;
console.log(num);
} catch (error) {
console.log("없는 단어");
{
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":replyToken,
"messages":[
{
"type":"text",
"text":"없는 단어입니다."
},
{
"type":"text",
"text":"패배했습니다."
}
]
}
},(error, response, body) => {
console.log(body);
});
exit_(userId);
return false;
}
}
}
});
}
}
} else {
if(!check_length(word)) {
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":replyToken,
"messages":[
{
"type":"text",
"text":"한 글자 단어입니다."
},
{
"type":"text",
"text":"패배했습니다."
}
]
}
},(error, response, body) => {
console.log(body);
});
exit_(userId);
return false;
} else {
var uncondition = 0;
if(uncondition == 0) {
request.post(
{
url: STDICT_URL,
headers: {
'Referer': 'https://stdict.korean.go.kr/api/search.do',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${word}&` + 'req_type=json&' + 'advanced=y&' + 'method=exact&' + 'type1=word'
},(error, response, body) => {
if(!error && response.statusCode == 200) {
try {console.log(body);
const parsedData = JSON.parse(body);
var num = parsedData.channel.total;
console.log(num);
} catch (error) {
console.log("없는 단어");
{
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":replyToken,
"messages":[
{
"type":"text",
"text":"없는 단어입니다."
},
{
"type":"text",
"text":"패배했습니다."
}
]
}
},(error, response, body) => {
console.log(body);
});
exit_(userId);
return false;
}
}
}
});
}
}
}
return true;
}
function check_endtoend(word, index) {
return word.slice(0, 1) == words[index].slice(-1);
}
function check_exist(word) {
}
function check_length(word) {
return word.length != 1;
}
function wordchain(replyToken, message, userId) {
if (isCommand(message)){
if(message == '/시작') {
playingId.push(userId);
words.push(0);
request.post(
{
url: TARGET_URL,
......@@ -60,7 +268,7 @@ function wordchain(replyToken, message, userId) {
"messages":[
{
"type":"text",
"text": "게임이 시작되었습니다.\n단어를 제시해주세요"
"text": "게임이 시작되었습니다.\n단어를 제시해주세요."
}
]
}
......@@ -68,8 +276,6 @@ function wordchain(replyToken, message, userId) {
console.log(body);
});
} else {
var num = playingId.indexOf(userId);
playingId.splice(num, 1);
request.post(
{
url: TARGET_URL,
......@@ -88,6 +294,7 @@ function wordchain(replyToken, message, userId) {
},(error, response, body) => {
console.log(body);
});
exit_(userId);
};
} else {
......@@ -103,7 +310,7 @@ function wordchain(replyToken, message, userId) {
"messages":[
{
"type":"text",
"text": "안녕하세요. 끝말잇기봇입니다.\n-표준어 내의 명사만 가능\n\t(표준국어대사전에 근거)\n-한 글자 단어 금지\n-중복 단어 금지\n시작: '/시작', 종료: '/종료' 입력"
"text": "안녕하세요. 끝말잇기봇입니다.\n-표준어 내의 명사만 가능\n\t(표준국어대사전에 근거)\n-한 글자 단어 금지\n시작: '/시작', 종료: '/종료' 입력"
}
]
}
......@@ -111,25 +318,29 @@ function wordchain(replyToken, message, userId) {
console.log(body);
});
} else {
var word = message;
let letter = word.slice(-1);
console.log(letter);
index = getIdindex(userId);
request.post(
{
url: STDICT_URL,
headers: {
'Referer': 'https://stdict.korean.go.kr/api/search.do',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${letter}&` + 'req_type=json&' + 'advanced=y&' + 'method=start&' + 'letter_s=2'
if(all_check(replyToken, userId, word, index)) {
let letter = word.slice(-1);
},(error, response, body) => {
request.post(
{
url: STDICT_URL,
headers: {
'Referer': 'https://stdict.korean.go.kr/api/search.do',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${letter}&` + 'req_type=json&' + 'advanced=y&' + 'method=start&' + 'type1=word&' + 'letter_s=2'
},(error, response, body) => {
if(!error && response.statusCode == 200) {
console.log(body);
const parsedData = JSON.parse(body);
var nextword = parsedData.channel.item[0].word;
var word_definition = parsedData.channel.item[0].sense.definition;
words.splice(getIdindex(userId), 1, nextword);
request.post(
{
......@@ -150,8 +361,10 @@ function wordchain(replyToken, message, userId) {
console.log(body);
});
}
});
}
});
}
}
}
}
......