Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Bios-Marcel committed Jan 5, 2025
1 parent 38f7529 commit e9d3b36
Show file tree
Hide file tree
Showing 16 changed files with 615 additions and 911 deletions.
1 change: 1 addition & 0 deletions internal/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (handler *V1Handler) SetupRoutes(rootPath string, register func(string, str
// We support both path parameter and cookie.
register("GET", path.Join(v1, "lobby", "ws"), handler.websocketUpgrade)

register("POST", path.Join(v1, "lobby", "resurrect"), handler.resurrectLobby)
register("POST", path.Join(v1, "lobby", "{lobby_id}", "player"), handler.postPlayer)
}

Expand Down
25 changes: 24 additions & 1 deletion internal/api/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package api

import (
"encoding/base64"
"errors"
"fmt"
"log"
Expand Down Expand Up @@ -71,7 +72,7 @@ func (handler *V1Handler) getLobbies(writer http.ResponseWriter, _ *http.Request
MaxClientsPerIP: lobby.ClientsPerIPLimit,
Wordpack: lobby.Wordpack,
State: lobby.State,
Scoring: lobby.ScoreCalculation.Identifier(),
Scoring: lobby.ScoreCalculationIdentifier,
})
}

Expand All @@ -83,6 +84,28 @@ func (handler *V1Handler) getLobbies(writer http.ResponseWriter, _ *http.Request
}
}

func (handler *V1Handler) resurrectLobby(writer http.ResponseWriter, request *http.Request) {
var data game.LobbyRestoreData
base64Decoder := base64.NewDecoder(base64.StdEncoding, request.Body)
if err := easyjson.UnmarshalFromReader(base64Decoder, &data); err != nil {
log.Println("Error unmarshalling lobby resurrection data:", err)
http.Error(writer, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

lobby := data.Lobby
// We add the lobby, while the lobby mutex is aqcuired. This prevents us
// from attempting to connect to the lobby, before the internal state has
// been restored correctly.
lobby.Synchronized(func() {
if state.ResurrectLobby(lobby) {
lobby.WriteObject = WriteObject
lobby.WritePreparedMessage = WritePreparedMessage
lobby.ResurrectUnsynchronized(&data)
}
})
}

func (handler *V1Handler) postLobby(writer http.ResponseWriter, request *http.Request) {
if err := request.ParseForm(); err != nil {
http.Error(writer, err.Error(), http.StatusBadRequest)
Expand Down
Loading

0 comments on commit e9d3b36

Please sign in to comment.