2023-07-18 11:58:48 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-08-20 12:50:21 +01:00
|
|
|
"encoding/json"
|
2023-07-26 22:09:31 +01:00
|
|
|
"fmt"
|
2023-07-28 18:17:01 +01:00
|
|
|
_ "github.com/joho/godotenv/autoload"
|
2023-07-26 21:16:25 +01:00
|
|
|
"net/http"
|
2023-07-18 11:58:48 +01:00
|
|
|
"os"
|
2023-08-05 12:56:01 +01:00
|
|
|
|
|
|
|
// Stylish stuff
|
|
|
|
"github.com/charmbracelet/log"
|
2023-08-20 12:50:21 +01:00
|
|
|
|
|
|
|
// Websockets
|
|
|
|
"github.com/gorilla/websocket"
|
2023-07-18 11:58:48 +01:00
|
|
|
)
|
|
|
|
|
2023-07-26 21:16:25 +01:00
|
|
|
type App struct {
|
|
|
|
UserHandler *UserHandler
|
2023-08-20 12:50:21 +01:00
|
|
|
clients map[*websocket.Conn]bool
|
|
|
|
broadcast chan []byte
|
|
|
|
upgrader websocket.Upgrader
|
|
|
|
}
|
|
|
|
|
|
|
|
type Player struct {
|
|
|
|
X int `json:"x"`
|
|
|
|
Y int `json:"y"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GameState struct {
|
|
|
|
Players []Player `json:"players"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *App) handlePlayerMovement(conn *websocket.Conn) {
|
|
|
|
var player Player
|
|
|
|
player.X = 0
|
|
|
|
player.Y = 0
|
|
|
|
|
|
|
|
for {
|
|
|
|
// Read the next message from the client
|
|
|
|
_, message, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to read message:", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the player's position based on the received message
|
|
|
|
switch string(message) {
|
|
|
|
case "up":
|
|
|
|
player.Y--
|
|
|
|
case "down":
|
|
|
|
player.Y++
|
|
|
|
case "left":
|
|
|
|
player.X--
|
|
|
|
case "right":
|
|
|
|
player.X++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a JSON representation of the game state
|
|
|
|
gameState := GameState{
|
|
|
|
Players: []Player{player},
|
|
|
|
}
|
|
|
|
gameStateData, err := json.Marshal(gameState)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to marshal game state:", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Broadcast the game state to all clients
|
|
|
|
s.broadcast <- gameStateData
|
|
|
|
}
|
2023-07-26 21:16:25 +01:00
|
|
|
}
|
2023-07-18 11:58:48 +01:00
|
|
|
|
2023-08-20 12:50:21 +01:00
|
|
|
func (s *App) handleBroadcasts() {
|
|
|
|
for {
|
|
|
|
// Read the next message from the broadcast channel
|
|
|
|
message := <-s.broadcast
|
|
|
|
|
|
|
|
// Broadcast the message to all clients
|
|
|
|
for client := range s.clients {
|
|
|
|
err := client.WriteMessage(websocket.TextMessage, message)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to write message:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{}
|
|
|
|
|
2023-07-26 22:09:31 +01:00
|
|
|
// Define the serve function
|
2023-08-20 12:50:21 +01:00
|
|
|
func (s *App) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
2023-07-26 21:16:25 +01:00
|
|
|
var head string
|
|
|
|
head, req.URL.Path = ShiftPath(req.URL.Path)
|
|
|
|
switch head {
|
2023-07-26 22:09:31 +01:00
|
|
|
// Start the user handler should the requested user be found
|
2023-07-26 21:16:25 +01:00
|
|
|
case "user":
|
2023-08-20 12:50:21 +01:00
|
|
|
s.UserHandler.Handle(res, req)
|
2023-07-26 22:09:31 +01:00
|
|
|
// Return a `Not Found` if the user is not found
|
2023-07-26 21:16:25 +01:00
|
|
|
default:
|
|
|
|
http.Error(res, "Not Found", http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|
2023-07-18 11:58:48 +01:00
|
|
|
|
2023-07-26 22:09:31 +01:00
|
|
|
// Run the server
|
2023-08-20 12:50:21 +01:00
|
|
|
func (s *App) Run() {
|
|
|
|
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Upgrade the connection to a WebSocket connection
|
|
|
|
conn, err := s.upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to upgrade connection:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the client connection to the clients map
|
|
|
|
s.clients[conn] = true
|
|
|
|
|
|
|
|
// Log when a client connects
|
|
|
|
log.Info("Client connected:", conn.RemoteAddr())
|
|
|
|
|
|
|
|
// Allow the server to handle player movement
|
|
|
|
go s.handlePlayerMovement(conn)
|
|
|
|
|
|
|
|
// Close the connection and remove it from the clients map
|
|
|
|
defer func() {
|
|
|
|
// Log when a client disconnects
|
|
|
|
log.Info("Client disconnected:", conn.RemoteAddr())
|
|
|
|
|
|
|
|
conn.Close()
|
|
|
|
delete(s.clients, conn)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Handle incoming messages
|
|
|
|
for {
|
|
|
|
_, message, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to read message:", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Broadcast the received message to all clients
|
|
|
|
s.broadcast <- message
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
go s.handleBroadcasts()
|
|
|
|
|
|
|
|
http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
msgType, msg, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%s sent: %s\n", conn.RemoteAddr(), string(msg))
|
|
|
|
|
|
|
|
if err = conn.WriteMessage(msgType, msg); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-26 21:16:25 +01:00
|
|
|
func main() {
|
2023-08-20 12:50:21 +01:00
|
|
|
// Make the jwt_secret file within the server configuration directory
|
|
|
|
makeSecret()
|
|
|
|
|
2023-07-26 22:09:31 +01:00
|
|
|
// Initialise the user handler
|
2023-07-26 21:16:25 +01:00
|
|
|
user_handler, err := NewUserHandler()
|
2023-07-26 22:09:31 +01:00
|
|
|
|
|
|
|
// Log any errors
|
2023-07-18 11:58:48 +01:00
|
|
|
if err != nil {
|
2023-08-05 12:56:01 +01:00
|
|
|
log.Fatal(err)
|
2023-07-18 11:58:48 +01:00
|
|
|
}
|
|
|
|
|
2023-07-26 21:16:25 +01:00
|
|
|
a := &App{
|
|
|
|
UserHandler: user_handler,
|
2023-07-18 11:58:48 +01:00
|
|
|
}
|
|
|
|
|
2023-07-26 21:16:25 +01:00
|
|
|
port := os.Getenv("PORT")
|
|
|
|
if port == "" {
|
|
|
|
port = "7741"
|
|
|
|
}
|
2023-07-26 22:09:31 +01:00
|
|
|
// Log that the program has successfully started listening to the port
|
2023-08-05 12:56:01 +01:00
|
|
|
log.Info(fmt.Sprintf("Ambition backend listening to port %v", port))
|
2023-07-26 21:16:25 +01:00
|
|
|
http.ListenAndServe(":"+port, a)
|
2023-08-20 12:50:21 +01:00
|
|
|
|
2023-07-18 11:58:48 +01:00
|
|
|
}
|