Fix the versioned save reader for an RPG
Your task
Your RPG wrote saves field by field with no version and no item count. When a patch added one field, every old save loaded with a broken inventory. The new format starts with a version number and counts the inventory, so the reader knows where each part ends.
An assistant wrote read_save_record(record). It is in the editor. It has a bug. Find it and fix it.
Input and output:
record: a save, as comma-separated integers: version, level, health, item count, that many item ids, gold, and for version 2 only, mana.- Return
["level=<n>", "health=<n>", "inventory=<ids joined by ;>", "gold=<n>", "mana=<n>"], or a single error string.
Rules the tests check, in this order:
- An empty record is
["error=corrupt"]. Split the rest on,. Fewer than 5 parts is["error=corrupt"]. - Every part must be an integer: an optional
-, then one or more ASCII digits. Otherwise["error=corrupt"]. - A version below 1 is
["error=corrupt"]. A version above 2 is["error=unsupported_version"]. - A negative item count is
["error=corrupt"]. - The record must have exactly 5 parts plus the item count for version 1, or exactly 6 parts plus the item count for version 2. Any other length is
["error=corrupt"].
- Write every number in the output in plain form, as the integer it parses to:
007becomes7and-0becomes0. - A version 1 save has no mana field, so mana is 0.
Examples
read_save_record(record: str) -> list[str]Example 1
Input
read_save_record("2,5,100,3,11,12,13,250,40")Output
["level=5", "health=100", "inventory=11;12;13", "gold=250", "mana=40"]Example 2
Input
read_save_record("1,5,100,3,11,12,13,250")Output
["level=5", "health=100", "inventory=11;12;13", "gold=250", "mana=0"]