dcs-retribution/game/lasercodes/lasercoderegistry.py
Dan Albert 279572ae09
Add tests for LaserCodeRegistry, clean up.
* Store a deque rather than an iterator so it can be pickled
* Remove mangling from staticmethod (and rename now that it's no longer
  a generator)
* Rename "get" to "alloc" to make the mutation clear
* Move to its own package (the changes I'm working on make this no
  longer mission generator specific)
* Remove useless exception class. It's never caught so the unique type
  isn't needed
2023-10-01 20:11:37 +02:00

29 lines
984 B
Python

from collections import deque
class LaserCodeRegistry:
def __init__(self) -> None:
self.allocated_codes: set[int] = set()
self.available_codes = LaserCodeRegistry._all_valid_laser_codes()
def alloc_laser_code(self) -> int:
try:
code = self.available_codes.popleft()
self.allocated_codes.add(code)
return code
except IndexError:
raise RuntimeError("All laser codes have been allocated")
@staticmethod
def _all_valid_laser_codes() -> deque[int]:
# Valid laser codes are as follows
# First digit is always 1
# Second digit is 5-7
# Third and fourth digits are 1 - 8
# We iterate backward (reversed()) so that 1687 follows 1688
q = deque(int(oct(code)[2:]) + 11 for code in reversed(range(0o1500, 0o2000)))
# We start with the default of 1688 and wrap around when we reach the end
q.rotate(-q.index(1688))
return q