ai/src/modules/reversi/index.ts

176 lines
3.7 KiB
TypeScript
Raw Normal View History

2018-08-11 03:42:06 +02:00
import * as childProcess from 'child_process';
2018-10-10 14:55:33 +02:00
import from '../../ai';
2018-08-11 03:42:06 +02:00
import IModule from '../../module';
import serifs from '../../serifs';
import config from '../../config';
import MessageLike from '../../message-like';
2018-08-11 05:19:34 +02:00
import * as WebSocket from 'ws';
2018-08-28 02:12:59 +02:00
import Friend from '../../friend';
import getDate from '../../utils/get-date';
2018-08-11 03:42:06 +02:00
export default class ReversiModule implements IModule {
2018-08-31 01:20:49 +02:00
public readonly name = 'reversi';
2018-08-11 03:42:06 +02:00
private ai: ;
/**
*
*/
private reversiConnection?: any;
public install = (ai: ) => {
if (!config.reversiEnabled) return;
2018-08-11 03:42:06 +02:00
this.ai = ai;
2018-10-09 17:47:03 +02:00
this.reversiConnection = this.ai.connection.useSharedConnection('gamesReversi');
2018-08-11 03:42:06 +02:00
2018-10-09 17:47:03 +02:00
// 招待されたとき
this.reversiConnection.on('invited', msg => this.onReversiInviteMe(msg.parent));
2018-08-11 03:42:06 +02:00
2018-10-09 17:47:03 +02:00
// マッチしたとき
this.reversiConnection.on('matched', msg => this.onReversiGameStart(msg));
2018-08-11 03:42:06 +02:00
}
public onMention = (msg: MessageLike) => {
2018-09-02 14:50:57 +02:00
if (msg.includes(['リバーシ', 'オセロ', 'reversi', 'othello'])) {
2018-08-11 03:42:06 +02:00
if (config.reversiEnabled) {
2018-08-28 02:12:59 +02:00
msg.reply(serifs.reversi.ok);
2018-08-11 03:42:06 +02:00
this.ai.api('games/reversi/match', {
userId: msg.userId
});
} else {
2018-08-28 02:12:59 +02:00
msg.reply(serifs.reversi.decline);
2018-08-11 03:42:06 +02:00
}
return true;
} else {
return false;
}
}
private onReversiInviteMe = async (inviter: any) => {
console.log(`Someone invited me: @${inviter.username}`);
if (config.reversiEnabled) {
// 承認
const game = await this.ai.api('games/reversi/match', {
userId: inviter.id
});
this.onReversiGameStart(game);
} else {
// todo (リバーシできない旨をメッセージで伝えるなど)
}
}
private onReversiGameStart = (game: any) => {
// ゲームストリームに接続
2018-10-09 17:47:03 +02:00
const gw = this.ai.connection.connectToChannel('gamesReversiGame', {
game: game.id
2018-08-11 03:42:06 +02:00
});
function send(msg) {
try {
gw.send(JSON.stringify(msg));
} catch (e) {
console.error(e);
}
}
2018-10-09 17:47:03 +02:00
// フォーム
const form = [{
id: 'publish',
type: 'switch',
label: '藍が対局情報を投稿するのを許可',
value: true
}, {
id: 'strength',
type: 'radio',
label: '強さ',
value: 3,
items: [{
label: '接待',
value: 0
2018-08-11 03:42:06 +02:00
}, {
2018-10-09 17:47:03 +02:00
label: '弱',
value: 2
}, {
label: '中',
value: 3
}, {
label: '強',
value: 4
}, {
label: '最強',
value: 5
}]
}];
//#region バックエンドプロセス開始
const ai = childProcess.fork(__dirname + '/back.js');
// バックエンドプロセスに情報を渡す
ai.send({
type: '_init_',
game,
form,
account: this.ai.account
});
2018-08-11 03:42:06 +02:00
2018-10-09 17:47:03 +02:00
ai.on('message', msg => {
if (msg.type == 'put') {
2018-08-11 03:42:06 +02:00
send({
2018-10-09 17:47:03 +02:00
type: 'set',
pos: msg.pos
2018-08-11 03:42:06 +02:00
});
2018-10-09 17:47:03 +02:00
} else if (msg.type == 'ended') {
gw.dispose();
2018-08-11 03:42:06 +02:00
2018-10-09 17:47:03 +02:00
this.onGameEnded(game);
}
2018-08-11 03:42:06 +02:00
});
2018-10-09 17:47:03 +02:00
// ゲームストリームから情報が流れてきたらそのままバックエンドプロセスに伝える
gw.addEventListener('*', message => {
ai.send(message);
2018-08-11 03:42:06 +02:00
});
2018-10-09 17:47:03 +02:00
//#endregion
// フォーム初期化
setTimeout(() => {
send({
type: 'initForm',
body: form
});
}, 1000);
// どんな設定内容の対局でも受け入れる
setTimeout(() => {
send({
type: 'accept'
});
}, 2000);
2018-08-11 03:42:06 +02:00
}
2018-08-28 02:12:59 +02:00
private onGameEnded(game: any) {
const user = game.user1Id == this.ai.account.id ? game.user2 : game.user1;
//#region 1日に1回だけ親愛度を上げる
const today = getDate();
const friend = new Friend(this.ai, { user: user });
const data = friend.getPerModulesData(this);
if (data.lastPlayedAt != today) {
data.lastPlayedAt = today;
friend.setPerModulesData(this, data);
friend.incLove();
}
//#endregion
}
2018-08-11 03:42:06 +02:00
}