def test_string(self): s = """ mission = { ["trig"] = { ["actions"] = { [1] = "a_(get(\\"ResKey_Action_62\\")); mission.trig.func[1]=nil;", [2] = "a_do_script_file(getValueResourceByKey(\\"ResKey_Action_64\\")); mission.trig.func[2]=nil;", [3] = "a_out_text_delay(getValueDictByKey(\\"DictKey_ActionText_66\\"), 10, false);a_do_script(getValueDictByKey(\\"DictKey_ActionText_255\\")); mission.trig.func[3]=nil;", [4] = "a_add_radio_item(getValueDictByKey(\\"Destroy Red Ground Group\\"), 1500, 1); mission.trig.func[4]=nil;", [5] = "a_do_script(getValueDictByKey(\\"DictKey_ActionText_213\\"));a_clear_flag(1500);", }, -- end of ["actions"] } } """ r = loads(s) r = loads( 'x = "a_do_script_file(getValueResourceByKey(\\"ResKey_Action_62\\")); mission.trig.func[1]=nil;"' ) self.assertEqual( r, { "x": "a_do_script_file(getValueResourceByKey(\"ResKey_Action_62\")); mission.trig.func[1]=nil;" })
def test_missing_curly(self): with self.assertRaises(SyntaxError): loads("""t= { ["payload"] = { ["num"] = 1 } """)
def test_integer(self): r = loads("int = 3") self.assertEqual(r, {"int": 3}) r = loads("int = 193984") self.assertEqual(r, {"int": 193984}) r = loads("int = -23") self.assertEqual(r, {"int": -23})
def test_object_without_keys(self): luas = """ local unitPayloads = { ["name"] = "JF-17", ["payloads"] = { { ["name"] = "PL-5Ex2, C802AKx2, 800L Tank", ["pylons"] = { [1] = { ["CLSID"] = "DIS_C-802AK", ["num"] = 5 } } } } } """ ref = { 'unitPayloads': { 'name': "JF-17", 'payloads': { 1: { 'name': "PL-5Ex2, C802AKx2, 800L Tank", 'pylons': { 1: { 'CLSID': "DIS_C-802AK", 'num': 5 } } } } } } r = loads(luas) self.assertEqual(r, ref)
def test_object(self): luas = """ mission= { ["trig"]= { }, ["coalitions"]= { ["blue"]= { [1]=11, [2]=4, [3]=6 } }, -- end of ["coalitions"] ["maxDictId"]=18, ["descriptionBlueTask"]="DictKey_descriptionBlueTask_3" } """ ref = {'mission': { 'coalitions': {'blue': {1: 11.0, 2: 4.0, 3: 6.0}}, 'descriptionBlueTask': 'DictKey_descriptionBlueTask_3', 'trig': {}, 'maxDictId': 18.0}} r = loads(luas) self.assertEqual(r, ref)
def prepare(cls, game: Game) -> None: with open("resources/default_options.lua", "r") as f: options_dict = loads(f.read())["options"] cls._set_mission(Mission(game.theater.terrain)) cls.game = game cls._setup_mission_coalitions() cls.current_mission.options.load_from_dict(options_dict)
def parse(cls, path: str): dead_units = [] def append_dead_object(object_mission_id_str): nonlocal dead_units object_mission_id = int(object_mission_id_str) if object_mission_id in dead_units: logging.info("debriefing: failed to append_dead_object {}: already exists!".format(object_mission_id)) return dead_units.append(object_mission_id) def parse_dead_object(event): try: append_dead_object(event["initiatorMissionID"]) except Exception as e: logging.error(e) with open(path, "r") as f: table_string = f.read() try: table = parse.loads(table_string) except Exception as e: table = parse_mutliplayer_debriefing(table_string) events = table.get("debriefing", {}).get("events", {}) for event in events.values(): event_type = event.get("type", None) if event_type in ["crash", "dead"]: parse_dead_object(event) trigger_state = table.get("debriefing", {}).get("triggers_state", {}) return Debriefing(dead_units, trigger_state)
def parse(cls, path: str): dead_units = [] def append_dead_object(object_mission_id_str): nonlocal dead_units object_mission_id = int(object_mission_id_str) if object_mission_id in dead_units: logging.error( "debriefing: failed to append_dead_object {}: already exists!" .format(object_mission_id)) return dead_units.append(object_mission_id) def parse_dead_object(event): try: append_dead_object(event["initiatorMissionID"]) except KeyError: try: append_dead_object([ x['initiatorMissionID'] for x in event.values() if 'initiatorMissionID' in x ][0]) except Exception as e: logging.error(e) except Exception as e: logging.error(e) with open(path, "r") as f: table_string = f.read() try: table = parse.loads(table_string) except Exception as e: table = parse_mutliplayer_debriefing(table_string) try: events = table["debriefing"].get("events", {}) except KeyError: events = table.get('events', {}) for event in events.values(): try: event_type = event["type"] except KeyError: # this means we've encountered a different version of the parsed debrief. This means we must search # for the key we want :| try: event_type = [ x['type'] for x in event.values() if 'type' in x ][0] except Exception as e: print("Failed to parse event - {} ({})".format( event, e)) if event_type in ["crash", "dead"]: parse_dead_object(event) trigger_state = table.get("debriefing", {}).get("triggers_state", {}) return Debriefing(dead_units, trigger_state)
def test_string(self): s = """ mission = { ["trig"] = { ["actions"] = { [1] = "a_(get(\\"ResKey_Action_62\\")); mission.trig.func[1]=nil;", [2] = "a_do_script_file(getValueResourceByKey(\\"ResKey_Action_64\\")); mission.trig.func[2]=nil;", [3] = "a_out_text_delay(getValueDictByKey(\\"DictKey_ActionText_66\\"), 10, false);a_do_script(getValueDictByKey(\\"DictKey_ActionText_255\\")); mission.trig.func[3]=nil;", [4] = "a_add_radio_item(getValueDictByKey(\\"Destroy Red Ground Group\\"), 1500, 1); mission.trig.func[4]=nil;", [5] = "a_do_script(getValueDictByKey(\\"DictKey_ActionText_213\\"));a_clear_flag(1500);", }, -- end of ["actions"] } } """ r = loads(s) r = loads('x = "a_do_script_file(getValueResourceByKey(\\"ResKey_Action_62\\")); mission.trig.func[1]=nil;"') self.assertEqual(r, {"x": "a_do_script_file(getValueResourceByKey(\"ResKey_Action_62\")); mission.trig.func[1]=nil;"})
def test_mixed_objects(self): luas = """ o = { "x", ["a"]=2, "y" } """ ref = {'o': {1: "x", "a": 2, 2: "y"}} r = loads(luas) self.assertEqual(r, ref)
def prepare(self, terrain: Terrain, is_quick: bool): with open("resources/default_options.lua", "r") as f: options_dict = loads(f.read())["options"] self.current_mission = dcs.Mission(terrain) print(self.game.player_country) print(country_dict[db.country_id_from_name(self.game.player_country)]) print(country_dict[db.country_id_from_name( self.game.player_country)]()) # Setup coalition : self.current_mission.coalition["blue"] = Coalition("blue") self.current_mission.coalition["red"] = Coalition("red") if self.game.player_country and self.game.player_country in db.BLUEFOR_FACTIONS: self.current_mission.coalition["blue"].add_country( country_dict[db.country_id_from_name( self.game.player_country)]()) self.current_mission.coalition["red"].add_country( country_dict[db.country_id_from_name( self.game.enemy_country)]()) else: self.current_mission.coalition["blue"].add_country( country_dict[db.country_id_from_name( self.game.enemy_country)]()) self.current_mission.coalition["red"].add_country( country_dict[db.country_id_from_name( self.game.player_country)]()) print([ c for c in self.current_mission.coalition["blue"].countries.keys() ]) print([ c for c in self.current_mission.coalition["red"].countries.keys() ]) if is_quick: self.quick_mission = self.current_mission else: self.regular_mission = self.current_mission self.current_mission.options.load_from_dict(options_dict) self.is_quick = is_quick if is_quick: self.attackers_starting_position = None self.defenders_starting_position = None else: self.attackers_starting_position = self.departure_cp.at self.defenders_starting_position = self.to_cp.at
def test_dictmix(self): luas = """ o = { ["callsign"] = { [1] = 1, [2] = 1, [3] = 1, ["name"] = "Enfield11", } -- end of ["callsign"] } """ ref = {"o": {'callsign': {1: 1, 2: 1, 3: 1, "name": "Enfield11"}}} r = loads(luas) self.assertEqual(r, ref)
def prepare(self, terrain: Terrain, is_quick: bool): with open("resources/default_options.lua", "r") as f: options_dict = loads(f.read())["options"] self.current_mission = dcs.Mission(terrain) if is_quick: self.quick_mission = self.current_mission else: self.regular_mission = self.current_mission self.current_mission.options.load_from_dict(options_dict) self.is_quick = is_quick if is_quick: self.attackers_starting_position = None self.defenders_starting_position = None else: self.attackers_starting_position = self.departure_cp.at self.defenders_starting_position = self.to_cp.at
def test_dictmix(self): luas = """ o = { ["callsign"] = { [1] = 1, [2] = 1, [3] = 1, ["name"] = "Enfield11", } -- end of ["callsign"] } """ ref = {"o": { 'callsign': { 1: 1, 2: 1, 3: 1, "name": "Enfield11" } }} r = loads(luas) self.assertEqual(r, ref)
def test_object(self): luas = """ mission= { ["trig"]= { }, ["coalitions"]= { ["blue"]= { [1]=11, [2]=4, [3]=6 } }, -- end of ["coalitions"] ["maxDictId"]=18, ["descriptionBlueTask"]="DictKey_descriptionBlueTask_3" } """ ref = { 'mission': { 'coalitions': { 'blue': { 1: 11.0, 2: 4.0, 3: 6.0 } }, 'descriptionBlueTask': 'DictKey_descriptionBlueTask_3', 'trig': {}, 'maxDictId': 18.0 } } r = loads(luas) self.assertEqual(r, ref)
def test_decimal(self): r = loads("dec = 666.6") self.assertEqual(r, {"dec": 666.6})
def test_exponent(self): r = loads("dec = 666.6e10") self.assertEqual(r, {"dec": 666.6e10}) r = loads("dec = 666.6e-10") self.assertEqual(r, {"dec": 666.6e-10})
def test_syntaxerr(self): with self.assertRaises(SyntaxError): loads("""m= { ["x"] 12 }""")
def test_payload_var_ref(self): luas = """ local pylon_1A,pylon_1B,pylon_2,pylon_3,pylon_4,pylon_5,pylon_6,pylon_7,pylon_8B,pylon_8A = 1,2,3,4,5,6,7,8,9,10 local unitPayloads = { ["name"] = "F-14B", ["payloads"] = { { ["name"] = "XT*2", ["pylons"] = { [1] = { ["CLSID"] = "{F14-300gal}", ["num"] = pylon_7, }, [2] = { ["CLSID"] = "{F14-300gal}", ["num"] = pylon_2, }, }, ["tasks"] = { [1] = Intercept, [2] = CAP, [3] = Escort, [4] = FighterSweep, }, }, { ["name"] = "AIM-54A-MK47*6, AIM-9M*2, XT*2", ["pylons"] = { [1] = { ["CLSID"] = "{LAU-138 wtip - AIM-9M}", ["num"] = pylon_8A, }, [2] = { ["CLSID"] = "{SHOULDER AIM_54A_Mk47 R}", ["num"] = pylon_8B, }, [3] = { ["CLSID"] = "{F14-300gal}", ["num"] = pylon_7, }, [4] = { ["CLSID"] = "{AIM_54A_Mk47}", ["num"] = pylon_6, }, [5] = { ["CLSID"] = "{AIM_54A_Mk47}", ["num"] = pylon_5, }, [6] = { ["CLSID"] = "{AIM_54A_Mk47}", ["num"] = pylon_4, }, [7] = { ["CLSID"] = "{AIM_54A_Mk47}", ["num"] = pylon_3, }, [8] = { ["CLSID"] = "{F14-300gal}", ["num"] = pylon_2, }, [9] = { ["CLSID"] = "{SHOULDER AIM_54A_Mk47 L}", ["num"] = pylon_1B, }, [10] = { ["CLSID"] = "{LAU-138 wtip - AIM-9M}", ["num"] = pylon_1A, }, }, ["tasks"] = { [1] = Intercept, [2] = CAP, [3] = Escort, [4] = FighterSweep, }, }, }, ["unitType"] = "F-14B", } return unitPayloads """ r = loads( luas, { 'Intercept': 'Intercept', 'CAP': 'CAP', 'Escort': 'Escort', 'FighterSweep': 'FighterSweep' }) self.assertEqual(r['unitPayloads']['name'], 'F-14B') self.assertEqual(r['unitPayloads']['payloads'][1]['name'], "XT*2") self.assertEqual(r['unitPayloads']['payloads'][1]['pylons'][1]['num'], 8) self.assertEqual(r['unitPayloads']['payloads'][2]['name'], "AIM-54A-MK47*6, AIM-9M*2, XT*2") self.assertEqual(r['unitPayloads']['payloads'][2]['tasks'][1], "Intercept")