1
0
Fork 0
mirror of https://github.com/Luzifer/sii.git synced 2024-12-21 08:21:16 +00:00
sii/cmd/sii-editor/api_profiles.go
Knut Ahlers ab39609f45
Implement Save-Game Editor (#1)
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2019-12-28 13:06:28 +00:00

97 lines
2 KiB
Go

package main
import (
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
)
const savePollTime = 10 * time.Second
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func init() {
router.HandleFunc("/api/profiles", handleListProfiles).Methods(http.MethodGet)
router.HandleFunc("/api/profiles/{profileID}/saves", handleGetProfileSaves).Methods(http.MethodGet)
}
func handleGetProfileSaves(w http.ResponseWriter, r *http.Request) {
var (
subscribe = r.FormValue("subscribe") == "true"
vars = mux.Vars(r)
)
saves, err := listSaves(vars["profileID"])
if err != nil {
apiGenericError(w, http.StatusInternalServerError, err)
return
}
if !subscribe {
apiGenericJSONResponse(w, http.StatusOK, saves)
return
}
// Remember latest save
var latestSave time.Time
for _, s := range saves {
if s.SaveTime.After(latestSave) {
latestSave = s.SaveTime
}
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.WithError(err).Debug("Unable to open websocket")
return
}
defer conn.Close()
if err = conn.WriteJSON(saves); err != nil {
log.WithError(err).Debug("Unable to send saves list")
return
}
for t := time.NewTicker(savePollTime); ; <-t.C {
saves, err = listSaves(vars["profileID"])
if err != nil {
log.WithError(err).Error("Unable to list saves during socket")
return
}
var newSaveTime time.Time
for _, s := range saves {
if s.SaveTime.After(latestSave) {
newSaveTime = s.SaveTime
}
}
if newSaveTime.IsZero() {
// Nothing new
continue
}
if err = conn.WriteJSON(saves); err != nil {
log.WithError(err).Error("Unable to send saves list")
return
}
latestSave = newSaveTime
}
}
func handleListProfiles(w http.ResponseWriter, r *http.Request) {
profiles, err := listProfiles()
if err != nil {
apiGenericError(w, http.StatusInternalServerError, err)
return
}
apiGenericJSONResponse(w, http.StatusOK, profiles)
}