ap_manual 0.2.0

A rust package to cannonically interact with manual AP worlds
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import logging
import re
import json
from worlds.AutoWorld import World
from BaseClasses import MultiWorld, ItemClassification


class ValidationError(Exception):
    pass

class DataValidation():
    game_table = {}
    item_table = []
    location_table = []
    region_table = {}


    @staticmethod
    def checkItemNamesInLocationRequires():
        for location in DataValidation.location_table:
            if "requires" not in location:
                continue

            if isinstance(location["requires"], str):
                # parse user written statement into list of each item
                for item in re.findall(r'\|[^|]+\|', location["requires"]):
                    if item.lower() == "or" or item.lower() == "and" or item == ")" or item == "(":
                        continue
                    else:
                        # if it's a category, validate that the category exists
                        if '@' in item:
                            item = item.replace("|", "")
                            item_parts = item.split(":")
                            item_name = item

                            if len(item_parts) > 1:
                                item_name = item_parts[0]

                            item_name = item_name[1:]
                            item_category_exists = len([item for item in DataValidation.item_table if item_name in item.get('category', [])]) > 0

                            if not item_category_exists:
                                raise ValidationError("Item category %s is required by location %s but is misspelled or does not exist." % (item_name, location["name"]))

                            continue

                        item = item.replace("|", "")

                        item_parts = item.split(":")
                        item_name = item

                        if len(item_parts) > 1:
                            item_name = item_parts[0]

                        item_exists = len([item["name"] for item in DataValidation.item_table if item["name"] == item_name]) > 0

                        if not item_exists:
                            raise ValidationError("Item %s is required by location %s but is misspelled or does not exist." % (item_name, location["name"]))

            else:  # item access is in dict form
                for item in location["requires"]:
                    # if the require entry is an object with "or" or a list of items, treat it as a standalone require of its own
                    if (isinstance(item, dict) and "or" in item and isinstance(item["or"], list)) or (isinstance(item, list)):
                        or_items = item

                        if isinstance(item, dict):
                            or_items = item["or"]

                        for or_item in or_items:
                            or_item_parts = or_item.split(":")
                            or_item_name = or_item

                            if len(or_item_parts) > 1:
                                or_item_name = or_item_parts[0]

                            item_exists = len([item["name"] for item in DataValidation.item_table if item["name"] == or_item_name]) > 0

                            if not item_exists:
                                raise ValidationError("Item %s is required by location %s but is misspelled or does not exist." % (or_item_name, location["name"]))
                    else:
                        item_parts = item.split(":")
                        item_name = item

                        if len(item_parts) > 1:
                            item_name = item_parts[0]

                        item_exists = len([item["name"] for item in DataValidation.item_table if item["name"] == item_name]) > 0

                        if not item_exists:
                            raise ValidationError("Item %s is required by location %s but is misspelled or does not exist." % (item_name, location["name"]))

    @staticmethod
    def checkItemNamesInRegionRequires():
        for region_name in DataValidation.region_table:
            region = DataValidation.region_table[region_name]

            if "requires" not in region:
                continue

            if isinstance(region["requires"], str):
                # parse user written statement into list of each item
                for item in re.findall(r'\|[^|]+\|', region["requires"]):
                    if item.lower() == "or" or item.lower() == "and" or item == ")" or item == "(":
                        continue
                    else:
                        # if it's a category, validate that the category exists
                        if '@' in item:
                            item = item.replace("|", "")
                            item_parts = item.split(":")
                            item_name = item

                            if len(item_parts) > 1:
                                item_name = item_parts[0]

                            item_name = item_name[1:]
                            item_category_exists = len([item for item in DataValidation.item_table if item_name in item.get('category', [])]) > 0

                            if not item_category_exists:
                                raise ValidationError("Item category %s is required by region %s but is misspelled or does not exist." % (item_name, region_name))

                            continue

                        item = item.replace("|", "")

                        item_parts = item.split(":")
                        item_name = item

                        if len(item_parts) > 1:
                            item_name = item_parts[0]

                        item_exists = len([item["name"] for item in DataValidation.item_table if item["name"] == item_name]) > 0

                        if not item_exists:
                            raise ValidationError("Item %s is required by region %s but is misspelled or does not exist." % (item_name, region_name))

            else:  # item access is in dict form
                for item in region["requires"]:
                    # if the require entry is an object with "or" or a list of items, treat it as a standalone require of its own
                    if (isinstance(item, dict) and "or" in item and isinstance(item["or"], list)) or (isinstance(item, list)):
                        or_items = item

                        if isinstance(item, dict):
                            or_items = item["or"]

                        for or_item in or_items:
                            or_item_parts = or_item.split(":")
                            or_item_name = or_item

                            if len(or_item_parts) > 1:
                                or_item_name = or_item_parts[0]

                            item_exists = len([item["name"] for item in DataValidation.item_table if item["name"] == or_item_name]) > 0

                            if not item_exists:
                                raise ValidationError("Item %s is required by region %s but is misspelled or does not exist." % (or_item_name, region_name))
                    else:
                        item_parts = item.split(":")
                        item_name = item

                        if len(item_parts) > 1:
                            item_name = item_parts[0]

                        item_exists = len([item["name"] for item in DataValidation.item_table if item["name"] == item_name]) > 0

                        if not item_exists:
                            raise ValidationError("Item %s is required by region %s but is misspelled or does not exist." % (item_name, region_name))

    @staticmethod
    def checkRegionNamesInLocations():
        for location in DataValidation.location_table:
            if "region" not in location or location["region"] in ["Menu", "Manual"]:
                continue

            region_exists = len([name for name in DataValidation.region_table if name == location["region"]]) > 0

            if not region_exists:
                raise ValidationError("Region %s is set for location %s, but the region is misspelled or does not exist." % (location["region"], location["name"]))

    @staticmethod
    def checkItemsThatShouldBeRequired():
        for item in DataValidation.item_table:
            # if the item is already progression, no need to check
            if "progression" in item and item["progression"]:
                continue

            # progression_skip_balancing is also progression, so no check needed
            if "progression_skip_balancing" in item and item["progression_skip_balancing"]:
                continue

            # check location requires for the presence of item name
            for location in DataValidation.location_table:
                if "requires" not in location:
                    continue

                # convert to json so we don't have to guess the data type
                location_requires = json.dumps(location["requires"])

                # if boolean, else legacy
                if isinstance(location_requires, str):
                    if '|{}|'.format(item["name"]) in location_requires:
                        raise ValidationError("Item %s is required by location %s, but the item is not marked as progression." % (item["name"], location["name"]))
                else:
                    if item["name"] in location_requires:
                        raise ValidationError("Item %s is required by location %s, but the item is not marked as progression." % (item["name"], location["name"]))

            # check region requires for the presence of item name
            for region_name in DataValidation.region_table:
                region = DataValidation.region_table[region_name]

                if "requires" not in region:
                    continue

                # convert to json so we don't have to guess the data type
                region_requires = json.dumps(region["requires"])

                # if boolean, else legacy
                if isinstance(region_requires, str):
                    if '|{}|'.format(item["name"]) in region_requires:
                        raise ValidationError("Item %s is required by region %s, but the item is not marked as progression." % (item["name"], region_name))
                else:
                    if item["name"] in region_requires:
                        raise ValidationError("Item %s is required by region %s, but the item is not marked as progression." % (item["name"], region_name))

    @staticmethod
    def _checkLocationRequiresForItemValueWithRegex(values_requested: dict[str, int], requires) -> dict[str, int]:
        if isinstance(requires, str) and 'ItemValue' in requires:
            for result in re.findall(r'\{ItemValue\(([^:]*)\:(.*?)\)\}', requires):
                value = result[0].lower().strip()
                count = int(result[1].split(",")[0])
                if not values_requested.get(value):
                    values_requested[value] = count
                else:
                    values_requested[value] = max(values_requested[value], count)
        return values_requested


    @staticmethod
    def preFillCheckIfEnoughItemsForValue(world: World, multiworld: MultiWorld):
        from .Helpers import get_items_with_value, get_items_for_player, filter_used_regions
        player = world.player
        values_requested = {}
        player_regions = []

        #Grab all the player's regions
        for region in multiworld.regions:
            if region.player != player:
                continue
            player_regions.append(region)

        used_regions = filter_used_regions(player_regions)
        used_regions_names = {r.name for r in set(used_regions)}

        #Check used regions (and their parent(s)) for ItemValue requirement
        for region in used_regions:
            manualregion = DataValidation.region_table.get(region.name, {})
            if manualregion:
                if manualregion.get("requires"):
                    DataValidation._checkLocationRequiresForItemValueWithRegex(values_requested, json.dumps(manualregion["requires"]))

                for region_entrance, require in manualregion.get('entrance_requires', {}).items():
                    if region_entrance in used_regions_names:
                        DataValidation._checkLocationRequiresForItemValueWithRegex(values_requested, json.dumps(require))

                for region_exit, require in manualregion.get('exit_requires', {}).items():
                    if region_exit in used_regions_names:
                        DataValidation._checkLocationRequiresForItemValueWithRegex(values_requested, json.dumps(require))

            for location in region.locations:
                manualLocation = world.location_name_to_location.get(location.name, {})
                if "requires" in manualLocation and manualLocation["requires"]:
                    DataValidation._checkLocationRequiresForItemValueWithRegex(values_requested, json.dumps(manualLocation["requires"]))

        # compare whats available vs requested but only if there's anything requested
        if values_requested:
            errors = []
            existing_items = [item for item in get_items_for_player(multiworld, player, True) if
                              item.code is not None and ItemClassification.progression in item.classification]
            for value, val_count in values_requested.items():
                items_value = get_items_with_value(world, multiworld, value, player)
                found_count = 0
                if items_value:
                    for item in existing_items:
                        if item.name in items_value:
                            found_count += items_value[item.name]

                if found_count < val_count:
                    errors.append(f"   '{value}': {found_count} out of the {val_count} {value} worth of progression items required can be found.")
            if errors:
                raise ValidationError("There are not enough progression items for the following value(s): \n" + "\n".join(errors))

    @staticmethod
    def checkRegionsConnectingToOtherRegions():
        for region_name in DataValidation.region_table:
            region = DataValidation.region_table[region_name]

            if "connects_to" not in region:
                continue

            for connecting_region in region["connects_to"]:
                region_exists = len([name for name in DataValidation.region_table if name == connecting_region]) > 0

                if not region_exists:
                    raise ValidationError("Region %s connects to a region %s, which is misspelled or does not exist." % (region_name, connecting_region))

    @staticmethod
    def checkForDuplicateItemNames():
        for item in DataValidation.item_table:
            name_count = len([i for i in DataValidation.item_table if i["name"] == item["name"]])

            if name_count > 1:
                raise ValidationError("Item %s is defined more than once." % (item["name"]))

    @staticmethod
    def checkForDuplicateLocationNames():
        for location in DataValidation.location_table:
            name_count = len([l for l in DataValidation.location_table if l["name"] == location["name"]])

            if name_count > 1:
                raise ValidationError("Location %s is defined more than once." % (location["name"]))

    @staticmethod
    def checkForDuplicateRegionNames():
        # this currently does nothing because the region name is a dict key, which will never be non-unique / limited to 1
        for region_name in DataValidation.region_table:
            name_count = len([r for r in DataValidation.region_table if r == region_name])

            if name_count > 1:
                raise ValidationError("Region %s is defined more than once." % (region_name))

    @staticmethod
    def checkStartingItemsForValidItemsAndCategories():
        if "starting_items" not in DataValidation.game_table:
            return

        starting_items = DataValidation.game_table["starting_items"]

        for starting_block in starting_items:
            if "items" in starting_block and "item_categories" in starting_block:
                raise ValidationError("One of your starting item definitions has both 'items' and 'item_categories' defined, but only one will be applied.")

            if "items" in starting_block:
                for item_name in starting_block["items"]:
                    if not item_name in [item["name"] for item in DataValidation.item_table]:
                        raise ValidationError("Item %s is set as a starting item, but is misspelled or is not defined." % (item_name))

            if "item_categories" in starting_block:
                for category_name in starting_block["item_categories"]:
                    if len([item for item in DataValidation.item_table if "category" in item and category_name in item["category"]]) == 0:
                        raise ValidationError("Item category %s is set as a starting item category, but is misspelled or is not defined on any items." % (category_name))

    @staticmethod
    def checkStartingItemsForBadSyntax():
        if not (starting_items := DataValidation.game_table.get("starting_items", False)):
            return

        for starting_block in starting_items:
            if type(starting_block) is not dict or len(starting_block.keys()) == 0:
                raise ValidationError("One of your starting item definitions is not a valid dictionary.\n   Each definition must be inside {}, as demonstrated in the Manual documentation.")

            valid_keys = ["items", "item_categories", "random", "if_previous_item", "_comment", "yaml_option"] # _comment is provided by schema
            invalid_keys = [f'"{key}"' for key in starting_block.keys() if key not in valid_keys]

            if len(invalid_keys) > 0:
                raise ValidationError("One of your starting item definitions is invalid and may have unexpected results.\n   The invalid starting item definition specifies the following incorrect keys: {}".format(", ".join(invalid_keys)))

    @staticmethod
    def checkPlacedItemsAndCategoriesForBadSyntax():
        for location in DataValidation.location_table:
            place_item = location.get("place_item", False)
            place_item_category = location.get("place_item_category", False)

            if not place_item and not place_item_category:
                continue

            if place_item and type(place_item) is not list:
                raise ValidationError("One of your location has an incorrectly formatted place_item.\n   The items, even just one, must be inside [].")

            if place_item_category and type(place_item_category) is not list:
                raise ValidationError("One of your location has an incorrectly formatted place_item_category.\n   The categories, even just one, must be inside [].")

    @staticmethod
    def checkPlacedItemsForValidItems():
        for location in DataValidation.location_table:
            if not (place_item := location.get("place_item", False)):
                continue

            # don't bother checking for valid items if the syntax is wrong
            if type(place_item) is not list:
                continue

            for item_name in place_item:
                if not item_name in [item["name"] for item in DataValidation.item_table]:
                    raise ValidationError("Item %s is placed (using place_item) on a location, but is misspelled or is not defined." % (item_name))

    @staticmethod
    def checkPlacedItemCategoriesForValidItemCategories():
        for location in DataValidation.location_table:
            if not (place_item_category := location.get("place_item_category", False)):
                continue

            # don't bother checking for valid item categories if the syntax is wrong
            if type(place_item_category) is not list:
                continue

            for category_name in place_item_category:
                if len([item for item in DataValidation.item_table if "category" in item and category_name in item["category"]]) == 0:
                    raise ValidationError("Item category %s is placed (using place_item_category) on a location, but is misspelled or is not defined." % (category_name))

    @staticmethod
    def checkForGameBeingInvalidJSON():
        if len(DataValidation.game_table) == 0:
            raise ValidationError("No settings were found in your game.json. This likely indicates that your JSON is incorrectly formatted. Use https://jsonlint.com/ to validate your JSON files.")

    @staticmethod
    def checkForItemsBeingInvalidJSON():
        if len(DataValidation.item_table) == 0:
            raise ValidationError("No items were found in your items.json. This likely indicates that your JSON is incorrectly formatted. Use https://jsonlint.com/ to validate your JSON files.")

    @staticmethod
    def checkForLocationsBeingInvalidJSON():
        if len(DataValidation.location_table) == 0:
            raise ValidationError("No locations were found in your locations.json. This likely indicates that your JSON is incorrectly formatted. Use https://jsonlint.com/ to validate your JSON files.")

    @staticmethod
    def checkForNonStartingRegionsThatAreUnreachable():
        using_starting_regions = len([region for region in DataValidation.region_table if DataValidation.region_table[region].get("starting")]) > 0

        if not using_starting_regions:
            return

        nonstarting_regions = [region for region in DataValidation.region_table if not DataValidation.region_table[region].get("starting")]

        for nonstarter in nonstarting_regions:
            regions_that_connect_to = [region for region in DataValidation.region_table if "connects_to" in DataValidation.region_table[region] and nonstarter in DataValidation.region_table[region]["connects_to"]]

            if len(regions_that_connect_to) == 0:
                raise ValidationError("The region '%s' is set as a non-starting region, but has no regions that connect to it. It will be inaccessible." % nonstarter)


