Fix the loot pickup check for an online RPG
Your task
Your RPG's loot pickup endpoint wrote whatever item, rarity and gold the client sent into the player's inventory, so players granted themselves legendary items. The fix treats the request only as "this player wants this drop" and takes the reward from the server's own record of the drop.
An assistant wrote resolve_pickup(world_drops, request). It is in the editor. It has a bug. Find it and fix it.
Input and output:
world_drops: the server's drops, each"dropId|itemId|rarity|gold|claimed|eligiblePlayerId".claimedis"0"for a drop nobody has picked up.request: the client's request,"playerId|dropId|itemId|rarity|gold". The last three fields are never trusted.- Return
"granted|<itemId>|<rarity>|<gold>"with the values from the server's drop, or"rejected|<reason>".
Rules the tests check, in this order:
- The request must split on
|into exactly 5 fields, or return"rejected|bad_request". - An empty player id returns
"rejected|unauthenticated". - Find the first drop that splits into exactly 6 fields and whose drop id equals the request's. Skip drops that do not split into 6 fields. If none matches, return
"rejected|unknown_drop". - If that drop's
claimedfield is not exactly"0", return"rejected|already_claimed". - If its eligible player is not the requesting player, return
"rejected|not_eligible". - Otherwise return
"granted|"and the drop's item id, rarity and gold.
- All comparisons are exact.
Examples
resolve_pickup(world_drops: list[str], request: str) -> strExample 1
Input
resolve_pickup(["d1|sword_of_dawn|legendary|50|0|p1", "d2|health_potion|common|5|1|p2"], "p1|d1|sword_of_dawn|legendary|50")Output
"granted|sword_of_dawn|legendary|50"Example 2
Input
resolve_pickup(["d1|sword_of_dawn|legendary|50|0|p1", "d2|health_potion|common|5|1|p2"], "p1|d1|crown_of_gods|mythic|999999")Output
"granted|sword_of_dawn|legendary|50"