Generate basic world & recieve data on client

This commit is contained in:
2020-08-16 18:53:39 -04:00
parent abfb0f4284
commit fb43cb1fb7
16 changed files with 237 additions and 8 deletions

View File

@ -3,8 +3,14 @@ package control
import (
"context"
"database/sql"
"github.com/heroiclabs/nakama-common/runtime"
"github.com/josephbmanley/family/server/plugin/gamemap"
)
type OpCode int64
const (
OpCodeTileUpdate = 1
)
type Match struct{}
@ -12,20 +18,19 @@ 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
positions map[string]map[string]int
names map[string]string
worldMap *gamemap.WorldMap
}
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{},
positions: map[string]map[string]int{},
names: map[string]string{},
worldMap: gamemap.IntializeMap(),
}
tickRate := 10
label := "{\"name\": \"Game World\"}"
@ -47,6 +52,20 @@ func (m *Match) MatchJoin(ctx context.Context, logger runtime.Logger, db *sql.DB
mState, _ := state.(*MatchState)
for _, precense := range presences {
mState.presences[precense.GetUserId()] = precense
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 {
logger.Error(err.Error())
return mState
} else {
if sendErr := dispatcher.BroadcastMessage(OpCodeTileUpdate, regionData, []runtime.Presence{precense}, precense, true); sendErr != nil {
logger.Error(sendErr.Error())
return mState
}
}
}
return mState
}

View File

@ -0,0 +1,52 @@
package gamemap
import (
"encoding/json"
"fmt"
)
type WorldMap struct {
data [][]int
max_x int
max_y int
}
func (m WorldMap) GetTile(x int, y int) (int, error) {
if x > m.max_x || y > m.max_y {
return -1, fmt.Errorf("Map out of bounds error: %d, %d", x, y)
}
return m.data[x][y], nil
}
func (m WorldMap) GetJsonRegion(start_x, end_x, start_y, end_y int) ([]byte, error) {
regionMap := make(map[int]map[int]int)
for x := start_x; x < end_x; x++ {
for y := start_y; y < end_y; y++ {
if result, err := m.GetTile(x, y); err != nil {
return nil, err
} else {
regionMap[x][y] = result
}
}
}
jsonString, err := json.Marshal(regionMap)
return jsonString, err
}
func IntializeMap() *WorldMap {
worldMap := new(WorldMap)
worldMap.max_x = 64
worldMap.max_y = 64
for x := 0; x < worldMap.max_x; x++ {
for y := 0; y < worldMap.max_y; y++ {
worldMap.data[x][y] = 0
}
}
// Add dot at top left for testing
worldMap.data[0][0] = 1
return worldMap
}

View File

@ -15,6 +15,7 @@ github.com/heroiclabs/nakama-common v1.5.1 h1:ViCm9AvYYdQOCSKEa34SuSQ80JyZOHl6OD
github.com/heroiclabs/nakama-common v1.5.1/go.mod h1:nZAXHdeo4SyPlCyf7pU9rCVizxEhBF74gt7teDe/EaQ=
github.com/heroiclabs/nakama-common v1.7.2 h1:FQedePGCorBl3tXW4Ro8+XLGbEDQfGrT5Tb07j1UaLc=
github.com/josephbmanley/family v0.0.0-20200815220504-0d9d05943cef h1:6oijVkew6eKI1fGE+YMaxmiNlp/hkN9wDpStoid9/ZI=
github.com/josephbmanley/family v0.0.0-20200816202226-abfb0f428423 h1:ynsJFMYkfs3JspzvLCfmPGJwdKY/4QeX457U0+y4J1I=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@ -42,6 +43,7 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk=
google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=