Skip to content

Commit

Permalink
Add methods to serialise comms messages
Browse files Browse the repository at this point in the history
  • Loading branch information
z-riley committed Nov 4, 2024
1 parent 3030a5c commit 7838989
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 105 deletions.
79 changes: 55 additions & 24 deletions comms/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@ type Message struct {
}

// ParseMessage returns a message from a byte slice.
func ParseMessage(b []byte) (Message, error) {
var msg Message
err := json.Unmarshal(b, &msg)
return msg, err
func ParseMessage(b []byte) (m Message, err error) {
err = json.Unmarshal(b, &m)
return m, err
}

// MessageType defines the type of message.
type MessageType string

const (
TypePlayerData MessageType = "playerData"
TypeGameData = "gameData"
TypeEventData = "eventData"
TypeRequest = "request"
TypePlayerData MessageType = "playerData"
TypeGameData = "gameData"
TypeEventData = "eventData"
TypeRequestData = "request"
)

// PlayerData contains data about a player.
Expand All @@ -38,10 +37,18 @@ type PlayerData struct {
}

// ParsePlayerData returns player data from a byte slice.
func ParsePlayerData(b []byte) (PlayerData, error) {
var data PlayerData
err := json.Unmarshal(b, &data)
return data, err
func ParsePlayerData(b []byte) (d PlayerData, err error) {
err = json.Unmarshal(b, &d)
return d, err
}

// Serialise converts player data into a byte slice.
func (d PlayerData) Serialise() ([]byte, error) {
b, err := json.Marshal(d)
if err != nil {
return nil, err
}
return json.Marshal(Message{TypePlayerData, b})
}

// GameData contains a game's current state.
Expand All @@ -50,10 +57,18 @@ type GameData struct {
}

// ParseGameData returns game data from a byte slice.
func ParseGameData(b []byte) (GameData, error) {
var data GameData
err := json.Unmarshal(b, &data)
return data, err
func ParseGameData(b []byte) (d GameData, err error) {
err = json.Unmarshal(b, &d)
return d, err
}

// Serialise converts game data into a byte slice.
func (d GameData) Serialise() ([]byte, error) {
b, err := json.Marshal(d)
if err != nil {
return nil, err
}
return json.Marshal(Message{TypeGameData, b})
}

// EventData contains an event which has occurred.
Expand All @@ -72,10 +87,18 @@ const (
)

// ParseEventData returns event data from a byte slice.
func ParseEventData(b []byte) (EventData, error) {
var data EventData
err := json.Unmarshal(b, &data)
return data, err
func ParseEventData(b []byte) (d EventData, err error) {
err = json.Unmarshal(b, &d)
return d, err
}

// Serialise converts event data into a byte slice.
func (d EventData) Serialise() ([]byte, error) {
b, err := json.Marshal(d)
if err != nil {
return nil, err
}
return json.Marshal(Message{TypeEventData, b})
}

// RequestData contains a request for data of a certain type.
Expand All @@ -84,8 +107,16 @@ type RequestData struct {
}

// ParseRequestData returns request data from a byte slice.
func ParseRequestData(b []byte) (RequestData, error) {
var data RequestData
err := json.Unmarshal(b, &data)
return data, err
func ParseRequestData(b []byte) (d RequestData, err error) {
err = json.Unmarshal(b, &d)
return d, err
}

// Serialise converts request data into a byte slice.
func (d RequestData) Serialise() ([]byte, error) {
b, err := json.Marshal(d)
if err != nil {
return nil, err
}
return json.Marshal(Message{TypeRequestData, b})
}
54 changes: 13 additions & 41 deletions screen/multiplayer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package screen

import (
"encoding/json"
"fmt"
"image/color"

Expand Down Expand Up @@ -348,7 +347,7 @@ func (s *MultiplayerScreen) handleOpponentData(data []byte) error {
}
return s.handleEventData(eventData)

case comms.TypeRequest:
case comms.TypeRequestData:
requestData, err := comms.ParseRequestData(msg.Content)
if err != nil {
return fmt.Errorf("failed to parse request data: %w", err)
Expand All @@ -362,20 +361,11 @@ func (s *MultiplayerScreen) handleOpponentData(data []byte) error {

// sendGameData sends the local game state to the opponent.
func (s *MultiplayerScreen) sendGameData() error {
gameData, err := json.Marshal(
comms.GameData{
Game: *s.backend,
})
if err != nil {
return fmt.Errorf("failed to marshal game data: %w", err)
}
msg, err := json.Marshal(
comms.Message{
Type: comms.TypeGameData,
Content: gameData,
})
msg, err := comms.GameData{
Game: *s.backend,
}.Serialise()
if err != nil {
return fmt.Errorf("failed to marshal message: %w", err)
return fmt.Errorf("failed to serialise game data: %w", err)
}

return s.sendToOpponent(msg)
Expand All @@ -389,20 +379,11 @@ func (s *MultiplayerScreen) handleGameData(data comms.GameData) error {

// sendScreenLoadedEvent sends the screen loaded event to the opponent.
func (s *MultiplayerScreen) sendScreenLoadedEvent() error {
eventData, err := json.Marshal(
comms.EventData{
Event: comms.EventScreenLoaded,
})
if err != nil {
return fmt.Errorf("failed to marshal event data: %w", err)
}
msg, err := json.Marshal(
comms.Message{
Type: comms.TypeEventData,
Content: eventData,
})
msg, err := comms.EventData{
Event: comms.EventScreenLoaded,
}.Serialise()
if err != nil {
return fmt.Errorf("failed to marshal message: %w", err)
return fmt.Errorf("failed to serialise event data: %w", err)
}

return s.sendToOpponent(msg)
Expand All @@ -428,20 +409,11 @@ func (s *MultiplayerScreen) handleEventData(data comms.EventData) error {

// requestOpponentData sends a request for the opponent to send their game data.
func (s *MultiplayerScreen) requestOpponentGameData() error {
request, err := json.Marshal(
comms.RequestData{
Request: comms.TypeGameData,
})
if err != nil {
return fmt.Errorf("failed to marshal event data: %w", err)
}
msg, err := json.Marshal(
comms.Message{
Type: comms.TypeRequest,
Content: request,
})
msg, err := comms.RequestData{
Request: comms.TypeGameData,
}.Serialise()
if err != nil {
return fmt.Errorf("failed to marshal message: %w", err)
return fmt.Errorf("failed to serialise request data: %w", err)
}

if err := s.sendToOpponent(msg); err != nil {
Expand Down
38 changes: 11 additions & 27 deletions screen/multiplayer_host.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package screen

import (
"encoding/json"
"errors"
"fmt"
"time"
Expand Down Expand Up @@ -206,34 +205,25 @@ func (s *MultiplayerHostScreen) handleClientData(data []byte) error {
}
return s.handlePlayerData(data)
default:
// Ignore other message types - don't return error
// Ignore other message type - don't return error
return nil
}
}

// sendPlayerData sends the player data to all connected guests.
func (s *MultiplayerHostScreen) sendPlayerData() error {
playerData, err := json.Marshal(
comms.PlayerData{
Version: config.Version,
Username: s.nameEntry.Text(),
})
if err != nil {
return fmt.Errorf("failed to marshal player data: %w", err)
}
msg, err := json.Marshal(
comms.Message{
Type: comms.TypePlayerData,
Content: playerData,
})
msg, err := comms.PlayerData{
Version: config.Version,
Username: s.nameEntry.Text(),
}.Serialise()
if err != nil {
return fmt.Errorf("failed to marshal joining message: %w", err)
return fmt.Errorf("failed to serialise player data: %w", err)
}

// Send data to client
for _, id := range s.server.GetClientIDs() {
if err := s.server.WriteToClient(id, msg); err != nil {
return fmt.Errorf("failed to send message to server: %w", err)
return fmt.Errorf("failed to send message to client: %w", err)
}
}

Expand Down Expand Up @@ -288,17 +278,11 @@ func (s *MultiplayerHostScreen) startGame() error {
}

// Inform other players that game is starting
eventData, err := json.Marshal(comms.EventData{Event: comms.EventHostStartGame})
if err != nil {
return fmt.Errorf("failed to marshal event data: %w", err)
}
msg, err := json.Marshal(
comms.Message{
Type: comms.TypeEventData,
Content: eventData,
})
msg, err := comms.EventData{
Event: comms.EventHostStartGame,
}.Serialise()
if err != nil {
return fmt.Errorf("failed to marshal event message: %w", err)
return fmt.Errorf("failed to serialise event data: %w", err)
}
for _, id := range s.server.GetClientIDs() {
if err := s.server.WriteToClient(id, msg); err != nil {
Expand Down
16 changes: 3 additions & 13 deletions screen/multiplayer_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package screen

import (
"context"
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -293,23 +292,14 @@ func (s *MultiplayerJoinScreen) handleEventData(data comms.EventData) error {

// sendPlayerData sends the player data to the host.
func (s *MultiplayerJoinScreen) sendPlayerData() error {
// Construct message containing player data
playerData, err := json.Marshal(comms.PlayerData{
msg, err := comms.PlayerData{
Version: config.Version,
Username: s.nameEntry.Text(),
})
if err != nil {
return fmt.Errorf("failed to marshal player data: %w", err)
}
msg, err := json.Marshal(comms.Message{
Type: comms.TypePlayerData,
Content: playerData,
})
}.Serialise()
if err != nil {
return fmt.Errorf("failed to marshal joining message: %w", err)
return fmt.Errorf("failed to serialise player data: %w", err)
}

// Send data to host
return s.client.Write(msg)
}

Expand Down

0 comments on commit 7838989

Please sign in to comment.