Compare commits

..

No commits in common. "c81e5b79743714ed91f6ca4b2b0f73e62bb29df6" and "296b63c6cfad1a1e0af65817501e45ac632615e4" have entirely different histories.

44 changed files with 470 additions and 809 deletions

View file

@ -1,48 +1,48 @@
{
"_v": "2.0.0",
"type": "module",
"_v": "1.5.0",
"main": "./built/index.js",
"scripts": {
"start": "node ./built",
"start-daemon": "nodemon ./built",
"build": "tspc",
"build": "tsc",
"test": "jest"
},
"dependencies": {
"@types/chalk": "2.2.0",
"@types/lokijs": "1.5.14",
"@types/node": "20.11.5",
"@types/promise-retry": "1.1.6",
"@types/random-seed": "0.3.5",
"@types/seedrandom": "3.0.8",
"@types/twemoji-parser": "13.1.4",
"@types/uuid": "9.0.7",
"@types/ws": "8.5.10",
"canvas": "2.11.2",
"chalk": "5.3.0",
"got": "14.0.0",
"@types/lokijs": "1.5.4",
"@types/node": "16.0.1",
"@types/promise-retry": "1.1.3",
"@types/random-seed": "0.3.3",
"@types/request-promise-native": "1.0.18",
"@types/seedrandom": "2.4.28",
"@types/twemoji-parser": "13.1.1",
"@types/uuid": "8.3.1",
"@types/ws": "7.4.6",
"autobind-decorator": "2.4.0",
"canvas": "2.10.2",
"chalk": "4.1.1",
"lokijs": "1.5.12",
"memory-streams": "0.1.3",
"misskey-reversi": "0.0.5",
"nodemon": "3.0.3",
"module-alias": "2.2.2",
"promise-retry": "2.0.1",
"random-seed": "0.3.0",
"reconnecting-websocket": "4.4.0",
"request": "2.88.2",
"request-promise-native": "1.0.9",
"seedrandom": "3.0.5",
"ts-patch": "3.1.2",
"twemoji-parser": "14.0.0",
"typescript": "5.3.3",
"typescript-transform-paths": "3.4.6",
"uuid": "9.0.1",
"ws": "8.16.0"
"timeout-as-promise": "1.0.0",
"ts-node": "10.0.0",
"twemoji-parser": "13.1.0",
"typescript": "4.3.5",
"uuid": "8.3.2",
"ws": "7.5.2"
},
"devDependencies": {
"@koa/router": "9.4.0",
"@types/jest": "26.0.23",
"@types/koa": "2.13.1",
"@types/koa__router": "8.0.4",
"@types/websocket": "1.0.10",
"@types/websocket": "1.0.2",
"jest": "26.6.3",
"koa": "2.13.1",
"koa-json-body": "5.3.0",
@ -70,8 +70,5 @@
"^@/(.+)": "<rootDir>/src/$1",
"^#/(.+)": "<rootDir>/test/$1"
}
},
"nodemonConfig": {
"ignore": ["memory.json"]
}
}

View file

