Cleanup plugin code
This commit is contained in:
		@ -7,14 +7,21 @@ import (
 | 
			
		||||
	"github.com/josephbmanley/family/server/plugin/gamemap"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// OpCode represents a enum for valid OpCodes
 | 
			
		||||
// used by the match logic
 | 
			
		||||
type OpCode int64
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	// OpCodeTileUpdate is used for tile updates
 | 
			
		||||
	OpCodeTileUpdate = 1
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Match is the object registered
 | 
			
		||||
// as a runtime.Match interface
 | 
			
		||||
type Match struct{}
 | 
			
		||||
 | 
			
		||||
// MatchState holds information that is passed between
 | 
			
		||||
// Nakama match methods
 | 
			
		||||
type MatchState struct {
 | 
			
		||||
	presences map[string]runtime.Presence
 | 
			
		||||
	inputs    map[string]string
 | 
			
		||||
@ -23,6 +30,7 @@ type MatchState struct {
 | 
			
		||||
	worldMap  *gamemap.WorldMap
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MatchInit is called when a new match is created
 | 
			
		||||
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{
 | 
			
		||||
@ -38,12 +46,16 @@ func (m *Match) MatchInit(ctx context.Context, logger runtime.Logger, db *sql.DB
 | 
			
		||||
	return state, tickRate, label
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MatchJoinAttempt is called when a player tried to join a match
 | 
			
		||||
// and validates their attempt
 | 
			
		||||
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, ok := state.(*MatchState)
 | 
			
		||||
	if !ok {
 | 
			
		||||
		logger.Error("Invalid match state on join attempt!")
 | 
			
		||||
		return state, false, "Invalid match state!"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Validate user is not already connected
 | 
			
		||||
	if _, ok := mState.presences[presence.GetUserId()]; ok {
 | 
			
		||||
		return mState, false, "User already logged in."
 | 
			
		||||
	} else {
 | 
			
		||||
@ -52,32 +64,39 @@ func (m *Match) MatchJoinAttempt(ctx context.Context, logger runtime.Logger, db
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MatchJoin is called when a player successfully joins the match
 | 
			
		||||
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, ok := state.(*MatchState)
 | 
			
		||||
	if !ok {
 | 
			
		||||
		logger.Error("Invalid match state on join!")
 | 
			
		||||
		return state, false, "Invalid match state!"
 | 
			
		||||
		return state
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, precense := range presences {
 | 
			
		||||
 | 
			
		||||
		// Add presence to map
 | 
			
		||||
		mState.presences[precense.GetUserId()] = precense
 | 
			
		||||
 | 
			
		||||
		// Set player spawn pos
 | 
			
		||||
		mState.positions[precense.GetUserId()] = map[string]int{"x": 16, "y": 16}
 | 
			
		||||
 | 
			
		||||
		mState.names[precense.GetUserId()] = "User"
 | 
			
		||||
 | 
			
		||||
		if regionData, err := mState.worldMap.GetJsonRegion(16-8, 16+8, 16-8, 16+8); err != nil {
 | 
			
		||||
		// Get intial tile data around player
 | 
			
		||||
		if regionData, err := mState.worldMap.GetJSONRegionAround(16, 16, 8); err != nil {
 | 
			
		||||
			logger.Error(err.Error())
 | 
			
		||||
			return mState
 | 
			
		||||
		} else {
 | 
			
		||||
 | 
			
		||||
			// Broadcast tile data to client
 | 
			
		||||
			if sendErr := dispatcher.BroadcastMessage(OpCodeTileUpdate, regionData, []runtime.Presence{precense}, precense, true); sendErr != nil {
 | 
			
		||||
				logger.Error(sendErr.Error())
 | 
			
		||||
				return mState
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return mState
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MatchLeave is called when a player leaves the match
 | 
			
		||||
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, ok := state.(*MatchState)
 | 
			
		||||
	if !ok {
 | 
			
		||||
@ -90,6 +109,7 @@ func (m *Match) MatchLeave(ctx context.Context, logger runtime.Logger, db *sql.D
 | 
			
		||||
	return mState
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MatchLoop is code that is executed every tick
 | 
			
		||||
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.
 | 
			
		||||
@ -99,6 +119,7 @@ func (m *Match) MatchLoop(ctx context.Context, logger runtime.Logger, db *sql.DB
 | 
			
		||||
	return state
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MatchTerminate is code that is executed when the match ends
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user