aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/game/message.go
blob: bb57eb537d94f7d40f35073266027117c6e5eb5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package game

import (
	"encoding/json"
	"fmt"

	"github.com/nsfisis/iosdc-2024-albatross/backend/api"
)

const (
	playerMessageTypeS2CPrepare    = "player:s2c:prepare"
	playerMessageTypeS2CStart      = "player:s2c:start"
	playerMessageTypeS2CExecResult = "player:s2c:execreslut"
	playerMessageTypeC2SEntry      = "player:c2s:entry"
	playerMessageTypeC2SReady      = "player:c2s:ready"
	playerMessageTypeC2SCode       = "player:c2s:code"
)

type playerMessageC2SWithClient struct {
	client  *playerClient
	message playerMessageC2S
}

type playerMessage = api.GamePlayerMessage

type playerMessageS2C = interface{}
type playerMessageS2CPrepare = api.GamePlayerMessageS2CPrepare
type playerMessageS2CPreparePayload = api.GamePlayerMessageS2CPreparePayload
type playerMessageS2CStart = api.GamePlayerMessageS2CStart
type playerMessageS2CStartPayload = api.GamePlayerMessageS2CStartPayload
type playerMessageS2CExecResult = api.GamePlayerMessageS2CExecResult
type playerMessageS2CExecResultPayload = api.GamePlayerMessageS2CExecResultPayload

type playerMessageC2S = interface{}
type playerMessageC2SEntry = api.GamePlayerMessageC2SEntry
type playerMessageC2SReady = api.GamePlayerMessageC2SReady
type playerMessageC2SCode = api.GamePlayerMessageC2SCode
type playerMessageC2SCodePayload = api.GamePlayerMessageC2SCodePayload

func asPlayerMessageC2S(raw map[string]json.RawMessage) (playerMessageC2S, error) {
	var typ string
	if err := json.Unmarshal(raw["type"], &typ); err != nil {
		return nil, err
	}

	switch typ {
	case playerMessageTypeC2SEntry:
		return &playerMessageC2SEntry{
			Type: playerMessageTypeC2SEntry,
		}, nil
	case playerMessageTypeC2SReady:
		return &playerMessageC2SReady{
			Type: playerMessageTypeC2SReady,
		}, nil
	case playerMessageTypeC2SCode:
		var payload playerMessageC2SCodePayload
		if err := json.Unmarshal(raw["data"], &payload); err != nil {
			return nil, err
		}
		return &playerMessageC2SCode{
			Type: playerMessageTypeC2SCode,
			Data: payload,
		}, nil
	default:
		return nil, fmt.Errorf("unknown message type: %s", typ)
	}
}

const (
	watcherMessageTypeS2CStart      = "watcher:s2c:start"
	watcherMessageTypeS2CExecResult = "watcher:s2c:execreslut"
	watcherMessageTypeS2CCode       = "watcher:s2c:code"
)

type watcherMessageS2C = interface{}
type watcherMessageS2CStart = api.GameWatcherMessageS2CStart
type watcherMessageS2CStartPayload = api.GameWatcherMessageS2CStartPayload
type watcherMessageS2CCode = api.GameWatcherMessageS2CCode
type watcherMessageS2CCodePayload = api.GameWatcherMessageS2CCodePayload
type watcherMessageS2CExecResult = api.GameWatcherMessageS2CExecResult
type watcherMessageS2CExecResultPayload = api.GameWatcherMessageS2CExecResultPayload