Skip to content

Commit

Permalink
更新至2.0
Browse files Browse the repository at this point in the history
fixes #8
  • Loading branch information
RShock committed Aug 1, 2022
1 parent 5f52951 commit 3d0e8dc
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 59 deletions.
9 changes: 7 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 股海风云V1.96
# 股海风云V2.0

适配真寻的炒股小游戏,使用金币为真寻经济系统的金币,使用的是真实数据

Expand All @@ -7,11 +7,16 @@
注意:现阶段来说,这个插件对于玩过股市的人来说基本是必赚的,还请注意限制杠杆以控制盈利幅度

### 更新内容
v2.0
* 将所有信息全部图片化(除了适配版的`我的持仓`指令,不过我觉得这个指令其实没人在用)
* 修正变更杠杆时剩余资金显示不正确的问题
* 持仓页面添加持仓所属人的信息

v1.99
* 添加`查看股票`指令,功能等同于之前的`买股票 代码 0`
* 股票k线默认改为东方财富通,更加专业,且支持基金显示,如果感觉不习惯的可以在配置内调回来
* 表格版查看持仓添加持仓收益总计功能
* 接下来7天内我都有事,有bug也修不了。所以这个版本不敢直接更新,放在release里了,代码仓库依然为1.96
* 接下来7天内我都有事,有bug也修不了。所以这个版本不敢直接更新,放在名为`v1.99`的压缩包里了,代码仓库依然为1.96

v1.96
* 收益又被我搞坏了,已经修好
Expand Down
137 changes: 90 additions & 47 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

from nonebot import on_command
from nonebot.adapters.onebot.v11 import MessageEvent, GroupMessageEvent, Message, Bot, MessageSegment
from nonebot.params import CommandArg
from nonebot.params import CommandArg, ArgPlainText, Arg
from nonebot.permission import SUPERUSER
from nonebot.typing import T_State
from nonebot.matcher import Matcher

from services.log import logger
from configs.config import Config
from .utils import get_stock_img, send_forward_msg_group, convert_stocks_to_md_table, fill_stock_id
from .utils import get_stock_img, send_forward_msg_group, convert_stocks_to_md_table, fill_stock_id, get_stock_img_v2
from ..nonebot_plugin_htmlrender import text_to_pic, md_to_pic

