This commit is contained in:
syuilo 2018-03-15 20:19:26 +09:00
parent fe81d2b865
commit f06f7a46bc
2 changed files with 40 additions and 34 deletions

View file

@ -6,24 +6,32 @@
* *
*/ */
import * as request from 'request-promise-native';
import Othello, { Color } from '../core'; import Othello, { Color } from '../core';
import conf from '../../../conf';
let game; let game;
let form; 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); console.log(msg);
// 親プロセスからデータをもらう // 親プロセスからデータをもらう
if (msg.type == '_init_') { if (msg.type == '_init_') {
game = msg.game; game = msg.game;
form = msg.form; form = msg.form;
id = msg.id;
} }
// フォームが更新されたとき // フォームが更新されたとき
@ -37,16 +45,18 @@ process.on('message', msg => {
//#region TLに投稿する //#region TLに投稿する
const game = msg.body; 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 user = game.user1_id == id ? game.user2 : game.user1;
const isSettai = form[0].value === 0; const isSettai = form[0].value === 0;
const text = isSettai const text = isSettai
? `?[${user.name}](https://misskey.xyz/${user.username})さんの接待を始めました!` ? `?[${user.name}](https://${conf.host}/${user.username})さんの接待を始めました!`
: `対局を?[${user.name}](https://misskey.xyz/${user.username})さんと始めました! (強さ${form[0].value})`; : `対局を?[${user.name}](https://${conf.host}/${user.username})さんと始めました! (強さ${form[0].value})`;
process.send({ const res = await request.post(`https://api.${conf.host}/posts/create`, {
type: 'tl', json: { i,
text: `${text}\n→[観戦する](${url})` text: `${text}\n→[観戦する](${url})`
}
}); });
post = res.created_post;
//#endregion //#endregion
} }
@ -58,23 +68,24 @@ process.on('message', msg => {
}); });
//#region TLに投稿する //#region TLに投稿する
const url = `https://misskey.xyz/othello/${msg.body.game.id}`;
const user = game.user1_id == id ? game.user2 : game.user1; const user = game.user1_id == id ? game.user2 : game.user1;
const isSettai = form[0].value === 0; const isSettai = form[0].value === 0;
const text = isSettai const text = isSettai
? msg.body.winner_id === null ? msg.body.winner_id === null
? `?[${user.name}](https://misskey.xyz/${user.username})さんに接待で引き分けました...` ? `?[${user.name}](https://${conf.host}/${user.username})さんに接待で引き分けました...`
: msg.body.winner_id == id : msg.body.winner_id == id
? `?[${user.name}](https://misskey.xyz/${user.username})さんに接待で勝ってしまいました...` ? `?[${user.name}](https://${conf.host}/${user.username})さんに接待で勝ってしまいました...`
: `?[${user.name}](https://misskey.xyz/${user.username})さんに接待で負けてあげました♪` : `?[${user.name}](https://${conf.host}/${user.username})さんに接待で負けてあげました♪`
: msg.body.winner_id === null : 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 : msg.body.winner_id == id
? `?[${user.name}](https://misskey.xyz/${user.username})さんに勝ちました♪ (強さ${form[0].value})` ? `?[${user.name}](https://${conf.host}/${user.username})さんに勝ちました♪ (強さ${form[0].value})`
: `?[${user.name}](https://misskey.xyz/${user.username})さんに負けました... (強さ${form[0].value})`; : `?[${user.name}](https://${conf.host}/${user.username})さんに負けました... (強さ${form[0].value})`;
process.send({ request.post(`https://api.${conf.host}/posts/create`, {
type: 'tl', json: { i,
text: `${text}\n→[結果を見る](${url})` reply_id: post.id,
text: text
}
}); });
//#endregion //#endregion
} }

View file

@ -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 constructor: WebSocket
}); });
@ -48,6 +48,8 @@ homeStream.on('message', message => {
if (msg.type == 'mention' || msg.type == 'reply') { if (msg.type == 'mention' || msg.type == 'reply') {
const post = msg.body; const post = msg.body;
if (post.user_id == id) return;
// リアクションする // リアクションする
request.post('https://api.misskey.xyz/posts/reactions/create', { request.post('https://api.misskey.xyz/posts/reactions/create', {
json: { i, json: { i,
@ -75,7 +77,7 @@ homeStream.on('message', message => {
const message = msg.body; const message = msg.body;
if (message.text) { if (message.text) {
if (message.text.indexOf('オセロ') > -1) { 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, json: { i,
user_id: message.user_id, user_id: message.user_id,
text: '良いですよ~' text: '良いですよ~'
@ -90,7 +92,7 @@ homeStream.on('message', message => {
// ユーザーを対局に誘う // ユーザーを対局に誘う
function invite(userId) { function invite(userId) {
request.post('https://api.misskey.xyz/othello/match', { request.post(`https://api.${conf.host}/othello/match`, {
json: { i, json: { i,
user_id: userId 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 constructor: WebSocket
}); });
@ -132,7 +134,7 @@ othelloStream.on('message', message => {
*/ */
function gameStart(game) { 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 constructor: WebSocket
}); });
@ -170,8 +172,7 @@ function gameStart(game) {
ai.send({ ai.send({
type: '_init_', type: '_init_',
game, game,
form, form
id
}); });
ai.on('message', msg => { ai.on('message', msg => {
@ -180,12 +181,6 @@ function gameStart(game) {
type: 'set', type: 'set',
pos: msg.pos 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') { } else if (msg.type == 'close') {
gw.close(); gw.close();
} }
@ -227,7 +222,7 @@ async function onInviteMe(inviter) {
console.log(`Someone invited me: @${inviter.username}`); 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: { json: {
i, i,
user_id: inviter.id user_id: inviter.id