ai/src/friend.ts

206 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-01-21 04:27:02 +00:00
import { bindThis } from '@/decorators.js';
import from '@/ai.js';
import IModule from '@/module.js';
import getDate from '@/utils/get-date.js';
import type { User } from '@/misskey/user.js';
2024-01-21 04:27:02 +00:00
import { genItem } from '@/vocabulary.js';
2018-08-27 11:22:59 +00:00
export type FriendDoc = {
userId: string;
2018-08-28 00:12:59 +00:00
user: User;
2020-09-02 12:54:01 +00:00
name?: string | null;
2018-08-27 11:22:59 +00:00
love?: number;
lastLoveIncrementedAt?: string;
todayLoveIncrements?: number;
perModulesData?: any;
2019-02-01 17:06:51 +00:00
married?: boolean;
2020-08-25 01:08:27 +00:00
transferCode?: string;
2024-01-21 05:21:48 +00:00
reversiStrength?: number | null;
2018-08-27 11:22:59 +00:00
};
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;
}
2019-02-01 17:06:51 +00:00
public get married() {
return this.doc.married;
}
2018-08-27 11:22:59 +00:00
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) {
2020-09-02 12:54:01 +00:00
const exist = this.ai.friends.findOne({
2018-08-27 11:22:59 +00:00
userId: opts.user.id
});
2020-09-02 12:54:01 +00:00
if (exist == null) {
const inserted = this.ai.friends.insertOne({
2018-08-27 11:22:59 +00:00
userId: opts.user.id,
user: opts.user
});
2020-09-02 12:54:01 +00:00
if (inserted == null) {
throw new Error('Failed to insert friend doc');
}
this.doc = inserted;
2018-08-27 11:22:59 +00:00
} else {
2020-09-02 12:54:01 +00:00
this.doc = exist;
2021-01-03 06:04:08 +00:00
this.doc.user = { ...this.doc.user, ...opts.user };
2018-08-27 11:22:59 +00:00
this.save();
}
2020-09-02 12:54:01 +00:00
} else if (opts.doc) {
2018-08-27 11:22:59 +00:00
this.doc = opts.doc;
2020-09-02 12:54:01 +00:00
} else {
throw new Error('No friend info specified');
2018-08-27 11:22:59 +00:00
}
}
2024-01-21 04:27:02 +00:00
@bindThis
2021-01-03 06:04:08 +00:00
public updateUser(user: Partial<User>) {
this.doc.user = {
...this.doc.user,
...user,
};
2018-08-27 11:22:59 +00:00
this.save();
}
2024-01-21 04:27:02 +00:00
@bindThis
2019-01-15 03:29:11 +00:00
public getPerModulesData(module: IModule) {
2018-08-27 11:22:59 +00:00
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];
}
2024-01-21 04:27:02 +00:00
@bindThis
2019-01-15 03:29:11 +00:00
public setPerModulesData(module: IModule, data: any) {
2018-08-27 11:22:59 +00:00
if (this.doc.perModulesData == null) {
this.doc.perModulesData = {};
}
this.doc.perModulesData[module.name] = data;
this.save();
}
2024-01-21 04:27:02 +00:00
@bindThis
public incLove(amount = 1) {
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
2020-09-02 12:54:01 +00:00
if (this.doc.lastLoveIncrementedAt == today && (this.doc.todayLoveIncrements || 0) >= 3) return;
2018-08-27 11:22:59 +00:00
if (this.doc.love == null) this.doc.love = 0;
this.doc.love += amount;
2018-09-01 10:36:58 +00:00
// 最大 100
if (this.doc.love > 100) this.doc.love = 100;
2018-08-27 11:22:59 +00:00
this.doc.lastLoveIncrementedAt = today;
this.doc.todayLoveIncrements = (this.doc.todayLoveIncrements || 0) + amount;
2018-08-27 11:22:59 +00:00
this.save();
this.ai.log(`💗 ${this.userId} +${amount}`);
2018-08-27 11:22:59 +00:00
}
2024-01-21 04:27:02 +00:00
@bindThis
public decLove(amount = 1) {
// 親愛度MAXなら下げない
if (this.doc.love === 100) return;
2018-09-01 10:36:58 +00:00
if (this.doc.love == null) this.doc.love = 0;
this.doc.love -= amount;
2018-09-01 10:36:58 +00:00
// 最低 -30
if (this.doc.love < -30) this.doc.love = -30;
// 親愛度マイナスなら名前を忘れる
if (this.doc.love < 0) {
this.doc.name = null;
}
this.save();
this.ai.log(`💢 ${this.userId} -${amount}`);
2018-09-01 10:36:58 +00:00
}
2024-01-21 04:27:02 +00:00
@bindThis
2019-01-15 03:29:11 +00:00
public updateName(name: string) {
2018-08-27 11:22:59 +00:00
this.doc.name = name;
this.save();
}
2024-01-21 05:21:48 +00:00
@bindThis
public updateReversiStrength(strength: number | null) {
if (strength == null) {
this.doc.reversiStrength = null;
this.save();
return;
}
if (strength < 0) strength = 0;
if (strength > 5) strength = 5;
this.doc.reversiStrength = strength;
this.save();
}
2024-01-21 04:27:02 +00:00
@bindThis
2019-01-15 03:29:11 +00:00
public save() {
2018-08-27 11:22:59 +00:00
this.ai.friends.update(this.doc);
}
2020-08-25 01:08:27 +00:00
2024-01-21 04:27:02 +00:00
@bindThis
2020-08-25 01:08:27 +00:00
public generateTransferCode(): string {
2020-08-31 11:46:00 +00:00
const code = genItem();
2020-08-25 01:08:27 +00:00
this.doc.transferCode = code;
this.save();
return code;
}
2024-01-21 04:27:02 +00:00
@bindThis
2020-08-25 01:08:27 +00:00
public transferMemory(code: string): boolean {
const src = this.ai.friends.findOne({
transferCode: code
});
if (src == null) return false;
this.doc.name = src.name;
this.doc.love = src.love;
this.doc.married = src.married;
this.doc.perModulesData = src.perModulesData;
this.save();
// TODO: 合言葉を忘れる
return true;
}
2018-08-27 11:22:59 +00:00
}