fix: break circular import in horse racing commands
Extract shared.py from commands/__init__.py to break circular dependency: - shared.py: shared variables/services/helper functions - access.py: get_scope/check_access/get_event_id (canonical source) - __init__.py: re-exports from shared.py for backward compat - register/bet/race/help: import from .shared instead of package
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
import asyncio
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.onebot.v11 import Bot, Event
|
||||
from . import (
|
||||
from nonebot.adapters.onebot.v11 import Bot, Event, GroupMessageEvent
|
||||
from .shared import (
|
||||
room_store, race_engine, config, logger,
|
||||
get_scope, check_access, get_event_id,
|
||||
_send_to_scope, _build_race_image_message,
|
||||
_get_user_name, _get_horses_in_order, _format_horse_label,
|
||||
run_race_with_settlement, points_service,
|
||||
)
|
||||
from ..models import RoomState, HorseState
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent
|
||||
from ..models import RoomState
|
||||
|
||||
race_list_cmd = on_command("赛马列表", priority=5)
|
||||
|
||||
|
||||
@race_list_cmd.handle()
|
||||
async def handle_race_list(bot: Bot, event: Event):
|
||||
@@ -61,7 +63,6 @@ async def handle_start(bot: Bot, event: Event):
|
||||
await start_cmd.finish("至少需要2匹马才能开赛")
|
||||
return
|
||||
|
||||
# 开赛权限限制:仅参赛者或群管理员可手动开赛(满8匹自动开赛不受影响)
|
||||
is_participant = user_id in [h.owner_id for h in room.horses.values()]
|
||||
is_admin = False
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
@@ -79,13 +80,11 @@ async def handle_start(bot: Bot, event: Event):
|
||||
await start_cmd.finish("只有参赛者或群管理员可以开赛")
|
||||
return
|
||||
|
||||
# Set all horses to racing state
|
||||
for horse in room.horses.values():
|
||||
horse.state = HorseState.RACING
|
||||
|
||||
await start_cmd.send("比赛开始!")
|
||||
|
||||
# Run race in background (outside command handler)
|
||||
task = asyncio.create_task(run_race_with_settlement(bot, room, scope))
|
||||
race_engine.register_task(scope, task)
|
||||
|
||||
@@ -114,7 +113,6 @@ async def handle_cancel_race(bot: Bot, event: Event):
|
||||
await cancel_race_cmd.finish("当前没有进行中的比赛")
|
||||
return
|
||||
|
||||
# 权限:只有参赛者或群管理员可以取消
|
||||
is_participant = user_id in [h.owner_id for h in room.horses.values()]
|
||||
is_admin = False
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
@@ -132,31 +130,22 @@ async def handle_cancel_race(bot: Bot, event: Event):
|
||||
await cancel_race_cmd.finish("只有参赛者或群管理员可以取消比赛")
|
||||
return
|
||||
|
||||
# 停止后台比赛任务
|
||||
race_engine.stop_race(scope)
|
||||
|
||||
# 退还所有下注积分
|
||||
total_refund = 0
|
||||
for bet in room.bets[:]: # 遍历副本
|
||||
for bet in room.bets[:]:
|
||||
success, _ = await points_service.refund_bet_points(
|
||||
bet.user_id, bet.amount, "比赛取退还下注"
|
||||
bet.user_id, bet.amount, "比赛取消退还下注"
|
||||
)
|
||||
if success:
|
||||
total_refund += bet.amount
|
||||
|
||||
# 清空下注记录
|
||||
room.bets.clear()
|
||||
|
||||
# 重置马匹状态为等待
|
||||
for horse in room.horses.values():
|
||||
horse.state = HorseState.WAITING
|
||||
|
||||
# 重置房间状态
|
||||
room.state = RoomState.WAITING
|
||||
room.tick_count = 0
|
||||
|
||||
await _send_to_scope(scope, f"🏇 比赛已取消!共退还 {total_refund} 积分。")
|
||||
|
||||
help_cmd = on_command("赛马帮助", priority=5)
|
||||
|
||||
|
||||
await _send_to_scope(bot, scope, f"🏇 比赛已取消!共退还 {total_refund} 积分。")
|
||||
|
||||
Reference in New Issue
Block a user