Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch1130 #34

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ jobs:
echo "$DEPLOY_PRI" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.name '$DEPLOY_PRI'
git config --global user.email '$DEPLOY_PRI'
git config --global user.name 'BuildBot'
git config --global user.email "$GIT_EMAIL"

- name: Commit Build # 提交文档到Git仓库
env:
Expand Down
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

## How to use

### Dev Install 开发者安装

适用于希望对该bot做出开发,或者可能改动代码的人员

复制 `config.example.yml` 到 `config.yml`

```
Expand All @@ -14,13 +18,66 @@ cp config.example.yml config.yml

接下来,按 `config.example.yml` 的说明配置 `config.yml`

安装依赖(包括devDependences)

```
npm install
```

安装其他依赖见 User Install部分

最后,启动

```
npm run build
npm run start
```

### User Install 用户安装

适用于不希望对该bot做出改动,直接上手的人员

创建一个文件夹容纳该bot (注意:bot会将数据文件存放在该文件夹的data子文件夹)

```
mkdir linquebot2

cd linquebot2

git clone https://github.com/Lhcfl/Linquebot_v2 -b build

cp Linquebot_v2/config.example.yml ./config.yml
```

配置 `config.yml`

安装依赖

```
npm install --omit=dev

```

安装其他依赖:

- **waife模块**:

- Graphviz:
```
sudo apt install graphviz -y
```
- 全字体:
```
sudo apt install -y --force-yes --no-install-recommends fonts-noto fonts-noto-cjk fonts-noto-cjk-extra fonts-noto-color-emoji ttf-ancient-fonts
```

启动

```
cd Linquebot_v2
npm run run
```

## Develop Plugin

[Api referances](https://lhcfl.github.io/Linquebot_v2/)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,5 @@ export function botOnOffRegister(func: typeof on_off_mode) {
* @returns bot是否处于打开状态
*/
export async function botOnOff(app: App, msg: Message): Promise<boolean> {
return await on_off_mode(app, msg);
return Boolean(await on_off_mode(app, msg));
}
2 changes: 1 addition & 1 deletion src/plugins/bot_on_off/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const bot_status: commandHandleFunction = async (app, msg) => {
};

const init: PluginInit = (app) => {
console.log('xxx loaded!');
console.log('bot_on_off loaded!');
app.db.register('is turned on', [() => true]);
botOnOffRegister(
async (_, msg) => await app.db.peek_path<boolean>(['is turned on', msg.chat.id], (val) => val)
Expand Down
76 changes: 51 additions & 25 deletions src/plugins/chengyu/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { User } from 'node-telegram-bot-api';
import { commandHandleFunction, handleFunction } from '../../lib/command.js';
import { PluginInit } from '../../types/plugin.js';
import { cy as _cy, cylist as _cylist, py as _py } from './lib_loader.js';
Expand All @@ -8,6 +9,12 @@ const cylist: { [key: string]: string[] } = _cylist; // 首拼音对应的成语
const py: string[] = _py; // 所有可能的成语开局

type userIdType = string | number;
type UserStatusType = {
username?: string;
score?: number;
combo?: number;
uid?: userIdType;
};
class jielongStatus {
/** 是否已开始接龙 */
started: boolean = false;
Expand All @@ -16,24 +23,28 @@ class jielongStatus {
/** 上一个被接龙的成语 */
stcy?: string;
/** 已经接龙的词语Map 真值说明已经接过。 */
counted: { [key: string]: number | boolean } = {};
counted?: { [key: string]: number | boolean } = {};
/** 上一个接龙的userid */
lastJielonger?: userIdType;
/** 目前连击数 */
combo?: number;
/** 用户得分信息 */
userStatus?: {
[key: userIdType]: {
username?: string;
score?: number;
combo?: number;
uid?: number;
};
[key: userIdType]: UserStatusType | undefined;
};
/** 开始时间(转换为Number) */
startAt?: number;
}

function getDefaultUserStatus(u?: User): UserStatusType {
return {
uid: u?.id,
username: u?.username || u?.first_name,
score: 0,
combo: 1,
};
}

function gameEnded(data: jielongStatus): boolean {
if (data.started) {
const startAt = data.startAt;
Expand All @@ -53,7 +64,14 @@ function setRandomChenyu(data: jielongStatus, starter?: string): string {
return setRandomChenyu(data, py[Math.floor(Math.random() * py.length)]);
} else {
const find: string = cylist[starter][Math.floor(Math.random() * cylist[starter].length)];
data.counted[find] = true;

if (data.counted) {
data.counted[find] = true;
} else {
const newDataCounted: Record<string, number | boolean> = {};
newDataCounted[find] = true;
data.counted = newDataCounted;
}
data.st = cy[find].ed;
data.stcy = find;
return find;
Expand All @@ -74,9 +92,10 @@ function getJielongInfo(data: jielongStatus): string {
return '无数据!';
}

const uList = [];
const uList: UserStatusType[] = [];
for (const uid of Object.keys(userStatus)) {
uList.push(userStatus[uid]);
const us = userStatus[uid];
if (us) uList.push(us);
}
uList.sort((a, b) => Number(b.score) - Number(a.score));
for (let i = 0; i < uList.length; i++) {
Expand Down Expand Up @@ -173,30 +192,34 @@ const newMessageHandle: handleFunction = async (app, msg) => {
if (data.st !== cy[msg.text].st) {
return;
}
if (data.counted[msg.text]) {
if (data.counted && data.counted[msg.text]) {
void app.bot?.sendMessage(msg.chat.id, '这个成语接过了哦');
} else {
if (!msg.from?.id) {
return;
}
// 接龙成功
data.counted[msg.text] = true;
if (data.counted) {
data.counted[msg.text] = true;
} else {
const newDataCounted: Record<string, number | boolean> = {};
newDataCounted[msg.text] = true;
data.counted = newDataCounted;
}

data.st = cy[msg.text].ed;
data.stcy = msg.text;
let userStatus = data.userStatus;
if (!userStatus) {
data.userStatus = {};
userStatus = {};
}
if (!userStatus[msg.from?.id]) {
userStatus[msg.from?.id] = {
uid: msg.from?.id,
username: msg.from?.username,
score: 0,
combo: 1,
};
}
userStatus[msg.from?.id].score = Number(userStatus[msg.from?.id].score) + 1;
const userStatusToUpdate = userStatus[msg.from?.id] || getDefaultUserStatus(msg.from);

userStatusToUpdate.score = Number(userStatusToUpdate.score) + 1;

userStatus[msg.from?.id] = userStatusToUpdate;

// 判断combo
if (!data.combo) {
data.combo = 0;
Expand All @@ -206,10 +229,13 @@ const newMessageHandle: handleFunction = async (app, msg) => {
} else {
const lastJielonger = data.lastJielonger;
if (lastJielonger) {
userStatus[lastJielonger].combo = Math.max(
userStatus[lastJielonger].combo ?? 0,
data.combo
);
const lastJielongerUserStatus = userStatus[lastJielonger] || {
uid: lastJielonger,
score: 0,
combo: 1,
};
lastJielongerUserStatus.combo = Math.max(lastJielongerUserStatus.combo ?? 0, data.combo);
userStatus[lastJielonger] = lastJielongerUserStatus;
}
data.combo = 1;
data.lastJielonger = msg.from?.id;
Expand Down
Loading