后边慢慢优化
[image: 1767879258627-2555386c-a39e-456b-bf6e-695debf3c628-image-resized.png]
# 清华西游记状态栏和状态窗口
import webbrowser, asyncio, ast
from pymud import Session, IConfig, alias, trigger, timer, gmcp, exception, Trigger, SimpleTrigger, SimpleAlias, GMCPTrigger, Command
from wcwidth import wcswidth
from utils.utils import experience_in_M
class MyConfig(IConfig):
def __init__(self, session: Session, *args, **kwargs) -> None:
super().__init__(session, *args, **kwargs)
# 将自定义的状态窗口函数赋值给会话的status_maker属性,这样会话就会使用该函数来显示状态信息。
self.session.application.set_status('当前任务:未知') # 未来可以作为当前任务的状态栏
self.session.status_maker = self.status_window
def __unload__(self):
super().__unload__()
# 创建自定义的健康条用作分隔符
def create_status_bar(self, current, maximum, actual_max, bar_length = 20, bar_char = "—"):
"""
创建一个健康条,用于显示当前健康值、有效健康值和剩余健康值。
:param current: 当前健康值
:param maximum: 当前最大健康值
:param actual_max: 真实最大值
:param bar_length: 健康条的长度
:param bar_char: 健康条的填充字符
:return: 一个包含健康条的列表
"""
# 气血: 1153/ 1702 ( 64%) 内力: 10943 / 5659 (100%) (+360)
bar_line = list()
char_width = wcswidth(bar_char)
# 计算有效健康值
filled_length = int(round(bar_length * current / actual_max / char_width))
# 计算有效健康值部分的长度
current_max_length = int(round(bar_length * maximum / actual_max / char_width))
# 计算剩余部分长度
remaining_length = bar_length - current_max_length
# 构造健康条
bar_line.append(("fg:lightcyan", bar_char * filled_length))
bar_line.append(("fg:yellow", bar_char * (current_max_length - filled_length)))
bar_line.append(("fg:red", bar_char * remaining_length))
return bar_line
# 自定义状态栏窗口。该函数会被渲染框架高频调用,请注意不要在该函数中 info 或者执行其他输出信息!!!
def status_window(self):
styles = {
"title" : "bold",
"value" : "lightgreen",
"value.better" : "lightcyan",
"value.worse" : "yellow",
"value.worst" : "red"
}
try:
formatted_list = list()
# line 0. hp bar
kee = int(self.session.getVariable("kee", 0))
max_kee = int(self.session.getVariable("max_kee", 1))
kee_percent = int(self.session.getVariable("kee_percent", 1))
actual_max_kee = int(round(max_kee / (kee_percent / 100)))
sen = int(self.session.getVariable("sen", 0))
max_sen = int(self.session.getVariable("max_sen", 1))
sen_percent = int(self.session.getVariable("sen_percent", 1))
actual_max_sen = int(round(max_sen / (sen_percent / 100)))
bar_char = "━"
screenwidth = self.session.application.get_width()
bar_length = screenwidth // 2 - 1
span = screenwidth - 2 * bar_length
kee_bar = self.create_status_bar(kee, max_kee, actual_max_kee, bar_length, bar_char)
sen_bar = self.create_status_bar(sen, max_sen, actual_max_sen, bar_length, bar_char)
formatted_list.extend(kee_bar)
formatted_list.append(("", " " * span))
formatted_list.extend(sen_bar)
formatted_list.append(("", "\n"))
# line 1. char, family, master, deposit, food, water, exp, pot
formatted_list.append((styles["title"], "【角色】"))
formatted_list.append((styles["value"], "{0}({1})".format(self.session.getVariable('name'), self.session.getVariable('id'))))
formatted_list.append(("", " "))
formatted_list.append((styles["title"], "【门派】"))
formatted_list.append((styles["value"], "{}".format(self.session.getVariable('family'))))
formatted_list.append(("", " "))
formatted_list.append((styles["title"], "【师父】"))
formatted_list.append((styles["value"], "{}".format(self.session.getVariable('master'))))
formatted_list.append(("", " "))
formatted_list.append((styles["title"], "【武学】"))
formatted_list.append((styles["value"], "{}".format(self.session.getVariable('combat_exp'))))
formatted_list.append((styles["value"], "({})".format(experience_in_M(self.session.getVariable('combat_exp')))))
formatted_list.append(("", " "))
formatted_list.append((styles["title"], "【道行】"))
formatted_list.append((styles["value"], "{}".format(self.session.getVariable('daoxing_exp'))))
formatted_list.append((styles["value"], "({})".format(experience_in_M(self.session.getVariable('daoxing_exp')))))
formatted_list.append(("", " "))
formatted_list.append((styles["title"], "【潜能】"))
formatted_list.append((styles["value"], "{}".format(self.session.getVariable('potential'))))
# line 2. hp
# a new-line
formatted_list.append(("", "\n"))
formatted_list.append((styles["title"], "【气血】"))
if max_kee < actual_max_sen:
style = styles["value.worst"]
elif kee < 0.8 * max_kee:
style = styles["value.worse"]
else:
style = styles["value"]
if max_kee == 0:
pct1 = pct2 = 0
else:
pct1 = 100.0 * kee / actual_max_kee
pct2 = 100.0 * max_kee / actual_max_kee
formatted_list.append((style, "{0}[{1:3.0f}%] / {2}[{3:3.0f}%]".format(kee, pct1, max_kee, pct2)))
formatted_list.append(("", " "))
formatted_list.append((styles["title"], "【精神】"))
if max_sen < actual_max_sen:
style = styles["value.worst"]
elif sen < 0.8 * max_sen:
style = styles["value.worse"]
else:
style = styles["value"]
if max_sen == 0:
pct1 = pct2 = 0
else:
pct1 = 100.0 * sen / actual_max_sen
pct2 = 100.0 * max_sen / actual_max_sen
formatted_list.append((style, "{0}[{1:3.0f}%] / {2}[{3:3.0f}%]".format(sen, pct1, max_sen, pct2)))
formatted_list.append(("", " "))
formatted_list.append((styles["title"], "【食物】"))
food = int(self.session.getVariable('food', '0'))
max_food = int(self.session.getVariable('max_food', 350))
if food < 100:
style = styles["value.worst"]
elif food < 200:
style = styles["value.worse"]
elif food < max_food:
style = styles["value"]
else:
style = styles["value.better"]
formatted_list.append((style, "{0}/{1}".format(food, max_food)))
formatted_list.append(("", " "))
formatted_list.append((styles["title"], "【饮水】"))
water = int(self.session.getVariable('water', '0'))
max_water = int(self.session.getVariable('max_water', 350))
if water < 100:
style = styles["value.worst"]
elif water < 200:
style = styles["value.worse"]
elif water < max_water:
style = styles["value"]
else:
style = styles["value.better"]
formatted_list.append((style, "{0}/{1}".format(water, max_water)))
formatted_list.append(("", " "))
formatted_list.append((styles["title"], "【存款】"))
formatted_list.append((styles["value"], "{}金".format(self.session.getVariable('deposit'))))
formatted_list.append(("", " "))
# line 3. hp
# a new-line
formatted_list.append(("", "\n"))
force = int(self.session.getVariable("force", 0))
max_force = int(self.session.getVariable("max_force", 1))
mana = int(self.session.getVariable("mana", 0))
max_mana = int(self.session.getVariable("max_mana", 1))
# 内力
formatted_list.append((styles["title"], "【内力】"))
if force < 0.6 * max_force:
style = styles["value.worst"]
elif force < 0.8 * max_force:
style = styles["value.worse"]
elif force < 1.2 * max_force:
style = styles["value"]
else:
style = styles["value.better"]
if max_force == 0:
pct = 0
else:
pct = 100.0 * force / max_force
formatted_list.append((style, "{0} / {1}[{2:3.0f}%]".format(force, max_force, pct)))
formatted_list.append(("", " "))
# 精力
formatted_list.append((styles["title"], "【法力】"))
if mana < 0.6 * max_mana:
style = styles["value.worst"]
elif mana < 0.8 * max_mana:
style = styles["value.worse"]
elif mana < 1.2 * max_mana:
style = styles["value"]
else:
style = styles["value.better"]
if max_mana == 0:
pct = 0
else:
pct = 100.0 * mana / max_mana
formatted_list.append((style, "{0} / {1}[{2:3.0f}%]".format(mana, max_mana, pct)))
formatted_list.append(("", " "))
return formatted_list
except Exception as e:
return f"{e}"