makhidkarun / traveller_pyroute

Traveller trade route generator
MIT License
14 stars 5 forks source link

Fencepost in ehex-to-int mapping #37

Closed CyberiaResurrection closed 1 year ago

CyberiaResurrection commented 1 year ago

While I'm on a roll digging through dust-caked sections of the codebase, I put together the following unit test in testStar.py:

    def test_ehex_to_numeric_mapping_for_TL_A_and_up(self):
        # shut up log output - is completely irrelevant
        logger = logging.getLogger('PyRoute.Star')
        logger.setLevel(logging.CRITICAL)

        # taken from https://travellermap.com/doc/secondsurvey#ehex
        ehex_map = [
            ('A', 10), ('B', 11), ('C', 12), ('D', 13), ('E', 14), ('F', 15), ('G', 16), ('H', 17), ('J', 18), ('K', 19),
            ('L', 20), ('M', 21), ('N', 22), ('P', 23), ('Q', 24), ('R', 25), ('S', 26), ('T', 27), ('U', 28), ('V', 29),
            ('W', 30), ('X', 31), ('Y', 32), ('Z', 33)
        ]

        for chunk in ehex_map:
            with self.subTest():
                letter = chunk[0]
                target = chunk[1]

                star1 = Star.parse_line_into_star(
                    "0104 Shana Ma             E551112-"+letter+" Lo Po                { -3 } (301-3) [1113] B     - - 913 9  Im K2 IV M7 V     ",
                    Sector(' Core', ' 0, 0'), 'fixed', 'fixed')

                self.assertEqual(target, star1.tl, "Unexpected mapping for TL " + letter)

TL P maps as 24, not 23 (as https://travellermap.com/doc/secondsurvey#ehex states), TL Q maps as 25, not 24, and so on up tl TL Z mapping as 34, not 33.

Following change appears to fix it:

    def _ehex_to_int(self, value):
        val = int(value, 36) if value in '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' else 0
        val -= 1 if val > 18 else 0
+        val -= 1 if val > 22 else 0
        return val
CyberiaResurrection commented 1 year ago

@tjoneslo , is it worth worrying about the excluded codes (I and O) specifically?