fix multiplayer simultaneous

This commit is contained in:
Beric Bearnson 2024-10-24 17:52:14 -06:00
parent 067d22f3a9
commit d25f9c987a
2 changed files with 15 additions and 18 deletions

View File

@ -84,9 +84,9 @@ func GamesListen() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer gameListener.Close()
for { for {
defer gameListener.Close()
conn, err := gameListener.Accept() conn, err := gameListener.Accept()
if err != nil { if err != nil {
log.Println(err) log.Println(err)

View File

@ -17,12 +17,6 @@ type GameClient struct {
Conn net.Conn Conn net.Conn
} }
var player1 GameClient
var player2 GameClient
var ingress chan StateUpdate
var egress chan StateUpdate
const posXBound = 52 const posXBound = 52
const negXBound = posXBound * -1 const negXBound = posXBound * -1
const posYBound = 50 const posYBound = 50
@ -30,8 +24,11 @@ const negYBound = posYBound * -1
func StartGame(conn1, conn2 net.Conn, username1, username2 string) { func StartGame(conn1, conn2 net.Conn, username1, username2 string) {
egress = make(chan StateUpdate) var player1 GameClient
ingress = make(chan StateUpdate) var player2 GameClient
egress := make(chan StateUpdate)
ingress := make(chan StateUpdate)
player1 = GameClient{ player1 = GameClient{
Username: username1, Username: username1,
@ -75,17 +72,17 @@ func StartGame(conn1, conn2 net.Conn, username1, username2 string) {
broadcastUpdate(StateUpdate{ broadcastUpdate(StateUpdate{
FieldPath: "Message", FieldPath: "Message",
Value: []byte("Ready..."), Value: []byte("Ready..."),
}) }, player1, player2)
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
broadcastUpdate(StateUpdate{ broadcastUpdate(StateUpdate{
FieldPath: "Message", FieldPath: "Message",
Value: []byte("Set..."), Value: []byte("Set..."),
}) }, player1, player2)
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
broadcastUpdate(StateUpdate{ broadcastUpdate(StateUpdate{
FieldPath: "Message", FieldPath: "Message",
Value: []byte("Go!"), Value: []byte("Go!"),
}) }, player1, player2)
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
bv := float32(rand.Intn(2)*2 - 1) bv := float32(rand.Intn(2)*2 - 1)
@ -124,10 +121,10 @@ func StartGame(conn1, conn2 net.Conn, username1, username2 string) {
}, },
}, },
} }
go gameLoop(&state) go gameLoop(&state, ingress, egress, player1, player2)
} }
func gameLoop(state *GameState) { func gameLoop(state *GameState, ingress, egress chan (StateUpdate), player1, player2 GameClient) {
// Player 1 read loop // Player 1 read loop
go func() { go func() {
for { for {
@ -169,7 +166,7 @@ func gameLoop(state *GameState) {
go func() { go func() {
for { for {
msg := <-egress msg := <-egress
broadcastUpdate(msg) broadcastUpdate(msg, player1, player2)
if msg.FieldPath == "Winner" { if msg.FieldPath == "Winner" {
return return
} }
@ -191,7 +188,7 @@ func gameLoop(state *GameState) {
} }
case _ = <-ticker.C: case _ = <-ticker.C:
update := process(state) update := process(state, player1, player2)
egress <- update egress <- update
if update.FieldPath == "Winner" { if update.FieldPath == "Winner" {
slog.Debug("Closing game loop") slog.Debug("Closing game loop")
@ -201,7 +198,7 @@ func gameLoop(state *GameState) {
} }
} }
func process(state *GameState) StateUpdate { func process(state *GameState, player1, player2 GameClient) StateUpdate {
// Move players // Move players
// Check if player edge is out of bounds // Check if player edge is out of bounds
// If out of bounds reset velocity to zero and position to edge // If out of bounds reset velocity to zero and position to edge
@ -350,7 +347,7 @@ func handlePlayerRequest(update StateUpdate, state *GameState) error {
return nil return nil
} }
func broadcastUpdate(update StateUpdate) error { func broadcastUpdate(update StateUpdate, player1, player2 GameClient) error {
msg, err := json.Marshal(update) msg, err := json.Marshal(update)
if err != nil { if err != nil {
return err return err