Newer
Older
import * as EventEmitter from 'events';
import * as bcrypt from 'bcryptjs';
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
};
}
protected _import(data) {
this.user = data.user ? initUser(data.user) : null;
this.setContext(data.context ? Context.import(this, data.context) : null);
}
if (/^@[a-zA-Z0-9-]+$/.test(query)) {
return await this.showUserCommand(query);
}
'help: これです\n' +
'me: アカウント情報を見ます\n' +
'login, signin: サインインします\n' +
'logout, signout: サインアウトします\n' +
'post: 投稿します\n' +
return this.user ? `${this.user.name}としてサインインしています。\n\n${getUserSummary(this.user)}` : 'サインインしていません';
case 'logout':
case 'signout':
case 'ログアウト':
case 'サインアウト':
if (this.user == null) return '今はサインインしてないですよ!';
this.signout();
return 'ご利用ありがとうございました <3';
case 'post':
case '投稿':
if (this.user == null) return 'まずサインインしてください。';
this.setContext(new PostContext(this));
return await this.context.greet();
case 'tl':
case 'タイムライン':
case 'othello':
case 'オセロ':
this.setContext(new OthelloContext(this));
return await this.context.greet();
public signout() {
const user = this.user;
this.user = null;
this.emit('signout', user);
this.emit('updated');
}
public async refreshUser() {
this.user = await User.findOne({
_id: this.user._id
}, {
fields: {
data: false
}
});
this.emit('updated');
}
const tl = await require('../endpoints/posts/timeline')({
limit: 5
}, this.user);
const text = tl
.map(post => getPostSummary(post))
.join('\n-----\n');
return text;
}
public async showUserCommand(q: string): Promise<string | void> {
try {
username: q.substr(1)
}, this.user);
const text = getUserSummary(user);
return text;
} catch (e) {
return `問題が発生したようです...: ${e}`;
}
}
public abstract async greet(): Promise<string>;
public abstract async q(query: string): Promise<string>;
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);
public async greet(): Promise<string> {
return 'まずユーザー名を教えてください:';
}
public async q(query: string): Promise<string> {
if (this.temporaryUser == null) {
// Fetch user
const user: IUser = await User.findOne({
username_lower: query.toLowerCase()
}, {
fields: {
}
});
if (user === null) {
return `${query}というユーザーは存在しませんでした... もう一度教えてください:`;
} else {
this.temporaryUser = user;
return `パスワードを教えてください:`;
}
} else {
// Compare password
const same = bcrypt.compareSync(query, this.temporaryUser.password);
if (same) {
return `${this.temporaryUser.name}さん、おかえりなさい!`;
} else {
return `パスワードが違います... もう一度教えてください:`;
}
}
}
public static import(bot: BotCore, data: any) {
const context = new SigninContext(bot);
class PostContext extends Context {
public async greet(): Promise<string> {
return '内容:';
}
public async q(query: string): Promise<string> {
await require('../endpoints/posts/create')({
text: query
return '投稿しましたよ!';
}
public export() {
return {
type: 'post'
};
}
public static import(bot: BotCore, data: any) {
const context = new PostContext(bot);
class OthelloContext extends Context {
private othello: Othello = null;
return this.othello.toPatternString('black');
}
public async q(query: string): Promise<string> {
this.othello.setByNumber('black', parseInt(query, 10));
if (this.othello.getPattern('black').length === 0) {
this.bot.clearContext();
return '~終了~';
} else {
this.emit('updated');
return s + 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;
}
}