更新 gupiao/scripts/gupiao.py
This commit is contained in:
parent
22336380ad
commit
ea6fc42717
@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
# pyright: reportExplicitAny=false, reportAny=false, reportUnknownMemberType=false, reportUnknownArgumentType=false, reportUnknownVariableType=false, reportImplicitStringConcatenation=false
|
||||||
"""股票游戏插件 - 10只真实股票交易,与签到积分联动"""
|
"""股票游戏插件 - 10只真实股票交易,与签到积分联动"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@ -12,8 +13,6 @@ import urllib.request
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
# 注意:不重定向 stderr,让框架单独处理错误输出
|
|
||||||
|
|
||||||
# ---------- 路径配置 ----------
|
# ---------- 路径配置 ----------
|
||||||
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
DATA_DIR = os.path.join(SCRIPT_DIR, "..", "data")
|
DATA_DIR = os.path.join(SCRIPT_DIR, "..", "data")
|
||||||
@ -231,7 +230,7 @@ def stock_display_name(sd: dict[str, Any]) -> str:
|
|||||||
return f"{sd['symbol']}股票"
|
return f"{sd['symbol']}股票"
|
||||||
|
|
||||||
|
|
||||||
def market(user_id: str, nickname: str) -> str:
|
def market(_user_id: str, _nickname: str) -> str:
|
||||||
"""查看行情。"""
|
"""查看行情。"""
|
||||||
prices = fetch_all_prices()
|
prices = fetch_all_prices()
|
||||||
lines = ["📈 股票行情", "━" * 22]
|
lines = ["📈 股票行情", "━" * 22]
|
||||||
@ -424,8 +423,8 @@ def portfolio(user_id: str, nickname: str) -> str:
|
|||||||
|
|
||||||
emoji = "📈" if profit >= 0 else "📉"
|
emoji = "📈" if profit >= 0 else "📉"
|
||||||
lines.append(
|
lines.append(
|
||||||
f"{emoji} 【{stock_display_name(sd)}】{shares}股\n"
|
f"{emoji} 【{stock_display_name(sd)}】{shares}股\n" +
|
||||||
f" 均价:{holding['avg_cost']:.2f} | 现价:{price:.2f}\n"
|
f" 均价:{holding['avg_cost']:.2f} | 现价:{price:.2f}\n" +
|
||||||
f" 市值:{market_value:.2f} | {emoji} {profit:+.2f}({profit_pct:+.1f}%)"
|
f" 市值:{market_value:.2f} | {emoji} {profit:+.2f}({profit_pct:+.1f}%)"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -438,7 +437,7 @@ def portfolio(user_id: str, nickname: str) -> str:
|
|||||||
return "\n".join(lines)
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
def rank(user_id: str, nickname: str, top_n: int = 10) -> str:
|
def rank(_user_id: str, _nickname: str, top_n: int = 10) -> str:
|
||||||
"""股市排行榜。"""
|
"""股市排行榜。"""
|
||||||
data = load_game_data()
|
data = load_game_data()
|
||||||
prices = fetch_all_prices()
|
prices = fetch_all_prices()
|
||||||
@ -474,7 +473,7 @@ def rank(user_id: str, nickname: str, top_n: int = 10) -> str:
|
|||||||
medal = {1: "🥇", 2: "🥈", 3: "🥉"}.get(rank_i, f"#{rank_i}")
|
medal = {1: "🥇", 2: "🥈", 3: "🥉"}.get(rank_i, f"#{rank_i}")
|
||||||
profit_str = f"+{profit}" if profit >= 0 else f"{profit}"
|
profit_str = f"+{profit}" if profit >= 0 else f"{profit}"
|
||||||
lines.append(
|
lines.append(
|
||||||
f"{medal} {name}\n"
|
f"{medal} {name}\n" +
|
||||||
f" 市值:{mv:.1f} | 盈亏:{profit_str} ({pct:.1f}%)"
|
f" 市值:{mv:.1f} | 盈亏:{profit_str} ({pct:.1f}%)"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -497,7 +496,7 @@ def history(user_id: str, nickname: str, limit: int = 10) -> str:
|
|||||||
for txn in reversed(txns):
|
for txn in reversed(txns):
|
||||||
emoji = "🟢" if txn["type"] == "BUY" else "🔴"
|
emoji = "🟢" if txn["type"] == "BUY" else "🔴"
|
||||||
lines.append(
|
lines.append(
|
||||||
f"{emoji} [{txn['time']}] {txn['type']} {txn['symbol']}\n"
|
f"{emoji} [{txn['time']}] {txn['type']} {txn['symbol']}\n" +
|
||||||
f" 数量 {txn['shares']} 股 × {txn['price']:.2f} | 手续费 {txn['fee']:.2f}"
|
f" 数量 {txn['shares']} 股 × {txn['price']:.2f} | 手续费 {txn['fee']:.2f}"
|
||||||
)
|
)
|
||||||
return "\n".join(lines)
|
return "\n".join(lines)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user