def runPreFillDataValidation(world: World, multiworld: MultiWorld):
    validation_errors = []

    # check if there is enough items with values
    try: DataValidation.preFillCheckIfEnoughItemsForValue(world, multiworld)
    except ValidationError as e: validation_errors.append(e)

    if validation_errors:
        heading = f"ValidationError(s) for pre_fill of {world.game}:";
        newline = "\n"
        raise Exception(f"\n\n{heading} \n\n{newline.join([' - ' + str(validation_error) for validation_error in validation_errors])}\n\n")

# Called during stage_assert_generate
def runGenerationDataValidation(cls) -> None:
    validation_errors = []

    # check that requires have correct item names in locations and regions
    try: DataValidation.checkItemNamesInLocationRequires()
    except ValidationError as e: validation_errors.append(e)

    try: DataValidation.checkItemNamesInRegionRequires()
    except ValidationError as e: validation_errors.append(e)

    # check that region names are correct in locations
    try: DataValidation.checkRegionNamesInLocations()
    except ValidationError as e: validation_errors.append(e)

    # check that items that are required by locations and regions are also marked required
    try: DataValidation.checkItemsThatShouldBeRequired()
    except ValidationError as e: validation_errors.append(e)

    # check that regions that are connected to are correct
    try: DataValidation.checkRegionsConnectingToOtherRegions()
    except ValidationError as e: validation_errors.append(e)

    # check for duplicate names in items, locations, and regions
    try: DataValidation.checkForDuplicateItemNames()
    except ValidationError as e: validation_errors.append(e)

    try: DataValidation.checkForDuplicateLocationNames()
    except ValidationError as e: validation_errors.append(e)

    try: DataValidation.checkForDuplicateRegionNames()
    except ValidationError as e: validation_errors.append(e)

    # check that starting items are actually valid starting item definitions
    try: DataValidation.checkStartingItemsForBadSyntax()
    except ValidationError as e: validation_errors.append(e)

    # check that starting items and starting item categories actually exist in the items json
    try: DataValidation.checkStartingItemsForValidItemsAndCategories()
    except ValidationError as e: validation_errors.append(e)

    # check that placed items are actually valid place item definitions
    try: DataValidation.checkPlacedItemsAndCategoriesForBadSyntax()
    except ValidationError as e: validation_errors.append(e)

    # check placed item and item categories for valid options for each
    try: DataValidation.checkPlacedItemsForValidItems()
    except ValidationError as e: validation_errors.append(e)

    try: DataValidation.checkPlacedItemCategoriesForValidItemCategories()
    except ValidationError as e: validation_errors.append(e)

    # check for regions that are set as non-starting regions and have no connectors to them (so are unreachable)
    try: DataValidation.checkForNonStartingRegionsThatAreUnreachable()
    except ValidationError as e: validation_errors.append(e)

    if len(validation_errors) > 0:
        heading = f"ValidationError(s) in {cls.game}:";

        raise Exception("\n\n%s \n\n%s\n\n" % (heading, "\n".join([' - ' + str(validation_error) for validation_error in validation_errors])))