Skip to content

Commit

Permalink
赛季支持配置PT精度
Browse files Browse the repository at this point in the history
(cherry picked from commit 1980575)
  • Loading branch information
ssttkkl committed Feb 24, 2023
1 parent d744226 commit 0812808
Show file tree
Hide file tree
Showing 22 changed files with 778 additions and 196 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,6 @@ dmypy.json
# End of https://www.toptal.com/developers/gitignore/api/python

/.idea/
.env.*
*.db
bot.py
390 changes: 389 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nonebot-plugin-mahjong-scoreboard"
version = "0.1.10"
version = "0.1.11"
description = "日麻寄分器(NoneBot插件)"
authors = ["ssttkkl <[email protected]>"]
readme = "README.MD"
Expand All @@ -25,6 +25,7 @@ nonebot-plugin-gocqhttp-cross-machine-upload-file = "^0.1.0"
nonebug = "^0.2.1"
flake8 = "^5.0.4"
sqlalchemy2-stubs = "^0.0.2a29"
nonebot2 = {extras = ["fastapi"], version = "^2.0.0rc3"}

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
12 changes: 6 additions & 6 deletions src/nonebot_plugin_mahjong_scoreboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
- {default_cmd_start}结束赛季
- {default_cmd_start}删除赛季 [<代号>]
赛季PT:
- {default_cmd_start}设置赛季PT <PT> [@<用户>]
赛季查询:
- {default_cmd_start}查询榜单 [<赛季代号>]
- {default_cmd_start}导出榜单 [<赛季代号>]
- {default_cmd_start}查询最近走势 [@<用户>]
- {default_cmd_start}查询PT [@<用户>]
- {default_cmd_start}查询榜单
- {default_cmd_start}导出榜单
- {default_cmd_start}设置PT <PT> [@<用户>]
以上命令格式中,以<>包裹的表示一个参数,以[]包裹的表示一个可选项。
Expand All @@ -49,8 +50,7 @@
__plugin_meta__ = PluginMetadata(
name='日麻寄分器',
description='为群友提供日麻计分及榜单统计功能',
usage=help_text,
extra={'version': '0.1.0'}
usage=help_text
)

from . import controller
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
require_game_code_from_context, require_parse_unary_integer_arg
from nonebot_plugin_mahjong_scoreboard.controller.interceptor import general_interceptor
from nonebot_plugin_mahjong_scoreboard.controller.mapper.game_mapper import map_game
from nonebot_plugin_mahjong_scoreboard.controller.utils import split_message, parse_int_or_error, try_parse_wind
from nonebot_plugin_mahjong_scoreboard.controller.utils import split_message, parse_int_or_error, try_parse_wind, \
parse_float_or_error
from nonebot_plugin_mahjong_scoreboard.errors import BadRequestError
from nonebot_plugin_mahjong_scoreboard.model.enums import PlayerAndWind, GameState
from nonebot_plugin_mahjong_scoreboard.service import game_service, group_service, user_service
Expand Down Expand Up @@ -196,7 +197,7 @@ async def parse_record_point_args(event: GroupMessageEvent, matcher: Matcher):
elif arg.type == 'at':
user_id = int(arg.data["qq"])

point = parse_int_or_error(point, 'PT')
point = parse_float_or_error(point, 'PT')

if game_code is None:
raise BadRequestError("请指定对局编号")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from nonebot_plugin_mahjong_scoreboard.controller.context import get_context
from nonebot_plugin_mahjong_scoreboard.controller.interceptor import general_interceptor
from nonebot_plugin_mahjong_scoreboard.controller.utils import get_group_info, split_message, \
parse_int_or_error
parse_int_or_error, parse_float_or_error
from nonebot_plugin_mahjong_scoreboard.errors import BadRequestError
from nonebot_plugin_mahjong_scoreboard.service.group_service import get_group_by_binding_qq, ensure_group_admin
from nonebot_plugin_mahjong_scoreboard.service.user_service import get_user_by_binding_qq
Expand Down Expand Up @@ -82,6 +82,23 @@ async def receive(event: MessageEvent, matcher: Matcher):
return matcher_type


def require_float(matcher_type: Type[Matcher], arg_name: str, desc: str):
@matcher_type.handle()
async def check(matcher: Matcher):
if arg_name not in matcher.state:
await matcher.pause(desc + "?")

@matcher_type.handle()
@general_interceptor(matcher_type)
async def receive(event: MessageEvent, matcher: Matcher):
if arg_name not in matcher.state:
arg = event.message.extract_plain_text()
arg = parse_float_or_error(arg, desc)
matcher.state[arg_name] = arg

return matcher_type


