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 7.3KB

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