甲骨文
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-12-16 16:13:55 +08:00
parent 7026337a43
commit 770d7567fb
4 changed files with 107 additions and 14 deletions

View File

@ -4,6 +4,8 @@ import json
import secrets
import csv
import zipfile
from PIL import Image
from io import BytesIO
from enum import Enum
from pathlib import Path
from typing import Optional
@ -126,12 +128,12 @@ class oracleGame:
def get_oracle_image(self) -> bytes:
IMAGE_PATH = ASSETS_PATH / "oracle" / "image"
with open(IMAGE_PATH / self.current_oracle_id / self.current_oracle_id / f"{self.current_oracle_id}.png", "rb") as f:
with open(IMAGE_PATH / self.ALL_ORACLES[self.current_oracle_id]["image"], "rb") as f:
img_data = f.read()
return img_data
def get_oracle_name(self) -> str:
return self.ALL_ORACLES.get(self.current_oracle_id, "?")[0]
return self.ALL_ORACLES.get(self.current_oracle_id, {}).get("oracle", "?")[0]
@staticmethod
async def random_oracle() -> str:
@ -205,14 +207,19 @@ class oracleGame:
async def _verify_oracle(self, oracle: str, user_id: str) -> list[TryVerifyState]:
state = []
if oracle not in self.ALL_ORACLES[self.current_oracle_id]:
if oracle.strip() not in self.ALL_ORACLES[self.current_oracle_id].get("oracle", ""):
return [TryVerifyState.NOT_ORACLE]
if oracle.strip() == "":
return [TryVerifyState.NOT_ORACLE]
# 甲骨文合法,更新状态
state.append(TryVerifyState.VERIFIED)
self.add_score(user_id, 1) # 加 1 分
self.remain_rounds -= 1
if self.remain_rounds <= 0:
self.now_playing = False
state.append(TryVerifyState.GAME_END)
else:
await self._skip_oracle_async()
return state
def get_user_score(self, user_id: str) -> float:
@ -248,7 +255,11 @@ class oracleGame:
for row in reader:
char = row["子字头"].strip()
oracle = row["释文"].strip()
cls.ALL_ORACLES[char] = oracle
img_path = row.get("路径", "").strip()
cls.ALL_ORACLES[char] = {
"oracle": oracle,
"image": img_path,
}
logger.info(f"加载甲骨文字典,共计 {len(cls.ALL_ORACLES)} 条记录")
@ -377,14 +388,22 @@ async def _(event: BaseEvent, target: DepLongTaskTarget):
# 打开好吧狗本地文件
with open(ASSETS_PATH / "img" / "dog" / "haoba_dog.jpg", "rb") as f:
img_data = f.read()
# 把好吧狗变成 GIF 格式以缩小尺寸
img_data = await convert_image_to_gif(img_data)
await evt.send(await UniMessage().image(raw=img_data).export())
await end_game(event, group_id)
else:
await evt.send(
await UniMessage().text("当前没有甲骨文游戏在进行中!").export()
)
# await evt.send(
# await UniMessage().text("当前没有甲骨文游戏在进行中!").export()
# )
return
async def convert_image_to_gif(image_data: bytes) -> bytes:
with Image.open(BytesIO(image_data)) as img:
with BytesIO() as output:
img.save(output, format="GIF")
return output.getvalue()
# 跳过
evt = on_alconna(
Alconna("跳过甲骨文"), use_cmd_start=True, use_cmd_sep=False, skip_for_unmatch=True
@ -401,6 +420,8 @@ async def _(target: DepLongTaskTarget):
# 发送哈哈狗图片
with open(ASSETS_PATH / "img" / "dog" / "haha_dog.jpg", "rb") as f:
img_data = f.read()
# 把哈哈狗变成 GIF 格式以缩小尺寸
img_data = await convert_image_to_gif(img_data)
oracle = instance.get_oracle_name()
await evt.send(await UniMessage().image(raw=img_data).export())
await evt.send(await UniMessage().text(f"你们太菜了全部扣100分这个甲骨文是「{oracle}」!").export())
@ -438,10 +459,11 @@ async def _(event: BaseEvent, msg: UniMsg, target: DepLongTaskTarget):
state = await instance.try_verify_oracle(user_oracle, user_id)
if TryVerifyState.NOT_ORACLE in state:
return
if TryVerifyState.VERIFIED:
if TryVerifyState.VERIFIED in state:
await evt.send(
await UniMessage()
.text(f"{user_name} 答对了!获得 1 分!")
.at(user_id)
.text(" 答对了!获得 1 分!")
.export()
)
if TryVerifyState.GAME_END in state: