From 45313dc3c2fadd128181a2bb39f8c52e43120b50 Mon Sep 17 00:00:00 2001 From: edge2992 Date: Sun, 30 Apr 2023 12:04:27 +0900 Subject: [PATCH 1/2] refactor(messages): rename message types --- client/client.go | 16 ++++++++-------- client/client_test.go | 12 ++++++------ messages/messages.go | 34 +++++++++++++++++----------------- server/gameplay_controller.go | 6 +++--- server/room_controller.go | 16 ++++++++-------- server/room_controller_test.go | 6 +++--- 6 files changed, 45 insertions(+), 45 deletions(-) diff --git a/client/client.go b/client/client.go index cfb4bb5..4270fff 100644 --- a/client/client.go +++ b/client/client.go @@ -88,9 +88,9 @@ func (c *Client) handleMessages() { func (c *Client) processMessage(message *messages.Message) { switch message.Type { - case messages.CreateRoom: + case messages.RoomCreated: c.handleCreateRoomMessage(message) - case messages.JoinRoom: + case messages.RoomJoined: c.handleJoinRoomMessage(message) case messages.PlayerLeft: c.handleLeaveRoomMessage(message) @@ -124,7 +124,7 @@ func (c *Client) Run() { func (c *Client) CreateRoom(roomName string) { message := messages.Message{ - Type: messages.CreateRoom, + Type: messages.RequestCreateRoom, RoomID: roomName, // ignored by server } c.connection.Encode(&message) @@ -132,7 +132,7 @@ func (c *Client) CreateRoom(roomName string) { func (c *Client) requestRoomList() []string { message := messages.Message{ - Type: messages.ListRooms, + Type: messages.RequestRoomList, } c.sendMessage(&message) @@ -144,7 +144,7 @@ func (c *Client) requestRoomList() []string { func (c *Client) JoinRoom(roomID string) { message := messages.Message{ - Type: messages.JoinRoom, + Type: messages.RequestJoinRoom, RoomID: roomID, } c.sendMessage(&message) @@ -190,7 +190,7 @@ func (c *Client) handleTurnStarted(roomID string) { log.Println("It's your turn!") c.turnFlag = true message := messages.Message{ - Type: messages.DiceRolled, + Type: messages.RequestRollDice, RoomID: roomID, } c.sendMessage(&message) @@ -211,7 +211,7 @@ func (c *Client) reRollDice(dice []game.Dice, roomID string) { selectedIndices := c.ioHandler.GetPlayerHoldInput(dice) game.HoldDice(dice, selectedIndices) message := messages.Message{ - Type: messages.RerollDice, + Type: messages.RequestRerollDice, RoomID: roomID, Dice: dice, } @@ -221,7 +221,7 @@ func (c *Client) reRollDice(dice []game.Dice, roomID string) { func (c *Client) chooseCategory(player *game.PlayerInfo, dice []game.Dice, roomID string) { category := c.ioHandler.ChooseCategory(player, dice) message := messages.Message{ - Type: messages.ChooseCategory, + Type: messages.RequestChooseCategory, RoomID: roomID, Player: player, Category: category, diff --git a/client/client_test.go b/client/client_test.go index 8180476..fc43ed3 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -22,7 +22,7 @@ func TestCreateRoom(t *testing.T) { msg := conn.TopEncodedMessage().(*messages.Message) - if msg.Type != messages.CreateRoom { + if msg.Type != messages.RequestCreateRoom { t.Fatalf("Expected message type to be CreateRoom, got %d", msg.Type) } } @@ -41,7 +41,7 @@ func TestJoinRoom(t *testing.T) { msg := conn.TopEncodedMessage().(*messages.Message) - if msg.Type != messages.JoinRoom { + if msg.Type != messages.RequestJoinRoom { t.Fatalf("Expected message type to be JoinRoom, got %d", msg.Type) } } @@ -99,7 +99,7 @@ func TestHandleTurnStarted(t *testing.T) { t.Fatal("Expected messages.Message type") } - if msg.Type != messages.DiceRolled { + if msg.Type != messages.RequestRollDice { t.Fatalf("Expected message type to be DiceRolled, got %d", msg.Type) } } @@ -144,7 +144,7 @@ func TestHandleDiceRolled(t *testing.T) { t.Fatal("Expected messages.Message type") } - if msg.Type != messages.RerollDice { + if msg.Type != messages.RequestRerollDice { t.Fatalf("Expected message type to be RerollDice, got %d", msg.Type) } @@ -182,7 +182,7 @@ func TestReRollDice(t *testing.T) { t.Fatal("Expected messages.Message type") } - if msg.Type != messages.RerollDice { + if msg.Type != messages.RequestRerollDice { t.Fatalf("Expected message type to be RerollDice, got %d", msg.Type) } @@ -216,7 +216,7 @@ func TestChooseCategory(t *testing.T) { t.Fatal("Expected messages.Message type") } - if msg.Type != messages.ChooseCategory { + if msg.Type != messages.RequestChooseCategory { t.Fatalf("Expected message type to be ChooseCategory, got %d", msg.Type) } diff --git a/messages/messages.go b/messages/messages.go index d635cf1..171aa10 100644 --- a/messages/messages.go +++ b/messages/messages.go @@ -5,31 +5,31 @@ import "yatzcli/game" type MessageType int const ( - GameJoined MessageType = iota - GameLeft + ServerJoin MessageType = iota + WaitForPlayers + RoomFull + RoomCreated + RoomJoined + RoomLeft + GamePlayerJoined + GamePlayerLeft GameStart GameStarted - PlayerReady - PlayerJoined - PlayerJoinedRoom + GameOver PlayerLeft DiceRolled - RerollDice TurnStarted TurnPlayed - ChooseCategory UpdateScorecard - RollDice UpdateGameState - GameOver - CreateRoom - JoinRoom - LeaveRoom - ListRooms - ListRoomsResponse - WaitForPlayers - RoomFull - ServerJoin + RequestRollDice + RequestRerollDice + RequestChooseCategory + RequestCreateRoom + RequestJoinRoom + RequestLeaveRoom + RequestRoomList + RoomListResponse ) type Message struct { diff --git a/server/gameplay_controller.go b/server/gameplay_controller.go index 75be87d..b84d0f8 100644 --- a/server/gameplay_controller.go +++ b/server/gameplay_controller.go @@ -169,11 +169,11 @@ func (gpc *GamePlayController) HandleMessage(message *messages.Message, player * switch message.Type { case messages.TurnStarted: gpc.StartTurn(message.RoomID, player) - case messages.DiceRolled: + case messages.RequestRollDice: gpc.RollDice(message.RoomID, player) - case messages.RerollDice: + case messages.RequestRerollDice: gpc.RerollDice(message.RoomID, player, message.Dice) - case messages.ChooseCategory: + case messages.RequestChooseCategory: gpc.ChooseScoreCategory(message.RoomID, player, message.Category) } } diff --git a/server/room_controller.go b/server/room_controller.go index c00868f..36f94dd 100644 --- a/server/room_controller.go +++ b/server/room_controller.go @@ -41,7 +41,7 @@ func (rc *RoomController) CreateRoom(player *Player) { rc.roomManager.JoinRoom(roomID, player) message := messages.Message{ - Type: messages.CreateRoom, + Type: messages.RoomCreated, Player: player.PlayerInfo(), RoomID: roomID, } @@ -91,7 +91,7 @@ func (rc *RoomController) addPlayerToRoom(roomID string, player *Player) { func (rc *RoomController) notifyPlayerJoinedRoomToOthers(room *Room, player *Player) { message := messages.Message{ - Type: messages.JoinRoom, + Type: messages.RoomJoined, Player: player.PlayerInfo(), RoomID: room.ID, } @@ -107,7 +107,7 @@ func (rc *RoomController) ListRooms(player *Player) { } message := messages.Message{ - Type: messages.ListRoomsResponse, + Type: messages.RoomListResponse, Player: player.PlayerInfo(), RoomList: roomList, } @@ -122,7 +122,7 @@ func (rc *RoomController) LeaveRoom(roomID string, player *Player) { } message := messages.Message{ - Type: messages.LeaveRoom, + Type: messages.RoomLeft, Player: player.PlayerInfo(), RoomID: roomID, } @@ -138,13 +138,13 @@ func (rc *RoomController) LeaveRoom(roomID string, player *Player) { func (rc *RoomController) HandleMessage(message *messages.Message, player *Player) { switch message.Type { - case messages.CreateRoom: + case messages.RequestCreateRoom: rc.CreateRoom(player) - case messages.JoinRoom: + case messages.RequestJoinRoom: rc.JoinRoom(message.RoomID, player) - case messages.ListRooms: + case messages.RequestRoomList: rc.ListRooms(player) - case messages.LeaveRoom: + case messages.RequestLeaveRoom: rc.LeaveRoom(message.RoomID, player) } } diff --git a/server/room_controller_test.go b/server/room_controller_test.go index 9943aa2..8c8d883 100644 --- a/server/room_controller_test.go +++ b/server/room_controller_test.go @@ -48,7 +48,7 @@ func TestRoomController_CreateRoom(t *testing.T) { } msg := mockConn.EncodedMessages[0].(*messages.Message) - if msg.Type != messages.CreateRoom { + if msg.Type != messages.RoomCreated { t.Error("Expected message to be CreateRoom") } @@ -105,7 +105,7 @@ func TestRoomController_ListRooms(t *testing.T) { t.Fatalf("Expected message, got %T", mockConn.EncodedMessages[0]) } - if message.Type != messages.ListRoomsResponse { + if message.Type != messages.RoomListResponse { t.Errorf("Expected message type ListRoomsResponse, got %d", message.Type) } @@ -150,7 +150,7 @@ func TestRoomController_LeaveRoom(t *testing.T) { msg := mockConn.TopEncodedMessage().(*messages.Message) - if msg.Type != messages.LeaveRoom { + if msg.Type != messages.RoomLeft { t.Error("Expected message to be LeaveRoom") } From 1d48d87be3670fa8d47c494e6e8d3d2608fad57f Mon Sep 17 00:00:00 2001 From: edge2992 Date: Sun, 30 Apr 2023 15:30:22 +0900 Subject: [PATCH 2/2] refactor: rename messages.PlayerLeft to messages.RoomLeft & remove unused message types --- client/client.go | 2 +- messages/messages.go | 6 ------ server/room_controller_test.go | 2 +- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/client/client.go b/client/client.go index 4270fff..8537180 100644 --- a/client/client.go +++ b/client/client.go @@ -92,7 +92,7 @@ func (c *Client) processMessage(message *messages.Message) { c.handleCreateRoomMessage(message) case messages.RoomJoined: c.handleJoinRoomMessage(message) - case messages.PlayerLeft: + case messages.RoomLeft: c.handleLeaveRoomMessage(message) case messages.GameStarted: c.handleGameStartedMessage(message) diff --git a/messages/messages.go b/messages/messages.go index 171aa10..e71b75a 100644 --- a/messages/messages.go +++ b/messages/messages.go @@ -11,17 +11,11 @@ const ( RoomCreated RoomJoined RoomLeft - GamePlayerJoined - GamePlayerLeft - GameStart GameStarted GameOver - PlayerLeft DiceRolled TurnStarted - TurnPlayed UpdateScorecard - UpdateGameState RequestRollDice RequestRerollDice RequestChooseCategory diff --git a/server/room_controller_test.go b/server/room_controller_test.go index 8c8d883..e490d85 100644 --- a/server/room_controller_test.go +++ b/server/room_controller_test.go @@ -49,7 +49,7 @@ func TestRoomController_CreateRoom(t *testing.T) { msg := mockConn.EncodedMessages[0].(*messages.Message) if msg.Type != messages.RoomCreated { - t.Error("Expected message to be CreateRoom") + t.Error("Expected message to be RoomCreated") } if msg.Player.Name != player.Name {