Skip to content

Commit

Permalink
Multiple changes
Browse files Browse the repository at this point in the history
* Throw out easyjson, the generator bugs annoyed me
* move usersessions into the lobby for resurrection
  • Loading branch information
Bios-Marcel committed Jan 19, 2025
1 parent ab82b80 commit 36fe9f6
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 3,763 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/go-chi/cors v1.2.1
github.com/gofrs/uuid/v5 v5.3.0
github.com/lxzan/gws v1.8.8
github.com/mailru/easyjson v0.9.0
github.com/prometheus/client_golang v1.20.5
github.com/stretchr/testify v1.10.0
github.com/subosito/gotenv v1.6.0
Expand All @@ -21,7 +20,6 @@ require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dolthub/maphash v0.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ github.com/gofrs/uuid/v5 v5.3.0 h1:m0mUMr+oVYUdxpMLgSYCZiXe7PuVPnI94+OMeVBNedk=
github.com/gofrs/uuid/v5 v5.3.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand All @@ -31,8 +29,6 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lxzan/gws v1.8.8 h1:st193ZG8qN8sSw8/g/UituFhs7etmKzS7jUqhijg5wM=
github.com/lxzan/gws v1.8.8/go.mod h1:FcGeRMB7HwGuTvMLR24ku0Zx0p6RXqeKASeMc4VYgi4=
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
29 changes: 19 additions & 10 deletions internal/api/v1.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
//go:generate easyjson -all ${GOFILE}

// This file contains the API methods for the public API

package api

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strconv"
"strings"

"github.com/gofrs/uuid/v5"
"github.com/mailru/easyjson"
"github.com/scribble-rs/scribble.rs/internal/config"
"github.com/scribble-rs/scribble.rs/internal/game"
"github.com/scribble-rs/scribble.rs/internal/state"
Expand All @@ -23,7 +22,6 @@ import (

var ErrLobbyNotExistent = errors.New("the requested lobby doesn't exist")

//easyjson:skip
type V1Handler struct {
cfg *config.Config
}
Expand All @@ -34,7 +32,18 @@ func NewHandler(cfg *config.Config) *V1Handler {
}
}

//easyjson:json
func marshalToHTTPWriter(data any, writer http.ResponseWriter) (bool, error) {
bytes, err := json.Marshal(data)
if err != nil {
return false, err
}

writer.Header().Set("Content-Type", "application/json")
writer.Header().Set("Content-Length", strconv.Itoa(len(bytes)))
_, err = writer.Write(bytes)
return true, err
}

type LobbyEntries []*LobbyEntry

// LobbyEntry is an API object for representing a join-able public lobby.
Expand Down Expand Up @@ -75,7 +84,7 @@ func (handler *V1Handler) getLobbies(writer http.ResponseWriter, _ *http.Request
})
}

if started, _, err := easyjson.MarshalToHTTPResponseWriter(lobbyEntries, writer); err != nil {
if started, err := marshalToHTTPWriter(lobbyEntries, writer); err != nil {
if !started {
http.Error(writer, err.Error(), http.StatusInternalServerError)
}
Expand All @@ -86,7 +95,7 @@ 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 {
if err := json.NewDecoder(base64Decoder).Decode(&data); err != nil {
log.Println("Error unmarshalling lobby resurrection data:", err)
http.Error(writer, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
Expand Down Expand Up @@ -202,7 +211,7 @@ func (handler *V1Handler) postLobby(writer http.ResponseWriter, request *http.Re

lobbyData := CreateLobbyData(handler.cfg, lobby)

if started, _, err := easyjson.MarshalToHTTPResponseWriter(lobbyData, writer); err != nil {
if started, err := marshalToHTTPWriter(lobbyData, writer); err != nil {
if !started {
http.Error(writer, err.Error(), http.StatusInternalServerError)
}
Expand Down Expand Up @@ -251,7 +260,7 @@ func (handler *V1Handler) postPlayer(writer http.ResponseWriter, request *http.R
})

if lobbyData != nil {
if started, _, err := easyjson.MarshalToHTTPResponseWriter(lobbyData, writer); err != nil {
if started, err := marshalToHTTPWriter(lobbyData, writer); err != nil {
if !started {
http.Error(writer, err.Error(), http.StatusInternalServerError)
}
Expand Down Expand Up @@ -437,7 +446,7 @@ func (handler *V1Handler) patchLobby(writer http.ResponseWriter, request *http.R
}

func (handler *V1Handler) getStats(writer http.ResponseWriter, _ *http.Request) {
if started, _, err := easyjson.MarshalToHTTPResponseWriter(state.Stats(), writer); err != nil {
if started, err := marshalToHTTPWriter(state.Stats(), writer); err != nil {
if !started {
http.Error(writer, err.Error(), http.StatusInternalServerError)
}
Expand Down
Loading

0 comments on commit 36fe9f6

Please sign in to comment.