...

Source file src/code.rocket9labs.com/tslocum/bei/event.go

Documentation: code.rocket9labs.com/tslocum/bei

     1  package bei
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  )
     7  
     8  const (
     9  	EventTypeOkBEI      = "okbei"
    10  	EventTypeOkList     = "oklist"
    11  	EventTypeFailSet    = "failset"
    12  	EventTypeOkSet      = "okset"
    13  	EventTypeFailDouble = "faildouble"
    14  	EventTypeOkDouble   = "okdouble"
    15  	EventTypeFailMove   = "failmove"
    16  	EventTypeOkMove     = "okmove"
    17  	EventTypeFailChoose = "failchoose"
    18  	EventTypeOkChoose   = "okchoose"
    19  )
    20  
    21  func EncodeEvent(e interface{}) ([]byte, error) {
    22  	// JSON formatted messages.
    23  	switch ev := e.(type) {
    24  	case *EventOkBEI:
    25  		ev.Type = EventTypeOkBEI
    26  	case *EventOkList:
    27  		ev.Type = EventTypeOkList
    28  	case *EventFailSet:
    29  		ev.Type = EventTypeFailSet
    30  	case *EventOkSet:
    31  		ev.Type = EventTypeOkSet
    32  	case *EventFailDouble:
    33  		ev.Type = EventTypeFailDouble
    34  	case *EventOkDouble:
    35  		ev.Type = EventTypeOkDouble
    36  	case *EventFailMove:
    37  		ev.Type = EventTypeFailMove
    38  	case *EventOkMove:
    39  		ev.Type = EventTypeOkMove
    40  	case *EventFailChoose:
    41  		ev.Type = EventTypeFailChoose
    42  	case *EventOkChoose:
    43  		ev.Type = EventTypeOkChoose
    44  	default:
    45  		return nil, fmt.Errorf("unknown event type %+v", ev)
    46  	}
    47  
    48  	return json.Marshal(e)
    49  }
    50  
    51  func DecodeEvent(message []byte) (interface{}, error) {
    52  	e := &Event{}
    53  	err := json.Unmarshal(message, e)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	var ev interface{}
    59  	switch e.Type {
    60  	case EventTypeOkBEI:
    61  		ev = &EventOkBEI{}
    62  	case EventTypeOkList:
    63  		ev = &EventOkList{}
    64  	case EventTypeFailSet:
    65  		ev = &EventFailSet{}
    66  	case EventTypeOkSet:
    67  		ev = &EventOkSet{}
    68  	case EventTypeFailDouble:
    69  		ev = &EventFailDouble{}
    70  	case EventTypeOkDouble:
    71  		ev = &EventOkDouble{}
    72  	case EventTypeFailMove:
    73  		ev = &EventFailMove{}
    74  	case EventTypeOkMove:
    75  		ev = &EventOkMove{}
    76  	case EventTypeFailChoose:
    77  		ev = &EventFailChoose{}
    78  	case EventTypeOkChoose:
    79  		ev = &EventOkChoose{}
    80  	default:
    81  		return nil, fmt.Errorf("failed to decode event: unknown event type: %s", e.Type)
    82  	}
    83  
    84  	err = json.Unmarshal(message, ev)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	return ev, nil
    89  }
    90  
    91  type Event struct {
    92  	Type string
    93  }
    94  
    95  type EventOkBEI struct {
    96  	Event
    97  	Version int
    98  	ID      map[string]string
    99  }
   100  
   101  type SettingType int
   102  
   103  const (
   104  	SettingBoolean SettingType = iota
   105  	SettingInteger
   106  	SettingString
   107  )
   108  
   109  type ListEntry struct {
   110  	Name        string
   111  	Type        SettingType
   112  	Description string
   113  	Default     string
   114  	Value       string
   115  	Min         int
   116  	Max         int
   117  }
   118  
   119  type EventOkList struct {
   120  	Event
   121  	List []*ListEntry
   122  }
   123  
   124  type EventFailSet struct {
   125  	Event
   126  	Name   string
   127  	Reason string
   128  }
   129  
   130  type EventOkSet struct {
   131  	Event
   132  	Name  string
   133  	Value string
   134  }
   135  
   136  type EventFailDouble struct {
   137  	Event
   138  	Reason string
   139  }
   140  
   141  type CubeEvaluation struct {
   142  	Offer bool
   143  	Take  bool
   144  }
   145  
   146  type Probabilities struct {
   147  	LoseBackgammon float64
   148  	LoseGammon     float64
   149  	Win            float64
   150  	WinBackgammon  float64
   151  	WinGammon      float64
   152  }
   153  
   154  type EventOkDouble struct {
   155  	Event
   156  	Cube          CubeEvaluation
   157  	Probabilities Probabilities
   158  }
   159  
   160  type EventFailMove struct {
   161  	Event
   162  	Reason string
   163  }
   164  
   165  type Play struct {
   166  	From int
   167  	To   int
   168  }
   169  
   170  type Move struct {
   171  	Play          []*Play
   172  	Probabilities Probabilities
   173  }
   174  
   175  type EventOkMove struct {
   176  	Event
   177  	Moves []*Move
   178  }
   179  
   180  type EventFailChoose struct {
   181  	Event
   182  	Reason string
   183  }
   184  
   185  type ChooseRoll struct {
   186  	Roll          int
   187  	Probabilities Probabilities
   188  }
   189  
   190  type EventOkChoose struct {
   191  	Event
   192  	Rolls []*ChooseRoll
   193  }
   194  

View as plain text