feat: support add more skills

This commit is contained in:
xtaodada 2023-10-25 15:03:53 +08:00
parent 5af2c49e22
commit a407971f91
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
3 changed files with 204 additions and 84 deletions

View File

@ -3,6 +3,7 @@ from typing import Dict, Any
from .character import character from .character import character
from .models import CharacterDamage, CharacterSkill, CharacterConfig, WeaponConfig from .models import CharacterDamage, CharacterSkill, CharacterConfig, WeaponConfig
from .skill_data import SkillData
class Data: class Data:
@ -88,35 +89,6 @@ class Data:
return return
del self.file_data[character_name] del self.file_data[character_name]
def get_skill_value(self, character_name: str, skill: CharacterSkill) -> bool:
if not self.file_data.get(character_name):
return False
skill_id = skill.index
for i in self.file_data[character_name].skills:
if i.index == skill_id:
return True
return False
def set_skill_value(self, character_name: str, skill: CharacterSkill, enable: bool):
self.first_init(character_name)
in_data = None
for i in self.file_data[character_name].skills:
if i.index == skill.index:
in_data = i
break
if enable:
if not in_data:
self.file_data[character_name].skills.append(skill.to_data())
else:
in_data.name = skill.custom_name or skill.show_name
in_data.damage_key = skill.damage_key
in_data.transformative_damage_key = skill.transformative_damage_key
else:
if in_data:
self.file_data[character_name].skills.remove(in_data)
self.file_data[character_name].skills.sort(key=lambda x: x.index)
self.last_close(character_name)
def get_character_config_value( def get_character_config_value(
self, character_name: str, config_: CharacterConfig self, character_name: str, config_: CharacterConfig
) -> Any: ) -> Any:
@ -245,3 +217,4 @@ class Data:
data = Data() data = Data()
skill_data = SkillData(data.file_data)

View File

