def attribute_mods(self): # Take mods from DNA combines with any ongoing mods and merge those if not self.qualities_base or not self.attribute_base: self.qualities_base, self.attribute_base = dna_helpers.qualities_from_dna(self.dna) attributes = self.attribute_base.copy() return math_helpers.add_or_merge_dicts(attributes, self.attribute_mods_dict)
def apply_event_effects(person_data={}, world_data={}, event_data={}, event_type='birthplace', year=None, age=None, tag_manager={}, event_id=42): message = "" if not year: year = world_data.get("year", 1100) world_data["year"] = year if age is None: age = person_data.get("age", 16 + numpy.random.randint(20)) person_data["age"] = age name = event_data.get("name", "in a barn") properties = event_data.get("properties", {}) if isinstance(properties, basestring): properties = math_helpers.convert_string_to_properties_object(properties) math_helpers.add_tags(tag_manager, 'event', event_data.get("tags", [])) effect_was_applied = False generated_items = [] addional_messages = [] effects = event_data.get("effects", []) for effect in effects: # Don't consider if it's a unique effect prevent = effect.get("unique", False) if prevent and effect_was_applied: continue family = world_data.get("family", {}) # Roll the dice, if the chance occurs then parse the rest chance = effect.get("chance", None) if chance: chance_modifier = family.get("conflict", 1) try: chance_modifier = float(chance_modifier) except ValueError: chance_modifier = 1 try: chance = math_helpers.value_of_variable(chance) chance += chance_modifier chance /= 100 if not numpy.random.random() < chance: continue except ValueError: continue else: pass # Check the requirements # family.enemy has Elves, "family.*.profession sailor", family.*.profession has doctor requirement = effect.get("requirement", None) if requirement: passes = math_helpers.check_requirements(requirement, world_data) if not passes: continue # Update the message # "An innkeeper named [barmaid.name] assists, You were born the day your father was killed in battle. His [weapon.type] and [armor.type] are passed to you. ef_message = effect.get("message", None) if ef_message: addional_messages.append(ef_message) # Generators can create objects/items for use in the story #TODO: Generate a generator also if it's in a message text and not called out explicitly generator_data = {"generator": effect.get("generator", None), "role": effect.get("role", None), "years": effect.get("years", None), "override": effect.get("override", None), "power": effect.get("refresh", None) or effect.get("power", None) } # Run methods for everything in effects effect_data = effect.get("effect", None) # pay = good, father.leave, disease = infection effect_data = math_helpers.convert_string_to_properties_object(effect_data) effect_data = math_helpers.add_or_merge_dicts(effect_data, generator_data) generated_items_new = apply_effects(person_data, world_data, effect_data, tag_manager) generated_items = generated_items + generated_items_new # Properties are always applied to the person after all effects are run ef_properties = effect.get("properties", None) # JSON or str, "mother.profession = waitress, lifespan +2 if ef_properties: if isinstance(ef_properties, basestring): ef_properties = math_helpers.convert_string_to_properties_object(ef_properties) properties = math_helpers.add_or_merge_dicts(properties, ef_properties) effect_was_applied = True severity = 1 if event_type == 'birthplace': if isinstance(name, basestring) and name is not '': name_lower = name[0].lower() + name[1:] else: name_lower = name person_name = person_data.get("name", "Jon Snow") message += "<b>" + person_name + " was born " + str(name_lower) + "</b>" addional_messages = [message] + addional_messages if properties: message_new = apply_properties(person_data, world_data, properties, generated_items) if message_new: addional_messages.append(message_new) if generated_items and "item_list" in person_data: person_data["item_list"] += generated_items addional_messages = [m.strip() for m in addional_messages] message = "<br/>".join(addional_messages) return {"id": event_id, "age": age, "year": year, "message": message, "world_data": json.dumps(world_data), "severity": severity}
def apply_event_effects( person_data={}, world_data={}, event_data={}, event_type="birthplace", year=None, age=None, tag_manager={}, event_id=42, ): message = "" if not year: year = world_data.get("year", 1100) world_data["year"] = year if age is None: age = person_data.get("age", 16 + numpy.random.randint(20)) person_data["age"] = age name = event_data.get("name", "in a barn") properties = event_data.get("properties", {}) if isinstance(properties, basestring): properties = math_helpers.convert_string_to_properties_object(properties) math_helpers.add_tags(tag_manager, "event", event_data.get("tags", [])) effect_was_applied = False generated_items = [] addional_messages = [] effects = event_data.get("effects", []) for effect in effects: # Don't consider if it's a unique effect prevent = effect.get("unique", False) if prevent and effect_was_applied: continue family = world_data.get("family", {}) # Roll the dice, if the chance occurs then parse the rest chance = effect.get("chance", None) if chance: chance_modifier = family.get("conflict", 1) try: chance_modifier = float(chance_modifier) except ValueError: chance_modifier = 1 try: chance = math_helpers.value_of_variable(chance) chance += chance_modifier chance /= 100 if not numpy.random.random() < chance: continue except ValueError: continue else: pass # Check the requirements # family.enemy has Elves, "family.*.profession sailor", family.*.profession has doctor requirement = effect.get("requirement", None) if requirement: passes = math_helpers.check_requirements(requirement, world_data) if not passes: continue # Update the message # "An innkeeper named [barmaid.name] assists, You were born the day your father was killed in battle. His [weapon.type] and [armor.type] are passed to you. ef_message = effect.get("message", None) if ef_message: addional_messages.append(ef_message) # Generators can create objects/items for use in the story # TODO: Generate a generator also if it's in a message text and not called out explicitly generator_data = { "generator": effect.get("generator", None), "role": effect.get("role", None), "years": effect.get("years", None), "override": effect.get("override", None), "power": effect.get("refresh", None) or effect.get("power", None), } # Run methods for everything in effects effect_data = effect.get("effect", None) # pay = good, father.leave, disease = infection effect_data = math_helpers.convert_string_to_properties_object(effect_data) effect_data = math_helpers.add_or_merge_dicts(effect_data, generator_data) generated_items_new = apply_effects(person_data, world_data, effect_data, tag_manager) generated_items = generated_items + generated_items_new # Properties are always applied to the person after all effects are run ef_properties = effect.get("properties", None) # JSON or str, "mother.profession = waitress, lifespan +2 if ef_properties: if isinstance(ef_properties, basestring): ef_properties = math_helpers.convert_string_to_properties_object(ef_properties) properties = math_helpers.add_or_merge_dicts(properties, ef_properties) effect_was_applied = True severity = 1 if event_type == "birthplace": if isinstance(name, basestring) and name is not "": name_lower = name[0].lower() + name[1:] else: name_lower = name person_name = person_data.get("name", "Jon Snow") message += "<b>" + person_name + " was born " + str(name_lower) + "</b>" addional_messages = [message] + addional_messages if properties: message_new = apply_properties(person_data, world_data, properties, generated_items) if message_new: addional_messages.append(message_new) if generated_items and "item_list" in person_data: person_data["item_list"] += generated_items addional_messages = [m.strip() for m in addional_messages] message = "<br/>".join(addional_messages) return { "id": event_id, "age": age, "year": year, "message": message, "world_data": json.dumps(world_data), "severity": severity, }