2018-08-27 11:22:59 +00:00
|
|
|
import 藍 from './ai';
|
|
|
|
import IModule from './module';
|
2018-08-28 00:12:59 +00:00
|
|
|
import getDate from './utils/get-date';
|
|
|
|
import { User } from './misskey/user';
|
2018-08-27 11:22:59 +00:00
|
|
|
|
|
|
|
export type FriendDoc = {
|
|
|
|
userId: string;
|
2018-08-28 00:12:59 +00:00
|
|
|
user: User;
|
2018-08-27 11:22:59 +00:00
|
|
|
name?: string;
|
|
|
|
love?: number;
|
|
|
|
lastLoveIncrementedAt?: string;
|
|
|
|
todayLoveIncrements?: number;
|
|
|
|
perModulesData?: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default class Friend {
|
|
|
|
private ai: 藍;
|
|
|
|
|
|
|
|
public get userId() {
|
|
|
|
return this.doc.userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get name() {
|
|
|
|
return this.doc.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get love() {
|
|
|
|
return this.doc.love || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public doc: FriendDoc;
|
|
|
|
|
2018-08-28 04:36:23 +00:00
|
|
|
constructor(ai: 藍, opts: { user?: User, doc?: FriendDoc }) {
|
2018-08-27 11:22:59 +00:00
|
|
|
this.ai = ai;
|
|
|
|
|
|
|
|
if (opts.user) {
|
|
|
|
this.doc = this.ai.friends.findOne({
|
|
|
|
userId: opts.user.id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.doc == null) {
|
|
|
|
this.doc = this.ai.friends.insertOne({
|
|
|
|
userId: opts.user.id,
|
|
|
|
user: opts.user
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.doc.user = opts.user;
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.doc = opts.doc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 00:12:59 +00:00
|
|
|
public updateUser = (user: User) => {
|
2018-08-27 11:22:59 +00:00
|
|
|
this.doc.user = user;
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public getPerModulesData = (module: IModule) => {
|
|
|
|
if (this.doc.perModulesData == null) {
|
|
|
|
this.doc.perModulesData = {};
|
|
|
|
this.doc.perModulesData[module.name] = {};
|
|
|
|
this.save();
|
|
|
|
} else if (this.doc.perModulesData[module.name] == null) {
|
|
|
|
this.doc.perModulesData[module.name] = {};
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.doc.perModulesData[module.name];
|
|
|
|
}
|
|
|
|
|
|
|
|
public setPerModulesData = (module: IModule, data: any) => {
|
|
|
|
if (this.doc.perModulesData == null) {
|
|
|
|
this.doc.perModulesData = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.doc.perModulesData[module.name] = data;
|
|
|
|
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public incLove = () => {
|
2018-08-28 00:12:59 +00:00
|
|
|
const today = getDate();
|
2018-08-27 11:22:59 +00:00
|
|
|
|
|
|
|
if (this.doc.lastLoveIncrementedAt != today) {
|
|
|
|
this.doc.todayLoveIncrements = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1日に上げられる親愛度は最大3
|
|
|
|
if (this.doc.lastLoveIncrementedAt == today && this.doc.todayLoveIncrements >= 3) return;
|
|
|
|
|
|
|
|
if (this.doc.love == null) this.doc.love = 0;
|
|
|
|
this.doc.love++;
|
|
|
|
this.doc.lastLoveIncrementedAt = today;
|
|
|
|
this.doc.todayLoveIncrements = (this.doc.todayLoveIncrements || 0) + 1;
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public updateName = (name: string) => {
|
|
|
|
this.doc.name = name;
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public save = () => {
|
|
|
|
this.ai.friends.update(this.doc);
|
|
|
|
}
|
|
|
|
}
|