Messages now using envelopes.

This commit is contained in:
2024-12-11 07:41:21 -05:00
parent 9345b49f57
commit 9610fc5322
5 changed files with 617 additions and 267 deletions

View File

@@ -6,6 +6,7 @@ import (
"client/fonts"
"client/gamedata"
"client/pb"
"fmt"
"maps"
"sync"
"time"
@@ -59,7 +60,7 @@ func NewGame() *Game {
}
g.clients = make(map[string]ClientData)
g.gameId = g.gameclient.GetIdentity()
//g.gameId = g.gameclient.GetIdentity()
go g.gameclient.ReadData(g.HandleServerData)
return g
}
@@ -85,7 +86,15 @@ func (g *Game) Update() error {
},
}
g.gameclient.SendProtoData(cd)
envelope := &pb.ClientEnvelope{
Payload: &pb.ClientEnvelope_Coordinates{
Coordinates: cd,
},
}
g.gameclient.SendMessage(envelope)
//g.gameclient.SendProtoData(cd)
/*
cd := *client.ClientData{
Name: g.name,
@@ -144,66 +153,34 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (screenwidth, screenheigh
return screenWidth, screenHeight
}
func (g *Game) HandleServerData(allclients *pb.AllClients) {
func (g *Game) HandleServerData(envelope *pb.ServerEnvelope) {
for _, client := range allclients.Clients {
//fmt.Println(client.Coordinates.X)
switch payload := envelope.Payload.(type) {
case *pb.ServerEnvelope_Broadcast:
//fmt.Println("Here comes the broadcast!")
//localaddr := g.gameclient.GetLocalAddr()
if client.Id != int32(g.gameId) {
update := ClientData{
Address: client.Address,
Name: client.Name,
Position: gamedata.Coordinates{
X: client.Coordinates.X,
Y: client.Coordinates.Y,
},
}
g.mu.Lock()
g.clients[update.Address] = update
g.mu.Unlock()
}
}
//log.Println(data)
/*
raw := data[1 : len(data)-1]
clientinfo := strings.Split(raw, ";")
for _, info := range clientinfo {
subdata := strings.Split(info, ",")
if len(subdata) == 4 {
if g.gameclient.GetLocalAddr() != subdata[0] {
x, err := strconv.Atoi(subdata[2])
if err != nil {
x = 0
}
y, err := strconv.Atoi(subdata[3])
if err != nil {
y = 0
}
update := ClientData{
Address: subdata[0],
Name: subdata[1],
Position: gamedata.Coordinates{
X: float64(x),
Y: float64(y),
},
}
g.mu.Lock()
g.clients[update.Address] = update
g.mu.Unlock()
for _, client := range payload.Broadcast.Clients {
if client.Id != int32(g.gameId) {
update := ClientData{
Id: int(client.Id),
Address: client.Address,
Name: client.Name,
Position: gamedata.Coordinates{
X: client.Coordinates.X,
Y: client.Coordinates.Y,
},
}
g.mu.Lock()
g.clients[update.Address] = update
g.mu.Unlock()
}
}
*/
case *pb.ServerEnvelope_Identity:
fmt.Println("Server is trying to give us our id.")
g.gameId = int(payload.Identity.Id)
}
}
func (g *Game) CleanupClients() {