boardgame
boardgame copied to clipboard
{Int,Bool,Float,String,PlayerIndex}EnumMap object
As an allowed property type in PropertyReader.
Would have map-like semantics. Useful for e.g. a computed property of which regions in a board are "active".
Originally inspired thinking through #476
type EnumMap struct {
data map[string]<TYPE>
enum *enum.Enum
}
func (e *EnumMap) Get(val *enum.Const) <TYPE> {
//Checks if the val is in the enum otherwise err
}
func (e *EnumMap) Set(val *enum.Const, data <TYPE>) {
//Fails if enum.Const not member of our enum
//Disabled if in read-only mode
}
func (e *EnumMap) Has(val *enum.Const) bool {
}
This might also be useful for #491 (groups for sanitization policy)
Isn't a (non-sparse) EnumMap just a TYPESlice where the raw enum values are used to get the keys?
I guess it comes down to how easy that is to do clientside
Useful for the pointCard of commoditytrader
Also need Immutable variant, for use in e.g. components
Although there are needs for this to also be Stack variants (for example, GoldBowl, SilverBowl, etc in metaltrader). But that obviously basically doubles number of types on property reader
Instead of having a million property types, we have one: EnumMap. And under the covers they are one of a concrete type, but all implement EnumMap and have up-converters. And then you're supposed to embed the actual type you want
All of this is done in a sub-package called 'map', similar to graph.
func (e *Enum) BoolEnumMap() BoolEnumMap {
//returns a new empty BoolEnumMap that is associated with this Enum.
}
func (e *Enum) IntEnumMap() IntEnumMap {
}
type EnumMap interface {
//the enum this is associate with, who lists the legal values
Enum() enum.Enum
GetGeneric(key enum.ImmutableVal) (interface{}, bool)
//Will error if the key isn't in enum, or if the value is not a legal type for the underlying type
SetGeneric(key enum.ImmutableVal, value interface {}) error
//Returns a COPY of the underlying data as a generic map
RawGenericMap() map[int]interface{}
//Copies from the other EnumMap. This is how the engine copies from one to the other. Will error if they havedifferent underlying types.
CopyFrom(other EnumMap) error
//Returns a non-nil BoolEnumMap if this EnumMap has a type compatible with that.
//Only one of these typed getters will return a non-nil value.
BoolEnumMap() BoolEnumMap
IntEnumMap() IntEnumMap
}
//Just doing one typed example but there'd be one per
type BoolEnumMap interface {
EnumMap
Get(key enum.ImmutableVal) (bool, bool)
Set(key enum.ImmutableVal, val bool) error
RawMap() map[int]bool
}
There are no such things as EnumMaps that have stacks as values. That's because the number of stacks has to be (somewhat) fixed at all times, otherwise you could move a component to a stack and then get rid of it. (Wait, but you can't do that currently, right?)
If you want an EnumMap of stacks, instead create a Board, and then use the index of "spaces" based on the int values of the enum.
- [ ] Create raw enum/map code
- [ ] Sanitize machinery
- [ ] Codegen machinery
- [ ] Struct tags machinery
- [ ] PropertyType
- [ ] Test in everything
- [ ] When these exist, gameDelegate.SanitizationPolicy (and all related methods that take or return map[int]string) should use enum maps. (See also #491)