def require_str(matcher_type: Type[Matcher], arg_name: str, desc: str):
@matcher_type.handle()
async def check(matcher: Matcher):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,35 @@ async def map_game(game: GameOrm, *, detailed: bool = False) -> Message:
session = data_source.session()

group = await session.get(GroupOrm, game.group_id)
season = await session.get(SeasonOrm, game.season_id)

with StringIO() as io:
# 对局22090901 四人南
io.write('对局')
io.write(str(game.code))
io.write(' ')
io.write(player_and_wind_mapping[game.player_and_wind])
io.write('\n')
io.write(f'对局{game.code} {player_and_wind_mapping[game.player_and_wind]}\n')

if detailed:
# 所属赛季:Season Name
io.write('所属赛季:')
if game.season_id is None:
io.write('无')
else:
season = await session.get(SeasonOrm, game.season_id)
io.write(season.name)
io.write('\n')
season_name = '无'
if game.season_id is not None:
season_name = season.name
io.write(f'所属赛季:{season_name}\n')

promoter = await session.get(UserOrm, game.promoter_user_id)
io.write('创建者:')
io.write(await get_user_nickname(promoter, group))
io.write('\n')
io.write(f'创建者:{await get_user_nickname(promoter, group)}\n')

# 状态:未完成
io.write('状态:')
io.write(game_state_mapping[GameState(game.state)])
io.write(f'状态:{game_state_mapping[GameState(game.state)]}')
if game.state != GameState.completed:
io.write(" (合计")
io.write(str(sum(map(lambda r: r.score, game.records))))
io.write("点)")
sum_score = sum(map(lambda r: r.score, game.records))
io.write(f" (合计{sum_score}点)")
io.write('\n')

if detailed and game.state == GameState.completed:
io.write('完成时间:')
io.write(map_datetime(game.complete_time))
io.write('\n')
io.write(f'完成时间:{map_datetime(game.complete_time)}\n')

progress = await session.get(GameProgressOrm, game.id)
if progress is not None:
io.write('进度:')
io.write(map_game_progress(progress))
io.write('\n')
io.write(f'进度:{map_game_progress(progress)}\n')

if len(game.records) > 0:
# [空行]
Expand All @@ -87,26 +73,18 @@ async def map_game(game: GameOrm, *, detailed: bool = False) -> Message:
for rank, r in ranked(game.records, key=lambda r: r.point, reverse=True):
user = await session.get(UserOrm, r.user_id)
name = await get_user_nickname(user, group)
io.write('#')
io.write(str(rank))
io.write(f'#{rank}')
if r.wind is not None:
io.write(' [')
io.write(wind_mapping[r.wind])
io.write(']')
io.write(' ')
io.write(name)
io.write(' ')
io.write(str(r.score))
io.write('点')
io.write(f' [{wind_mapping[r.wind]}]')
io.write(f' {name} {r.score}点')

if game.state == GameState.completed:
io.write(' (')
point_text = str(r.point)
if r.point > 0:
io.write('+')
point_text = f'+{point_text}'
elif r.point == 0:
io.write('±')
io.write(str(r.point))
io.write(')')
point_text = f'±{point_text}'
io.write(f' ({point_text})')

io.write('\n')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from nonebot.adapters.onebot.v11 import Message, MessageSegment

from nonebot_plugin_mahjong_scoreboard.controller.mapper import season_state_mapping, datetime_format, map_datetime
from nonebot_plugin_mahjong_scoreboard.controller.mapper import season_state_mapping, map_datetime
from nonebot_plugin_mahjong_scoreboard.model.orm.season import SeasonOrm


Expand Down Expand Up @@ -43,11 +43,11 @@ def map_season(season: SeasonOrm, *, group_info: Optional[dict] = None) -> Messa

# 半庄战:返点:30000 马点:50 30 -10 -30
io.write('半庄战:')
if season.config["south_game_enabled"]:
if season.config.south_game_enabled:
io.write('返点:')
io.write(str(season.config["south_game_origin_point"]))
io.write(str(season.config.south_game_origin_point))
io.write(' 马点:')
for i in season.config["south_game_horse_point"]:
for i in season.config.south_game_horse_point:
io.write(str(i))
io.write(' ')
else:
Expand All @@ -56,15 +56,20 @@ def map_season(season: SeasonOrm, *, group_info: Optional[dict] = None) -> Messa

# 东风战:关闭
io.write('东风战:')
if season.config["east_game_enabled"]:
if season.config.east_game_enabled:
io.write('返点:')
io.write(str(season.config["east_game_origin_point"]))
io.write(str(season.config.east_game_origin_point))
io.write(' 马点:')
for i in season.config["east_game_horse_point"]:
for i in season.config.east_game_horse_point:
io.write(str(i))
io.write(' ')
else:
io.write('关闭')
io.write('\n')

