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

[Fix] Fix jielong #33

Merged
merged 2 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
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