Noëlle 9 місяці тому
джерело
коміт
4415bf6870
Не вдалося знайти GPG ключ що відповідає даному підпису
2 змінених файлів з 70 додано та 36 видалено
  1. 1
    1
      README.md
  2. 69
    35
      swjg.py

+ 1
- 1
README.md Переглянути файл

A generator for *Star Wars* interstellar shipping and passenger jobs, based loosely on the missions you can take on in *Escape Velocity* and *Endless Sky*. A generator for *Star Wars* interstellar shipping and passenger jobs, based loosely on the missions you can take on in *Escape Velocity* and *Endless Sky*.




SW Job Generator © 2023 by [Noëlle Anthony](https://chat.noelle.codes/@noelle) is licensed under [CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/?ref=chooser-v1) <img alt="Creative Commons logo" src="https://chooser-beta.creativecommons.org/img/cc-logo.f0ab4ebe.svg" height=16 style="max-height: 16px;" /> <img alt="cc-by symbol" src="https://chooser-beta.creativecommons.org/img/cc-by.21b728bb.svg" style="max-height: 16px;" height=16 />
SW Job Generator © 2023 by [Noëlle Anthony](https://chat.noelle.codes/@noelle) is licensed under [CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/?ref=chooser-v1) <img alt="Creative Commons logo" src="https://chooser-beta.creativecommons.org/img/cc-logo.f0ab4ebe.svg" height=16 style="max-height: 16px;" /> <img alt="cc-by symbol" src="https://chooser-beta.creativecommons.org/img/cc-by.21b728bb.svg" style="max-height: 16px;" height=16 />

+ 69
- 35
swjg.py Переглянути файл

import argparse, re
import argparse
import re
from enum import Enum
from string import Template from string import Template
from typing import NamedTuple from typing import NamedTuple




# global variables # global variables


env = Environment(
loader=PackageLoader("swjg"),
autoescape=select_autoescape
)
env = Environment(loader=PackageLoader("swjg"), autoescape=select_autoescape)


parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()


# helper functions
# helper definitions



def get_identifiers_from_template(template): def get_identifiers_from_template(template):
# Template.get_identifiers() doesn't appear until Python 3.11 # Template.get_identifiers() doesn't appear until Python 3.11
return matches return matches




# classes and such
class DangerLevel(Enum):
NONE = 1
LOW = 2
MODERATE = 3
HIGH = 4
EXTREME = 5


class DangerType(Enum):
HOSTILE = 1
AT_WAR = 2
DESERT = 3
JUNGLE = 4
OCEAN = 5
ATMOSPHERE = 6



class Destination(NamedTuple): class Destination(NamedTuple):
world: str world: str
system: str|None
sector: str|None
system: str | None
sector: str | None




class Mission: class Mission:
output_destination_template = Template("to $world in the $system system") output_destination_template = Template("to $world in the $system system")
output_sector_template = Template(", part of the $sector,") output_sector_template = Template(", part of the $sector,")
value_template = Template("for $value credits") value_template = Template("for $value credits")
error_template = Template("You haven't given this mission some of the values it needs. Please check: $potential_values")
error_template = Template(
"You haven't given this mission some of the values it needs. Please check: $potential_values"
)


def __init__(self, destination:Destination, value:int, *args, **kwargs):
def __init__(self, destination: Destination, value: int, *args, **kwargs):
# destination: Destination (see above) # destination: Destination (see above)
self.world = destination.world if destination.world is not None else None self.world = destination.world if destination.world is not None else None
self.system = destination.system if destination.system is not None else destination.world
self.system = (
destination.system if destination.system is not None else destination.world
)
self.sector = destination.sector if destination.sector is not None else None self.sector = destination.sector if destination.sector is not None else None
self.value = value self.value = value
# for arg, val in kwargs.items(): # for arg, val in kwargs.items():
"world": self.world, "world": self.world,
"system": self.system, "system": self.system,
"sector": self.sector, "sector": self.sector,
"value": self.value
"value": self.value,
} }


def missing_values(self) -> list: def missing_values(self) -> list:
def assemble_text(self) -> str: def assemble_text(self) -> str:
missing_vals = self.missing_values() missing_vals = self.missing_values()
templates = self.assemble_templates() templates = self.assemble_templates()
if len(templates) == 0 or len(missing_vals) != 0: # either both of these should be true or neither should
raise ValueError(self.error_template.substitute(potential_values=missing_vals))
if (
len(templates) == 0 or len(missing_vals) != 0
): # either both of these should be true or neither should
raise ValueError(
self.error_template.substitute(potential_values=missing_vals)
)


