Updated read/writes, fully using buffers now.

This commit is contained in:
2024-12-10 20:30:51 -05:00
parent ca681f08cd
commit cc9edb7825
8 changed files with 906 additions and 293 deletions

View File

@@ -5,10 +5,8 @@ import (
"client/elements"
"client/fonts"
"client/gamedata"
"fmt"
"client/pb"
"maps"
"strconv"
"strings"
"sync"
"time"
@@ -73,7 +71,23 @@ func (g *Game) Update() error {
//broadcast our position
if g.cycle%2 == 0 {
if g.gameclient.IsConnected() {
g.gameclient.SendData(fmt.Sprintf("%s,%.0f,%.0f\n", g.name, g.position.X, g.position.Y))
//g.gameclient.SendData(fmt.Sprintf("%s,%.0f,%.0f\n", g.name, g.position.X, g.position.Y))
cd := &pb.ClientCoordinates{
Name: g.name,
Coordinates: &pb.Coordinates{
X: g.position.X,
Y: g.position.Y,
},
}
g.gameclient.SendProtoData(cd)
/*
cd := *client.ClientData{
Name: g.name,
Address: g.
}*/
}
}
@@ -126,44 +140,63 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (screenwidth, screenheigh
return screenWidth, screenHeight
}
func (g *Game) HandleServerData(data string) {
//log.Println(data)
func (g *Game) HandleServerData(allclients *pb.AllClients) {
raw := data[1 : len(data)-1]
clientinfo := strings.Split(raw, ";")
for _, info := range clientinfo {
subdata := strings.Split(info, ",")
for _, client := range allclients.Clients {
//fmt.Println(client.Coordinates.X)
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()
}
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()
}
}
}
*/
}
func (g *Game) CleanupClients() {