Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,88 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
#
|
| 4 |
groups = {}
|
| 5 |
|
| 6 |
def initialize_groups(group_info, health):
|
|
|
|
| 7 |
global groups
|
| 8 |
-
groups = {}
|
| 9 |
-
group_lines = group_info.split("\n")
|
| 10 |
for idx, line in enumerate(group_lines, 1):
|
|
|
|
| 11 |
groups[f"小隊{idx}"] = {
|
| 12 |
-
"students":
|
| 13 |
"health": health,
|
| 14 |
"current_health": health
|
| 15 |
}
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
def attack_team(team_name,
|
|
|
|
| 19 |
if team_name not in groups:
|
| 20 |
-
return "
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
# Gradio
|
| 33 |
with gr.Blocks() as app:
|
| 34 |
-
gr.Markdown("##
|
| 35 |
|
| 36 |
-
#
|
| 37 |
with gr.Row():
|
| 38 |
-
group_input = gr.Textbox(
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
attack_output = gr.Textbox(label="攻擊結果")
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
# 初始化組別並更新 Dropdown 選項
|
| 55 |
-
def initialize_groups_and_update_dropdown(group_info, health):
|
| 56 |
-
message = initialize_groups(group_info, health) # 呼叫初始化函式
|
| 57 |
-
dropdown_choices = list(groups.keys()) # 取得小隊名稱列表
|
| 58 |
-
return message, dropdown_choices
|
| 59 |
-
|
| 60 |
-
# 綁定事件:初始化按鈕
|
| 61 |
init_button.click(
|
| 62 |
-
|
| 63 |
-
inputs=[group_input, health_input],
|
| 64 |
outputs=[output_init, team_dropdown]
|
| 65 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
# 啟動應用程式
|
| 72 |
app.launch()
|
| 73 |
|
| 74 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# 全域變數儲存小隊資料
|
| 4 |
groups = {}
|
| 5 |
|
| 6 |
def initialize_groups(group_info, health):
|
| 7 |
+
"""初始化小隊資料"""
|
| 8 |
global groups
|
| 9 |
+
groups = {} # 清空現有小隊資料
|
| 10 |
+
group_lines = group_info.strip().split("\n") # 逐行解析輸入
|
| 11 |
for idx, line in enumerate(group_lines, 1):
|
| 12 |
+
students = [name.strip() for name in line.split(",")]
|
| 13 |
groups[f"小隊{idx}"] = {
|
| 14 |
+
"students": students,
|
| 15 |
"health": health,
|
| 16 |
"current_health": health
|
| 17 |
}
|
| 18 |
+
team_names = list(groups.keys()) # 小隊名稱清單
|
| 19 |
+
return f"已成功建立 {len(groups)} 個小隊,每隊初始血量 {health}。", team_names
|
| 20 |
|
| 21 |
+
def attack_team(team_name, attack_value):
|
| 22 |
+
"""對指定小隊進行攻擊"""
|
| 23 |
if team_name not in groups:
|
| 24 |
+
return "請先選擇有效的小隊名稱!", get_team_health_status()
|
| 25 |
+
|
| 26 |
+
# 計算血量變化
|
| 27 |
+
groups[team_name]["current_health"] = max(
|
| 28 |
+
groups[team_name]["current_health"] - attack_value, 0
|
| 29 |
+
)
|
| 30 |
+
result = f"{team_name} 受到 {attack_value} 點傷害,剩餘血量 {groups[team_name]['current_health']}/{groups[team_name]['health']}。"
|
| 31 |
+
return result, get_team_health_status()
|
| 32 |
|
| 33 |
+
def get_team_health_status():
|
| 34 |
+
"""回傳所有小隊的血量狀態"""
|
| 35 |
+
status = []
|
| 36 |
+
for team, data in groups.items():
|
| 37 |
+
current = data["current_health"]
|
| 38 |
+
total = data["health"]
|
| 39 |
+
status.append(f"{team}: {current}/{total}")
|
| 40 |
+
return "\n".join(status)
|
| 41 |
|
| 42 |
+
# Gradio 介面設置
|
| 43 |
with gr.Blocks() as app:
|
| 44 |
+
gr.Markdown("## 小隊攻擊系統")
|
| 45 |
|
| 46 |
+
# 初始化小隊部分
|
| 47 |
with gr.Row():
|
| 48 |
+
group_input = gr.Textbox(
|
| 49 |
+
placeholder="請輸入小隊資訊,例如:1號, 2號, 3號, 4號\n5號, 6號, 7號, 8號",
|
| 50 |
+
label="輸入小隊成員 (每行一個小隊)"
|
| 51 |
+
)
|
| 52 |
+
health_input = gr.Number(value=600, label="初始血量")
|
| 53 |
+
init_button = gr.Button("初始化")
|
| 54 |
+
output_init = gr.Textbox(label="初始化結果")
|
| 55 |
|
| 56 |
+
# 攻擊小隊部分
|
| 57 |
+
team_dropdown = gr.Dropdown(choices=[], label="選擇攻擊小隊")
|
| 58 |
+
attack_input = gr.Number(value=200, label="輸入攻擊值")
|
| 59 |
+
attack_button = gr.Button("攻擊")
|
| 60 |
+
output_attack = gr.Textbox(label="攻擊結果")
|
|
|
|
| 61 |
|
| 62 |
+
# 小隊血量顯示
|
| 63 |
+
team_status = gr.Textbox(label="小隊血量狀態", interactive=False)
|
| 64 |
+
|
| 65 |
+
# 初始化按鈕事件
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
init_button.click(
|
| 67 |
+
initialize_groups,
|
| 68 |
+
inputs=[group_input, health_input],
|
| 69 |
outputs=[output_init, team_dropdown]
|
| 70 |
)
|
| 71 |
+
|
| 72 |
+
# 攻擊按鈕事件
|
| 73 |
+
attack_button.click(
|
| 74 |
+
attack_team,
|
| 75 |
+
inputs=[team_dropdown, attack_input],
|
| 76 |
+
outputs=[output_attack, team_status]
|
| 77 |
+
)
|
| 78 |
|
| 79 |
+
# 初始顯示所有小隊血量狀態
|
| 80 |
+
init_button.click(
|
| 81 |
+
get_team_health_status,
|
| 82 |
+
inputs=None,
|
| 83 |
+
outputs=team_status
|
| 84 |
+
)
|
| 85 |
|
|
|
|
| 86 |
app.launch()
|
| 87 |
|
| 88 |
|