current_values = self.get_current_values() current_values = self.get_current_values()


out_text += "." out_text += "."
return out_text return out_text


class PassengerMission(Mission): class PassengerMission(Mission):
output_initial_template = Template("Bring $number passengers") output_initial_template = Template("Bring $number passengers")


def __init__(self, number:int, *args, **kwargs):
def __init__(self, number: int, *args, **kwargs):
super(PassengerMission, self).__init__(*args, **kwargs) super(PassengerMission, self).__init__(*args, **kwargs)
self.number = number self.number = number


def get_current_values(self) -> dict: def get_current_values(self) -> dict:
base_dict = super(PassengerMission, self).get_current_values() base_dict = super(PassengerMission, self).get_current_values()
new_dict = {
"number": self.number
}
new_dict = {"number": self.number}
base_dict.update(new_dict) base_dict.update(new_dict)
return base_dict return base_dict


output_initial_template = Template("Deliver $tons tons of $item") output_initial_template = Template("Deliver $tons tons of $item")
output_time_timeplate = Template("in the next $time days") output_time_timeplate = Template("in the next $time days")


def __init__(self, tons:int, item:str, time:int, *args, **kwargs):
def __init__(self, tons: int, item: str, time: int, *args, **kwargs):
# tons: integer # tons: integer
# item: string (this will not be pluralized in the text) # item: string (this will not be pluralized in the text)
# time: integer (number of days the crew have to complete delivery) # time: integer (number of days the crew have to complete delivery)


def get_current_values(self) -> dict: def get_current_values(self) -> dict:
base_dict = super(CargoMission, self).get_current_values() base_dict = super(CargoMission, self).get_current_values()
new_dict = {
"tons": self.tons,
"item": self.item,
"time": self.time
}
new_dict = {"tons": self.tons, "item": self.item, "time": self.time}
base_dict.update(new_dict) base_dict.update(new_dict)
return base_dict return base_dict


class GroupMission(Mission): class GroupMission(Mission):
output_initial_template = Template("Bring $number members of a $group") output_initial_template = Template("Bring $number members of a $group")


def __init__(self, number:int, group:str, *args, **kwargs):
def __init__(self, number: int, group: str, *args, **kwargs):
super(GroupMission, self).__init__(*args, **kwargs) super(GroupMission, self).__init__(*args, **kwargs)
self.number = number self.number = number
self.group = group self.group = group


def get_current_values(self) -> dict: def get_current_values(self) -> dict:
base_dict = super(GroupMission, self).get_current_values() base_dict = super(GroupMission, self).get_current_values()
new_dict = {
"number": self.number,
"group": self.group
}
new_dict = {"number": self.number, "group": self.group}
base_dict.update(new_dict) base_dict.update(new_dict)
return base_dict return base_dict




# the function that does the thing # the function that does the thing



def main(): def main():
import random import random

mission_types = [PassengerMission, CargoMission, GroupMission] mission_types = [PassengerMission, CargoMission, GroupMission]
missions = [] missions = []
for _ in range(5): for _ in range(5):
current_mission = random.choice(mission_types) current_mission = random.choice(mission_types)
destination = Destination("Alderaan", None, "Core Worlds") destination = Destination("Alderaan", None, "Core Worlds")
number = random.randint(1,8)
number = random.randint(1, 8)
tons = random.randint(4, 12) tons = random.randint(4, 12)
item = random.choice(["gravel", "computer chips", "spice", "live ysalamiri", "empty shipping containers"])
item = random.choice(
[
"gravel",
"computer chips",
"spice",
"live ysalamiri",
"empty shipping containers",
]
)
time = random.randint(7, 31) time = random.randint(7, 31)
value = random.randint(20, 120) * 1000 value = random.randint(20, 120) * 1000
group = None group = None
if current_mission == GroupMission: if current_mission == GroupMission:
group = random.choice(["family", "performing troupe", "acrobatic troupe"]) group = random.choice(["family", "performing troupe", "acrobatic troupe"])
missions.append(current_mission(destination=destination, number=number, tons=tons, item=item, time=time, value=value, group=group))
missions.append(
current_mission(
destination=destination,
number=number,
tons=tons,
item=item,
time=time,
value=value,
group=group,
)
)
for mission in missions: for mission in missions:
# print(mission, mission.__dict__) # print(mission, mission.__dict__)
print(mission.assemble_text()) print(mission.assemble_text())
# Don't do anything if the module is loaded wholesale into something else # Don't do anything if the module is loaded wholesale into something else


if __name__ == "__main__": if __name__ == "__main__":
main()
main()

Завантаження…
Відмінити
Зберегти