@ -1,23 +1,23 @@
from typing import List from typing import List
from flet_core import MainAxisAlignment
import flet as ft import flet as ft
from flet_core import MainAxisAlignment
from src.data import Page from src.data import Page
from .artifact import artifact from .artifact import artifact
from .character import character from .character import character
from .data import data from .data import data, skill_data
from .models import ( from .models import (
CharacterSkill,
CharacterDamageSkillDamageKey, CharacterDamageSkillDamageKey,
CharacterDamageSkillTransformativeDamageKey, CharacterDamageSkillTransformativeDamageKey,
CharacterConfig, CharacterConfig,
Weapon as WeaponModel, Weapon as WeaponModel,
Element4OP, Element4OP,
Element8OP, Element8OP,
CharacterDamageSkill,
) )
from .weapon import weapon from .weapon import weapon
from ..components import show_snack_bar
def edit_damage_view(page: "Page"): def edit_damage_view(page: "Page"):
@ -62,37 +62,107 @@ def edit_damage_view(page: "Page"):
ch_name = character.current_name ch_name = character.current_name
skill_list.controls.clear() skill_list.controls.clear()
def search_skills(e: ft.ControlEvent = None): def add_new_skill(_: ft.ControlEvent = None):
temp = skill_list.controls[0] skill_data.create_first(ch_name)
skill_list.controls.clear()
skill_list.controls.append(temp) def skill_key_change_to_transformative(e_: ft.ControlEvent = None):
for con in skill_list_data: value = e_.data == "true"
con: "ft.Container" if value:
cb: "ft.Checkbox" = con.content # noqa normal_skill_ft.visible = False
if e is None or e.data in cb.label: transformative_skill_ft.visible = True
skill_list.controls.append(con) else:
normal_skill_ft.visible = True
transformative_skill_ft.visible = False
page.update()
def create_confirm(_):
result = skill_data.create_confirm()
bs.open = False
choose_character()
if not result:
show_snack_bar(page, "添加失败,请确认数据已填写完毕", ft.colors.RED)
normal_skill_ft = ft.Dropdown(
label="输出数据",
options=[
ft.dropdown.Option(key=v, text=k)
for k, v in CharacterDamageSkillDamageKey.normal.data_map.items()
],
value="",
on_change=skill_data.create_handle_damage_key,
)
transformative_skill_ft = ft.Dropdown(
label="输出数据",
options=[
ft.dropdown.Option(key=v, text=k)
for k, v in CharacterDamageSkillTransformativeDamageKey.swirl_cryo.data_map.items()
],
value="",
on_change=skill_data.create_handle_transformative_damage_key,
visible=False,
)
bs = ft.BottomSheet(
ft.Container(
ft.Column(
[
ft.Dropdown(
label="技能名称",
options=skill_data.get_dropdown_options(ch_name),
value="",
on_change=skill_data.create_handle_index,
),
ft.TextField(
label="自定义显示名称",
value="",
on_change=skill_data.create_handle_name,
),
ft.Switch(
label="剧变反应伤害",
value=False,
on_change=skill_key_change_to_transformative,
),
normal_skill_ft,
transformative_skill_ft,
ft.ElevatedButton(
"确定添加",
icon=ft.icons.DONE,
on_click=create_confirm,
),
],
tight=True,
),
padding=10,
),
open=True,
)
page.overlay.append(bs)
page.update() page.update()
bs.update()
skill_list.controls.append( skill_list.controls.append(
ft.Container( ft.Container(
content=ft.TextField( content=ft.Row(
label="技能配置", [
on_change=search_skills, ft.Text("技能配置"),
ft.IconButton(
icon=ft.icons.ADD_BOX,
on_click=add_new_skill,
),
]
), ),
) )
) )
for i in character.skills_map[ch_name]: for i in skill_data.get(ch_name):
container = ft.Container( container = ft.Container(
content=ft.Checkbox( content=ft.Checkbox(
label=i.show_name, label=i.name,
value=data.get_skill_value(ch_name, i), value=True,
disabled=True, disabled=True,
data=i, data=i,
), ),
on_click=choose_skill, on_click=choose_skill,
) )
skill_list.controls.append(container) skill_list.controls.append(container)
skill_list_data = skill_list.controls[1:]
def update_weapon_component(): def update_weapon_component():
ch_name = character.current_name ch_name = character.current_name
@ -273,44 +343,27 @@ def edit_damage_view(page: "Page"):
def choose_skill(e: ft.ControlEvent = None): def choose_skill(e: ft.ControlEvent = None):
checkbox: ft.Checkbox = e.control.content checkbox: ft.Checkbox = e.control.content
skill: CharacterSkill = checkbox.data skill: CharacterDamageSkill = checkbox.data
skill_data.temp = skill
skill_data.init_temp_index(character.current_name)
page.overlay.clear() page.overlay.clear()
def skill_status_change(e_: ft.ControlEvent = None):
value = e_.data == "true"
data.set_skill_value(character.current_name, skill, value)
def skill_custom_name_change(e_: ft.ControlEvent = None):
skill.custom_name = e_.data
data.set_skill_value(character.current_name, skill, True)
def skill_key_change(e_: ft.ControlEvent = None):
skill.damage_key = CharacterDamageSkillDamageKey(e_.data)
data.set_skill_value(character.current_name, skill, True)
def skill_transformative_key_change(e_: ft.ControlEvent = None):
skill.transformative_damage_key = (
CharacterDamageSkillTransformativeDamageKey(e_.data)
)
data.set_skill_value(character.current_name, skill, True)
def skill_key_change_to_transformative(e_: ft.ControlEvent = None): def skill_key_change_to_transformative(e_: ft.ControlEvent = None):
value = e_.data == "true" value = e_.data == "true"
if value: if value:
skill.transformative_damage_key = (
CharacterDamageSkillTransformativeDamageKey.swirl_cryo
)
skill.damage_key = None
normal_skill_ft.visible = False normal_skill_ft.visible = False
transformative_skill_ft.visible = True transformative_skill_ft.visible = True
else: else:
skill.transformative_damage_key = None
skill.damage_key = CharacterDamageSkillDamageKey.normal
normal_skill_ft.visible = True normal_skill_ft.visible = True
transformative_skill_ft.visible = False transformative_skill_ft.visible = False
data.set_skill_value(character.current_name, skill, True)
page.update() page.update()
def delete(_):
skill_data.delete_skill()
data.last_close(character.current_name)
bs.open = False
choose_character()
normal_skill_ft = ft.Dropdown( normal_skill_ft = ft.Dropdown(
label="输出数据", label="输出数据",
options=[ options=[
@ -318,7 +371,7 @@ def edit_damage_view(page: "Page"):
for k, v in CharacterDamageSkillDamageKey.normal.data_map.items() for k, v in CharacterDamageSkillDamageKey.normal.data_map.items()
], ],
value=skill.damage_key.value if skill.damage_key else "", value=skill.damage_key.value if skill.damage_key else "",
on_change=skill_key_change, on_change=skill_data.create_handle_damage_key,
visible=not bool(skill.transformative_damage_key), visible=not bool(skill.transformative_damage_key),
) )
transformative_skill_ft = ft.Dropdown( transformative_skill_ft = ft.Dropdown(
@ -330,7 +383,7 @@ def edit_damage_view(page: "Page"):
value=skill.transformative_damage_key.value value=skill.transformative_damage_key.value
if skill.transformative_damage_key if skill.transformative_damage_key
else "", else "",
on_change=skill_transformative_key_change, on_change=skill_data.create_handle_transformative_damage_key,
visible=bool(skill.transformative_damage_key), visible=bool(skill.transformative_damage_key),
) )
@ -338,16 +391,15 @@ def edit_damage_view(page: "Page"):
ft.Container( ft.Container(
ft.Column( ft.Column(
[ [
ft.Text(skill.show_name), ft.Text(
ft.Switch( skill_data.get_skill_show_name(
label="显示此数值", character.current_name, skill
value=checkbox.value, )
on_change=skill_status_change,
), ),
ft.TextField( ft.TextField(
label="自定义显示名称", label="自定义显示名称",
value=skill.custom_name or skill.show_name, value=skill.name,
on_change=skill_custom_name_change, on_change=skill_data.create_handle_name,
), ),
ft.Switch( ft.Switch(
label="剧变反应伤害", label="剧变反应伤害",
@ -356,6 +408,11 @@ def edit_damage_view(page: "Page"):
), ),
normal_skill_ft, normal_skill_ft,
transformative_skill_ft, transformative_skill_ft,
ft.ElevatedButton(
"删除",
icon=ft.icons.DELETE_SWEEP,
on_click=delete,
),
], ],
tight=True, tight=True,
), ),

90
src/damage/skill_data.py Normal file
View File

@ -0,0 +1,90 @@
import flet as ft
from typing import Dict, List, Optional
from src.damage.character import character
from src.damage.models import (
CharacterDamage,
CharacterDamageSkill,
CharacterSkill,
CharacterDamageSkillDamageKey,
CharacterDamageSkillTransformativeDamageKey,
)
class SkillData:
def __init__(self, file_data: Dict[str, CharacterDamage]):
self.file_data = file_data
self.temp: Optional[CharacterDamageSkill] = None
self.temp_index: Dict[int, CharacterSkill] = {}
def get(self, character_name: str) -> List[CharacterDamageSkill]:
if character_name not in self.file_data:
return []
return self.file_data[character_name].skills
def delete_skill(self):
if self.temp and self.temp in self.file_data[character.current_name].skills:
self.file_data[character.current_name].skills.remove(self.temp)
self.temp = None
self.temp_index.clear()
@staticmethod
def get_skill_show_name(character_name: str, skill: CharacterDamageSkill) -> str:
for i in character.skills_map[character_name]:
if i.index == skill.index:
return i.show_name
return skill.name
@staticmethod
def get_dropdown_options(character_name: str) -> List[ft.dropdown.Option]:
skills = []
for skill in character.skills_map[character_name]:
skills.append(
ft.dropdown.Option(
key=skill.index,
text=skill.show_name,
)
)
return skills
def init_temp_index(self, character_name: str):
self.temp_index.clear()
for skill in character.skills_map[character_name]:
self.temp_index[skill.index] = skill
def create_first(self, character_name: str):
self.temp = CharacterDamageSkill(name="", index=-1)
self.init_temp_index(character_name)
def create_handle_index(self, e: ft.ControlEvent = None):
index = int(e.data)
self.temp.index = index
self.temp.name = self.temp_index[index].show_name
def create_handle_name(self, e: ft.ControlEvent = None):
if e.data == "":
self.temp.name = self.temp_index[self.temp.index].show_name
else:
self.temp.name = e.data
def create_handle_damage_key(self, e: ft.ControlEvent = None):
self.temp.damage_key = CharacterDamageSkillDamageKey(e.data)
self.temp.transformative_damage_key = None
def create_handle_transformative_damage_key(self, e: ft.ControlEvent = None):
self.temp.transformative_damage_key = (
CharacterDamageSkillTransformativeDamageKey(e.data)
)
self.temp.damage_key = None
def create_confirm(self) -> bool:
if self.temp.index == -1:
return False
if not self.temp.damage_key and not self.temp.transformative_damage_key:
return False
if not self.file_data.get(character.current_name):
self.file_data[character.current_name] = CharacterDamage(skills=[])
self.file_data[character.current_name].skills.append(self.temp)
self.temp = None
self.temp_index.clear()
return True