-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor messages #9
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,15 +124,15 @@ 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) | ||
} | ||
|
||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このパッチのコードレビューを行います。
改善点としては、 |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このコードパッチは単体テストを修正するためのものです、以下の改善提案とバグのリスクがあります。
以上のような改善策や注目点があげられます。 |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このコードパッチは、
以上のような変更を行うことによって、コードの可読性と保守性が向上する可能性があります。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このコードパッチは、 バグリスクがあるかどうかは自明ではありませんが、 |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このコードパッチは、messagesパッケージに含まれる様々なメッセージタイプに対応するGamePlayControllerの関数をアップデートしています。メッセージタイプの名称が変更されており、全て "Request" を含んでいます。
以上の点を考慮することで、より読みやすくかつエラーの少ないコードを書くことができます。 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このコードパッチは、メッセージ型定数(messages)での変更と関数名の微調整を含みます。このパッチ自体には問題はなさそうで、リファクタリングされていますが、他のソースコードとの互換性の観点から、機能の変更や不足がないか確認が必要です。 改善提案:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Request, responseの命名規則を採用します |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このパッチのコードレビューを行います。 48行目のif文で、Messages型の変数msgがmessages.RoomCreatedと一致するか確認しています。この場合、messages.CreateRoomと比較されるべきです。また、47行目にmsg変数の宣言が欠落しているため、その修正が必要です。 105行目のif文では、message.Typeがmessages.RoomListResponseと一致するかどうかを確認しています。これは正しい比較です。 TestRoomController_LeaveRoom ()関数の150行目のif文で、msg.Typeがmessages.RoomLeftという値であることを確認しています。これは正しい比較です。 以上から、欠陥や問題は見つかりませんでした。しかし、47行目の修正が必要です。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このコードのパッチでは、以下のような変更がされています。
この変更によって、テストで期待されるメッセージと実際のメッセージが一致しなかった場合にエラーメッセージが出力されるようになりました。 このコードに問題があるかどうか、また改善提案があるかどうかは、与えられたコード断片自体からは決定できません。全体的にどのように使われているのか、その文脈に基づいて評価する必要があります。ただし、与えられた部分自体は、メッセージタイプの比較を正しく行っているように見えます。 |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以下は、ローカルにあるGoコードですね。いくつかの問題や改善提案があります。
以上、ご参考までに。