def _save_train(train: Train) -> None: """Saves the train in the state.""" saved_trains = checkerlib.load_state("saved_trains") if not saved_trains: saved_trains = [] saved_trains.insert(0, train) saved_trains = saved_trains[:2] checkerlib.store_state("saved_trains", saved_trains)
def _get_random_train(max_length: int = 15) -> Train: """Returns a random train from the state.""" saved_trains = checkerlib.load_state("saved_trains") if not saved_trains: return None trains = [ train for train in saved_trains if train and len(train.wagons) <= max_length ] return random.choice(trains) if trains else None
def place_flag(self, tick): if checkerlib.load_state('key2') is not None: raise Exception('Got state where there should be none') if tick == 0: if checkerlib.load_state('key1') is not None: raise Exception('Got state where there should be none') checkerlib.get_flag(tick) if self.team == 92: if tick == 0: checkerlib.store_state('key1', 'Wir können Zustände speichern 🥳') else: if checkerlib.load_state( 'key1') != 'Wir können Zustände speichern 🥳': raise Exception('Did not get stored state back') if tick == 0: checkerlib.store_state('🔑ser', 'Söze') if checkerlib.load_state('🔑ser') != 'Söze': raise Exception('Did not get stored state back') elif tick == 1: if checkerlib.load_state('🔑ser') != 'Söze': raise Exception('Did not get stored state back') checkerlib.store_state('🔑ser', ['Roger', '"Verbal"', 'Kint']) elif tick == 2: if checkerlib.load_state('🔑ser') != [ 'Roger', '"Verbal"', 'Kint' ]: raise Exception('Did not get stored state back') elif self.team == 93: if tick == 1: if checkerlib.load_state('key1') is not None: raise Exception('Got state where there should be none') data = [{'number': 42}, {'number': 1337}] checkerlib.store_state('key1', data) checkerlib.set_flagid('value identifier') elif tick >= 2: if checkerlib.load_state('key1') != [{ 'number': 42 }, { 'number': 1337 }]: raise Exception('Did not get stored state back') return checkerlib.CheckResult.OK
def _get_key_for_flag(flag: str) -> str: """Returns a key for the given flag. If flag is new, a fresh key is generated.""" flag_keys = checkerlib.load_state("flag_keys") if not flag_keys: flag_keys = {} if flag in flag_keys.keys(): return flag_keys[flag] def _new_flag_key(): """Returns a new random key for flag trains.""" return str(random.randrange(10**10, 10**11)) new_key = _new_flag_key() while new_key in flag_keys.values(): new_key = _new_flag_key() flag_keys[flag] = new_key checkerlib.store_state("flag_keys", flag_keys) return new_key
def _get_new_key(self) -> str: """Returns a new key for general purpose.""" used_keys = checkerlib.load_state("used_keys") if not used_keys: used_keys = [] def _init_new_key(): """Returns a new random key.""" separator = random.choice([" ", "_", "-", ""]) return random.choice(self._words) + separator + str( random.randrange(10**3, 10**6)) def _valid_key(key): """Returns true, if key is a valid key.""" return key not in used_keys and len(key) <= 24 new_key = _init_new_key() while not _valid_key(new_key): new_key = _init_new_key() used_keys.append(new_key) checkerlib.store_state("used_keys", used_keys) return new_key
def test_state_object(self): self.assertIsNone(checkerlib.load_state('object')) obj = {'data': [b'foo', b'bar']} checkerlib.store_state('object', obj) self.assertEqual(checkerlib.load_state('object'), obj)
def test_state_primitive(self): self.assertIsNone(checkerlib.load_state('primitive')) checkerlib.store_state('primitive', 1337) self.assertEqual(checkerlib.load_state('primitive'), 1337)