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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
| package example
import (
"context"
"database/sql"
"strconv"
"github.com/heroiclabs/nakama-common/runtime"
)
type MatchState struct {
presences map[string]runtime.Presence
}
type Match struct{}
func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error {
logger.Info("Hello Multiplayer!")
err := initializer.RegisterMatch("standard_match", newMatch)
if err != nil {
logger.Error("[RegisterMatch] error: ", err.Error())
return err
}
return nil
}
func newMatch(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule) (m runtime.Match, err error) {
return &Match{}, nil
}
func (m *Match) MatchInit(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, params map[string]interface{}) (interface{}, int, string) {
state := &MatchState{
presences: make(map[string]runtime.Presence),
}
tickRate := 1
label := ""
return state, tickRate, label
}
func (m *Match) MatchJoinAttempt(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, presence runtime.Presence, metadata map[string]string) (interface{}, bool, string) {
acceptUser := true
return state, acceptUser, ""
}
func (m *Match) MatchJoin(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, presences []runtime.Presence) interface{} {
mState, _ := state.(*MatchState)
for _, p := range presences {
mState.presences[p.GetUserId()] = p
}
return mState
}
func (m *Match) MatchLeave(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, presences []runtime.Presence) interface{} {
mState, _ := state.(*MatchState)
for _, p := range presences {
delete(mState.presences, p.GetUserId())
}
return mState
}
func (m *Match) MatchLoop(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, messages []runtime.MatchData) interface{} {
mState, _ := state.(*MatchState)
for _, presence := range mState.presences {
logger.Info("Presence %v named %v", presence.GetUserId(), presence.GetUsername())
}
for _, message := range messages {
logger.Info("Received %v from %v", string(message.GetData()), message.GetUserId())
reliable := true
dispatcher.BroadcastMessage(1, message.GetData(), []runtime.Presence{message}, reliable)
}
return mState
}
func (m *Match) MatchTerminate(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, graceSeconds int) interface{} {
message := "Server shutting down in " + strconv.Itoa(graceSeconds) + " seconds."
reliable := true
dispatcher.BroadcastMessage(2, []byte(message), []runtime.Presence{}, reliable)
return state
}
func (m *Match) MatchSignal(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, data string) (interface{}, string) {
return state, "signal received: " + data
}
|