Skip to content

Commit e9d3b36

Browse files
committed
wip
1 parent 38f7529 commit e9d3b36

16 files changed

+615
-911
lines changed

internal/api/http.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func (handler *V1Handler) SetupRoutes(rootPath string, register func(string, str
3131
// We support both path parameter and cookie.
3232
register("GET", path.Join(v1, "lobby", "ws"), handler.websocketUpgrade)
3333

34+
register("POST", path.Join(v1, "lobby", "resurrect"), handler.resurrectLobby)
3435
register("POST", path.Join(v1, "lobby", "{lobby_id}", "player"), handler.postPlayer)
3536
}
3637

internal/api/v1.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package api
66

77
import (
8+
"encoding/base64"
89
"errors"
910
"fmt"
1011
"log"
@@ -71,7 +72,7 @@ func (handler *V1Handler) getLobbies(writer http.ResponseWriter, _ *http.Request
7172
MaxClientsPerIP: lobby.ClientsPerIPLimit,
7273
Wordpack: lobby.Wordpack,
7374
State: lobby.State,
74-
Scoring: lobby.ScoreCalculation.Identifier(),
75+
Scoring: lobby.ScoreCalculationIdentifier,
7576
})
7677
}
7778

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

87+
func (handler *V1Handler) resurrectLobby(writer http.ResponseWriter, request *http.Request) {
88+
var data game.LobbyRestoreData
89+
base64Decoder := base64.NewDecoder(base64.StdEncoding, request.Body)
90+
if err := easyjson.UnmarshalFromReader(base64Decoder, &data); err != nil {
91+
log.Println("Error unmarshalling lobby resurrection data:", err)
92+
http.Error(writer, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
93+
return
94+
}
95+
96+
lobby := data.Lobby
97+
// We add the lobby, while the lobby mutex is aqcuired. This prevents us
98+
// from attempting to connect to the lobby, before the internal state has
99+
// been restored correctly.
100+
lobby.Synchronized(func() {
101+
if state.ResurrectLobby(lobby) {
102+
lobby.WriteObject = WriteObject
103+
lobby.WritePreparedMessage = WritePreparedMessage
104+
lobby.ResurrectUnsynchronized(&data)
105+
}
106+
})
107+
}
108+
86109
func (handler *V1Handler) postLobby(writer http.ResponseWriter, request *http.Request) {
87110
if err := request.ParseForm(); err != nil {
88111
http.Error(writer, err.Error(), http.StatusBadRequest)

0 commit comments

Comments
 (0)