From f06f7a46bc5e9096d608742f2251d7bdda8dc880 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 15 Mar 2018 20:19:26 +0900 Subject: [PATCH] #1241 --- src/common/othello/ai/back.ts | 51 +++++++++++++++++++++------------- src/common/othello/ai/front.ts | 23 ++++++--------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/common/othello/ai/back.ts b/src/common/othello/ai/back.ts index 38a564f55e..61dde52d13 100644 --- a/src/common/othello/ai/back.ts +++ b/src/common/othello/ai/back.ts @@ -6,24 +6,32 @@ * 切断されてしまうので、別々のプロセスで行うようにします */ +import * as request from 'request-promise-native'; import Othello, { Color } from '../core'; +import conf from '../../../conf'; let game; let form; /** - * このBotのユーザーID + * BotアカウントのユーザーID */ -let id; +const id = conf.othello_ai.id; -process.on('message', msg => { +/** + * BotアカウントのAPIキー + */ +const i = conf.othello_ai.i; + +let post; + +process.on('message', async msg => { console.log(msg); // 親プロセスからデータをもらう if (msg.type == '_init_') { game = msg.game; form = msg.form; - id = msg.id; } // フォームが更新されたとき @@ -37,16 +45,18 @@ process.on('message', msg => { //#region TLに投稿する const game = msg.body; - const url = `https://misskey.xyz/othello/${game.id}`; + const url = `https://${conf.host}/othello/${game.id}`; const user = game.user1_id == id ? game.user2 : game.user1; const isSettai = form[0].value === 0; const text = isSettai - ? `?[${user.name}](https://misskey.xyz/${user.username})さんの接待を始めました!` - : `対局を?[${user.name}](https://misskey.xyz/${user.username})さんと始めました! (強さ${form[0].value})`; - process.send({ - type: 'tl', - text: `${text}\n→[観戦する](${url})` + ? `?[${user.name}](https://${conf.host}/${user.username})さんの接待を始めました!` + : `対局を?[${user.name}](https://${conf.host}/${user.username})さんと始めました! (強さ${form[0].value})`; + const res = await request.post(`https://api.${conf.host}/posts/create`, { + json: { i, + text: `${text}\n→[観戦する](${url})` + } }); + post = res.created_post; //#endregion } @@ -58,23 +68,24 @@ process.on('message', msg => { }); //#region TLに投稿する - const url = `https://misskey.xyz/othello/${msg.body.game.id}`; const user = game.user1_id == id ? game.user2 : game.user1; const isSettai = form[0].value === 0; const text = isSettai ? msg.body.winner_id === null - ? `?[${user.name}](https://misskey.xyz/${user.username})さんに接待で引き分けました...` + ? `?[${user.name}](https://${conf.host}/${user.username})さんに接待で引き分けました...` : msg.body.winner_id == id - ? `?[${user.name}](https://misskey.xyz/${user.username})さんに接待で勝ってしまいました...` - : `?[${user.name}](https://misskey.xyz/${user.username})さんに接待で負けてあげました♪` + ? `?[${user.name}](https://${conf.host}/${user.username})さんに接待で勝ってしまいました...` + : `?[${user.name}](https://${conf.host}/${user.username})さんに接待で負けてあげました♪` : msg.body.winner_id === null - ? `?[${user.name}](https://misskey.xyz/${user.username})さんと引き分けました~ (強さ${form[0].value})` + ? `?[${user.name}](https://${conf.host}/${user.username})さんと引き分けました~ (強さ${form[0].value})` : msg.body.winner_id == id - ? `?[${user.name}](https://misskey.xyz/${user.username})さんに勝ちました♪ (強さ${form[0].value})` - : `?[${user.name}](https://misskey.xyz/${user.username})さんに負けました... (強さ${form[0].value})`; - process.send({ - type: 'tl', - text: `${text}\n→[結果を見る](${url})` + ? `?[${user.name}](https://${conf.host}/${user.username})さんに勝ちました♪ (強さ${form[0].value})` + : `?[${user.name}](https://${conf.host}/${user.username})さんに負けました... (強さ${form[0].value})`; + request.post(`https://api.${conf.host}/posts/create`, { + json: { i, + reply_id: post.id, + text: text + } }); //#endregion } diff --git a/src/common/othello/ai/front.ts b/src/common/othello/ai/front.ts index f9e6bca1b8..4d5f6e53eb 100644 --- a/src/common/othello/ai/front.ts +++ b/src/common/othello/ai/front.ts @@ -29,7 +29,7 @@ const id = conf.othello_ai.id; /** * ホームストリーム */ -const homeStream = new ReconnectingWebSocket(`wss://api.misskey.xyz/?i=${i}`, undefined, { +const homeStream = new ReconnectingWebSocket(`wss://api.${conf.host}/?i=${i}`, undefined, { constructor: WebSocket }); @@ -48,6 +48,8 @@ homeStream.on('message', message => { if (msg.type == 'mention' || msg.type == 'reply') { const post = msg.body; + if (post.user_id == id) return; + // リアクションする request.post('https://api.misskey.xyz/posts/reactions/create', { json: { i, @@ -75,7 +77,7 @@ homeStream.on('message', message => { const message = msg.body; if (message.text) { if (message.text.indexOf('オセロ') > -1) { - request.post('https://api.misskey.xyz/messaging/messages/create', { + request.post(`https://api.${conf.host}/messaging/messages/create`, { json: { i, user_id: message.user_id, text: '良いですよ~' @@ -90,7 +92,7 @@ homeStream.on('message', message => { // ユーザーを対局に誘う function invite(userId) { - request.post('https://api.misskey.xyz/othello/match', { + request.post(`https://api.${conf.host}/othello/match`, { json: { i, user_id: userId } @@ -100,7 +102,7 @@ function invite(userId) { /** * オセロストリーム */ -const othelloStream = new ReconnectingWebSocket(`wss://api.misskey.xyz/othello?i=${i}`, undefined, { +const othelloStream = new ReconnectingWebSocket(`wss://api.${conf.host}/othello?i=${i}`, undefined, { constructor: WebSocket }); @@ -132,7 +134,7 @@ othelloStream.on('message', message => { */ function gameStart(game) { // ゲームストリームに接続 - const gw = new ReconnectingWebSocket(`wss://api.misskey.xyz/othello-game?i=${i}&game=${game.id}`, undefined, { + const gw = new ReconnectingWebSocket(`wss://api.${conf.host}/othello-game?i=${i}&game=${game.id}`, undefined, { constructor: WebSocket }); @@ -170,8 +172,7 @@ function gameStart(game) { ai.send({ type: '_init_', game, - form, - id + form }); ai.on('message', msg => { @@ -180,12 +181,6 @@ function gameStart(game) { type: 'set', pos: msg.pos })); - } else if (msg.type == 'tl') { - request.post('https://api.misskey.xyz/posts/create', { - json: { i, - text: msg.text - } - }); } else if (msg.type == 'close') { gw.close(); } @@ -227,7 +222,7 @@ async function onInviteMe(inviter) { console.log(`Someone invited me: @${inviter.username}`); // 承認 - const game = await request.post('https://api.misskey.xyz/othello/match', { + const game = await request.post(`https://api.${conf.host}/othello/match`, { json: { i, user_id: inviter.id