A generator for *Star Wars* interstellar shipping and passenger jobs, based loosely on the missions you can take on in *Escape Velocity* and *Endless Space*.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

swjg.py 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import argparse
  2. import re
  3. from enum import Enum
  4. from string import Template
  5. from typing import NamedTuple
  6. from jinja2 import Environment, PackageLoader, select_autoescape
  7. import items
  8. from destinations import DESTINATIONS, SYSTEMS, Destination, DestinationType
  9. # global variables
  10. env = Environment(loader=PackageLoader("swjg"), autoescape=select_autoescape)
  11. parser = argparse.ArgumentParser()
  12. # helper definitions
  13. def get_identifiers_from_template(template):
  14. # Template.get_identifiers() doesn't appear until Python 3.11
  15. # Don't substitute anything; get the raw string
  16. template_str = Template.safe_substitute()
  17. matches = re.findall("\$[a-z0-9_\.]+", template_str)
  18. matches = [x[1:] for x in matches]
  19. return matches
  20. def get_destination_type_from_enum(destination):
  21. dtype = destination.type
  22. dtype_name = dtype._name_
  23. dtype_name_lower = dtype_name.lower()
  24. dtype_name_nou = dtype_name_lower.replace("_", " ")
  25. if dtype_name_nou[0] in ["a", "e", "i", "o", "u"]:
  26. # it's only "asteroid" and "ocean" now but it might expand
  27. dtype_name_final = "an " + dtype_name_nou
  28. else:
  29. dtype_name_final = "a " + dtype_name_nou
  30. return dtype_name_final
  31. # Class definitions
  32. class Mission:
  33. output_destination_template = Template(
  34. "to $world $destination_type in the $system system"
  35. )
  36. output_sector_template = Template(", part of the $sector,")
  37. value_template = Template("for $value credits")
  38. error_template = Template(
  39. "You haven't given this mission some of the values it needs. Please check: $potential_values"
  40. )
  41. def __init__(self, destination: Destination, value: int, *args, **kwargs):
  42. # destination: Destination (see above)
  43. self.world = destination.name if destination.name is not None else None
  44. self.system = (
  45. destination.system if destination.system is not None else destination.name
  46. )
  47. self.sector = destination.sector if destination.sector is not None else None
  48. self.destination_type = get_destination_type_from_enum(destination)
  49. self.value = value
  50. def assemble_templates(self) -> list:
  51. # Override this in children
  52. template_list = []
  53. if self.world != None:
  54. template_list.append(self.output_destination_template)
  55. if self.sector != None:
  56. template_list.append(self.output_sector_template)
  57. template_list.append(self.value_template)
  58. return template_list
  59. def get_current_values(self) -> dict:
  60. return {
  61. "world": self.world + "," if self.world != "" else "",
  62. "system": self.system,
  63. "sector": self.sector,
  64. "destination_type": self.destination_type,
  65. "value": self.value,
  66. }
  67. def missing_values(self) -> list:
  68. object_vars = vars(self)
  69. missing = []
  70. for key, val in object_vars.items():
  71. if val is None and key != "time": # time is optional
  72. missing.append(key)
  73. return missing
  74. def assemble_text(self) -> str:
  75. missing_vals = self.missing_values()
  76. templates = self.assemble_templates()
  77. if (
  78. len(templates) == 0 or len(missing_vals) != 0
  79. ): # either both of these should be true or neither should
  80. raise ValueError(
  81. self.error_template.substitute(potential_values=missing_vals)
  82. )
  83. current_values = self.get_current_values()
  84. output = []
  85. for template in templates:
  86. output.append(template.substitute(**current_values))
  87. out_text = " ".join(output)
  88. out_text = out_text.replace(" ,", ",")
  89. out_text += "."
  90. return out_text
  91. class PassengerMission(Mission):
  92. output_initial_template = Template("Bring $number passengers")
  93. def __init__(self, number: int, *args, **kwargs):
  94. super(PassengerMission, self).__init__(*args, **kwargs)
  95. self.number = number
  96. def get_current_values(self) -> dict:
  97. base_dict = super(PassengerMission, self).get_current_values()
  98. new_dict = {"number": self.number}
  99. base_dict.update(new_dict)
  100. return base_dict
  101. def assemble_templates(self):
  102. template_list = [self.output_initial_template]
  103. template_list.extend(super(PassengerMission, self).assemble_templates())
  104. return template_list
  105. def assemble_text(self) -> str:
  106. return super(PassengerMission, self).assemble_text()
  107. class CargoMission(Mission):
  108. output_initial_template = Template("Deliver $tons tons of $item")
  109. output_time_timeplate = Template("in the next $time days")
  110. def __init__(self, tons: int, item: str, time: int | None, *args, **kwargs):
  111. # tons: integer
  112. # item: string (this will not be pluralized in the text)
  113. # time: integer (number of days the crew have to complete delivery)
  114. # value: integer (value in credits)
  115. super(CargoMission, self).__init__(*args, **kwargs)
  116. self.tons = tons
  117. self.item = item
  118. self.time = time
  119. def get_current_values(self) -> dict:
  120. base_dict = super(CargoMission, self).get_current_values()
  121. new_dict = {"tons": self.tons, "item": self.item, "time": self.time}
  122. base_dict.update(new_dict)
  123. return base_dict
  124. def assemble_templates(self):
  125. template_list = [self.output_initial_template]
  126. if self.time is not None:
  127. template_list.append(self.output_time_timeplate)
  128. template_list.extend(super(CargoMission, self).assemble_templates())
  129. return template_list
  130. def assemble_text(self) -> str:
  131. return super(CargoMission, self).assemble_text()
  132. class GroupMission(Mission):
  133. output_initial_template = Template("Bring $number members of a $group")
  134. def __init__(self, number: int, group: str, *args, **kwargs):
  135. super(GroupMission, self).__init__(*args, **kwargs)
  136. self.number = number
  137. self.group = group
  138. def get_current_values(self) -> dict:
  139. base_dict = super(GroupMission, self).get_current_values()
  140. new_dict = {"number": self.number, "group": self.group}
  141. base_dict.update(new_dict)
  142. return base_dict
  143. def assemble_templates(self):
  144. template_list = [self.output_initial_template]
  145. template_list.extend(super(GroupMission, self).assemble_templates())
  146. return template_list
  147. def assemble_text(self) -> str:
  148. return super(GroupMission, self).assemble_text()
  149. # the function that does the thing
  150. def main():
  151. import random
  152. mission_types = [PassengerMission, CargoMission, GroupMission]
  153. missions = []
  154. for _ in range(5):
  155. current_mission = random.choice(mission_types)
  156. dest_type = random.randint(1, 5)
  157. if dest_type < 3: # world, 40% chance
  158. destination = random.choice(DESTINATIONS)
  159. else:
  160. system = random.choice(SYSTEMS)
  161. destination = Destination(
  162. name="",
  163. system=system[0],
  164. sector=system[1],
  165. type=(
  166. DestinationType.STATION
  167. if random.randint(0, 1) == 0
  168. else DestinationType.ASTEROID_BASE
  169. ),
  170. )
  171. item, time, tons, number, value, group = [None] * 6
  172. if current_mission == CargoMission:
  173. current_item = random.choice(items.ITEMS)
  174. tons = random.randint(
  175. current_item.potential_tons[0], current_item.potential_tons[1]
  176. )
  177. value = random.randint(*current_item.potential_values) * 1000
  178. item = current_item.name
  179. if current_item.potential_times is not None:
  180. if current_item.chance_for_time is not None:
  181. is_there_a_time = random.random() * 100
  182. # print(current_item.chance_for_time, is_there_a_time, is_there_a_time < current_item.chance_for_time)
  183. if is_there_a_time < current_item.chance_for_time:
  184. time = random.randint(
  185. current_item.potential_times[0],
  186. current_item.potential_times[1],
  187. )
  188. else:
  189. number = random.randint(1, 8)
  190. time = random.randint(7, 31)
  191. value = random.randint(20, 120) * 1000
  192. if current_mission == GroupMission:
  193. group = random.choice(["family", "performing troupe", "acrobatic troupe"])
  194. missions.append(
  195. current_mission(
  196. destination=destination,
  197. number=number,
  198. tons=tons,
  199. item=item,
  200. time=time,
  201. value=value,
  202. group=group,
  203. )
  204. )
  205. for mission in missions:
  206. # print(mission, mission.__dict__)
  207. print(mission.assemble_text())
  208. # Don't do anything if the module is loaded wholesale into something else
  209. if __name__ == "__main__":
  210. main()