yqianjiang commited on
Commit
b41b224
1 Parent(s): bcf0302

find characters by name

Browse files
Files changed (5) hide show
  1. Character.py +4 -3
  2. WorldSimulation.py +14 -1
  3. app.py +8 -7
  4. plugins/ResourceDepletionPlugin.py +1 -0
  5. run.py +0 -3
Character.py CHANGED
@@ -27,9 +27,10 @@ class Character:
27
  self.clan = clan # 宗族
28
 
29
  def die(self):
30
- self.history.append(f"{self.real_age}岁,死亡")
31
- self.special_history.append(f"{self.real_age}岁,死亡")
32
- self.is_alive = False
 
33
 
34
  def cultivate(self, experience_points):
35
  if not self.is_alive:
 
27
  self.clan = clan # 宗族
28
 
29
  def die(self):
30
+ if self.is_alive:
31
+ self.history.append(f"{self.real_age}岁,死亡")
32
+ self.special_history.append(f"{self.real_age}岁,死亡")
33
+ self.is_alive = False
34
 
35
  def cultivate(self, experience_points):
36
  if not self.is_alive:
WorldSimulation.py CHANGED
@@ -97,4 +97,17 @@ class WorldSimulation:
97
  self.dead_characters.append(character)
98
 
99
  def consume_spiritual_energy(self, amount):
100
- self.world_spiritual_energy -= amount
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  self.dead_characters.append(character)
98
 
99
  def consume_spiritual_energy(self, amount):
100
+ self.world_spiritual_energy -= amount
101
+
102
+ def find_characters_by_name(self, name):
103
+ # 可能会有重名,把所有都返回
104
+ res = [c for c in self.characters if c.name == name]
105
+ if len(res) == 0:
106
+ return [c for c in self.dead_characters if c.name == name]
107
+ return res
108
+
109
+ def find_characters_by_clan(self, clan):
110
+ res = [c for c in self.characters if c.clan == clan]
111
+ if len(res) == 0:
112
+ return [c for c in self.dead_characters if c.clan == clan]
113
+ return res
app.py CHANGED
@@ -33,11 +33,12 @@ def run_simulation(rounds):
33
  return "请先初始化世界"
34
  return simulation.run_simulation(num_rounds)
35
 
36
- def get_character_info(character_index):
37
- if character_index < len(simulation.characters):
38
- return str(simulation.characters[character_index])
 
39
  else:
40
- return f"没有第{character_index}个角色,目前共有{len(simulation.characters)}个存活角色"
41
 
42
  def get_dead_character_info(character_index):
43
  if character_index < len(simulation.dead_characters):
@@ -111,15 +112,15 @@ with gr.Blocks() as demo:
111
 
112
  # 查看角色信息
113
  with gr.Row():
114
- character_info_output = gr.Number(label="角色信息", info="输入角色编号", precision=0)
115
  get_character_info_button = gr.Button("查看角色信息")
116
  character_info_output_display = gr.Textbox(label="角色信息")
117
  get_character_info_button.click(fn=get_character_info, inputs=[character_info_output], outputs=[character_info_output_display])
118
 
119
  with gr.Row():
120
- dead_character_info_output = gr.Number(label="死亡角色信息", info="输入死亡角色编号", precision=0)
121
  get_dead_character_info_button = gr.Button("查看死亡角色信息")
122
  dead_character_info_output_display = gr.Textbox(label="死亡角色信息")
123
  get_dead_character_info_button.click(fn=get_dead_character_info, inputs=[dead_character_info_output], outputs=[dead_character_info_output_display])
124
 
125
- demo.launch(debug=True)
 
33
  return "请先初始化世界"
34
  return simulation.run_simulation(num_rounds)
35
 
36
+ def get_character_info(name):
37
+ characters = simulation.find_characters_by_name(name)
38
+ if len(characters) == 0:
39
+ return f"没有找到名字为{name}的角色"
40
  else:
41
+ return "\n\n".join([str(c) for c in characters])
42
 
43
  def get_dead_character_info(character_index):
44
  if character_index < len(simulation.dead_characters):
 
112
 
113
  # 查看角色信息
114
  with gr.Row():
115
+ character_info_output = gr.Textbox(label="角色信息", info="输入角色名字")
116
  get_character_info_button = gr.Button("查看角色信息")
117
  character_info_output_display = gr.Textbox(label="角色信息")
118
  get_character_info_button.click(fn=get_character_info, inputs=[character_info_output], outputs=[character_info_output_display])
119
 
120
  with gr.Row():
121
+ dead_character_info_output = gr.Number(label="死亡角色信息", info="输入死亡角色序号(按死亡顺序排列)", precision=0)
122
  get_dead_character_info_button = gr.Button("查看死亡角色信息")
123
  dead_character_info_output_display = gr.Textbox(label="死亡角色信息")
124
  get_dead_character_info_button.click(fn=get_dead_character_info, inputs=[dead_character_info_output], outputs=[dead_character_info_output_display])
125
 
126
+ demo.queue().launch(debug=True)
plugins/ResourceDepletionPlugin.py CHANGED
@@ -26,6 +26,7 @@ class ResourceDepletionPlugin:
26
  killed = random.sample(clan_members, num_killed)
27
 
28
  for c in killed:
 
29
  character_die_callback(c)
30
 
31
  def execute(self, resources, characters, character_die_callback):
 
26
  killed = random.sample(clan_members, num_killed)
27
 
28
  for c in killed:
29
+ c.history.append(f"{c.real_age}岁,因资源耗尽死亡")
30
  character_die_callback(c)
31
 
32
  def execute(self, resources, characters, character_die_callback):
run.py CHANGED
@@ -17,6 +17,3 @@ print(stats.summarize_markdown())
17
 
18
  print(simulation.characters[0])
19
  print(simulation.characters[1])
20
-
21
- print(simulation.dead_characters[0])
22
- print(simulation.dead_characters[1])
 
17
 
18
  print(simulation.characters[0])
19
  print(simulation.characters[1])