from .data_source import (
Expand Down Expand Up @@ -55,13 +57,19 @@
Q: 我是超级新手,怎么玩?
A: 可以先输入‘买入躺平基金 x’ x为仓位数 最高10 可不填
Q: 股票代码是从哪里来的?
A: 需要从现实中的股市提取
Q: 为什么允许非整数的手数/是否应该加入汇率
A: 当基金玩的
————————————————
强制清仓+qq号 管理专用指令,爆仓人不愿意清仓就对他使用这个吧
强制清仓+qq号(不是at是qq号) 管理专用指令,爆仓人不愿意清仓就对他使用这个吧
""".strip()
__plugin_des__ = "谁才是股市传奇?"
__plugin_type__ = ("群内小游戏",)
__plugin_cmd__ = ["买股票 代码 金额]", "卖股票 代码 仓位(十分制)", "我的持仓", "强制清仓"]
__plugin_version__ = 1.96
__plugin_version__ = 2.0
__plugin_author__ = "XiaoR"
__plugin_settings__ = {
"level": 5,
Expand All @@ -84,6 +92,11 @@
"value": False,
"help": "如果我的持仓功能报错,且看了issue还是改不好,就把这个改成true",
"default_value": False,
},
"IMAGE_MODE": {
"value": 2,
"help": "1:股票提示图为百度股市通,比较新人 2:股票提示图为分时+日k且支持基金",
"default_value": 2,
}
}

Expand All @@ -94,16 +107,17 @@
look_stock = on_command("查看持仓", aliases={"偷看持仓", "他的持仓"}, priority=5, block=True)
revert_stock = on_command("反转持仓", priority=5, block=True)
help_stock = on_command("关于股海风云", priority=5, block=True)
query_stock = on_command("查看股票", priority=5, block=True)


@buy_stock.handle()
async def _(event: MessageEvent, arg: Message = CommandArg()):
if not isinstance(event, GroupMessageEvent):
await buy_stock.finish("这个游戏只能在群里玩哦")
await buy_stock.finish(await to_pic_msg("这个游戏只能在群里玩哦"))
msg = arg.extract_plain_text().strip().split()
if len(msg) < 1:
await buy_stock.finish(MessageSegment.image(await text_to_pic(
f"格式错误,请输入\n买股票 股票代码 金额 杠杆层数(可选)\n如 买股票 600888 1000 5", width=300)))
await buy_stock.finish(await to_pic_msg(
f"格式错误,请输入\n买股票 股票代码 金额 杠杆层数(可选)\n如 买股票 600888 1000 5", width=300))
if msg[0] == '躺平' or msg[0] == '躺平基金': # 买入躺平基金的特殊逻辑
await buy_lazy_handle(buy_stock, msg, event)
return
Expand All @@ -122,11 +136,11 @@ async def buy_handle(bot, msg, event):
# 第三个参数是杠杆
# 最大杠杆比率
if cost == 0 and len(msg) == 2: # 专门用来看行情,但是加上杠杆参数就是改杠杆了
await bot.send(MessageSegment.image(await text_to_pic(f"你看了看,但没有买", width=300)))
await bot.finish(await get_stock_img(origin_stock_id, stock_id, True))
await bot.send(await to_pic_msg(f"你看了看,但没有买", width=300))
await bot.finish(await get_stock_img_(origin_stock_id, stock_id))
if cost < 0:
if cost < -max_gearing:
await bot.finish(MessageSegment.image(await text_to_pic(f"想做空的话\n请使用负数的杠杆率哦", width=300)))
await bot.finish(await to_pic_msg(f"想做空的话\n请使用负数的杠杆率哦", width=300))
else: # 这个人输入了买股票xxxx -10 (-10应该是杠杆倍率而不是cost)
gearing = cost
cost = 10
Expand All @@ -135,88 +149,87 @@ async def buy_handle(bot, msg, event):
if gearing > max_gearing:
if -max_gearing <= cost <= max_gearing: # 防呆,这人把输入参数顺序搞反了
cost, gearing = gearing, cost
await bot.send(MessageSegment.image(await text_to_pic(
f"你的杠杆和花费金币参数顺序反了,已经帮你修好了", width=300)))
await bot.send(await to_pic_msg(
f"你的杠杆和花费金币参数顺序反了,已经帮你修好了", width=300))
else:
await bot.send(MessageSegment.image(await text_to_pic(
f"最高杠杆只能到{max_gearing}倍,\n已经修正为{max_gearing}倍", width=300)))
await bot.send(await to_pic_msg(
f"最高杠杆只能到{max_gearing}倍,\n已经修正为{max_gearing}倍", width=300))
gearing = max_gearing
if gearing < -max_gearing:
await bot.send(MessageSegment.image(await text_to_pic(
f"最高杠杆只能到-{max_gearing}倍,\n已经修正为-{max_gearing}倍", width=300)))
await bot.send(await to_pic_msg(
f"最高杠杆只能到-{max_gearing}倍,\n已经修正为-{max_gearing}倍", width=300))
gearing = -max_gearing
result = await buy_stock_action(event.user_id, event.group_id, stock_id, gearing, cost)
await bot.send(MessageSegment.image(await text_to_pic(result, width=300)))
await bot.finish(await get_stock_img(origin_stock_id, stock_id))
await bot.send(await to_pic_msg(result, width=300))
await bot.finish(await get_stock_img_(origin_stock_id, stock_id))


@sell_stock.handle()
async def _(
event: MessageEvent,
state: T_State,
arg: Message = CommandArg()):
if not isinstance(event, GroupMessageEvent):
await buy_stock.finish("这个游戏只能在群里玩哦")
await sell_stock.finish(await to_pic_msg("这个游戏只能在群里玩哦"))
msg = arg.extract_plain_text().strip().split()
if len(msg) < 1:
await sell_stock.finish(MessageSegment.image(await text_to_pic(
"格式错误,请输入\n卖股票 股票代码 [仓位(不填默认为十)]\n如 卖股票 601919 10", width=300)))
await sell_stock.finish(await to_pic_msg(
"格式错误,请输入\n卖股票 股票代码 [仓位(不填默认为十)]\n如 卖股票 601919 10", width=300))
stock_id = fill_stock_id(msg[0])
if len(msg) == 1:
percent = 10
else:
percent = round(int(msg[1]), 2)
if percent > 10:
await sell_stock.send("不能卖十成以上的仓位哦,已经帮你全卖了")
await sell_stock.send(await to_pic_msg("不能卖十成以上的仓位哦,已经帮你全卖了"))
percent = 10
if percent <= 0:
await sell_stock.finish("卖的仓位太低了!")
await sell_stock.finish(await to_pic_msg("卖的仓位太低了!"))
if msg[0] == '躺平' or msg[0] == '躺平基金': # 卖出躺平基金的特殊逻辑
await sell_lazy_handle(buy_stock, percent, event)
return
result = await sell_stock_action(event.user_id, event.group_id, stock_id, percent)
await sell_stock.send(MessageSegment.image(await text_to_pic(result, width=300)))
await sell_stock.send(await to_pic_msg(result, width=300))
origin_stock_id = stock_id[2:]
await sell_stock.finish(await get_stock_img(origin_stock_id, stock_id))
await sell_stock.finish(await get_stock_img_(origin_stock_id, stock_id))


@my_stock.handle()
async def _(event: MessageEvent, bot: Bot, state: T_State, arg: Message = CommandArg()):
async def _(event: MessageEvent, bot: Bot):
if not isinstance(event, GroupMessageEvent):
await buy_stock.finish("这个游戏只能在群里玩哦")
await my_stock.finish(await to_pic_msg("这个游戏只能在群里玩哦"))
username = await get_username(bot, event.group_id, event.user_id)

if Config.get_config("stock_legend", "WIN_FIT", False):
my_stocks = await get_stock_list_action_for_win(event.user_id, event.group_id)
await send_forward_msg_group(bot, event, "真寻炒股小助手", my_stocks if my_stocks else ["你还什么都没买呢!"])
else:
my_stocks = await get_stock_list_action(event.user_id, event.group_id)
if not my_stocks:
await sell_stock.finish(MessageSegment.image(await text_to_pic("你还什么都没买呢!", width=300)), at_sender=True)
txt = convert_stocks_to_md_table(my_stocks)
await sell_stock.finish(await to_pic_msg(f"{username}你还什么都没买呢!", width=300))
txt = convert_stocks_to_md_table(username, my_stocks)
logger.info(txt)
await sell_stock.finish(MessageSegment.image(await md_to_pic(f"{txt}", width=1000)), at_sender=True)


@look_stock.handle()
async def _(event: MessageEvent, bot: Bot, args: Message = CommandArg(), arg: Message = CommandArg()):
async def _(event: MessageEvent, bot: Bot, args: Message = CommandArg()):
if not isinstance(event, GroupMessageEvent):
await buy_stock.finish("这个游戏只能在群里玩哦")
await look_stock.finish(await to_pic_msg("这个游戏只能在群里玩哦"))

look_qq = event.user_id
for arg in args:
if arg.type == "at":
look_qq = arg.data.get("qq", "")

if not look_qq:
look_qq = event.user_id

username = await get_username(bot, event.group_id, look_qq)
if Config.get_config("stock_legend", "WIN_FIT", False):
my_stocks = await get_stock_list_action_for_win(event.user_id, event.group_id)
await send_forward_msg_group(bot, event, "真寻炒股小助手", my_stocks if my_stocks else ["仓位是空的"])
else:
my_stocks = await get_stock_list_action(look_qq, event.group_id)
if not my_stocks:
await sell_stock.finish(MessageSegment.image(await text_to_pic("他的仓位是空的", width=300)), at_sender=True)
txt = convert_stocks_to_md_table(my_stocks)
await sell_stock.finish(await to_pic_msg(f"{username}的仓位是空的", width=300))
txt = convert_stocks_to_md_table(username, my_stocks)
logger.info(txt)
await sell_stock.finish(MessageSegment.image(await md_to_pic(f"{txt}", width=1200)), at_sender=True)

Expand All @@ -226,45 +239,75 @@ async def _(event: MessageEvent, bot: Bot, args: Message = CommandArg(), arg: Me
@clear_stock.handle()
async def _(event: MessageEvent, arg: Message = CommandArg()):
if not isinstance(event, GroupMessageEvent):
await buy_stock.finish("这个游戏只能在群里玩哦")
await clear_stock.finish(await to_pic_msg("这个游戏只能在群里玩哦"))

msg = arg.extract_plain_text().strip().split()
if len(msg) < 1:
await buy_stock.finish("格式错误,请输入强制清仓 qq号")
await buy_stock.finish(await to_pic_msg("格式错误,请输入强制清仓 qq号"))
cnt = await force_clear_action(int(msg[0]), event.group_id)
await buy_stock.finish(MessageSegment.image(await text_to_pic(f"{msg[0]}{cnt}仓位都被卖了", width=300)))
await buy_stock.finish(await to_pic_msg(f"{msg[0]}{cnt}仓位都被卖了", width=300))


@revert_stock.handle()
async def _(event: MessageEvent, arg: Message = CommandArg()):
if not isinstance(event, GroupMessageEvent):
await revert_stock.finish("这个游戏只能在群里玩哦")
await revert_stock.finish(await to_pic_msg("这个游戏只能在群里玩哦"))

msg = arg.extract_plain_text().strip().split()
if len(msg) < 1:
await revert_stock.finish("格式错误,请输入反转持仓 股票代码")
await revert_stock.finish(await to_pic_msg("格式错误,请输入反转持仓 股票代码"))
stock_id = fill_stock_id(msg[0])
msg = await revert_stock_action(event.user_id, event.group_id, stock_id)
await revert_stock.finish(msg)
await revert_stock.finish(await to_pic_msg(msg))


async def to_pic_msg(msg, width=300):
return MessageSegment.image(await text_to_pic(msg, width=width))


@help_stock.handle()
async def _():
await help_stock.finish(
"""作者:小r
说明:这个插件可以帮多年后的你省很多钱!练习到每天盈利5%+就可以去玩真正的股市了
版本:v1.96
版本:v2.0
查看是否有更新:https://github.com/RShock/zhenxun_plugin_stock_legend""")


# 躺平基金是给不会炒股的人(以及周六日)玩的基金,每天收益为1.5%(默认)
# 虽然看起来很高但是实际上30天也就1.56倍,可以接受
async def buy_lazy_handle(bot, msg, event) -> str:
async def buy_lazy_handle(bot, msg, event) -> None:
cost = 10 if len(msg) <= 1 else float(msg[1])
await bot.finish(MessageSegment.image(
await text_to_pic(
await buy_lazy_stock_action(event.user_id, event.group_id, cost), width=300)))


async def sell_lazy_handle(bot, percent, event) -> str:
await bot.finish(await sell_lazy_stock_action(event.user_id, event.group_id, percent))
async def sell_lazy_handle(bot, percent, event) -> None:
tmp = await sell_lazy_stock_action(event.user_id, event.group_id, percent)
await bot.finish(await to_pic_msg(tmp, width=400))


@query_stock.handle()
async def _(event: MessageEvent, arg: Message = CommandArg()):
if not isinstance(event, GroupMessageEvent):
await revert_stock.finish(await to_pic_msg("这个游戏只能在群里玩哦"))

msg = arg.extract_plain_text().strip().split()
if len(msg) < 1:
await revert_stock.finish(await to_pic_msg("格式错误,请输入查看股票 股票/基金代码", width=300))
await query_stock.send(await to_pic_msg("正在查询...", width=200))
stock_id = fill_stock_id(msg[0])
await query_stock.finish(await get_stock_img_(msg[0], stock_id))


async def get_stock_img_(origin_stock_id, stock_id):
if Config.get_config("stock_legend", "IMAGE_MODE", 2) == 2:
return await get_stock_img_v2(origin_stock_id, stock_id)
else:
return await get_stock_img(origin_stock_id, stock_id)


async def get_username(bot, group_id, user_id):
user_name = await bot.get_group_member_info(group_id=group_id, user_id=user_id)
return user_name["card"] or user_name["nickname"]
4 changes: 2 additions & 2 deletions data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ async def buy_stock_action(user_id: int, group_id: int, stock_id: str, gearing:
f'当前持仓价值 {round((query.number * price - query.cost) * query.gearing + query.cost, 2)}\n' \
f'当前持仓成本 {round(query.cost, 2)}\n' \
f'杠杆比率 {query.gearing}\n' \
f'剩余资金 {have_gold - cost}'
f'剩余资金 {round(have_gold - origin_cost)}'
else:
return f"成功购买了 {round(num / 100, 2)}{name}\n" \
f"现价 {price}\n" \
f"当前持仓 {round(query.number / 100, 2)}\n" \
f"当前持仓价值 {round((query.number * price - query.cost) * query.gearing + query.cost, 2)}\n" \
f"当前持仓成本 {round(query.cost, 2)}\n" \
f"杠杆比率 {query.gearing}\n" \
f"剩余资金 {have_gold - cost}"
f"剩余资金 {round(have_gold - cost)}"


# 快速清仓指令
Expand Down
Loading

0 comments on commit 3d0e8dc

Please sign in to comment.