Rename world_control Fix package name Attempt with main package Go world_control debug Go world_control debug Added get world RPC method Remove '_' from module Nakama plugin testing Nakama plugin testing Nakama plugin testing Try updated pipeline Nakama plugin testing Update pipeline Rework plugin dir Fix path Fix imports Fix imports Load match Load match work Server changes Server changes Server changes Changes basic upon helpful suggestions Client side get match
74 lines
2.6 KiB
Go
74 lines
2.6 KiB
Go
package control
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"github.com/heroiclabs/nakama-common/runtime"
|
|
)
|
|
|
|
type Match struct{}
|
|
|
|
type MatchState struct {
|
|
presences map[string]runtime.Presence
|
|
inputs map[string]string
|
|
positions map[string]string
|
|
jumps map[string]string
|
|
colors map[string]string
|
|
names map[string]string
|
|
}
|
|
|
|
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: map[string]runtime.Presence{},
|
|
inputs: map[string]string{},
|
|
positions: map[string]string{},
|
|
jumps: map[string]string{},
|
|
colors: map[string]string{},
|
|
names: map[string]string{},
|
|
}
|
|
tickRate := 10
|
|
label := "{\"name\": \"Game World\"}"
|
|
|
|
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) {
|
|
mState, _ := state.(*MatchState)
|
|
if _, ok := mState.presences[presence.GetUserId()]; ok {
|
|
return mState, true, ""
|
|
} else {
|
|
return mState, false, "User already logged in."
|
|
}
|
|
|
|
}
|
|
|
|
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 _, precense := range presences {
|
|
mState.presences[precense.GetUserId()] = precense
|
|
}
|
|
return state
|
|
}
|
|
|
|
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 _, presence := range presences {
|
|
delete(mState.presences, presence.GetUserId())
|
|
}
|
|
return state
|
|
}
|
|
|
|
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{} {
|
|
// Custom code to:
|
|
// - Process the messages received.
|
|
// - Update the match state based on the messages and time elapsed.
|
|
// - Broadcast new data messages to match participants.
|
|
|
|
return state
|
|
}
|
|
|
|
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{} {
|
|
return state
|
|
}
|