2023-06-18 17:15:51 +00:00
|
|
|
from nullptr.store import Store
|
2023-06-17 18:18:14 +00:00
|
|
|
from nullptr.models.ship import Ship
|
2023-06-25 15:35:06 +00:00
|
|
|
from nullptr.missions import create_mission, get_mission_class
|
2023-06-17 18:18:14 +00:00
|
|
|
from random import choice
|
|
|
|
from time import sleep
|
2023-06-18 17:15:51 +00:00
|
|
|
from threading import Thread
|
2023-06-17 18:18:14 +00:00
|
|
|
|
2023-06-25 15:35:06 +00:00
|
|
|
class CentralCommandError(Exception):
|
|
|
|
pass
|
|
|
|
|
2023-06-17 18:18:14 +00:00
|
|
|
class CentralCommand:
|
2023-06-18 17:15:51 +00:00
|
|
|
def __init__(self, store, api):
|
2023-06-17 18:18:14 +00:00
|
|
|
self.missions = {}
|
|
|
|
self.stopping = False
|
2023-06-18 17:15:51 +00:00
|
|
|
self.store = store
|
2023-06-17 18:18:14 +00:00
|
|
|
self.api = api
|
|
|
|
self.update_missions()
|
|
|
|
|
|
|
|
def get_ready_missions(self):
|
|
|
|
result = []
|
|
|
|
for ship, mission in self.missions.items():
|
|
|
|
if mission.is_ready():
|
|
|
|
result.append(ship)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def tick(self):
|
|
|
|
missions = self.get_ready_missions()
|
2023-06-18 17:15:51 +00:00
|
|
|
if len(missions) == 0: return False
|
2023-06-17 18:18:14 +00:00
|
|
|
ship = choice(missions)
|
|
|
|
mission = self.missions[ship]
|
|
|
|
mission.step()
|
2023-06-18 17:15:51 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def run_interactive(self):
|
|
|
|
print('auto mode. hit enter to stop')
|
|
|
|
t = Thread(target=self.wait_for_stop)
|
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
|
|
|
self.run()
|
|
|
|
print('manual mode')
|
2023-07-03 17:12:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def wait_for_stop(self):
|
|
|
|
try:
|
|
|
|
input()
|
|
|
|
except EOFError:
|
|
|
|
pass
|
|
|
|
self.stopping = True
|
|
|
|
print('stopping...')
|
|
|
|
|
|
|
|
|
2023-06-17 18:18:14 +00:00
|
|
|
def run(self):
|
|
|
|
self.update_missions()
|
|
|
|
while not self.stopping:
|
2023-06-18 17:15:51 +00:00
|
|
|
did_step = True
|
|
|
|
request_counter = self.api.requests_sent
|
|
|
|
while request_counter == self.api.requests_sent and did_step:
|
|
|
|
did_step = self.tick()
|
|
|
|
self.store.flush()
|
2023-06-17 18:18:14 +00:00
|
|
|
sleep(0.5)
|
|
|
|
self.stopping = False
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.stopping = True
|
|
|
|
|
|
|
|
def set_mission_param(self, ship, nm, val):
|
|
|
|
if ship not in self.missions:
|
|
|
|
print('set a mission for this ship first')
|
|
|
|
return
|
|
|
|
mission = self.missions[ship]
|
|
|
|
params = mission.params()
|
|
|
|
if not nm in params:
|
|
|
|
print(f'{nm} is not a valid param')
|
|
|
|
return
|
|
|
|
param = params[nm]
|
|
|
|
try:
|
2023-06-18 17:15:51 +00:00
|
|
|
parsed_val = param.parse(val, self.store)
|
2023-06-17 18:18:14 +00:00
|
|
|
except ValueError as e:
|
2023-06-18 17:15:51 +00:00
|
|
|
raise MissionError(e)
|
2023-06-17 18:18:14 +00:00
|
|
|
return
|
2023-06-18 17:15:51 +00:00
|
|
|
ship.set_mission_state(nm, parsed_val)
|
2023-06-17 18:18:14 +00:00
|
|
|
|
|
|
|
def update_missions(self):
|
2023-06-18 17:15:51 +00:00
|
|
|
for s in self.store.all(Ship):
|
2023-06-17 18:18:14 +00:00
|
|
|
if s.mission is None:
|
|
|
|
if s in self.missions:
|
|
|
|
self.stop_mission(s)
|
|
|
|
elif s not in self.missions:
|
|
|
|
self.start_mission(s)
|
2023-06-18 18:02:33 +00:00
|
|
|
if s in self.missions:
|
|
|
|
m = self.missions[s]
|
|
|
|
m.next_step = max(s.cooldown, s.arrival)
|
2023-06-17 18:18:14 +00:00
|
|
|
|
2023-06-25 15:35:06 +00:00
|
|
|
def init_mission(self, s, mtyp):
|
|
|
|
if mtyp == 'none':
|
|
|
|
s.mission_state = {}
|
|
|
|
s.mission_status = None
|
|
|
|
s.mission = None
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
mclass = get_mission_class(mtyp)
|
|
|
|
except ValueError:
|
|
|
|
raise CentralCommandError('no such mission')
|
|
|
|
s.mission = mtyp
|
|
|
|
s.mission_status = 'init'
|
|
|
|
s.mission_state = {k: v.default for k,v in mclass.params().items()}
|
|
|
|
self.start_mission(s)
|
|
|
|
|
2023-06-17 18:18:14 +00:00
|
|
|
def start_mission(self, s):
|
|
|
|
mtype = s.mission
|
2023-06-18 17:15:51 +00:00
|
|
|
m = create_mission(mtype, s, self.store, self.api)
|
2023-06-17 18:18:14 +00:00
|
|
|
self.missions[s] = m
|
|
|
|
return m
|
|
|
|
|
|
|
|
def stop_mission(self, s):
|
|
|
|
if s in self.missions:
|
|
|
|
del self.missions[s]
|