@ -1,21 +1,21 @@
// AI CORE
import * as fs from 'fs';
import { bindThis } from '@/decorators.js';
import loki from 'lokijs';
import got from 'got';
import chalk from 'chalk';
import autobind from 'autobind-decorator';
import * as loki from 'lokijs';
import * as request from 'request-promise-native';
import * as chalk from 'chalk';
import { v4 as uuid } from 'uuid';
const delay = require('timeout-as-promise');
import config from '@/config.js';
import Module from '@/module.js';
import Message from '@/message.js';
import Friend, { FriendDoc } from '@/friend.js';
import { User } from '@/misskey/user.js';
import Stream from '@/stream.js';
import log from '@/utils/log.js';
import { sleep } from './utils/sleep.js';
import pkg from '../package.json' assert { type: 'json' };
import config from '@/config';
import Module from '@/module';
import Message from '@/message';
import Friend, { FriendDoc } from '@/friend';
import { User } from '@/misskey/user';
import Stream from '@/stream';
import log from '@/utils/log';
const pkg = require('../package.json');
type MentionHook = (msg: Message) => Promise<boolean | HandlerResult>;
type ContextHook = (key: any, msg: Message, data?: any) => Promise<void | boolean | HandlerResult>;
@ -103,12 +103,12 @@ export default class 藍 {
});
}
@bindThis
@autobind
public log(msg: string) {
log(chalk`[{magenta AiOS}]: ${msg}`);
}
@bindThis
@autobind
private run() {
//#region Init DB
this.meta = this.getCollection('meta', {});
@ -207,7 +207,7 @@ export default class 藍 {
*
* ()
*/
@bindThis
@autobind
private async onReceiveMessage(msg: Message): Promise<void> {
this.log(chalk.gray(`<<< An message received: ${chalk.underline(msg.id)}`));
@ -262,7 +262,7 @@ export default class 藍 {
//#endregion
if (!immediate) {
await sleep(1000);
await delay(1000);
}
// リアクションする
@ -274,7 +274,7 @@ export default class 藍 {
}
}
@bindThis
@autobind
private onNotification(notification: any) {
switch (notification.type) {
// リアクションされたら親愛度を少し上げる
@ -290,7 +290,7 @@ export default class 藍 {
}
}
@bindThis
@autobind
private crawleTimer() {
const timers = this.timers.find();
for (const timer of timers) {
@ -303,7 +303,7 @@ export default class 藍 {
}
}
@bindThis
@autobind
private logWaking() {
this.setMeta({
lastWakingAt: Date.now(),
@ -313,7 +313,7 @@ export default class 藍 {
/**
*
*/
@bindThis
@autobind
public getCollection(name: string, opts?: any): loki.Collection {
let collection: loki.Collection;
@ -326,7 +326,7 @@ export default class 藍 {
return collection;
}
@bindThis
@autobind
public lookupFriend(userId: User['id']): Friend | null {
const doc = this.friends.findOne({
userId: userId
@ -342,9 +342,9 @@ export default class 藍 {
/**
*
*/
@bindThis
@autobind
public async upload(file: Buffer | fs.ReadStream, meta: any) {
const res = await got.post({
const res = await request.post({
url: `${config.apiUrl}/drive/files/create`,
formData: {
i: config.i,
@ -354,14 +354,14 @@ export default class 藍 {
}
},
json: true
}).json();
});
return res;
}
/**
* 稿
*/
@bindThis
@autobind
public async post(param: any) {
const res = await this.api('notes/create', param);
return res.createdNote;
@ -370,7 +370,7 @@ export default class 藍 {
/**
*
*/
@bindThis
@autobind
public sendMessage(userId: any, param: any) {
return this.post(Object.assign({
visibility: 'specified',
@ -381,14 +381,13 @@ export default class 藍 {
/**
* APIを呼び出します
*/
@bindThis
@autobind
public api(endpoint: string, param?: any) {
this.log(`API: ${endpoint}`);
return got.post(`${config.apiUrl}/${endpoint}`, {
return request.post(`${config.apiUrl}/${endpoint}`, {
json: Object.assign({
i: config.i
}, param)
}).json();
});
};
/**
@ -398,7 +397,7 @@ export default class 藍 {
* @param id ID稿ID
* @param data
*/
@bindThis
@autobind
public subscribeReply(module: Module, key: string | null, id: string, data?: any) {
this.contexts.insertOne({
noteId: id,
@ -413,7 +412,7 @@ export default class 藍 {
* @param module 解除するモジュール名
* @param key
*/
@bindThis
@autobind
public unsubscribeReply(module: Module, key: string | null) {
this.contexts.findAndRemove({
key: key,
@ -428,7 +427,7 @@ export default class 藍 {
* @param delay
* @param data
*/
@bindThis
@autobind
public setTimeoutWithPersistence(module: Module, delay: number, data?: any) {
const id = uuid();
this.timers.insertOne({
@ -442,7 +441,7 @@ export default class 藍 {
this.log(`Timer persisted: ${module.name} ${id} ${delay}ms`);
}
@bindThis
@autobind
public getMeta() {
const rec = this.meta.findOne();
@ -458,7 +457,7 @@ export default class 藍 {
}
}
@bindThis
@autobind
public setMeta(meta: Partial<Meta>) {
const rec = this.getMeta();

View file

@ -17,7 +17,7 @@ type Config = {
memoryDir?: string;
};
import config from '../config.json' assert { type: 'json' };
const config = require('../config.json');
config.wsUrl = config.host.replace('http', 'ws');
config.apiUrl = config.host + '/api';

View file

@ -1,41 +0,0 @@
// https://github.com/andreypopp/autobind-decorator
/**
* Return a descriptor removing the value and returning a getter
* The getter will return a .bind version of the function
* and memoize the result against a symbol on the instance
*/
export function bindThis(target: any, key: string, descriptor: any) {
let fn = descriptor.value;
if (typeof fn !== 'function') {
throw new TypeError(`@bindThis decorator can only be applied to methods not: ${typeof fn}`);
}
return {
configurable: true,
get() {
// eslint-disable-next-line no-prototype-builtins
if (this === target.prototype || this.hasOwnProperty(key) ||
typeof fn !== 'function') {
return fn;
}
const boundFn = fn.bind(this);
Object.defineProperty(this, key, {
configurable: true,
get() {
return boundFn;
},
set(value) {
fn = value;
delete this[key];
},
});
return boundFn;
},
set(value: any) {
fn = value;
},
};
}

View file

@ -1,9 +1,9 @@
import { bindThis } from '@/decorators.js';
import from '@/ai.js';
import IModule from '@/module.js';
import getDate from '@/utils/get-date.js';
import { User } from '@/misskey/user.js';
import { genItem } from '@/vocabulary.js';
import autobind from 'autobind-decorator';
import from '@/ai';
import IModule from '@/module';
import getDate from '@/utils/get-date';
import { User } from '@/misskey/user';
import { genItem } from '@/vocabulary';
export type FriendDoc = {
userId: string;
@ -15,7 +15,6 @@ export type FriendDoc = {
perModulesData?: any;
married?: boolean;
transferCode?: string;
reversiStrength?: number | null;
};
export default class Friend {
@ -70,7 +69,7 @@ export default class Friend {
}
}
@bindThis
@autobind
public updateUser(user: Partial<User>) {
this.doc.user = {
...this.doc.user,
@ -79,7 +78,7 @@ export default class Friend {
this.save();
}
@bindThis
@autobind
public getPerModulesData(module: IModule) {
if (this.doc.perModulesData == null) {
this.doc.perModulesData = {};
@ -93,7 +92,7 @@ export default class Friend {
return this.doc.perModulesData[module.name];
}
@bindThis
@autobind
public setPerModulesData(module: IModule, data: any) {
if (this.doc.perModulesData == null) {
this.doc.perModulesData = {};
@ -104,7 +103,7 @@ export default class Friend {
this.save();
}
@bindThis
@autobind
public incLove(amount = 1) {
const today = getDate();
@ -128,7 +127,7 @@ export default class Friend {
this.ai.log(`💗 ${this.userId} +${amount}`);
}
@bindThis
@autobind
public decLove(amount = 1) {
// 親愛度MAXなら下げない
if (this.doc.love === 100) return;
@ -149,32 +148,18 @@ export default class Friend {
this.ai.log(`💢 ${this.userId} -${amount}`);
}
@bindThis
@autobind
public updateName(name: string) {
this.doc.name = name;
this.save();
}
@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();
}
@bindThis
@autobind
public save() {
this.ai.friends.update(this.doc);
}
@bindThis
@autobind
public generateTransferCode(): string {
const code = genItem();
@ -184,7 +169,7 @@ export default class Friend {
return code;
}
@bindThis
@autobind
public transferMemory(code: string): boolean {
const src = this.ai.friends.findOne({
transferCode: code

View file

@ -1,38 +1,40 @@
// AiOS bootstrapper
import chalk from 'chalk';
import got from 'got';
import promiseRetry from 'promise-retry';
import 'module-alias/register';
import from './ai.js';
import config from './config.js';
import _log from './utils/log.js';
import pkg from '../package.json' assert { type: 'json' };
import * as chalk from 'chalk';
import * as request from 'request-promise-native';
const promiseRetry = require('promise-retry');
import CoreModule from './modules/core/index.js';
import TalkModule from './modules/talk/index.js';
import BirthdayModule from './modules/birthday/index.js';
import ReversiModule from './modules/reversi/index.js';
import PingModule from './modules/ping/index.js';
import EmojiModule from './modules/emoji/index.js';
import EmojiReactModule from './modules/emoji-react/index.js';
import FortuneModule from './modules/fortune/index.js';
import GuessingGameModule from './modules/guessing-game/index.js';
import KazutoriModule from './modules/kazutori/index.js';
import KeywordModule from './modules/keyword/index.js';
import WelcomeModule from './modules/welcome/index.js';
import TimerModule from './modules/timer/index.js';
import DiceModule from './modules/dice/index.js';
import ServerModule from './modules/server/index.js';
import FollowModule from './modules/follow/index.js';
import ValentineModule from './modules/valentine/index.js';
import MazeModule from './modules/maze/index.js';
import ChartModule from './modules/chart/index.js';
import SleepReportModule from './modules/sleep-report/index.js';
import NotingModule from './modules/noting/index.js';
import PollModule from './modules/poll/index.js';
import ReminderModule from './modules/reminder/index.js';
import CheckCustomEmojisModule from './modules/check-custom-emojis/index.js';
import from './ai';
import config from './config';
import _log from './utils/log';
const pkg = require('../package.json');
import CoreModule from './modules/core';
import TalkModule from './modules/talk';
import BirthdayModule from './modules/birthday';
import ReversiModule from './modules/reversi';
import PingModule from './modules/ping';
import EmojiModule from './modules/emoji';
import EmojiReactModule from './modules/emoji-react';
import FortuneModule from './modules/fortune';
import GuessingGameModule from './modules/guessing-game';
import KazutoriModule from './modules/kazutori';
import KeywordModule from './modules/keyword';
import WelcomeModule from './modules/welcome';
import TimerModule from './modules/timer';
import DiceModule from './modules/dice';
import ServerModule from './modules/server';
import FollowModule from './modules/follow';
import ValentineModule from './modules/valentine';
import MazeModule from './modules/maze';
import ChartModule from './modules/chart';
import SleepReportModule from './modules/sleep-report';
import NotingModule from './modules/noting';
import PollModule from './modules/poll';
import ReminderModule from './modules/reminder';
import CheckCustomEmojisModule from './modules/check-custom-emojis';
console.log(' __ ____ _____ ___ ');
console.log(' /__\\ (_ _)( _ )/ __)');
@ -49,11 +51,11 @@ promiseRetry(retry => {
log(`Account fetching... ${chalk.gray(config.host)}`);
// アカウントをフェッチ
return got.post(`${config.apiUrl}/i`, {
return request.post(`${config.apiUrl}/i`, {
json: {
i: config.i
}
}).json().catch(retry);
}).catch(retry);
}, {
retries: 3
}).then(account => {

View file

@ -1,13 +1,13 @@
import { bindThis } from '@/decorators.js';
import chalk from 'chalk';
import autobind from 'autobind-decorator';
import * as chalk from 'chalk';
const delay = require('timeout-as-promise');
import from '@/ai.js';
import Friend from '@/friend.js';
import { User } from '@/misskey/user.js';
import includes from '@/utils/includes.js';
import or from '@/utils/or.js';
import config from '@/config.js';
import { sleep } from '@/utils/sleep.js';
import from '@/ai';
import Friend from '@/friend';
import { User } from '@/misskey/user';
import includes from '@/utils/includes';
import or from '@/utils/or';
import config from '@/config';
export default class Message {
private ai: ;
@ -68,7 +68,7 @@ export default class Message {
});
}
@bindThis
@autobind
public async reply(text: string | null, opts?: {
file?: any;
cw?: string;
@ -80,7 +80,7 @@ export default class Message {
this.ai.log(`>>> Sending reply to ${chalk.underline(this.id)}`);
if (!opts?.immediate) {
await sleep(2000);
await delay(2000);
}
return await this.ai.post({
@ -92,12 +92,12 @@ export default class Message {
});
}
@bindThis
@autobind
public includes(words: string[]): boolean {
return includes(this.text, words);
}
@bindThis
@autobind
public or(words: (string | RegExp)[]): boolean {
return or(this.text, words);
}

View file

@ -1,5 +1,5 @@
import { bindThis } from '@/decorators.js';
import , { InstallerResult } from '@/ai.js';
import autobind from 'autobind-decorator';
import , { InstallerResult } from '@/ai';
export default abstract class Module {
public abstract readonly name: string;
@ -24,7 +24,7 @@ export default abstract class Module {
public abstract install(): InstallerResult;
@bindThis
@autobind
protected log(msg: string) {
this.ai.log(`[${this.name}]: ${msg}`);
}
@ -35,7 +35,7 @@ export default abstract class Module {
* @param id ID稿ID
* @param data
*/
@bindThis
@autobind
protected subscribeReply(key: string | null, id: string, data?: any) {
this.ai.subscribeReply(this, key, id, data);
}
@ -44,7 +44,7 @@ export default abstract class Module {
*
* @param key
*/
@bindThis
@autobind
protected unsubscribeReply(key: string | null) {
this.ai.unsubscribeReply(this, key);
}
@ -55,17 +55,17 @@ export default abstract class Module {
* @param delay
* @param data
*/
@bindThis
@autobind
public setTimeoutWithPersistence(delay: number, data?: any) {
this.ai.setTimeoutWithPersistence(this, delay, data);
}
@bindThis
@autobind
protected getData() {
return this.doc.data;
}
@bindThis
@autobind
protected setData(data: any) {
this.doc.data = data;
this.ai.moduleData.update(this.doc);

View file

@ -1,7 +1,7 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import Friend from '@/friend.js';
import serifs from '@/serifs.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
import Friend from '@/friend';
import serifs from '@/serifs';
function zeroPadding(num: number, length: number): string {
return ('0000000000' + num).slice(-length);
@ -10,7 +10,7 @@ function zeroPadding(num: number, length: number): string {
export default class extends Module {
public readonly name = 'birthday';
@bindThis
@autobind
public install() {
this.crawleBirthday();
setInterval(this.crawleBirthday, 1000 * 60 * 3);
@ -21,7 +21,7 @@ export default class extends Module {
/**
* ()
*/
@bindThis
@autobind
private crawleBirthday() {
const now = new Date();
const m = now.getMonth();

View file

@ -1,15 +1,15 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import serifs from '@/serifs.js';
import Message from '@/message.js';
import { renderChart } from './render-chart.js';
import { items } from '@/vocabulary.js';
import config from '@/config.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
import serifs from '@/serifs';
import Message from '@/message';
import { renderChart } from './render-chart';
import { items } from '@/vocabulary';
import config from '@/config';
export default class extends Module {
public readonly name = 'chart';
@bindThis
@autobind
public install() {
if (config.chartEnabled === false) return {};
@ -21,7 +21,7 @@ export default class extends Module {
};
}
@bindThis
@autobind
private async post() {
const now = new Date();
if (now.getHours() !== 23) return;
@ -41,7 +41,7 @@ export default class extends Module {
});
}
@bindThis
@autobind
private async genChart(type, params?): Promise<any> {
this.log('Chart data fetching...');
@ -134,7 +134,7 @@ export default class extends Module {
return file;
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
if (!msg.includes(['チャート'])) {
return false;

View file

@ -1,9 +1,9 @@
import { bindThis } from '@/decorators.js';
import loki from 'lokijs';
import Module from '@/module.js';
import serifs from '@/serifs.js';
import config from '@/config.js';
import Message from '@/message.js';
import autobind from 'autobind-decorator';
import * as loki from 'lokijs';
import Module from '@/module';
import serifs from '@/serifs';
import config from '@/config';
import Message from '@/message';
export default class extends Module {
public readonly name = 'checkCustomEmojis';
@ -13,7 +13,7 @@ export default class extends Module {
updatedAt: number;
}>;
@bindThis
@autobind
public install() {
if (!config.checkEmojisEnabled) return {};
this.lastEmoji = this.ai.getCollection('lastEmoji', {
@ -28,7 +28,7 @@ export default class extends Module {
};
}
@bindThis
@autobind
private timeCheck() {
const now = new Date();
if (now.getHours() !== 23) return;
@ -42,7 +42,7 @@ export default class extends Module {
this.post();
}
@bindThis
@autobind
private async post() {
this.log('Start to Check CustomEmojis.');
const lastEmoji = this.lastEmoji.find({});
@ -99,7 +99,7 @@ export default class extends Module {
this.log('Check CustomEmojis finished!');
}
@bindThis
@autobind
private async checkCumstomEmojis(lastId : any) {
this.log('CustomEmojis fetching...');
let emojisData;
@ -140,7 +140,7 @@ export default class extends Module {
return emojisData;
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
if (!msg.includes(['カスタムえもじチェック','カスタムえもじを調べて','カスタムえもじを確認'])) {
return false;
@ -155,7 +155,7 @@ export default class extends Module {
};
}
@bindThis
@autobind
private async sleep(ms: number) {
return new Promise((res) => setTimeout(res, ms));
}

View file

@ -1,15 +1,15 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import Message from '@/message.js';
import serifs from '@/serifs.js';
import { safeForInterpolate } from '@/utils/safe-for-interpolate.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
import Message from '@/message';
import serifs from '@/serifs';
import { safeForInterpolate } from '@/utils/safe-for-interpolate';
const titles = ['さん', 'くん', '君', 'ちゃん', '様', '先生'];
export default class extends Module {
public readonly name = 'core';
@bindThis
@autobind
public install() {
return {
mentionHook: this.mentionHook,
@ -17,7 +17,7 @@ export default class extends Module {
};
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
if (!msg.text) return false;
@ -30,7 +30,7 @@ export default class extends Module {
);
}
@bindThis
@autobind
private transferBegin(msg: Message): boolean {
if (!msg.text) return false;
if (!msg.includes(['引継', '引き継ぎ', '引越', '引っ越し'])) return false;
@ -42,7 +42,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private transferEnd(msg: Message): boolean {
if (!msg.text) return false;
if (!msg.text.startsWith('「') || !msg.text.endsWith('」')) return false;
@ -60,13 +60,13 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private setName(msg: Message): boolean {
if (!msg.text) return false;
if (!msg.text.includes('って呼んで')) return false;
if (msg.text.startsWith('って呼んで')) return false;
const name = msg.text.match(/^(.+?)って呼んで/g)![1];
const name = msg.text.match(/^(.+?)って呼んで/)![1];
if (name.length > 10) {
msg.reply(serifs.core.tooLong);
@ -94,7 +94,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private modules(msg: Message): boolean {
if (!msg.text) return false;
if (!msg.or(['modules'])) return false;
@ -114,7 +114,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private version(msg: Message): boolean {
if (!msg.text) return false;
if (!msg.or(['v', 'version', 'バージョン'])) return false;
@ -126,7 +126,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private async contextHook(key: any, msg: Message, data: any) {
if (msg.text == null) return;

View file

@ -1,19 +1,19 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import Message from '@/message.js';
import serifs from '@/serifs.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
import Message from '@/message';
import serifs from '@/serifs';
export default class extends Module {
public readonly name = 'dice';
@bindThis
@autobind
public install() {
return {
mentionHook: this.mentionHook
};
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
if (msg.text == null) return false;

View file

@ -1,18 +1,18 @@
import { bindThis } from '@/decorators.js';
import autobind from 'autobind-decorator';
import { parse } from 'twemoji-parser';
const delay = require('timeout-as-promise');
import { Note } from '@/misskey/note.js';
import Module from '@/module.js';
import Stream from '@/stream.js';
import includes from '@/utils/includes.js';
import { sleep } from '../../utils/sleep.js';
import { Note } from '@/misskey/note';
import Module from '@/module';
import Stream from '@/stream';
import includes from '@/utils/includes';
export default class extends Module {
public readonly name = 'emoji-react';
private htl: ReturnType<Stream['useSharedConnection']>;
@bindThis
@autobind
public install() {
this.htl = this.ai.connection.useSharedConnection('homeTimeline');
this.htl.on('note', this.onNote);
@ -20,7 +20,7 @@ export default class extends Module {
return {};
}
@bindThis
@autobind
private async onNote(note: Note) {
if (note.reply != null) return;
if (note.text == null) return;
@ -28,7 +28,7 @@ export default class extends Module {
const react = async (reaction: string, immediate = false) => {
if (!immediate) {
await sleep(1500);
await delay(1500);
}
this.ai.api('notes/reactions/create', {
noteId: note.id,

View file

@ -1,7 +1,7 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import Message from '@/message.js';
import serifs from '@/serifs.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
import Message from '@/message';
import serifs from '@/serifs';
const hands = [
'👏',
@ -129,14 +129,14 @@ const faces = [
export default class extends Module {
public readonly name = 'emoji';
@bindThis
@autobind
public install() {
return {
mentionHook: this.mentionHook
};
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
if (msg.includes(['顔文字', '絵文字', 'emoji', '福笑い'])) {
const hand = hands[Math.floor(Math.random() * hands.length)];

View file

@ -1,18 +1,18 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import Message from '@/message.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
import Message from '@/message';
export default class extends Module {
public readonly name = 'follow';
@bindThis
@autobind
public install() {
return {
mentionHook: this.mentionHook
};
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
if (msg.text && msg.includes(['フォロー', 'フォロバ', 'follow me'])) {
if (!msg.user.isFollowing) {

View file

@ -1,9 +1,9 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import Message from '@/message.js';
import serifs from '@/serifs.js';
import seedrandom from 'seedrandom';
import { genItem } from '@/vocabulary.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
import Message from '@/message';
import serifs from '@/serifs';
import * as seedrandom from 'seedrandom';
import { genItem } from '@/vocabulary';
export const blessing = [
'藍吉',
@ -40,14 +40,14 @@ export const blessing = [
export default class extends Module {
public readonly name = 'fortune';
@bindThis
@autobind
public install() {
return {
mentionHook: this.mentionHook
};
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
if (msg.includes(['占', 'うらな', '運勢', 'おみくじ'])) {
const date = new Date();

View file

@ -1,8 +1,8 @@
import { bindThis } from '@/decorators.js';
import loki from 'lokijs';
import Module from '@/module.js';
import Message from '@/message.js';
import serifs from '@/serifs.js';
import autobind from 'autobind-decorator';
import * as loki from 'lokijs';
import Module from '@/module';
import Message from '@/message';
import serifs from '@/serifs';
export default class extends Module {
public readonly name = 'guessingGame';
@ -16,7 +16,7 @@ export default class extends Module {
endedAt: number | null;
}>;
@bindThis
@autobind
public install() {
this.guesses = this.ai.getCollection('guessingGame', {
indices: ['userId']
@ -28,7 +28,7 @@ export default class extends Module {
};
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
if (!msg.includes(['数当て', '数あて'])) return false;
@ -55,7 +55,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private async contextHook(key: any, msg: Message) {
if (msg.text == null) return;

View file

@ -1,10 +1,10 @@
import { bindThis } from '@/decorators.js';
import loki from 'lokijs';
import Module from '@/module.js';
import Message from '@/message.js';
import serifs from '@/serifs.js';
import { User } from '@/misskey/user.js';
import { acct } from '@/utils/acct.js';
import autobind from 'autobind-decorator';
import * as loki from 'lokijs';
import Module from '@/module';
import Message from '@/message';
import serifs from '@/serifs';
import { User } from '@/misskey/user';
import { acct } from '@/utils/acct';
type Game = {
votes: {
@ -27,7 +27,7 @@ export default class extends Module {
private games: loki.Collection<Game>;
@bindThis
@autobind
public install() {
this.games = this.ai.getCollection('kazutori');
@ -40,7 +40,7 @@ export default class extends Module {
};
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
if (!msg.includes(['数取り'])) return false;
@ -82,7 +82,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private async contextHook(key: any, msg: Message) {
if (msg.text == null) return {
reaction: 'hmm'
@ -139,7 +139,7 @@ export default class extends Module {
/**
*
*/
@bindThis
@autobind
private crawleGameEnd() {
const game = this.games.findOne({
isEnded: false
@ -156,7 +156,7 @@ export default class extends Module {
/**
*
*/
@bindThis
@autobind
private finish(game: Game) {
game.isEnded = true;
this.games.update(game);

View file

@ -1,9 +1,9 @@
import { bindThis } from '@/decorators.js';
import loki from 'lokijs';
import Module from '@/module.js';
import config from '@/config.js';
import serifs from '@/serifs.js';
import { mecab } from './mecab.js';
import autobind from 'autobind-decorator';
import * as loki from 'lokijs';
import Module from '@/module';
import config from '@/config';
import serifs from '@/serifs';
import { mecab } from './mecab';
function kanaToHira(str: string) {
return str.replace(/[\u30a1-\u30f6]/g, match => {
@ -20,7 +20,7 @@ export default class extends Module {
learnedAt: number;
}>;
@bindThis
@autobind
public install() {
if (!config.keywordEnabled) return {};
@ -33,7 +33,7 @@ export default class extends Module {
return {};
}
@bindThis
@autobind
private async learn() {
const tl = await this.ai.api('notes/local-timeline', {
limit: 30

View file

@ -1,5 +1,5 @@
import gen from 'random-seed';
import { CellType } from './maze.js';
import * as gen from 'random-seed';
import { CellType } from './maze';
const cellVariants = {
void: {

View file

@ -1,14 +1,14 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import serifs from '@/serifs.js';
import { genMaze } from './gen-maze.js';
import { renderMaze } from './render-maze.js';
import Message from '@/message.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
import serifs from '@/serifs';
import { genMaze } from './gen-maze';
import { renderMaze } from './render-maze';
import Message from '@/message';
export default class extends Module {
public readonly name = 'maze';
@bindThis
@autobind
public install() {
this.post();
setInterval(this.post, 1000 * 60 * 3);
@ -18,7 +18,7 @@ export default class extends Module {
};
}
@bindThis
@autobind
private async post() {
const now = new Date();
if (now.getHours() !== 22) return;
@ -38,7 +38,7 @@ export default class extends Module {
});
}
@bindThis
@autobind
private async genMazeFile(seed, size?): Promise<any> {
this.log('Maze generating...');
const maze = genMaze(seed, size);
@ -55,7 +55,7 @@ export default class extends Module {
return file;
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
if (msg.includes(['迷路'])) {
let size: string | null = null;

View file

@ -1,8 +1,8 @@
import gen from 'random-seed';
import * as gen from 'random-seed';
import { createCanvas } from 'canvas';
import { CellType } from './maze.js';
import { themes } from './themes.js';
import { CellType } from './maze';
import { themes } from './themes';
const imageSize = 4096; // px
const margin = 96 * 4;

View file

@ -1,13 +1,13 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import serifs from '@/serifs.js';
import { genItem } from '@/vocabulary.js';
import config from '@/config.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
import serifs from '@/serifs';
import { genItem } from '@/vocabulary';
import config from '@/config';
export default class extends Module {
public readonly name = 'noting';
@bindThis
@autobind
public install() {
if (config.notingEnabled === false) return {};
@ -20,7 +20,7 @@ export default class extends Module {
return {};
}
@bindThis
@autobind
private post() {
const notes = [
...serifs.noting.notes,

View file

@ -1,18 +1,18 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import Message from '@/message.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
import Message from '@/message';
export default class extends Module {
public readonly name = 'ping';
@bindThis
@autobind
public install() {
return {
mentionHook: this.mentionHook
};
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
if (msg.text && msg.text.includes('ping')) {
msg.reply('PONG!', {

View file

@ -1,15 +1,15 @@
import { bindThis } from '@/decorators.js';
import Message from '@/message.js';
import Module from '@/module.js';
import serifs from '@/serifs.js';
import { genItem } from '@/vocabulary.js';
import config from '@/config.js';
import { Note } from '@/misskey/note.js';
import autobind from 'autobind-decorator';
import Message from '@/message';
import Module from '@/module';
import serifs from '@/serifs';
import { genItem } from '@/vocabulary';
import config from '@/config';
import { Note } from '@/misskey/note';
export default class extends Module {
public readonly name = 'poll';
@bindThis
@autobind
public install() {
setInterval(() => {
if (Math.random() < 0.1) {
@ -23,7 +23,7 @@ export default class extends Module {
};
}
@bindThis
@autobind
private async post() {
const duration = 1000 * 60 * 15;
@ -89,7 +89,7 @@ export default class extends Module {
});
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
if (!msg.or(['/poll']) || msg.user.username !== config.master) {
return false;
@ -102,7 +102,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private async timeoutCallback({ title, noteId }) {
const note: Note = await this.ai.api('notes/show', { noteId });

View file

@ -1,10 +1,10 @@
import { bindThis } from '@/decorators.js';
import loki from 'lokijs';
import Module from '@/module.js';
import Message from '@/message.js';
import serifs, { getSerif } from '@/serifs.js';
import { acct } from '@/utils/acct.js';
import config from '@/config.js';
import autobind from 'autobind-decorator';
import * as loki from 'lokijs';
import Module from '@/module';
import Message from '@/message';
import serifs, { getSerif } from '@/serifs';
import { acct } from '@/utils/acct';
import config from '@/config';
const NOTIFY_INTERVAL = 1000 * 60 * 60 * 12;
@ -20,7 +20,7 @@ export default class extends Module {
createdAt: number;
}>;
@bindThis
@autobind
public install() {
this.reminds = this.ai.getCollection('reminds', {
indices: ['userId', 'id']
@ -33,7 +33,7 @@ export default class extends Module {
};
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
let text = msg.extractedText.toLowerCase();
if (!text.startsWith('remind') && !text.startsWith('todo')) return false;
@ -98,7 +98,7 @@ export default class extends Module {
};
}
@bindThis
@autobind
private async contextHook(key: any, msg: Message, data: any) {
if (msg.text == null) return;
@ -128,7 +128,7 @@ export default class extends Module {
}
}
@bindThis
@autobind
private async timeoutCallback(data) {
const remind = this.reminds.findOne({
id: data.id

View file

@ -6,11 +6,13 @@
*
*/
import got from 'got';
import * as Reversi from './engine.js';
import config from '@/config.js';
import serifs from '@/serifs.js';
import { User } from '@/misskey/user.js';
import 'module-alias/register';
import * as request from 'request-promise-native';
import Reversi, { Color } from 'misskey-reversi';
import config from '@/config';
import serifs from '@/serifs';
import { User } from '@/misskey/user';
function getUserName(user) {
return user.name || user.username;
@ -27,10 +29,8 @@ class Session {
private account: User;
private game: any;
private form: any;
private engine: Reversi.Game;
private botColor: Reversi.Color;
private appliedOps: string[] = [];
private o: Reversi;
private botColor: Color;
/**
* ()
@ -79,7 +79,7 @@ class Session {
}
private get url(): string {
return `${config.host}/reversi/g/${this.game.id}`;
return `${config.host}/games/reversi/${this.game.id}`;
}
constructor() {
@ -89,9 +89,10 @@ class Session {
private onMessage = async (msg: any) => {
switch (msg.type) {
case '_init_': this.onInit(msg.body); break;
case 'updateForm': this.onUpdateForn(msg.body); break;
case 'started': this.onStarted(msg.body); break;
case 'ended': this.onEnded(msg.body); break;
case 'log': this.onLog(msg.body); break;
case 'set': this.onSet(msg.body); break;
}
}
@ -102,17 +103,18 @@ class Session {
this.account = msg.account;
}
/**
*
*/
private onUpdateForn = (msg: any) => {
this.form.find(i => i.id == msg.id).value = msg.value;
}
/**
*
*/
private onStarted = (msg: any) => {
this.game = msg.game;
if (this.game.canPutEverywhere) { // 対応してない
process.send!({
type: 'ended'
});
process.exit();
}
this.game = msg;
// TLに投稿する
this.postGameStarted().then(note => {
@ -120,24 +122,24 @@ class Session {
});
// リバーシエンジン初期化
this.engine = new Reversi.Game(this.game.map, {
this.o = new Reversi(this.game.map, {
isLlotheo: this.game.isLlotheo,
canPutEverywhere: this.game.canPutEverywhere,
loopedBoard: this.game.loopedBoard
});
this.maxTurn = this.engine.map.filter(p => p === 'empty').length - this.engine.board.filter(x => x != null).length;
this.maxTurn = this.o.map.filter(p => p === 'empty').length - this.o.board.filter(x => x != null).length;
//#region 隅の位置計算など
//#region 隅
this.engine.map.forEach((pix, i) => {
this.o.map.forEach((pix, i) => {
if (pix == 'null') return;
const [x, y] = this.engine.posToXy(i);
const [x, y] = this.o.transformPosToXy(i);
const get = (x, y) => {
if (x < 0 || y < 0 || x >= this.engine.mapWidth || y >= this.engine.mapHeight) return 'null';
return this.engine.mapDataGet(this.engine.xyToPos(x, y));
if (x < 0 || y < 0 || x >= this.o.mapWidth || y >= this.o.mapHeight) return 'null';
return this.o.mapDataGet(this.o.transformXyToPos(x, y));
};
const isNotSumi = (
@ -169,15 +171,15 @@ class Session {
//#endregion
//#region 隅の隣
this.engine.map.forEach((pix, i) => {
this.o.map.forEach((pix, i) => {
if (pix == 'null') return;
if (this.sumiIndexes.includes(i)) return;
const [x, y] = this.engine.posToXy(i);
const [x, y] = this.o.transformPosToXy(i);
const check = (x, y) => {
if (x < 0 || y < 0 || x >= this.engine.mapWidth || y >= this.engine.mapHeight) return 0;
return this.sumiIndexes.includes(this.engine.xyToPos(x, y));
if (x < 0 || y < 0 || x >= this.o.mapWidth || y >= this.o.mapHeight) return 0;
return this.sumiIndexes.includes(this.o.transformXyToPos(x, y));
};
const isSumiNear = (
@ -251,23 +253,13 @@ class Session {
/**
*
*/
private onLog = (log: any) => {
if (log.id == null || !this.appliedOps.includes(log.id)) {
switch (log.operation) {
case 'put': {
this.engine.putStone(log.pos);
private onSet = (msg: any) => {
this.o.put(msg.color, msg.pos);
this.currentTurn++;
if (this.engine.turn === this.botColor) {
if (msg.next === this.botColor) {
this.think();
}
break;
}
default:
break;
}
}
}
/**
@ -276,10 +268,10 @@ class Session {
* TODO: 接待時はまるっと処理の中身を変え
*/
private staticEval = () => {
let score = this.engine.getPuttablePlaces(this.botColor).length;
let score = this.o.canPutSomewhere(this.botColor).length;
for (const index of this.sumiIndexes) {
const stone = this.engine.board[index];
const stone = this.o.board[index];
if (stone === this.botColor) {
score += 1000; // 自分が隅を取っていたらスコアプラス
@ -291,7 +283,7 @@ class Session {
// TODO: ここに (隅以外の確定石の数 * 100) をスコアに加算する処理を入れる
for (const index of this.sumiNearIndexes) {
const stone = this.engine.board[index];
const stone = this.o.board[index];
if (stone === this.botColor) {
score -= 10; // 自分が隅の周辺を取っていたらスコアマイナス(危険なので)
@ -327,13 +319,13 @@ class Session {
*/
const dive = (pos: number, alpha = -Infinity, beta = Infinity, depth = 0): number => {
// 試し打ち
this.engine.putStone(pos);
this.o.put(this.o.turn, pos);
const isBotTurn = this.engine.turn === this.botColor;
const isBotTurn = this.o.turn === this.botColor;
// 勝った
if (this.engine.turn === null) {
const winner = this.engine.winner;
if (this.o.turn === null) {
const winner = this.o.winner;
// 勝つことによる基本スコア
const base = 10000;
@ -342,14 +334,14 @@ class Session {
if (this.game.isLlotheo) {
// 勝ちは勝ちでも、より自分の石を少なくした方が美しい勝ちだと判定する
score = this.engine.winner ? base - (this.engine.blackCount * 100) : base - (this.engine.whiteCount * 100);
score = this.o.winner ? base - (this.o.blackCount * 100) : base - (this.o.whiteCount * 100);
} else {
// 勝ちは勝ちでも、より相手の石を少なくした方が美しい勝ちだと判定する
score = this.engine.winner ? base + (this.engine.blackCount * 100) : base + (this.engine.whiteCount * 100);
score = this.o.winner ? base + (this.o.blackCount * 100) : base + (this.o.whiteCount * 100);
}
// 巻き戻し
this.engine.undo();
this.o.undo();
// 接待なら自分が負けた方が高スコア
return this.isSettai
@ -362,11 +354,11 @@ class Session {
const score = this.staticEval();
// 巻き戻し
this.engine.undo();
this.o.undo();
return score;
} else {
const cans = this.engine.getPuttablePlaces(this.engine.turn);
const cans = this.o.canPutSomewhere(this.o.turn);
let value = isBotTurn ? -Infinity : Infinity;
let a = alpha;
@ -392,34 +384,24 @@ class Session {
}
// 巻き戻し
this.engine.undo();
this.o.undo();
return value;
}
};
const cans = this.engine.getPuttablePlaces(this.botColor);
const cans = this.o.canPutSomewhere(this.botColor);
const scores = cans.map(p => dive(p));
const pos = cans[scores.indexOf(Math.max(...scores))];
console.log('Thinked:', pos);
console.timeEnd('think');
this.engine.putStone(pos);
this.currentTurn++;
setTimeout(() => {
const id = Math.random().toString(36).slice(2);
process.send!({
type: 'putStone',
pos,
id
type: 'put',
pos
});
this.appliedOps.push(id);
if (this.engine.turn === this.botColor) {
this.think();
}
}, 500);
}
@ -451,9 +433,9 @@ class Session {
}
try {
const res = await got.post(`${config.host}/api/notes/create`, {
const res = await request.post(`${config.host}/api/notes/create`, {
json: body
}).json();
});
return res.createdNote;
} catch (e) {

View file

@ -1,212 +0,0 @@
/**
* true ...
* false ...
*/
export type Color = boolean;
const BLACK = true;
const WHITE = false;
export type MapCell = 'null' | 'empty';
export type Options = {
isLlotheo: boolean;
canPutEverywhere: boolean;
loopedBoard: boolean;
};
export type Undo = {
color: Color;
pos: number;
/**
*
*/
effects: number[];
turn: Color | null;
};
export class Game {
public map: MapCell[];
public mapWidth: number;
public mapHeight: number;
public board: (Color | null | undefined)[];
public turn: Color | null = BLACK;
public opts: Options;
public prevPos = -1;
public prevColor: Color | null = null;
private logs: Undo[] = [];
constructor(map: string[], opts: Options) {
//#region binds
this.putStone = this.putStone.bind(this);
//#endregion
//#region Options
this.opts = opts;
if (this.opts.isLlotheo == null) this.opts.isLlotheo = false;
if (this.opts.canPutEverywhere == null) this.opts.canPutEverywhere = false;
if (this.opts.loopedBoard == null) this.opts.loopedBoard = false;
//#endregion
//#region Parse map data
this.mapWidth = map[0].length;
this.mapHeight = map.length;
const mapData = map.join('');
this.board = mapData.split('').map(d => d === '-' ? null : d === 'b' ? BLACK : d === 'w' ? WHITE : undefined);
this.map = mapData.split('').map(d => d === '-' || d === 'b' || d === 'w' ? 'empty' : 'null');
//#endregion
// ゲームが始まった時点で片方の色の石しかないか、始まった時点で勝敗が決定するようなマップの場合がある
if (!this.canPutSomewhere(BLACK)) this.turn = this.canPutSomewhere(WHITE) ? WHITE : null;
}
public get blackCount() {
return this.board.filter(x => x === BLACK).length;
}
public get whiteCount() {
return this.board.filter(x => x === WHITE).length;
}
public posToXy(pos: number): number[] {
const x = pos % this.mapWidth;
const y = Math.floor(pos / this.mapWidth);
return [x, y];
}
public xyToPos(x: number, y: number): number {
return x + (y * this.mapWidth);
}
public putStone(pos: number) {
const color = this.turn;
if (color == null) return;
this.prevPos = pos;
this.prevColor = color;
this.board[pos] = color;
// 反転させられる石を取得
const effects = this.effects(color, pos);
// 反転させる
for (const pos of effects) {
this.board[pos] = color;
}
const turn = this.turn;
this.logs.push({
color,
pos,
effects,
turn,
});
this.calcTurn();
}
private calcTurn() {
// ターン計算
this.turn =
this.canPutSomewhere(!this.prevColor) ? !this.prevColor :
this.canPutSomewhere(this.prevColor!) ? this.prevColor :
null;
}
public undo() {
const undo = this.logs.pop()!;
this.prevColor = undo.color;
this.prevPos = undo.pos;
this.board[undo.pos] = null;
for (const pos of undo.effects) {
const color = this.board[pos];
this.board[pos] = !color;
}
this.turn = undo.turn;
}
public mapDataGet(pos: number): MapCell {
const [x, y] = this.posToXy(pos);
return x < 0 || y < 0 || x >= this.mapWidth || y >= this.mapHeight ? 'null' : this.map[pos];
}
public getPuttablePlaces(color: Color): number[] {
return Array.from(this.board.keys()).filter(i => this.canPut(color, i));
}
public canPutSomewhere(color: Color): boolean {
return this.getPuttablePlaces(color).length > 0;
}
public canPut(color: Color, pos: number): boolean {
return (
this.board[pos] !== null ? false : // 既に石が置いてある場所には打てない
this.opts.canPutEverywhere ? this.mapDataGet(pos) === 'empty' : // 挟んでなくても置けるモード
this.effects(color, pos).length !== 0); // 相手の石を1つでも反転させられるか
}
/**
*
* @param color
* @param initPos
*/
public effects(color: Color, initPos: number): number[] {
const enemyColor = !color;
const diffVectors: [number, number][] = [
[0, -1], // 上
[+1, -1], // 右上
[+1, 0], // 右
[+1, +1], // 右下
[0, +1], // 下
[-1, +1], // 左下
[-1, 0], // 左
[-1, -1], // 左上
];
const effectsInLine = ([dx, dy]: [number, number]): number[] => {
const nextPos = (x: number, y: number): [number, number] => [x + dx, y + dy];
const found: number[] = []; // 挟めるかもしれない相手の石を入れておく配列
let [x, y] = this.posToXy(initPos);
while (true) {
[x, y] = nextPos(x, y);
// 座標が指し示す位置がボード外に出たとき
if (this.opts.loopedBoard && this.xyToPos(
(x = ((x % this.mapWidth) + this.mapWidth) % this.mapWidth),
(y = ((y % this.mapHeight) + this.mapHeight) % this.mapHeight)) === initPos) {
// 盤面の境界でループし、自分が石を置く位置に戻ってきたとき、挟めるようにしている (ref: Test4のマップ)
return found;
} else if (x === -1 || y === -1 || x === this.mapWidth || y === this.mapHeight) return []; // 挟めないことが確定 (盤面外に到達)
const pos = this.xyToPos(x, y);
if (this.mapDataGet(pos) === 'null') return []; // 挟めないことが確定 (配置不可能なマスに到達)
const stone = this.board[pos];
if (stone === null) return []; // 挟めないことが確定 (石が置かれていないマスに到達)
if (stone === enemyColor) found.push(pos); // 挟めるかもしれない (相手の石を発見)
if (stone === color) return found; // 挟めることが確定 (対となる自分の石を発見)
}
};
return ([] as number[]).concat(...diffVectors.map(effectsInLine));
}
public get isEnded(): boolean {
return this.turn === null;
}
public get winner(): Color | null {
return this.isEnded ?
this.blackCount === this.whiteCount ? null :
this.opts.isLlotheo === this.blackCount > this.whiteCount ? WHITE : BLACK :
undefined as never;
}
}

View file

@ -1,16 +1,11 @@
import * as childProcess from 'child_process';
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import serifs from '@/serifs.js';
import config from '@/config.js';
import Message from '@/message.js';
import Friend from '@/friend.js';
import getDate from '@/utils/get-date.js';
import { fileURLToPath } from 'node:url';
import { dirname, resolve } from 'node:path';
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
import autobind from 'autobind-decorator';
import Module from '@/module';
import serifs from '@/serifs';
import config from '@/config';
import Message from '@/message';
import Friend from '@/friend';
import getDate from '@/utils/get-date';
export default class extends Module {
public readonly name = 'reversi';
@ -20,17 +15,17 @@ export default class extends Module {
*/
private reversiConnection?: any;
@bindThis
@autobind
public install() {
if (!config.reversiEnabled) return {};
this.reversiConnection = this.ai.connection.useSharedConnection('reversi');
this.reversiConnection = this.ai.connection.useSharedConnection('gamesReversi');
// 招待されたとき
this.reversiConnection.on('invited', msg => this.onReversiInviteMe(msg.user));
this.reversiConnection.on('invited', msg => this.onReversiInviteMe(msg.parent));
// マッチしたとき
this.reversiConnection.on('matched', msg => this.onReversiGameStart(msg.game));
this.reversiConnection.on('matched', msg => this.onReversiGameStart(msg));
if (config.reversiEnabled) {
const mainStream = this.ai.connection.useSharedConnection('main');
@ -48,17 +43,13 @@ export default class extends Module {
};
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
if (msg.includes(['リバーシ', 'オセロ', 'reversi', 'othello'])) {
if (config.reversiEnabled) {
msg.reply(serifs.reversi.ok);
if (msg.includes(['接待'])) {
msg.friend.updateReversiStrength(0);
}
this.ai.api('reversi/match', {
this.ai.api('games/reversi/match', {
userId: msg.userId
});
} else {
@ -71,13 +62,13 @@ export default class extends Module {
}
}
@bindThis
@autobind
private async onReversiInviteMe(inviter: any) {
this.log(`Someone invited me: @${inviter.username}`);
if (config.reversiEnabled) {
// 承認
const game = await this.ai.api('reversi/match', {
const game = await this.ai.api('games/reversi/match', {
userId: inviter.id
});
@ -87,19 +78,12 @@ export default class extends Module {
}
}
@bindThis
@autobind
private onReversiGameStart(game: any) {
let strength = 4;
const friend = this.ai.lookupFriend(game.user1Id !== this.ai.account.id ? game.user1Id : game.user2Id)!;
if (friend != null) {
strength = friend.doc.reversiStrength ?? 4;
friend.updateReversiStrength(null);
}
this.log(`enter reversi game room: ${game.id}`);
this.log('enter reversi game room');
// ゲームストリームに接続
const gw = this.ai.connection.connectToChannel('reversiGame', {
const gw = this.ai.connection.connectToChannel('gamesReversiGame', {
gameId: game.id
});
@ -108,12 +92,12 @@ export default class extends Module {
id: 'publish',
type: 'switch',
label: '藍が対局情報を投稿するのを許可',
value: true,
value: true
}, {
id: 'strength',
type: 'radio',
label: '強さ',
value: strength,
value: 3,
items: [{
label: '接待',
value: 0
@ -133,7 +117,7 @@ export default class extends Module {
}];
//#region バックエンドプロセス開始
const ai = childProcess.fork(_dirname + '/back.js');
const ai = childProcess.fork(__dirname + '/back.js');
// バックエンドプロセスに情報を渡す
ai.send({
@ -146,10 +130,9 @@ export default class extends Module {
});
ai.on('message', (msg: Record<string, any>) => {
if (msg.type == 'putStone') {
gw.send('putStone', {
pos: msg.pos,
id: msg.id,
if (msg.type == 'put') {
gw.send('set', {
pos: msg.pos
});
} else if (msg.type == 'ended') {
gw.dispose();
@ -161,26 +144,21 @@ export default class extends Module {
// ゲームストリームから情報が流れてきたらそのままバックエンドプロセスに伝える
gw.addListener('*', message => {
ai.send(message);
if (message.type === 'updateSettings') {
if (message.body.key === 'canPutEverywhere') {
if (message.body.value === true) {
gw.send('ready', false);
} else {
gw.send('ready', true);
}
}
}
});
//#endregion
// フォーム初期化
setTimeout(() => {
gw.send('initForm', form);
}, 1000);
// どんな設定内容の対局でも受け入れる
setTimeout(() => {
gw.send('ready', true);
}, 1000);
gw.send('accept', {});
}, 2000);
}
@bindThis
@autobind
private onGameEnded(game: any) {
const user = game.user1Id == this.ai.account.id ? game.user2 : game.user1;

View file

@ -1,7 +1,7 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import serifs from '@/serifs.js';
import config from '@/config.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
import serifs from '@/serifs';
import config from '@/config';
export default class extends Module {
public readonly name = 'server';
@ -16,7 +16,7 @@ export default class extends Module {
*/
private statsLogs: any[] = [];
@bindThis
@autobind
public install() {
if (!config.serverMonitoring) return {};
@ -35,7 +35,7 @@ export default class extends Module {
return {};
}
@bindThis
@autobind
private check() {
const average = (arr) => arr.reduce((a, b) => a + b) / arr.length;
@ -48,12 +48,12 @@ export default class extends Module {
}
}
@bindThis
@autobind
private async onStats(stats: any) {
this.recentStat = stats;
}
@bindThis
@autobind
private warn() {
//#region 前に警告したときから一旦落ち着いた状態を経験していなければ警告しない
// 常に負荷が高いようなサーバーで無限に警告し続けるのを防ぐため

View file

@ -1,18 +1,18 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import serifs from '@/serifs.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
import serifs from '@/serifs';
export default class extends Module {
public readonly name = 'sleepReport';
@bindThis
@autobind
public install() {
this.report();
return {};
}
@bindThis
@autobind
private report() {
const now = Date.now();

View file

@ -1,21 +1,21 @@
import { bindThis } from '@/decorators.js';
import { HandlerResult } from '@/ai.js';
import Module from '@/module.js';
import Message from '@/message.js';
import serifs, { getSerif } from '@/serifs.js';
import getDate from '@/utils/get-date.js';
import autobind from 'autobind-decorator';
import { HandlerResult } from '@/ai';
import Module from '@/module';
import Message from '@/message';
import serifs, { getSerif } from '@/serifs';
import getDate from '@/utils/get-date';
export default class extends Module {
public readonly name = 'talk';
@bindThis
@autobind
public install() {
return {
mentionHook: this.mentionHook,
};
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
if (!msg.text) return false;
@ -37,7 +37,7 @@ export default class extends Module {
);
}
@bindThis
@autobind
private greet(msg: Message): boolean {
if (msg.text == null) return false;
@ -106,7 +106,7 @@ export default class extends Module {
return false;
}
@bindThis
@autobind
private erait(msg: Message): boolean {
const match = msg.extractedText.match(/(.+?)た(から|ので)(褒|ほ)めて/);
if (match) {
@ -133,7 +133,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private omedeto(msg: Message): boolean {
if (!msg.includes(['おめでと'])) return false;
@ -142,7 +142,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private nadenade(msg: Message): boolean {
if (!msg.includes(['なでなで'])) return false;
@ -174,7 +174,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private kawaii(msg: Message): boolean {
if (!msg.includes(['かわいい', '可愛い'])) return false;
@ -186,7 +186,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private suki(msg: Message): boolean {
if (!msg.or(['好き', 'すき'])) return false;
@ -198,7 +198,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private hug(msg: Message): boolean {
if (!msg.or(['ぎゅ', 'むぎゅ', /^はぐ(し(て|よ|よう)?)?$/])) return false;
@ -229,7 +229,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private humu(msg: Message): boolean {
if (!msg.includes(['踏んで'])) return false;
@ -241,7 +241,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private batou(msg: Message): boolean {
if (!msg.includes(['罵倒して', '罵って'])) return false;
@ -253,7 +253,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private itai(msg: Message): boolean {
if (!msg.or(['痛い', 'いたい']) && !msg.extractedText.endsWith('痛い')) return false;
@ -262,7 +262,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private ote(msg: Message): boolean {
if (!msg.or(['お手'])) return false;
@ -274,7 +274,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private ponkotu(msg: Message): boolean | HandlerResult {
if (!msg.includes(['ぽんこつ'])) return false;
@ -285,7 +285,7 @@ export default class extends Module {
};
}
@bindThis
@autobind
private rmrf(msg: Message): boolean | HandlerResult {
if (!msg.includes(['rm -rf'])) return false;
@ -296,7 +296,7 @@ export default class extends Module {
};
}
@bindThis
@autobind
private shutdown(msg: Message): boolean | HandlerResult {
if (!msg.includes(['shutdown'])) return false;

View file

@ -1,12 +1,12 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import Message from '@/message.js';
import serifs from '@/serifs.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
import Message from '@/message';
import serifs from '@/serifs';
export default class extends Module {
public readonly name = 'timer';
@bindThis
@autobind
public install() {
return {
mentionHook: this.mentionHook,
@ -14,7 +14,7 @@ export default class extends Module {
};
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
const secondsQuery = (msg.text || '').match(/([0-9]+)秒/);
const minutesQuery = (msg.text || '').match(/([0-9]+)分/);
@ -55,7 +55,7 @@ export default class extends Module {
return true;
}
@bindThis
@autobind
private timeoutCallback(data) {
const friend = this.ai.lookupFriend(data.userId);
if (friend == null) return; // 処理の流れ上、実際にnullになることは無さそうだけど一応

View file

@ -1,12 +1,12 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import Friend from '@/friend.js';
import serifs from '@/serifs.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
import Friend from '@/friend';
import serifs from '@/serifs';
export default class extends Module {
public readonly name = 'valentine';
@bindThis
@autobind
public install() {
this.crawleValentine();
setInterval(this.crawleValentine, 1000 * 60 * 3);
@ -17,7 +17,7 @@ export default class extends Module {
/**
*
*/
@bindThis
@autobind
private crawleValentine() {
const now = new Date();

View file

@ -1,10 +1,10 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
export default class extends Module {
public readonly name = 'welcome';
@bindThis
@autobind
public install() {
const tl = this.ai.connection.useSharedConnection('localTimeline');
@ -13,7 +13,7 @@ export default class extends Module {
return {};
}
@bindThis
@autobind
private onLocalNote(note: any) {
if (note.isFirstNote) {
setTimeout(() => {

View file

@ -1,10 +1,8 @@
import { bindThis } from '@/decorators.js';
import autobind from 'autobind-decorator';
import { EventEmitter } from 'events';
import WebSocket from 'ws';
import _ReconnectingWebsocket from 'reconnecting-websocket';
import config from './config.js';
const ReconnectingWebsocket = _ReconnectingWebsocket as unknown as typeof _ReconnectingWebsocket['default'];
import * as WebSocket from 'ws';
const ReconnectingWebsocket = require('reconnecting-websocket');
import config from './config';
/**
* Misskey stream connection
@ -31,7 +29,7 @@ export default class Stream extends EventEmitter {
this.stream.addEventListener('message', this.onMessage);
}
@bindThis
@autobind
public useSharedConnection(channel: string): SharedConnection {
let pool = this.sharedConnectionPools.find(p => p.channel === channel);
@ -45,19 +43,19 @@ export default class Stream extends EventEmitter {
return connection;
}
@bindThis
@autobind
public removeSharedConnection(connection: SharedConnection) {
this.sharedConnections = this.sharedConnections.filter(c => c !== connection);
}
@bindThis
@autobind
public connectToChannel(channel: string, params?: any): NonSharedConnection {
const connection = new NonSharedConnection(this, channel, params);
this.nonSharedConnections.push(connection);
return connection;
}
@bindThis
@autobind
public disconnectToChannel(connection: NonSharedConnection) {
this.nonSharedConnections = this.nonSharedConnections.filter(c => c !== connection);
}
@ -65,7 +63,7 @@ export default class Stream extends EventEmitter {
/**
* Callback of when open connection
*/
@bindThis
@autobind
private onOpen() {
const isReconnect = this.state == 'reconnecting';
@ -93,7 +91,7 @@ export default class Stream extends EventEmitter {
/**
* Callback of when close connection
*/
@bindThis
@autobind
private onClose() {
this.state = 'reconnecting';
this.emit('_disconnected_');
@ -102,7 +100,7 @@ export default class Stream extends EventEmitter {
/**
* Callback of when received a message from connection
*/
@bindThis
@autobind
private onMessage(message) {
const { type, body } = JSON.parse(message.data);
@ -130,7 +128,7 @@ export default class Stream extends EventEmitter {
/**
* Send a message to connection
*/
@bindThis
@autobind
public send(typeOrPayload, payload?) {
const data = payload === undefined ? typeOrPayload : {
type: typeOrPayload,
@ -149,7 +147,7 @@ export default class Stream extends EventEmitter {
/**
* Close this connection
*/
@bindThis
@autobind
public close() {
this.stream.removeEventListener('open', this.onOpen);
this.stream.removeEventListener('message', this.onMessage);
@ -171,7 +169,7 @@ class Pool {
this.id = Math.random().toString();
}
@bindThis
@autobind
public inc() {
if (this.users === 0 && !this.isConnected) {
this.connect();
@ -186,7 +184,7 @@ class Pool {
}
}
@bindThis
@autobind
public dec() {
this.users--;
@ -200,7 +198,7 @@ class Pool {
}
}
@bindThis
@autobind
public connect() {
this.isConnected = true;
this.stream.send('connect', {
@ -209,7 +207,7 @@ class Pool {
});
}
@bindThis
@autobind
private disconnect() {
this.isConnected = false;
this.disposeTimerId = null;
@ -229,7 +227,7 @@ abstract class Connection extends EventEmitter {
this.channel = channel;
}
@bindThis
@autobind
public send(id: string, typeOrPayload, payload?) {
const type = payload === undefined ? typeOrPayload.type : typeOrPayload;
const body = payload === undefined ? typeOrPayload.body : payload;
@ -258,12 +256,12 @@ class SharedConnection extends Connection {
this.pool.inc();
}
@bindThis
@autobind
public send(typeOrPayload, payload?) {
super.send(this.pool.id, typeOrPayload, payload);
}
@bindThis
@autobind
public dispose() {
this.pool.dec();
this.removeAllListeners();
@ -284,7 +282,7 @@ class NonSharedConnection extends Connection {
this.connect();
}
@bindThis
@autobind
public connect() {
this.stream.send('connect', {
channel: this.channel,
@ -293,12 +291,12 @@ class NonSharedConnection extends Connection {
});
}
@bindThis
@autobind
public send(typeOrPayload, payload?) {
super.send(this.id, typeOrPayload, payload);
}
@bindThis
@autobind
public dispose() {
this.removeAllListeners();
this.stream.send('disconnect', { id: this.id });

View file

@ -1,4 +1,4 @@
import { katakanaToHiragana, hankakuToZenkaku } from './japanese.js';
import { katakanaToHiragana, hankakuToZenkaku } from './japanese';
export default function(text: string, words: string[]): boolean {
if (text == null) return false;

View file

@ -1,4 +1,4 @@
import chalk from 'chalk';
import * as chalk from 'chalk';
export default function(msg: string) {
const now = new Date();

View file

@ -1,4 +1,4 @@
import { hankakuToZenkaku, katakanaToHiragana } from './japanese.js';
import { hankakuToZenkaku, katakanaToHiragana } from './japanese';
export default function(text: string, words: (string | RegExp)[]): boolean {
if (text == null) return false;

View file

@ -1,7 +0,0 @@
export function sleep(msec: number) {
return new Promise<void>(res => {
setTimeout(() => {
res();
}, msec);
});
}

View file

@ -1,4 +1,4 @@
import seedrandom from 'seedrandom';
import * as seedrandom from 'seedrandom';
export const itemPrefixes = [
'プラチナ製',

View file

@ -1,18 +1,18 @@
import { bindThis } from '@/decorators.js';
import Module from '@/module.js';
import autobind from 'autobind-decorator';
import Module from '@/module';
import Message from '@/message';
export default class extends Module {
public readonly name = 'test';
@bindThis
@autobind
public install() {
return {
mentionHook: this.mentionHook
};
}
@bindThis
@autobind
private async mentionHook(msg: Message) {
if (msg.text && msg.text.includes('ping')) {
msg.reply('PONG!', {

View file

@ -1,46 +1,26 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"target": "ES2022",
"module": "nodenext",
"moduleResolution": "nodenext",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"rootDir": "./src",
"outDir": "./built/",
"removeComments": true,
"resolveJsonModule": true,
"strict": true,
"strictFunctionTypes": true,
"noEmitOnError": true,
"noImplicitAny": false,
"noImplicitReturns": true,
"noImplicitThis": true,
"noFallthroughCasesInSwitch": true,
"strictNullChecks": true,
"experimentalDecorators": true,
"noImplicitReturns": true,
"noImplicitAny": false,
"esModuleInterop": true,
"typeRoots": [
"./node_modules/@types"
],
"lib": [
"esnext",
"dom"
],
"sourceMap": false,
"target": "es2020",
"module": "commonjs",
"removeComments": false,
"noLib": false,
"outDir": "built",
"rootDir": "src",
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
"@/*": ["src/*"]
},
"plugins": [
// Transform paths in output .js files
{ "transform": "typescript-transform-paths" },
// Transform paths in output .d.ts files (Include this line if you output declarations files)
{ "transform": "typescript-transform-paths", "afterDeclarations": true }
]
},
"compileOnSave": false,
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"test/**/*"
],
"./src/**/*.ts"
]
}