Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ require (
golang.org/x/term v0.33.0
)

require golang.org/x/sys v0.34.0 // indirect
require (
github.com/gorilla/websocket v1.5.3 // indirect
golang.org/x/sys v0.34.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.33.0 h1:NuFncQrRcaRvVmgRkvM3j/F00gWIAlcmlB8ACEKmGIg=
Expand Down
88 changes: 88 additions & 0 deletions http_input_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"context"
"log"
"net/http"

_ "embed"

"github.com/google/uuid"
"github.com/gorilla/websocket"
)

//go:embed index.html
var frontend []byte

var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}

type HttpInputHandler struct {
playerService PlayerService
server *http.Server
mux *http.ServeMux
playerIdMap map[uuid.UUID]int
}

func NewHttpInputHandler(ps PlayerService) *HttpInputHandler {
handler := &HttpInputHandler{
playerService: ps,
playerIdMap: map[uuid.UUID]int{},

mux: http.NewServeMux(),
}

handler.mux.HandleFunc("GET /", handler.serveFrontend)
handler.mux.HandleFunc("GET /ws", handler.serveWebsocket)

return handler
}

func (k *HttpInputHandler) Listen(addr string) {
k.server = &http.Server{
Addr: addr,
Handler: k.mux,
}
k.server.ListenAndServe()
}

func (h *HttpInputHandler) serveFrontend(w http.ResponseWriter, req *http.Request) {
w.Write(frontend)
}

func (h *HttpInputHandler) serveWebsocket(w http.ResponseWriter, req *http.Request) {
conn, err := upgrader.Upgrade(w, req, nil)
if err != nil {
log.Println("Error upgrading connection:", err)
return
}
defer conn.Close()

playerId := uuid.New()
h.playerService.Join(playerId)

for {
_, msg, err := conn.ReadMessage()
if err != nil {
log.Println("Error reading message:", err)
break
}

switch string(msg) {
case "l":
h.playerService.TurnLeft(playerId)
case "r":
h.playerService.TurnRight(playerId)
}
}
}

func (k *HttpInputHandler) Close() {
if k.server == nil {
return
}
k.server.Shutdown(context.Background())
}
67 changes: 67 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Full Screen Buttons</title>
<style>
html, body {
height: 100%; /* Ensures html and body take full viewport height */
margin: 0; /* Removes default body margin */
overflow: hidden; /* Prevents scrolling if content exceeds viewport */
}

.container {
display: flex; /* Enables flexbox layout */
height: 100%; /* Makes the container fill the entire height of the body */
width: 100%; /* Makes the container fill the entire width of the body */
}

.button {
width: 100%; /* Each button takes an equal share of the available space */
height: 100%;
border: none; /* Removes default button border */
font-size: 64px; /* Adjust font size as needed */
color: white; /* Text color */
cursor: pointer; /* Indicates it's clickable */
touch-action: manipulation;
}

.button:first-child {
background-color: #4CAF50; /* Green for the first button */
}

.button:last-child {
background-color: #008CBA; /* Blue for the second button */
}
</style>
</head>
<body>
<div class="container">
<button onclick="sendAction('l')" class="button">Left</button>
<button onclick="sendAction('r')" class="button">Right</button>
</div>
<script>
const ws = new WebSocket("/ws")
const wsMsgIntervalMs = 100;
let intervalId
let isOpen = false;
function sendAction(action) {
if(isOpen) {
ws.send(action)
}
}
ws.addEventListener("open", (event) => {
intervalId = setInterval(() => {
ws.send("_")
}, wsMsgIntervalMs)
isOpen = true;
});

ws.addEventListener("close", (event) => {
clearInterval(intervalId)
})

</script>
</body>
</html>
File renamed without changes.
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ func main() {
go keyboard.Listen()
defer keyboard.Close()

httpServer := NewHttpInputHandler(game)
defer httpServer.Close()
go httpServer.Listen(":8080")

game.Run()
}
4 changes: 0 additions & 4 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"log"
"os"

"golang.org/x/term"
Expand Down Expand Up @@ -109,8 +108,6 @@ func NewRenderer() *Renderer {
if width > height && width > 100 {
gameCols -= scoreboardWidth
scoreboard = true
} else {
log.Println("Terminal too small for scoreboard, hiding it.")
}

r := Renderer{
Expand Down Expand Up @@ -175,7 +172,6 @@ func (r *Renderer) bufferScoreboard(g *Game, b [][]character) {
}

for i, p := range scoreboard {
log.Println(p.Id)
name := PlayerNames[p.Id]
scoreText := fmt.Sprintf("%s: %d", name, 100)
for j, char := range scoreText {
Expand Down