2022-10-22 18:01:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-05-28 15:43:05 +00:00
|
|
|
"context"
|
2023-05-31 21:55:40 +00:00
|
|
|
"local/m/mchess_server/chess"
|
|
|
|
"local/m/mchess_server/usher"
|
2022-11-19 10:41:10 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2022-12-18 14:43:27 +00:00
|
|
|
"os"
|
2023-04-18 20:12:05 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/autotls"
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-04-18 20:19:28 +00:00
|
|
|
"github.com/google/uuid"
|
2023-04-22 19:41:24 +00:00
|
|
|
"nhooyr.io/websocket"
|
2022-10-22 18:01:55 +00:00
|
|
|
)
|
|
|
|
|
2022-12-18 01:49:10 +00:00
|
|
|
var cert_path = "/etc/letsencrypt/live/chess.sw-gross.de/"
|
|
|
|
var cert_file = cert_path + "fullchain.pem"
|
|
|
|
var key_file = cert_path + "privkey.pem"
|
|
|
|
|
2023-04-18 20:12:05 +00:00
|
|
|
func main() {
|
2022-12-18 14:43:27 +00:00
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
2023-04-18 20:12:05 +00:00
|
|
|
log.Fatal(err)
|
2022-12-18 14:43:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 20:12:05 +00:00
|
|
|
router := gin.Default()
|
2023-04-18 20:47:51 +00:00
|
|
|
|
2023-04-22 19:41:24 +00:00
|
|
|
router.GET("/api/random", registerForRandomGame)
|
2023-05-28 15:43:05 +00:00
|
|
|
router.GET("/api/ws", registerWebSocketConnection)
|
2022-11-19 10:41:10 +00:00
|
|
|
|
2023-04-18 20:12:05 +00:00
|
|
|
if hostname == "mbook" {
|
|
|
|
log.Println("Starting service WITHOUT TLS")
|
|
|
|
log.Fatal(router.Run("localhost:8080"))
|
2022-12-18 14:56:06 +00:00
|
|
|
} else {
|
2023-04-18 20:12:05 +00:00
|
|
|
log.Fatal(autotls.Run(router, "chess.sw-gross.de"))
|
2022-11-19 10:41:10 +00:00
|
|
|
}
|
2023-04-18 20:12:05 +00:00
|
|
|
}
|
2022-11-19 10:41:10 +00:00
|
|
|
|
2023-04-22 19:41:24 +00:00
|
|
|
func registerForRandomGame(c *gin.Context) {
|
2023-05-30 21:36:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
What should be done:
|
|
|
|
1. Register player
|
|
|
|
2. Check if there is a game open that lacks one player
|
|
|
|
3. Fill open game, then respond with player id and game id.
|
|
|
|
OR
|
|
|
|
|
|
|
|
1. Register player
|
|
|
|
2. If there is no open game, open a game and wait for a second player to join
|
|
|
|
3. Only after a second player joins, respond with player id and game id.
|
|
|
|
*/
|
|
|
|
|
2023-05-31 21:55:40 +00:00
|
|
|
player := chess.NewPlayer(uuid.New())
|
2023-05-30 20:01:20 +00:00
|
|
|
|
2023-05-31 21:55:40 +00:00
|
|
|
usher := usher.GetUsher()
|
|
|
|
lobby := usher.WelcomeNewPlayer(player)
|
|
|
|
usher.AddPlayerToLobby(player, lobby)
|
|
|
|
|
|
|
|
// Counter point to this approach:
|
|
|
|
// Waiting for two players might not be necessary.
|
|
|
|
// Just open the lobby and respond with lobby/game id and player id
|
|
|
|
// The waiting can be done in the game handler until both players opened a ws connection
|
|
|
|
usher.WaitForTwoPlayersInLobby(lobby)
|
2023-04-22 19:41:24 +00:00
|
|
|
|
2023-05-28 15:43:05 +00:00
|
|
|
log.Println("responding with player id ", player.Uuid)
|
|
|
|
|
2023-05-31 21:55:40 +00:00
|
|
|
c.IndentedJSON(http.StatusOK, chess.PlayerInfo{
|
2023-04-22 19:41:24 +00:00
|
|
|
PlayerID: player.Uuid,
|
|
|
|
})
|
2023-04-18 20:12:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-22 19:41:24 +00:00
|
|
|
func registerWebSocketConnection(c *gin.Context) {
|
2023-05-28 15:43:05 +00:00
|
|
|
webSocketConn, err := websocket.Accept(c.Writer, c.Request, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go waitForAndHandlePlayerID(c, *webSocketConn)
|
|
|
|
}
|
2023-04-18 20:12:05 +00:00
|
|
|
|
2023-05-28 15:43:05 +00:00
|
|
|
func waitForAndHandlePlayerID(ctx context.Context, conn websocket.Conn) {
|
|
|
|
msgType, id, err := conn.Read(ctx)
|
|
|
|
|
|
|
|
log.Println("read from websocket: ", msgType, id, err)
|
|
|
|
log.Println("id as string", string(id))
|
|
|
|
|
|
|
|
uuid, err := uuid.ParseBytes(id)
|
2023-04-22 19:41:24 +00:00
|
|
|
if err != nil {
|
2023-05-28 15:43:05 +00:00
|
|
|
log.Println(err)
|
|
|
|
conn.Close(websocket.StatusCode(400), err.Error())
|
|
|
|
return
|
2023-04-22 19:41:24 +00:00
|
|
|
}
|
2023-04-18 20:12:05 +00:00
|
|
|
|
2023-05-31 21:55:40 +00:00
|
|
|
// since we cannot use the lobby anymore (it does not exist anymore after this change) the client needs to
|
|
|
|
// send game id and player id so we can make the connection to the player
|
|
|
|
//lobby := chess.GetLobby()
|
|
|
|
|
2023-05-30 20:01:20 +00:00
|
|
|
player, found := lobby.GetPlayer(uuid)
|
2023-05-28 15:43:05 +00:00
|
|
|
if !found {
|
|
|
|
conn.Close(websocket.StatusCode(400), "player not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if player.Conn != nil {
|
|
|
|
player.Conn.Close(websocket.StatusCode(400), "closing existing connection")
|
2023-04-18 20:12:05 +00:00
|
|
|
}
|
2023-05-30 20:01:20 +00:00
|
|
|
player.SetConnection(ctx, conn)
|
|
|
|
log.Println("player after setting connection: ", player)
|
2022-12-14 21:19:47 +00:00
|
|
|
}
|