summaryrefslogtreecommitdiff
path: root/src/api/bot/core.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-10-08 03:24:10 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-10-08 03:24:10 +0900
commitb18e4fea9851ce0692b3f1a627c1d220d5f16b9c (patch)
tree5af48612287cb3315d68cbae2387f75740ff4fe5 /src/api/bot/core.ts
parent:v: (diff)
downloadsharkey-b18e4fea9851ce0692b3f1a627c1d220d5f16b9c.tar.gz
sharkey-b18e4fea9851ce0692b3f1a627c1d220d5f16b9c.tar.bz2
sharkey-b18e4fea9851ce0692b3f1a627c1d220d5f16b9c.zip
:v:
Diffstat (limited to 'src/api/bot/core.ts')
-rw-r--r--src/api/bot/core.ts51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/api/bot/core.ts b/src/api/bot/core.ts
index bc5818d976..6042862d39 100644
--- a/src/api/bot/core.ts
+++ b/src/api/bot/core.ts
@@ -6,6 +6,8 @@ import User, { IUser, init as initUser } from '../models/user';
import getPostSummary from '../../common/get-post-summary';
import getUserSummary from '../../common/get-user-summary';
+import Othello, { ai as othelloAi } from '../../common/othello';
+
/**
* Botの頭脳
*/
@@ -106,6 +108,11 @@ export default class BotCore extends EventEmitter {
case 'タイムライン':
return await this.tlCommand();
+ case 'othello':
+ case 'オセロ':
+ this.setContext(new OthelloContext(this));
+ return await this.context.greet();
+
default:
return '?';
}
@@ -124,6 +131,18 @@ export default class BotCore extends EventEmitter {
this.emit('updated');
}
+ public async refreshUser() {
+ this.user = await User.findOne({
+ _id: this.user._id
+ }, {
+ fields: {
+ data: false
+ }
+ });
+
+ this.emit('updated');
+ }
+
public async tlCommand(): Promise<string | void> {
if (this.user == null) return 'まずサインインしてください。';
@@ -166,6 +185,7 @@ abstract class Context extends EventEmitter {
}
public static import(bot: BotCore, data: any) {
+ if (data.type == 'othello') return OthelloContext.import(bot, data.content);
if (data.type == 'post') return PostContext.import(bot, data.content);
if (data.type == 'signin') return SigninContext.import(bot, data.content);
return null;
@@ -251,3 +271,34 @@ class PostContext extends Context {
return context;
}
}
+
+class OthelloContext extends Context {
+ private othello: Othello = null;
+
+ public async greet(): Promise<string> {
+ this.othello = new Othello();
+ return this.othello.toPatternString('black');
+ }
+
+ public async q(query: string): Promise<string> {
+ this.othello.setByNumber('black', parseInt(query, 10));
+ othelloAi('white', this.othello);
+ return this.othello.toPatternString('black');
+ }
+
+ public export() {
+ return {
+ type: 'othello',
+ content: {
+ board: this.othello.board
+ }
+ };
+ }
+
+ public static import(bot: BotCore, data: any) {
+ const context = new OthelloContext(bot);
+ context.othello = new Othello();
+ context.othello.board = data.board;
+ return context;
+ }
+}