84 lines
2.1 KiB
Go
84 lines
2.1 KiB
Go
package nkode
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"go-nkode/core/model"
|
|
"go-nkode/util"
|
|
)
|
|
|
|
type CustomerAttributes struct {
|
|
attrVals []uint64
|
|
setVals []uint64
|
|
}
|
|
|
|
func NewCustomerAttributes() (*CustomerAttributes, error) {
|
|
attrVals, errAttr := util.GenerateRandomNonRepeatingUint64(m.KeypadMax.TotalAttrs())
|
|
if errAttr != nil {
|
|
return nil, errAttr
|
|
}
|
|
setVals, errSet := util.GenerateRandomNonRepeatingUint64(m.KeypadMax.AttrsPerKey)
|
|
if errSet != nil {
|
|
return nil, errSet
|
|
}
|
|
|
|
customerAttrs := CustomerAttributes{
|
|
attrVals: attrVals,
|
|
setVals: setVals,
|
|
}
|
|
return &customerAttrs, nil
|
|
}
|
|
|
|
func (c *CustomerAttributes) Renew() error {
|
|
attrVals, errAttr := util.GenerateRandomNonRepeatingUint64(m.KeypadMax.TotalAttrs())
|
|
if errAttr != nil {
|
|
return errAttr
|
|
}
|
|
setVals, errSet := util.GenerateRandomNonRepeatingUint64(m.KeypadMax.AttrsPerKey)
|
|
if errSet != nil {
|
|
return errSet
|
|
}
|
|
c.attrVals = attrVals
|
|
c.setVals = setVals
|
|
return nil
|
|
}
|
|
|
|
func (c *CustomerAttributes) IndexOfAttr(attrVal uint64) int {
|
|
// TODO: should this be mapped instead?
|
|
return util.IndexOf[uint64](c.attrVals, attrVal)
|
|
}
|
|
|
|
func (c *CustomerAttributes) IndexOfSet(setVal uint64) (int, error) {
|
|
// TODO: should this be mapped instead?
|
|
idx := util.IndexOf[uint64](c.setVals, setVal)
|
|
if idx == -1 {
|
|
return -1, errors.New(fmt.Sprintf("Set Val %d is invalid", setVal))
|
|
}
|
|
return idx, nil
|
|
}
|
|
|
|
func (c *CustomerAttributes) GetAttrSetVal(attrVal uint64, userKeypad m.KeypadDimension) (uint64, error) {
|
|
indexOfAttr := c.IndexOfAttr(attrVal)
|
|
if indexOfAttr == -1 {
|
|
return 0, errors.New(fmt.Sprintf("No attribute %d", attrVal))
|
|
}
|
|
setIdx := indexOfAttr % userKeypad.AttrsPerKey
|
|
return c.setVals[setIdx], nil
|
|
}
|
|
|
|
func (c *CustomerAttributes) AttrVals(userKp m.KeypadDimension) ([]uint64, error) {
|
|
err := userKp.IsValidKeypadDimension()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return c.attrVals[:userKp.TotalAttrs()], nil
|
|
}
|
|
|
|
func (c *CustomerAttributes) SetVals(userKp m.KeypadDimension) ([]uint64, error) {
|
|
err := userKp.IsValidKeypadDimension()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return c.setVals[:userKp.AttrsPerKey], nil
|
|
}
|