...

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

Documentation: code.rocket9labs.com/tslocum/bgammon

     1  package bgammon
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  )
     7  
     8  const (
     9  	SpeedSlow    int8 = 0
    10  	SpeedMedium  int8 = 1
    11  	SpeedFast    int8 = 2
    12  	SpeedInstant int8 = 3
    13  )
    14  
    15  type Event struct {
    16  	Type   string
    17  	Player string
    18  }
    19  
    20  type EventWelcome struct {
    21  	Event
    22  	PlayerName string
    23  	Clients    int
    24  	Games      int
    25  }
    26  
    27  type EventHelp struct {
    28  	Event
    29  	Topic   string
    30  	Message string
    31  }
    32  
    33  type EventPing struct {
    34  	Event
    35  	Message string
    36  }
    37  
    38  type EventNotice struct {
    39  	Event
    40  	Message string
    41  }
    42  
    43  type EventSay struct {
    44  	Event
    45  	Message string
    46  }
    47  
    48  type GameListing struct {
    49  	ID       int
    50  	Password bool
    51  	Points   int8
    52  	Players  int8
    53  	Rating   int
    54  	Name     string
    55  }
    56  
    57  type EventList struct {
    58  	Event
    59  	Games []GameListing
    60  }
    61  
    62  type EventJoined struct {
    63  	Event
    64  	GameID       int
    65  	PlayerNumber int8
    66  }
    67  
    68  type EventFailedJoin struct {
    69  	Event
    70  	Reason string
    71  }
    72  
    73  type EventLeft struct {
    74  	Event
    75  }
    76  
    77  type EventFailedLeave struct {
    78  	Event
    79  	Reason string
    80  }
    81  
    82  type EventBoard struct {
    83  	Event
    84  	GameState
    85  }
    86  
    87  type EventRolled struct {
    88  	Event
    89  	Roll1    int8
    90  	Roll2    int8
    91  	Roll3    int8 // Used in tabula games.
    92  	Selected bool // Whether the roll is selected by the player (used in acey-deucey games).
    93  }
    94  
    95  type EventFailedRoll struct {
    96  	Event
    97  	Reason string
    98  }
    99  
   100  type EventMoved struct {
   101  	Event
   102  	Moves [][]int8
   103  }
   104  
   105  type EventFailedMove struct {
   106  	Event
   107  	From   int8
   108  	To     int8
   109  	Reason string
   110  }
   111  
   112  type EventFailedOk struct {
   113  	Event
   114  	Reason string
   115  }
   116  
   117  type EventWin struct {
   118  	Event
   119  	Points int8
   120  }
   121  
   122  type EventSettings struct {
   123  	Event
   124  	AutoPlay  bool
   125  	Highlight bool
   126  	Pips      bool
   127  	Moves     bool
   128  	Flip      bool
   129  	Advanced  bool
   130  	Speed     int8
   131  }
   132  
   133  type EventReplay struct {
   134  	Event
   135  	ID      int
   136  	Content []byte
   137  }
   138  
   139  type HistoryMatch struct {
   140  	ID        int
   141  	Timestamp int64
   142  	Points    int8
   143  	Opponent  string
   144  	Winner    int8
   145  }
   146  
   147  type EventHistory struct {
   148  	Event
   149  	Page                   int
   150  	Pages                  int
   151  	Matches                []*HistoryMatch
   152  	CasualBackgammonSingle int
   153  	CasualBackgammonMulti  int
   154  	CasualAceyDeuceySingle int
   155  	CasualAceyDeuceyMulti  int
   156  	CasualTabulaSingle     int
   157  	CasualTabulaMulti      int
   158  }
   159  
   160  func DecodeEvent(message []byte) (interface{}, error) {
   161  	e := &Event{}
   162  	err := json.Unmarshal(message, e)
   163  	if err != nil {
   164  		return nil, err
   165  	}
   166  
   167  	var ev interface{}
   168  	switch e.Type {
   169  	case EventTypeWelcome:
   170  		ev = &EventWelcome{}
   171  	case EventTypeHelp:
   172  		ev = &EventHelp{}
   173  	case EventTypePing:
   174  		ev = &EventPing{}
   175  	case EventTypeNotice:
   176  		ev = &EventNotice{}
   177  	case EventTypeSay:
   178  		ev = &EventSay{}
   179  	case EventTypeList:
   180  		ev = &EventList{}
   181  	case EventTypeJoined:
   182  		ev = &EventJoined{}
   183  	case EventTypeFailedJoin:
   184  		ev = &EventFailedJoin{}
   185  	case EventTypeLeft:
   186  		ev = &EventLeft{}
   187  	case EventTypeFailedLeave:
   188  		ev = &EventFailedLeave{}
   189  	case EventTypeBoard:
   190  		ev = &EventBoard{}
   191  	case EventTypeRolled:
   192  		ev = &EventRolled{}
   193  	case EventTypeFailedRoll:
   194  		ev = &EventFailedRoll{}
   195  	case EventTypeMoved:
   196  		ev = &EventMoved{}
   197  	case EventTypeFailedMove:
   198  		ev = &EventFailedMove{}
   199  	case EventTypeFailedOk:
   200  		ev = &EventFailedOk{}
   201  	case EventTypeWin:
   202  		ev = &EventWin{}
   203  	case EventTypeSettings:
   204  		ev = &EventSettings{}
   205  	case EventTypeReplay:
   206  		ev = &EventReplay{}
   207  	case EventTypeHistory:
   208  		ev = &EventHistory{}
   209  	default:
   210  		return nil, fmt.Errorf("failed to decode event: unknown event type: %s", e.Type)
   211  	}
   212  
   213  	err = json.Unmarshal(message, ev)
   214  	if err != nil {
   215  		return nil, err
   216  	}
   217  	return ev, nil
   218  }
   219  

View as plain text