Due to the KCS API being a clusterfuck of all kinds of madness, we can't use a single, simple lens that will normalize the data straight up. Good thing is: this state is always specified as an array of either numbers, or objects.
Luckily, the partial.lenses
library offers the L.choose
function, that can be used to
create optics based on the underlying view.
The result we want is something like this:
const resource = { id: 1, type: 'fuel', value: 420 };
If the state is a simple list of numbers, we can rewrite this list (in all cases but one,
more or less) to use any index i
of the list to represent a resource r
that's i + 1
(see
table below for resource types).
However, if the state is a list of objects, we will map the can just rewrite the objects to conform
to the desired object, by rewriting the object and map the resource type to the id
specified in the object.
To decide on which transformation to use, we are just going to assume the list consists always of items of
a single type, and check the type of the list's head
.
To be on the safe side and assume normal operation, in case the head
of the list is of a type we haven't specified
a transformation function to, we'll simply return R.identity
as the default fallthrough.
ID | Type |
---|---|
1 | Fuel |
2 | Ammo |
3 | Steel |
4 | Bauxite |
5 | Instant repair (buckets) |
6 | Construction materials |
7 | Instant construction material (flamethrowers) |
8 | Modernization material (screws) |