mirror of
https://github.com/nullnyat/NullcatChan.git
synced 2025-08-04 09:36:29 +09:00
リファクタリングなど
This commit is contained in:
456
src/modules/reversi/back.ts
Normal file
456
src/modules/reversi/back.ts
Normal file
@ -0,0 +1,456 @@
|
||||
/**
|
||||
* -AI-
|
||||
* Botのバックエンド(思考を担当)
|
||||
*
|
||||
* 対話と思考を同じプロセスで行うと、思考時間が長引いたときにストリームから
|
||||
* 切断されてしまうので、別々のプロセスで行うようにします
|
||||
*/
|
||||
|
||||
import * as request from 'request-promise-native';
|
||||
import Reversi, { Color } from 'misskey-reversi';
|
||||
|
||||
const config = require('../config.json');
|
||||
|
||||
const db = {};
|
||||
|
||||
function getUserName(user) {
|
||||
return user.name || user.username;
|
||||
}
|
||||
|
||||
const titles = [
|
||||
'さん', 'サン', 'サン', '㌠',
|
||||
'ちゃん', 'チャン', 'チャン',
|
||||
'君', 'くん', 'クン', 'クン',
|
||||
'先生', 'せんせい', 'センセイ', 'センセイ'
|
||||
];
|
||||
|
||||
class Session {
|
||||
private game: any;
|
||||
private form: any;
|
||||
private o: Reversi;
|
||||
private botColor: Color;
|
||||
|
||||
/**
|
||||
* 各マスの強さ (-1.0 ~ 1.0)
|
||||
*/
|
||||
private cellWeights: number[];
|
||||
|
||||
|
||||
/**
|
||||
* 対局が開始したことを知らせた投稿
|
||||
*/
|
||||
private startedNote: any = null;
|
||||
|
||||
private get user(): any {
|
||||
return this.game.user1Id == config.id ? this.game.user2 : this.game.user1;
|
||||
}
|
||||
|
||||
private get userName(): string {
|
||||
const name = getUserName(this.user);
|
||||
return `?[${name}](${config.host}/@${this.user.username})${titles.some(x => name.endsWith(x)) ? '' : 'さん'}`;
|
||||
}
|
||||
|
||||
private get strength(): number {
|
||||
return this.form.find(i => i.id == 'strength').value;
|
||||
}
|
||||
|
||||
private get isSettai(): boolean {
|
||||
return this.strength === 0;
|
||||
}
|
||||
|
||||
private get allowPost(): boolean {
|
||||
return this.form.find(i => i.id == 'publish').value;
|
||||
}
|
||||
|
||||
private get url(): string {
|
||||
return `${config.host}/reversi/${this.game.id}`;
|
||||
}
|
||||
|
||||
constructor() {
|
||||
process.on('message', this.onMessage);
|
||||
}
|
||||
|
||||
private onMessage = async (msg: any) => {
|
||||
switch (msg.type) {
|
||||
case '_init_': this.onInit(msg); break;
|
||||
case 'update-form': this.onUpdateForn(msg); break;
|
||||
case 'started': this.onStarted(msg); break;
|
||||
case 'ended': this.onEnded(msg); break;
|
||||
case 'set': this.onSet(msg); break;
|
||||
}
|
||||
}
|
||||
|
||||
// 親プロセスからデータをもらう
|
||||
private onInit = (msg: any) => {
|
||||
this.game = msg.game;
|
||||
this.form = msg.form;
|
||||
}
|
||||
|
||||
/**
|
||||
* フォームが更新されたとき
|
||||
*/
|
||||
private onUpdateForn = (msg: any) => {
|
||||
this.form.find(i => i.id == msg.body.id).value = msg.body.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 対局が始まったとき
|
||||
*/
|
||||
private onStarted = (msg: any) => {
|
||||
this.game = msg.body;
|
||||
|
||||
// TLに投稿する
|
||||
this.postGameStarted().then(note => {
|
||||
this.startedNote = note;
|
||||
});
|
||||
|
||||
// リバーシエンジン初期化
|
||||
this.o = new Reversi(this.game.settings.map, {
|
||||
isLlotheo: this.game.settings.isLlotheo,
|
||||
canPutEverywhere: this.game.settings.canPutEverywhere,
|
||||
loopedBoard: this.game.settings.loopedBoard
|
||||
});
|
||||
|
||||
//#region 各マスの価値を計算しておく
|
||||
|
||||
// 標準的な 8*8 のマップなら予め定義した価値マップを使用
|
||||
if (this.o.mapWidth == 8 && this.o.mapHeight == 8 && !this.o.map.some(p => p == 'null')) {
|
||||
this.cellWeights = [
|
||||
1 , -0.4, 0 , -0.1, -0.1, 0 , -0.4, 1 ,
|
||||
-0.4, -0.5, -0.2, -0.2, -0.2, -0.2, -0.5, -0.4,
|
||||
0 , -0.2, 0 , -0.1, -0.1, 0 , -0.2, 0 ,
|
||||
-0.1, -0.2, -0.1, -0.1, -0.1, -0.1, -0.2, -0.1,
|
||||
-0.1, -0.2, -0.1, -0.1, -0.1, -0.1, -0.2, -0.1,
|
||||
0 , -0.2, 0 , -0.1, -0.1, 0 , -0.2, 0 ,
|
||||
-0.4, -0.5, -0.2, -0.2, -0.2, -0.2, -0.5, -0.4,
|
||||
1 , -0.4, 0 , -0.1, -0.1, 0 , -0.4, 1
|
||||
];
|
||||
} else {
|
||||
//#region 隅
|
||||
this.cellWeights = this.o.map.map((pix, i) => {
|
||||
if (pix == 'null') return 0;
|
||||
const [x, y] = this.o.transformPosToXy(i);
|
||||
let count = 0;
|
||||
const get = (x, y) => {
|
||||
if (x < 0 || y < 0 || x >= this.o.mapWidth || y >= this.o.mapHeight) return 'null';
|
||||
return this.o.mapDataGet(this.o.transformXyToPos(x, y));
|
||||
};
|
||||
|
||||
const isNotSumi = (
|
||||
// -
|
||||
// +
|
||||
// -
|
||||
(get(x - 1, y - 1) == 'empty' && get(x + 1, y + 1) == 'empty') ||
|
||||
|
||||
// -
|
||||
// +
|
||||
// -
|
||||
(get(x, y - 1) == 'empty' && get(x, y + 1) == 'empty') ||
|
||||
|
||||
// -
|
||||
// +
|
||||
// -
|
||||
(get(x + 1, y - 1) == 'empty' && get(x - 1, y + 1) == 'empty') ||
|
||||
|
||||
//
|
||||
// -+-
|
||||
//
|
||||
(get(x - 1, y) == 'empty' && get(x + 1, y) == 'empty')
|
||||
)
|
||||
|
||||
const isSumi = !isNotSumi;
|
||||
|
||||
return isSumi ? 1 : 0;
|
||||
});
|
||||
//#endregion
|
||||
|
||||
//#region 隅の隣は危険
|
||||
this.cellWeights.forEach((cell, i) => {
|
||||
const [x, y] = this.o.transformPosToXy(i);
|
||||
|
||||
if (cell === 1) return;
|
||||
if (this.o.mapDataGet(this.o.transformXyToPos(x, y)) == 'null') return;
|
||||
|
||||
const get = (x, y) => {
|
||||
if (x < 0 || y < 0 || x >= this.o.mapWidth || y >= this.o.mapHeight) return 0;
|
||||
return this.cellWeights[this.o.transformXyToPos(x, y)];
|
||||
};
|
||||
|
||||
const isSumiNear = (
|
||||
(get(x - 1, y - 1) === 1) || // 左上
|
||||
(get(x , y - 1) === 1) || // 上
|
||||
(get(x + 1, y - 1) === 1) || // 右上
|
||||
(get(x + 1, y ) === 1) || // 右
|
||||
(get(x + 1, y + 1) === 1) || // 右下
|
||||
(get(x , y + 1) === 1) || // 下
|
||||
(get(x - 1, y + 1) === 1) || // 左下
|
||||
(get(x - 1, y ) === 1) // 左
|
||||
)
|
||||
|
||||
if (isSumiNear) this.cellWeights[i] = -0.5;
|
||||
});
|
||||
//#endregion
|
||||
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
this.botColor = this.game.user1Id == config.id && this.game.black == 1 || this.game.user2Id == config.id && this.game.black == 2;
|
||||
|
||||
if (this.botColor) {
|
||||
this.think();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 対局が終わったとき
|
||||
*/
|
||||
private onEnded = async (msg: any) => {
|
||||
// ストリームから切断
|
||||
process.send({
|
||||
type: 'close'
|
||||
});
|
||||
|
||||
let text: string;
|
||||
|
||||
if (msg.body.game.surrendered) {
|
||||
if (this.isSettai) {
|
||||
text = `(${this.userName}を接待していたら投了されちゃいました... ごめんなさい)`;
|
||||
} else {
|
||||
text = `${this.userName}が投了しちゃいました`;
|
||||
}
|
||||
} else if (msg.body.winnerId) {
|
||||
if (msg.body.winnerId == config.id) {
|
||||
if (this.isSettai) {
|
||||
text = `${this.userName}に接待で勝ってしまいました...`;
|
||||
} else {
|
||||
text = `${this.userName}に勝ちました♪`;
|
||||
}
|
||||
} else {
|
||||
if (this.isSettai) {
|
||||
text = `(${this.userName}に接待で負けてあげました...♪)`;
|
||||
} else {
|
||||
text = `${this.userName}に負けました...`;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (this.isSettai) {
|
||||
text = `(${this.userName}に接待で引き分けました...)`;
|
||||
} else {
|
||||
text = `${this.userName}と引き分けました~`;
|
||||
}
|
||||
}
|
||||
|
||||
await this.post(text, this.startedNote);
|
||||
|
||||
process.exit();
|
||||
}
|
||||
|
||||
/**
|
||||
* 打たれたとき
|
||||
*/
|
||||
private onSet = (msg: any) => {
|
||||
this.o.put(msg.body.color, msg.body.pos);
|
||||
|
||||
if (msg.body.next === this.botColor) {
|
||||
this.think();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Botにとってある局面がどれだけ有利か取得する
|
||||
*/
|
||||
private staticEval = () => {
|
||||
let score = this.o.canPutSomewhere(this.botColor).length;
|
||||
|
||||
this.cellWeights.forEach((weight, i) => {
|
||||
// 係数
|
||||
const coefficient = 30;
|
||||
weight = weight * coefficient;
|
||||
|
||||
const stone = this.o.board[i];
|
||||
if (stone === this.botColor) {
|
||||
// TODO: 価値のあるマスに設置されている自分の石に縦か横に接するマスは価値があると判断する
|
||||
score += weight;
|
||||
} else if (stone !== null) {
|
||||
score -= weight;
|
||||
}
|
||||
});
|
||||
|
||||
// ロセオならスコアを反転
|
||||
if (this.game.settings.isLlotheo) score = -score;
|
||||
|
||||
// 接待ならスコアを反転
|
||||
if (this.isSettai) score = -score;
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
private think = () => {
|
||||
console.log('Thinking...');
|
||||
console.time('think');
|
||||
|
||||
// 接待モードのときは、全力(5手先読みくらい)で負けるようにする
|
||||
const maxDepth = this.isSettai ? 5 : this.strength;
|
||||
|
||||
/**
|
||||
* αβ法での探索
|
||||
*/
|
||||
const dive = (pos: number, alpha = -Infinity, beta = Infinity, depth = 0): number => {
|
||||
// 試し打ち
|
||||
this.o.put(this.o.turn, pos);
|
||||
|
||||
const key = this.o.board.toString();
|
||||
let cache = db[key];
|
||||
if (cache) {
|
||||
if (alpha >= cache.upper) {
|
||||
this.o.undo();
|
||||
return cache.upper;
|
||||
}
|
||||
if (beta <= cache.lower) {
|
||||
this.o.undo();
|
||||
return cache.lower;
|
||||
}
|
||||
alpha = Math.max(alpha, cache.lower);
|
||||
beta = Math.min(beta, cache.upper);
|
||||
} else {
|
||||
cache = {
|
||||
upper: Infinity,
|
||||
lower: -Infinity
|
||||
};
|
||||
}
|
||||
|
||||
const isBotTurn = this.o.turn === this.botColor;
|
||||
|
||||
// 勝った
|
||||
if (this.o.turn === null) {
|
||||
const winner = this.o.winner;
|
||||
|
||||
// 勝つことによる基本スコア
|
||||
const base = 10000;
|
||||
|
||||
let score;
|
||||
|
||||
if (this.game.settings.isLlotheo) {
|
||||
// 勝ちは勝ちでも、より自分の石を少なくした方が美しい勝ちだと判定する
|
||||
score = this.o.winner ? base - (this.o.blackCount * 100) : base - (this.o.whiteCount * 100);
|
||||
} else {
|
||||
// 勝ちは勝ちでも、より相手の石を少なくした方が美しい勝ちだと判定する
|
||||
score = this.o.winner ? base + (this.o.blackCount * 100) : base + (this.o.whiteCount * 100);
|
||||
}
|
||||
|
||||
// 巻き戻し
|
||||
this.o.undo();
|
||||
|
||||
// 接待なら自分が負けた方が高スコア
|
||||
return this.isSettai
|
||||
? winner !== this.botColor ? score : -score
|
||||
: winner === this.botColor ? score : -score;
|
||||
}
|
||||
|
||||
if (depth === maxDepth) {
|
||||
// 静的に評価
|
||||
const score = this.staticEval();
|
||||
|
||||
// 巻き戻し
|
||||
this.o.undo();
|
||||
|
||||
return score;
|
||||
} else {
|
||||
const cans = this.o.canPutSomewhere(this.o.turn);
|
||||
|
||||
let value = isBotTurn ? -Infinity : Infinity;
|
||||
let a = alpha;
|
||||
let b = beta;
|
||||
|
||||
// 次のターンのプレイヤーにとって最も良い手を取得
|
||||
for (const p of cans) {
|
||||
if (isBotTurn) {
|
||||
const score = dive(p, a, beta, depth + 1);
|
||||
value = Math.max(value, score);
|
||||
a = Math.max(a, value);
|
||||
if (value >= beta) break;
|
||||
} else {
|
||||
const score = dive(p, alpha, b, depth + 1);
|
||||
value = Math.min(value, score);
|
||||
b = Math.min(b, value);
|
||||
if (value <= alpha) break;
|
||||
}
|
||||
}
|
||||
|
||||
// 巻き戻し
|
||||
this.o.undo();
|
||||
|
||||
if (value <= alpha) {
|
||||
cache.upper = value;
|
||||
} else if (value >= beta) {
|
||||
cache.lower = value;
|
||||
} else {
|
||||
cache.upper = value;
|
||||
cache.lower = value;
|
||||
}
|
||||
|
||||
db[key] = cache;
|
||||
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
const cans = this.o.canPutSomewhere(this.botColor);
|
||||
const scores = cans.map(p => dive(p));
|
||||
const pos = cans[scores.indexOf(Math.max(...scores))];
|
||||
|
||||
console.log('Thinked:', pos);
|
||||
console.timeEnd('think');
|
||||
|
||||
setTimeout(() => {
|
||||
process.send({
|
||||
type: 'put',
|
||||
pos
|
||||
});
|
||||
}, 500);
|
||||
}
|
||||
|
||||
/**
|
||||
* 対局が始まったことをMisskeyに投稿します
|
||||
*/
|
||||
private postGameStarted = async () => {
|
||||
const text = this.isSettai
|
||||
? `${this.userName}の接待を始めました!`
|
||||
: `対局を${this.userName}と始めました! (強さ${this.strength})`;
|
||||
|
||||
return await this.post(`${text}\n→[観戦する](${this.url})`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Misskeyに投稿します
|
||||
* @param text 投稿内容
|
||||
*/
|
||||
private post = async (text: string, renote?: any) => {
|
||||
if (this.allowPost) {
|
||||
const body = {
|
||||
i: config.i,
|
||||
text: text
|
||||
} as any;
|
||||
|
||||
if (renote) {
|
||||
body.renoteId = renote.id;
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await request.post(`${config.host}/api/notes/create`, {
|
||||
json: body
|
||||
});
|
||||
|
||||
return res.createdNote;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new Session();
|
189
src/modules/reversi/index.ts
Normal file
189
src/modules/reversi/index.ts
Normal file
@ -0,0 +1,189 @@
|
||||
import * as childProcess from 'child_process';
|
||||
|
||||
const ReconnectingWebSocket = require('../../../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js');
|
||||
|
||||
import 藍 from '../..';
|
||||
import IModule from '../../module';
|
||||
import serifs from '../../serifs';
|
||||
import config from '../../config';
|
||||
import MessageLike from '../../message-like';
|
||||
|
||||
export default class ReversiModule implements IModule {
|
||||
private ai: 藍;
|
||||
|
||||
/**
|
||||
* リバーシストリーム
|
||||
*/
|
||||
private reversiConnection?: any;
|
||||
|
||||
public install = (ai: 藍) => {
|
||||
this.ai = ai;
|
||||
|
||||
this.reversiConnection = new ReconnectingWebSocket(`${config.wsUrl}/games/reversi?i=${config.i}`, [], {
|
||||
WebSocket: WebSocket
|
||||
});
|
||||
|
||||
this.reversiConnection.addEventListener('open', () => {
|
||||
console.log('reversi stream opened');
|
||||
});
|
||||
|
||||
this.reversiConnection.addEventListener('close', () => {
|
||||
console.log('reversi stream closed');
|
||||
});
|
||||
|
||||
this.reversiConnection.addEventListener('message', message => {
|
||||
const msg = JSON.parse(message.data);
|
||||
|
||||
this.onReversiConnectionMessage(msg);
|
||||
});
|
||||
}
|
||||
|
||||
public onMention = (msg: MessageLike) => {
|
||||
if (msg.text && msg.text.indexOf('リバーシ') > -1) {
|
||||
if (config.reversiEnabled) {
|
||||
msg.reply(serifs.REVERSI_OK);
|
||||
|
||||
this.ai.api('games/reversi/match', {
|
||||
userId: msg.userId
|
||||
});
|
||||
} else {
|
||||
msg.reply(serifs.REVERSI_DECLINE);
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private onReversiConnectionMessage = (msg: any) => {
|
||||
switch (msg.type) {
|
||||
|
||||
// 招待されたとき
|
||||
case 'invited': {
|
||||
this.onReversiInviteMe(msg.body.parent);
|
||||
break;
|
||||
}
|
||||
|
||||
// マッチしたとき
|
||||
case 'matched': {
|
||||
this.onReversiGameStart(msg.body);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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) => {
|
||||
// ゲームストリームに接続
|
||||
const gw = new ReconnectingWebSocket(`${config.wsUrl}/games/reversi-game?i=${config.i}&game=${game.id}`, [], {
|
||||
WebSocket: WebSocket
|
||||
});
|
||||
|
||||
function send(msg) {
|
||||
try {
|
||||
gw.send(JSON.stringify(msg));
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
gw.addEventListener('open', () => {
|
||||
console.log('reversi game stream opened');
|
||||
|
||||
// フォーム
|
||||
const form = [{
|
||||
id: 'publish',
|
||||
type: 'switch',
|
||||
label: '藍が対局情報を投稿するのを許可',
|
||||
value: true
|
||||
}, {
|
||||
id: 'strength',
|
||||
type: 'radio',
|
||||
label: '強さ',
|
||||
value: 3,
|
||||
items: [{
|
||||
label: '接待',
|
||||
value: 0
|
||||
}, {
|
||||
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
|
||||
});
|
||||
|
||||
ai.on('message', msg => {
|
||||
if (msg.type == 'put') {
|
||||
send({
|
||||
type: 'set',
|
||||
pos: msg.pos
|
||||
});
|
||||
} else if (msg.type == 'close') {
|
||||
gw.close();
|
||||
}
|
||||
});
|
||||
|
||||
// ゲームストリームから情報が流れてきたらそのままバックエンドプロセスに伝える
|
||||
gw.addEventListener('message', message => {
|
||||
const msg = JSON.parse(message.data);
|
||||
ai.send(msg);
|
||||
});
|
||||
//#endregion
|
||||
|
||||
// フォーム初期化
|
||||
setTimeout(() => {
|
||||
send({
|
||||
type: 'init-form',
|
||||
body: form
|
||||
});
|
||||
}, 1000);
|
||||
|
||||
// どんな設定内容の対局でも受け入れる
|
||||
setTimeout(() => {
|
||||
send({
|
||||
type: 'accept'
|
||||
});
|
||||
}, 2000);
|
||||
});
|
||||
|
||||
gw.addEventListener('close', () => {
|
||||
console.log('reversi game stream closed');
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user