# PT精度:1
io.write('PT精度:')
io.write(str(10 ** season.config.point_precision))
io.write('\n')

return Message(MessageSegment.text(io.getvalue().strip()))
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from nonebot.adapters.onebot.v11 import Message, MessageSegment

from nonebot_plugin_mahjong_scoreboard.controller.mapper import season_state_mapping, digit_mapping
from nonebot_plugin_mahjong_scoreboard.controller.mapper import season_state_mapping
from nonebot_plugin_mahjong_scoreboard.model.orm import data_source
from nonebot_plugin_mahjong_scoreboard.model.orm.game import GameRecordOrm, GameOrm
from nonebot_plugin_mahjong_scoreboard.model.orm.group import GroupOrm
Expand All @@ -14,6 +14,15 @@
from nonebot_plugin_mahjong_scoreboard.utils.rank import ranked


def map_point(point: int, precision: int) -> str:
point_text = str(point * 10 ** precision)
if point > 0:
point_text = f'+{point_text}'
elif point == 0:
point_text = f'±{point_text}'
return point_text


async def map_season_user_point(sup: SeasonUserPointOrm,
rank: Optional[int] = None,
total: Optional[int] = None) -> Message:
Expand All @@ -28,27 +37,17 @@ async def map_season_user_point(sup: SeasonUserPointOrm,
with StringIO() as io:
# [用户名]在赛季[赛季名]
# PT:+114
io.write(name)
io.write("在赛季")
io.write(season.name)
io.write('\n')

io.write("PT:")
if sup.point > 0:
io.write('+')
elif sup.point == 0:
io.write('±')
io.write(str(sup.point))
# 位次:30/36
io.write(f"[{name}]在赛季[{season.name}]\n")
io.write(f"PT:{map_point(sup.point, season.config.point_precision)}\n")

if rank is not None:
# 位次:+114
io.write('\n位次:')
io.write(str(rank))
io.write(f'位次:{rank}')
if total is not None:
io.write('/')
io.write(str(total))
io.write(f'/{total}')

return Message(MessageSegment.text(io.getvalue()))
return Message(MessageSegment.text(io.getvalue().strip()))


async def map_season_user_points(group: GroupOrm, season: SeasonOrm, sups: List[SeasonUserPointOrm]) -> List[Message]:
Expand All @@ -61,24 +60,14 @@ async def map_season_user_points(group: GroupOrm, season: SeasonOrm, sups: List[

# 赛季:[赛季名]
# 状态:进行中
pending_message.write("赛季:")
pending_message.write(season.name)
pending_message.write("\n状态:")
pending_message.write(season_state_mapping[season.state])
pending_message.write("\n\n")
pending_message.write(f"赛季:{season.name}\n")
pending_message.write(f"状态:{season_state_mapping[season.state]}\n\n")

for rank, sup in ranked(sups, key=lambda sup: sup.point, reverse=True):
user = await session.get(UserOrm, sup.user_id)
name = await get_user_nickname(user, group)

point_text = ""
if sup.point > 0:
point_text = '+'
elif sup.point == 0:
point_text = '±'
point_text += str(sup.point)

line = f"#{rank} {name} {point_text}\n"
line = f"#{rank} {name} {map_point(sup.point, season.config.point_precision)}\n"
pending_message.write(line)
pending += 1

Expand All @@ -96,29 +85,11 @@ async def map_season_user_points(group: GroupOrm, season: SeasonOrm, sups: List[
async def map_season_user_trend(group: GroupOrm, user: UserOrm, season: SeasonOrm,
result: List[Tuple[SeasonUserPointChangeLogOrm, GameOrm, GameRecordOrm]]) -> Message:
with StringIO() as sio:
sio.write("用户[")
sio.write(await get_user_nickname(user, group))
sio.write("]在赛季[")
sio.write(season.name)
sio.write("]的最近走势如下:\n")
sio.write(f"用户[{await get_user_nickname(user, group)}]在赛季[{season.name}]的最近走势如下:\n")

for log, game, record in result:
sio.write(" ")

sio.write(digit_mapping[record.rank])
sio.write("位 ")

sio.write(str(record.score))
sio.write('点 (')
if record.point > 0:
sio.write('+')
elif record.point == 0:
sio.write('±')
sio.write(str(record.point))
sio.write(') ')

sio.write("对局")
sio.write(str(game.code))
sio.write("\n")
sio.write(f" {record.rank}{record.score}点 "
f"({map_point(record.raw_point, record.point_scale)}) "
f"对局{game.code}\n")

return Message(MessageSegment.text(sio.getvalue().strip()))
Loading

0 comments on commit 0812808

Please sign in to comment.