pepperbot/apps/riic_report_analysis.py

270 lines
11 KiB
Python
Raw Permalink Normal View History

2023-08-14 10:40:47 +08:00
from pepperbot.core.message.chain import MessageChain, Image, Text, At
2023-08-16 12:20:51 +08:00
from pepperbot.extensions.command import as_command, CLIArgument
from pepperbot.extensions.command.sender import CommandSender
2023-09-07 10:21:15 +08:00
from pepperbot.adapters.universal.api import UniversalGroupBot
2023-08-14 09:24:03 +08:00
from rapidocr_onnxruntime import RapidOCR
2023-08-13 19:42:17 +08:00
import os
2023-08-13 20:18:55 +08:00
import asyncio
import functools
2023-08-14 09:24:03 +08:00
import re
2023-08-16 15:09:08 +08:00
from typing import Dict, Optional, Any
2023-09-03 00:25:04 +08:00
import math
2023-08-13 18:31:28 +08:00
2023-08-14 09:24:03 +08:00
rapid_ocr = None
if not rapid_ocr:
rapid_ocr = RapidOCR("rapid_ocr.yml")
2023-08-13 20:05:54 +08:00
2023-08-13 18:31:28 +08:00
class RIICReportAnalysis:
2023-09-07 10:21:15 +08:00
async def group_message(
self,
chain: MessageChain,
raw_event: Dict,
bot: UniversalGroupBot,
):
2023-08-13 19:50:02 +08:00
if len(chain) != 1 or len(chain.images) != 1:
2023-08-13 19:42:17 +08:00
return
img_seg: Image = chain[0]
img_path = await img_seg.download()
2023-08-13 20:18:55 +08:00
loop = asyncio.get_running_loop()
2023-08-14 09:24:03 +08:00
result, elapse = await loop.run_in_executor(
None, functools.partial(rapid_ocr, img_path)
2023-08-13 20:18:55 +08:00
)
os.remove(img_path)
if not result or not "副手简报" in [i[1] for i in result]:
return
card = raw_event["sender"]["card"]
2024-01-01 08:30:00 +08:00
err_msg = "请修改群昵称(群名片),标注基建布局。若使用2电站布局,请额外标注贸易站等级。如:\n"
err_msg += "EE0000 252 21贸\n"
err_msg += "教捐 252(33贸易站)\n"
err_msg += "千梵 243烟火 单推狮蝎\n"
err_msg += "琉璃 243-153"
2023-08-14 12:04:06 +08:00
use_tequila = False
2023-08-14 13:19:06 +08:00
power_plants = 2
2023-08-14 12:04:06 +08:00
if not card:
await chain.onebot_reply(Text(err_msg))
return
elif "153" in card or "243" in card:
2023-08-14 12:04:06 +08:00
use_tequila = True
2023-08-14 13:19:06 +08:00
power_plants = 3
elif "252" in card:
if m := re.search(r"([1-3][1-3])贸", card):
2023-08-14 12:04:06 +08:00
if "3" in m.group(1):
if m.group(1) != "33":
await chain.onebot_reply(Text("暂不支持此类基建布局"))
return
else:
use_tequila = True
else:
await chain.onebot_reply(Text(err_msg))
return
elif "342" in card:
if m := re.search(r"([1-3][1-3][1-3])贸", card):
2023-08-14 12:04:06 +08:00
if "3" in m.group(1):
if m.group(1) != "333":
await chain.onebot_reply(Text("暂不支持此类基建布局"))
return
else:
use_tequila = True
else:
await chain.onebot_reply(Text(err_msg))
return
else:
await chain.onebot_reply(Text(err_msg))
2023-08-14 09:24:03 +08:00
return
2023-08-14 09:24:03 +08:00
lmb_height = 0
lmb_list = []
2023-08-14 09:24:03 +08:00
date_list = []
exp_list = []
gold_list = []
for i in result:
if i[1] == "龙门币":
lmb_height = i[0][0][1]
break
for i in result:
if m := re.search(r"([0-9]+\.[0-9]+)", i[1]):
2023-08-14 10:40:47 +08:00
if float(i[2]) < 0.6:
continue
2023-08-14 09:24:03 +08:00
date_list.append([int(i[0][0][0]), m.group(1)])
continue
if m := re.search(r"EXP([0-9]+)的作战记录", i[1]):
exp_list.append([int(i[0][0][0]), m.group(1)])
continue
if m := re.search(r"([0-9]+)的贵金属", i[1]):
gold_list.append([int(i[0][0][0]), m.group(1)])
continue
if lmb_height - 20 < i[0][0][1] < lmb_height + 20:
if m := re.search(r"([0-9]+)", i[1]):
lmb_list.append([int(i[0][0][0]), m.group(1)])
2023-08-14 09:24:03 +08:00
lmb_list.sort(key=lambda x: x[0])
2023-08-14 09:24:03 +08:00
gold_list.sort(key=lambda x: x[0])
exp_list.sort(key=lambda x: x[0])
2023-09-07 10:21:15 +08:00
output_list = []
2023-08-14 09:24:03 +08:00
output = ""
2023-08-14 12:04:06 +08:00
lmb_avg = 0
order_avg = 0
gold_avg = 0
exp_avg = 0
external_gold = 5000
2023-08-14 13:25:57 +08:00
external_lmb = 43483
external_exp = 36284
2023-08-14 09:24:03 +08:00
2023-08-14 10:40:47 +08:00
try:
2023-08-14 12:35:06 +08:00
exp_list += [["manual", "0"]] * (len(date_list) - len(exp_list))
2023-08-14 10:40:47 +08:00
for i in range(len(date_list)):
2023-08-14 12:04:06 +08:00
lmb_avg += int(lmb_list[2 * i][1])
order_avg += int(lmb_list[2 * i + 1][1])
gold_avg += int(gold_list[i][1])
exp_avg += int(exp_list[i][1])
2023-09-03 00:25:04 +08:00
output += f"{date_list[i][1]}\n"
output += f"💵 {lmb_list[2 * i][1]}订单({lmb_list[2 * i + 1][1]})\n"
2024-01-01 08:30:00 +08:00
output += f"🪙 {gold_list[i][1]}赤金\n"
2023-09-03 00:25:04 +08:00
output += f"📼 {exp_list[i][1]}经验\n\n"
2023-08-14 09:24:03 +08:00
2023-09-07 10:21:15 +08:00
output_list.append(output.strip())
output = ""
2023-08-14 12:04:06 +08:00
lmb_avg /= len(date_list)
order_avg /= len(date_list)
gold_avg /= len(date_list)
exp_avg /= len(date_list)
if use_tequila:
trading_efficiency = lmb_avg / 1.62 / 500 * 70.138 / 24 / 60
gold_by_e = trading_efficiency * 0.07 * 24 * 60 / 72 * 500
balanced_gold_by_e = lmb_avg - external_gold - gold_by_e
overflow_gold_by_e = gold_avg - balanced_gold_by_e
more_exp_by_e = overflow_gold_by_e * 0.8
balanced_exp_by_e = exp_avg + more_exp_by_e
2023-08-14 13:25:57 +08:00
lmb_exp_ratio_by_e = (lmb_avg + external_lmb) / (
balanced_exp_by_e + external_exp
)
2023-08-14 12:04:06 +08:00
total_by_e = lmb_avg + balanced_exp_by_e
gold_by_order = order_avg * 0.2 * 500
balanced_gold_by_order = lmb_avg - external_gold - gold_by_order
overflow_gold_by_order = gold_avg - balanced_gold_by_order
more_exp_by_order = overflow_gold_by_order * 0.8
balanced_exp_by_order = exp_avg + more_exp_by_order
2023-08-14 13:25:57 +08:00
lmb_exp_ratio_by_order = (lmb_avg + external_lmb) / (
balanced_exp_by_order + external_exp
2023-08-14 12:04:06 +08:00
)
total_by_order = lmb_avg + balanced_exp_by_order
output += "====计算结果====\n\n"
2023-08-14 16:27:47 +08:00
output += f"订单平均:{lmb_avg:.0f}\n"
if power_plants == 3:
output += f"赤金平均:{gold_avg:.0f}\n"
output += f"经验平均:{exp_avg:.0f}\n"
if exp_avg != 0:
output += f"产出钱书比例:{lmb_avg / exp_avg:.2f}\n"
else:
output += f"产出钱书比例:正无穷\n"
output += f"综合钱书比例:{(lmb_avg + external_lmb) / (exp_avg + external_exp):.2f}\n"
output += f"钱书总和:{lmb_avg + exp_avg:.0f}\n"
output += "\n-----效率算法-----\n"
2023-08-16 15:09:08 +08:00
output += f"贸易效率:{trading_efficiency:.0%}\n"
2023-08-14 12:04:06 +08:00
output += f"等效赤金:{gold_by_e:.0f}\n"
output += f"经验调平:{balanced_exp_by_e:.0f}\n"
2023-08-14 13:19:06 +08:00
if power_plants == 2:
output += f"钱书比例:{lmb_exp_ratio_by_e:.2f}\n"
2023-08-14 12:04:06 +08:00
output += f"调平总和:{total_by_e:.0f}\n\n"
output += "-----数量算法-----\n"
output += f"等效赤金:{gold_by_order:.0f}\n"
output += f"经验调平:{balanced_exp_by_order:.0f}\n"
2023-08-14 13:19:06 +08:00
if power_plants == 2:
output += f"钱书比例:{lmb_exp_ratio_by_order:.2f}\n"
output += f"调平总和:{total_by_order:.0f}\n\n"
2023-08-14 12:04:06 +08:00
else:
balanced_gold = lmb_avg - external_gold
overflow_gold = gold_avg - balanced_gold
more_exp = overflow_gold * 0.8
balanced_exp = exp_avg + more_exp
2023-09-03 00:25:04 +08:00
lmb_exp_ratio = (lmb_avg + external_lmb) / (balanced_exp + external_exp)
total = (lmb_avg * 0.2 + gold_avg * 0.8 + exp_avg + 4000) / 10000
2023-09-03 08:24:46 +08:00
gongsun = (lmb_avg * 0.8 + gold_avg * 0.8 + exp_avg + 4000) / 10000
2023-09-03 00:25:04 +08:00
kaltsit = math.ceil((lmb_avg + gold_avg + exp_avg) / 10000)
stupid = (lmb_avg + exp_avg) / 10000
2023-08-14 12:04:06 +08:00
output += "====计算结果====\n"
output += f"订单平均:{lmb_avg:.0f}\n"
2023-09-03 00:25:04 +08:00
output += f"赤金平均:{gold_avg:.0f}\n"
output += f"经验平均:{exp_avg:.0f}\n"
2023-08-14 12:04:06 +08:00
output += f"经验调平:{balanced_exp:.0f}\n"
2023-09-03 00:25:04 +08:00
output += f"钱书比例:{lmb_exp_ratio:.2f}\n\n"
output += "======产能======\n"
2023-09-03 08:35:08 +08:00
output += f"{total:.2f}E = 0.2贸 + 0.8金 + 书 + 4000\n"
output += f"{gongsun:.2f}公 = 0.8贸 + 0.8金 + 书 + 4000\n"
output += f"{kaltsit:.0f}凯 = 贸 + 金 + 书向上取整到万\n"
output += f"{stupid:.1f}友 = 贸 + 书"
2023-08-14 12:04:06 +08:00
2023-09-07 10:21:15 +08:00
output_list.append(output.strip())
await bot.arbitrary.onebot.send_group_forward_msg(
raw_event["group_id"],
[
{
"type": "node",
"data": {
"name": card,
"uin": raw_event["user_id"],
"content": [{"type": "text", "data": {"text": i}}],
},
}
for i in output_list
],
)
2023-09-30 18:41:02 +08:00
return
2023-09-03 00:25:04 +08:00
except Exception as e:
2023-08-14 10:40:47 +08:00
err_msg = "出错啦!\n"
err_msg += f"date_list = {date_list}\n"
err_msg += f"lmb_list = {lmb_list}\n"
err_msg += f"gold_list = {gold_list}\n"
2023-09-03 00:25:04 +08:00
err_msg += f"exp_list = {exp_list}\n"
err_msg += str(e)
2023-09-30 18:41:02 +08:00
if str(e) != "timed out":
await chain.onebot_reply(Text(err_msg), At("1040110848"))
2023-08-16 12:20:51 +08:00
@as_command(
need_prefix=False,
aliases=["裁缝计算"],
include_class_name=False,
require_at=False,
)
class TailoringCommand:
2023-08-16 15:09:08 +08:00
async def initial(self, sender: CommandSender, arg: Optional[Any] = CLIArgument()):
if not arg or not isinstance(arg, str):
2023-08-16 12:20:51 +08:00
await sender.reply(Text("请输入效率!"))
return
if arg.endswith("%"):
arg = arg[:-1]
try:
efficiency = float(arg)
except:
await sender.reply(Text("输入无效!"))
return
if efficiency > 30:
efficiency /= 100
total_efficiency = efficiency + 1.1
2023-08-16 15:09:08 +08:00
equiv_efficiency = total_efficiency * 1.2410210128
gold_efficiency = total_efficiency * (1.2410210128 - 1.0141677) * 1.025
lmb = equiv_efficiency * 24 * 60 / 70.137931 * 500
2023-08-16 12:20:51 +08:00
gold = gold_efficiency * 24 * 60 / 72 * 500
2023-08-16 15:09:08 +08:00
output = f"整站效率:{total_efficiency:.0%}\n"
output += f"等效贸易:{equiv_efficiency - 1:.0%}\n"
output += f"等效制造:{gold_efficiency:.0%}\n"
2023-08-16 12:20:51 +08:00
output += f"订单收益:{lmb:.0f}\n"
output += f"赤金收益:{gold:.0f}"
await sender.reply(Text(output))