bgammon/event.go

212 lines
3.2 KiB
Go
Raw Normal View History

2023-07-31 23:46:28 +00:00
package bgammon
2023-08-27 02:47:12 +00:00
import (
"encoding/json"
"fmt"
)
2024-01-11 21:25:52 +00:00
const (
SpeedSlow int8 = 0
SpeedMedium int8 = 1
SpeedFast int8 = 2
SpeedInstant int8 = 3
)
2023-07-31 23:46:28 +00:00
type Event struct {
2023-08-27 02:47:12 +00:00
Type string
Player string
}
type EventWelcome struct {
2023-08-27 03:33:16 +00:00
Event
2023-08-27 02:47:12 +00:00
PlayerName string
Clients int
Games int
}
2023-08-27 06:44:41 +00:00
type EventPing struct {
Event
Message string
}
2023-08-27 03:33:16 +00:00
type EventNotice struct {
Event
Message string
}
type EventSay struct {
Event
Message string
2023-08-27 02:47:12 +00:00
}
type GameListing struct {
ID int
Password bool
2024-01-06 02:18:54 +00:00
Points int8
Players int8
2024-01-13 23:53:57 +00:00
Rating int
2023-08-27 02:47:12 +00:00
Name string
}
type EventList struct {
2023-08-27 03:33:16 +00:00
Event
2023-08-27 02:47:12 +00:00
Games []GameListing
}
2023-08-27 03:33:16 +00:00
type EventJoined struct {
Event
2023-08-27 21:10:18 +00:00
GameID int
2024-01-06 02:18:54 +00:00
PlayerNumber int8
2023-08-27 03:33:16 +00:00
}
type EventFailedJoin struct {
Event
Reason string
2023-08-27 02:47:12 +00:00
}
2023-08-27 21:10:18 +00:00
type EventLeft struct {
Event
}
type EventFailedLeave struct {
Event
Reason string
}
2023-08-27 02:47:12 +00:00
type EventBoard struct {
2023-08-27 03:33:16 +00:00
Event
2023-08-27 02:47:12 +00:00
GameState
}
2023-08-27 03:33:16 +00:00
type EventRolled struct {
Event
2024-01-06 02:18:54 +00:00
Roll1 int8
Roll2 int8
2024-01-08 05:25:49 +00:00
Roll3 int8 // Used in tabula games.
Selected bool // Whether the roll is selected by the player (used in acey-deucey games).
2023-08-27 02:47:12 +00:00
}
2023-08-27 21:10:18 +00:00
type EventFailedRoll struct {
Event
Reason string
}
2023-08-27 03:33:16 +00:00
type EventMoved struct {
Event
2024-01-06 02:18:54 +00:00
Moves [][]int8
2023-08-27 02:47:12 +00:00
}
2023-08-27 21:10:18 +00:00
type EventFailedMove struct {
Event
2024-01-06 02:18:54 +00:00
From int8
To int8
2023-08-27 21:10:18 +00:00
Reason string
}
type EventFailedOk struct {
Event
Reason string
}
2023-09-08 06:35:27 +00:00
type EventWin struct {
Event
2024-01-06 02:18:54 +00:00
Points int8
2023-09-08 06:35:27 +00:00
}
2023-12-15 08:23:45 +00:00
type EventSettings struct {
Event
AutoPlay bool
Highlight bool
Pips bool
Moves bool
Flip bool
Traditional bool
Advanced bool
Speed int8
2023-12-15 08:23:45 +00:00
}
2023-12-16 07:27:24 +00:00
type EventReplay struct {
Event
ID int
Content []byte
}
2023-12-20 00:26:05 +00:00
type HistoryMatch struct {
ID int
Timestamp int64
2024-01-06 02:18:54 +00:00
Points int8
2023-12-20 00:26:05 +00:00
Opponent string
2024-01-06 02:18:54 +00:00
Winner int8
2023-12-20 00:26:05 +00:00
}
type EventHistory struct {
Event
2024-01-14 01:45:02 +00:00
Page int
Pages int
2024-01-14 01:13:07 +00:00
Matches []*HistoryMatch
CasualBackgammonSingle int
CasualBackgammonMulti int
CasualAceyDeuceySingle int
CasualAceyDeuceyMulti int
CasualTabulaSingle int
CasualTabulaMulti int
2023-12-20 00:26:05 +00:00
}
2023-08-27 02:47:12 +00:00
func DecodeEvent(message []byte) (interface{}, error) {
e := &Event{}
err := json.Unmarshal(message, e)
if err != nil {
return nil, err
}
2023-08-27 21:10:18 +00:00
var ev interface{}
2023-08-27 02:47:12 +00:00
switch e.Type {
case EventTypeWelcome:
2023-08-27 21:10:18 +00:00
ev = &EventWelcome{}
2023-08-27 06:44:41 +00:00
case EventTypePing:
2023-08-27 21:10:18 +00:00
ev = &EventPing{}
2023-08-27 06:44:41 +00:00
case EventTypeNotice:
2023-08-27 21:10:18 +00:00
ev = &EventNotice{}
2023-08-27 06:44:41 +00:00
case EventTypeSay:
2023-08-27 21:10:18 +00:00
ev = &EventSay{}
2023-08-27 06:44:41 +00:00
case EventTypeList:
2023-08-27 21:10:18 +00:00
ev = &EventList{}
2023-08-27 02:47:12 +00:00
case EventTypeJoined:
2023-08-27 21:10:18 +00:00
ev = &EventJoined{}
2023-08-27 06:44:41 +00:00
case EventTypeFailedJoin:
2023-08-27 21:10:18 +00:00
ev = &EventFailedJoin{}
case EventTypeLeft:
ev = &EventLeft{}
2023-09-19 07:32:01 +00:00
case EventTypeFailedLeave:
ev = &EventFailedLeave{}
2023-08-27 06:44:41 +00:00
case EventTypeBoard:
2023-08-27 21:10:18 +00:00
ev = &EventBoard{}
2023-08-27 06:44:41 +00:00
case EventTypeRolled:
2023-08-27 21:10:18 +00:00
ev = &EventRolled{}
case EventTypeFailedRoll:
ev = &EventFailedRoll{}
2023-08-27 06:44:41 +00:00
case EventTypeMoved:
2023-08-27 21:10:18 +00:00
ev = &EventMoved{}
case EventTypeFailedMove:
ev = &EventFailedMove{}
case EventTypeFailedOk:
ev = &EventFailedOk{}
2023-09-08 06:35:27 +00:00
case EventTypeWin:
ev = &EventWin{}
2023-12-15 08:23:45 +00:00
case EventTypeSettings:
ev = &EventSettings{}
2023-12-16 07:27:24 +00:00
case EventTypeReplay:
ev = &EventReplay{}
2023-12-20 00:26:05 +00:00
case EventTypeHistory:
ev = &EventHistory{}
2023-08-27 02:47:12 +00:00
default:
return nil, fmt.Errorf("failed to decode event: unknown event type: %s", e.Type)
}
2023-08-27 21:10:18 +00:00
err = json.Unmarshal(message, ev)
if err != nil {
return nil, err
}
return ev, nil
2023-07-31 23:46:28 +00:00
}