Skip to content

Commit

Permalink
add reset
Browse files Browse the repository at this point in the history
  • Loading branch information
loan-mgt committed May 19, 2024
1 parent 6dfe851 commit 30c54a0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 16 deletions.
69 changes: 56 additions & 13 deletions internal/handlers/moveHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type MoveRequest struct {

type MoveData struct {
TargetId string
Move string
Move *string
}

type Messenger struct {
Expand Down Expand Up @@ -48,7 +48,7 @@ func HandleMove(message []byte, conn *websocket.Conn) error {

playerMoveData := MoveData{
TargetId: "player-selected-move",
Move: request.Move,
Move: &request.Move,
}

var tplBuffer bytes.Buffer
Expand All @@ -73,26 +73,53 @@ func HandleMove(message []byte, conn *websocket.Conn) error {
if err != nil {
log.Println("Failed to send opponent move:", err)
}

err = sendMove(player.Conn, "opponent", opponent.Move)
if err != nil {
log.Println("Failed to send opponent move:", err)
}

message := "next round in 3s"
if err := sendMessage(conn, message); err != nil {
log.Println("Error sending message to player:", err)
}

opponent, err := services.GetOpponent(player.Name)
if err != nil {
log.Println("Failed getting opponent:", err)
} else {
if err := sendMessage(opponent.Conn, message); err != nil {
log.Println("Error sending message to opponent:", err)
}
}

go func() {
time.Sleep(5 * time.Second)
message := "test after 5s"
if err := sendMessage(conn, message); err != nil {
log.Println("Error sending message to player:", err)
time.Sleep(3 * time.Second)

services.SetPlayerMove(player.Name, "")

services.SetPlayerMove(opponent.Name, "")

err = resetMove(opponent.Conn, "player")
if err != nil {
log.Println("Failed to send player move:", err)
}

err = resetMove(opponent.Conn, "opponent")
if err != nil {
log.Println("Failed to send opponent move:", err)
}

err = resetMove(player.Conn, "player")
if err != nil {
log.Println("Failed to send player move:", err)
}

opponent, err := services.GetOpponent(player.Name)
err = resetMove(player.Conn, "opponent")
if err != nil {
log.Println("Failed getting opponent:", err)
} else {
if err := sendMessage(opponent.Conn, message); err != nil {
log.Println("Error sending message to opponent:", err)
}
log.Println("Failed to send opponent move:", err)
}

}()

}
Expand Down Expand Up @@ -141,7 +168,23 @@ func sendMessage(conn *websocket.Conn, message string) error {
func sendMove(conn *websocket.Conn, to string, move string) error {
moveData := MoveData{
TargetId: fmt.Sprintf("%s-selected-move", to),
Move: move,
Move: &move,
}

var tplBufferOpponent bytes.Buffer
err := utils.Templates.ExecuteTemplate(&tplBufferOpponent, "move", moveData)
if err != nil {
log.Println("Error executing template for opponent:", err)
return err
}

return conn.WriteMessage(websocket.TextMessage, tplBufferOpponent.Bytes())

}

func resetMove(conn *websocket.Conn, to string) error {
moveData := MoveData{
TargetId: fmt.Sprintf("%s-selected-move", to),
}

var tplBufferOpponent bytes.Buffer
Expand Down
8 changes: 5 additions & 3 deletions template/move.html.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{{ define "move" }}
<div hx-swap-oob="innerHTML:#{{.TargetId}}">
<img class="w-full h-full" src="/assets/images/{{.Move}}.svg" alt="{{.Move}}" />
</div>
<div hx-swap-oob="innerHTML:#{{.TargetId}}">
{{ if .Move }}
<img class="w-full h-full" src="/assets/images/{{.Move}}.svg" alt="{{.Move}}" />
{{ end }}
</div>
{{ end }}

0 comments on commit 30c54a0

Please sign in to comment.