summaryrefslogtreecommitdiff
path: root/src/api/bot/core.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-10-07 05:50:01 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-10-07 05:50:01 +0900
commit0f2b503f2f545ce11fcca8c2c17a1084fd91df5e (patch)
treeaa215717e85e36b0f74e48cb6628e8c2157d90c7 /src/api/bot/core.ts
parent:v: (diff)
downloadsharkey-0f2b503f2f545ce11fcca8c2c17a1084fd91df5e.tar.gz
sharkey-0f2b503f2f545ce11fcca8c2c17a1084fd91df5e.tar.bz2
sharkey-0f2b503f2f545ce11fcca8c2c17a1084fd91df5e.zip
:v:
Diffstat (limited to 'src/api/bot/core.ts')
-rw-r--r--src/api/bot/core.ts52
1 files changed, 50 insertions, 2 deletions
diff --git a/src/api/bot/core.ts b/src/api/bot/core.ts
index 47989dbaa2..1f624c5f0a 100644
--- a/src/api/bot/core.ts
+++ b/src/api/bot/core.ts
@@ -14,6 +14,31 @@ export default class BotCore extends EventEmitter {
this.user = user;
}
+ private setContect(context: Context) {
+ this.context = context;
+ this.emit('updated');
+
+ if (context) {
+ context.on('updated', () => {
+ this.emit('updated');
+ });
+ }
+ }
+
+ public export() {
+ return {
+ user: this.user,
+ context: this.context ? this.context.export() : null
+ };
+ }
+
+ public static import(data) {
+ const core = new BotCore();
+ core.user = data.user;
+ core.setContect(data.context ? Context.import(core, data.context) : null);
+ return core;
+ }
+
public async q(query: string): Promise<string> {
if (this.context != null) {
return await this.context.q(query);
@@ -22,9 +47,11 @@ export default class BotCore extends EventEmitter {
switch (query) {
case 'ping':
return 'PONG';
+ case 'me':
+ return this.user ? `${this.user.name}としてサインインしています` : 'サインインしていません';
case 'ログイン':
case 'サインイン':
- this.context = new SigninContext(this);
+ this.setContect(new SigninContext(this));
return await this.context.greet();
default:
return '?';
@@ -34,18 +61,26 @@ export default class BotCore extends EventEmitter {
public setUser(user: IUser) {
this.user = user;
this.emit('set-user', user);
+ this.emit('updated');
}
}
-abstract class Context {
+abstract class Context extends EventEmitter {
protected core: BotCore;
public abstract async greet(): Promise<string>;
public abstract async q(query: string): Promise<string>;
+ public abstract export(): any;
constructor(core: BotCore) {
+ super();
this.core = core;
}
+
+ public static import(core: BotCore, data: any) {
+ if (data.type == 'signin') return SigninContext.import(core, data.content);
+ return null;
+ }
}
class SigninContext extends Context {
@@ -71,6 +106,7 @@ class SigninContext extends Context {
return `${query}というユーザーは存在しませんでした... もう一度教えてください:`;
} else {
this.temporaryUser = user;
+ this.emit('updated');
return `パスワードを教えてください:`;
}
} else {
@@ -85,4 +121,16 @@ class SigninContext extends Context {
}
}
}
+
+ public export() {
+ return {
+ temporaryUser: this.temporaryUser
+ };
+ }
+
+ public static import(core: BotCore, data: any) {
+ const context = new SigninContext(core);
+ context.temporaryUser = data.temporaryUser;
+ return context;
+ }
}