def filter_by_requirements(self, world_data): return check_requirements(self.requirements, world_data)
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 breakout_component_types(world_data, tags, pattern_list): component_types = {} component_tag_counts = {} active_components = [] try: from procyon.stories.models import Component active_components = cache.get('active_components', None) if not active_components: all_components = Component.objects.filter(active=True) active_components = [] for component in all_components: new_component = model_to_dict(component) r = component.requirements if ((r.startswith("{") and r.endswith("}")) or (r.startswith("[") and r.endswith("]"))) and len(r) > 4: try: r = json.dumps(r) except ValueError: r = '' elif len(r) > 3 and isinstance(r, basestring): r = math_helpers.convert_string_to_req_object(r) new_component['requirements'] = r active_components.append(new_component) cache.set('active_components', active_components) except ImportError: # Django not loaded, use local file cache instead - best for testing with open('procyon/fixtures/components_cache.txt', mode='r') as infile: c_text = str(infile.read()) try: comps = json.loads(c_text) for c in comps: active_components.append(c) except ValueError: pass tags_searching = tags_to_find(tags, world_data) for component in active_components: ctype = str(component.get("type", "None")) ctype = ctype.lower().strip() if ctype in pattern_list: is_match = math_helpers.check_requirements(component.get("requirements", ""), world_data) if is_match: tag_match = count_tag_matches(component.get("tags", ""), component.get("weighting", ""), tags_searching) if not ctype in component_types: component_types[ctype] = [] component_tag_counts[ctype] = [] component_types[ctype].append(component) component_tag_counts[ctype].append(tag_match) return component_types, component_tag_counts
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 breakout_component_types(world_data, tags, pattern_list): component_types = {} component_tag_counts = {} active_components = [] try: from procyon.stories.models import Component active_components = cache.get("active_components", None) if not active_components: all_components = Component.objects.filter(active=True) active_components = [] for component in all_components: new_component = model_to_dict(component) r = component.requirements if ((r.startswith("{") and r.endswith("}")) or (r.startswith("[") and r.endswith("]"))) and len(r) > 4: try: r = json.dumps(r) except ValueError: r = "" elif len(r) > 3 and isinstance(r, basestring): r = math_helpers.convert_string_to_req_object(r) new_component["requirements"] = r active_components.append(new_component) cache.set("active_components", active_components) except ImportError: # Django not loaded, use local file cache instead - best for testing with open("procyon/fixtures/components_cache.txt", mode="r") as infile: c_text = str(infile.read()) try: comps = json.loads(c_text) for c in comps: active_components.append(c) except ValueError: pass tags_searching = tags_to_find(tags, world_data) for component in active_components: ctype = str(component.get("type", "None")) ctype = ctype.lower().strip() if ctype in pattern_list: is_match = math_helpers.check_requirements(component.get("requirements", ""), world_data) if is_match: tag_match = count_tag_matches(component.get("tags", ""), component.get("weighting", ""), tags_searching) if not ctype in component_types: component_types[ctype] = [] component_tag_counts[ctype] = [] component_types[ctype].append(component) component_tag_counts[ctype].append(tag_match) return component_types, component_tag_counts