From 20055f24e5c5021b4e2af367c67a4bdd8c91ca13 Mon Sep 17 00:00:00 2001 From: Marcel Schramm Date: Sun, 17 Nov 2024 12:32:08 +0100 Subject: [PATCH] Colors are indices now Technically this makes the actual colors a client concern. However, it also allows the client to have a different color palette an easy way. The main reason for doing this however, was reducing payload size, in preparation for the drawing gallery, to reduce memory usage when saving drawings and potentially persisting them. We might also change this to a binary format, as it'll be much smaller, given the amount of JSON overhead. A minimum line event will currently have a size of 70b, while in binary, it would only have 11 bytes, if we don't microoptimise (less is possible). --- internal/api/v1.go | 17 +- internal/api/v1_easyjson.go | 4 +- internal/frontend/templates/lobby.html | 125 +++++++---- internal/game/shared.go | 34 +-- internal/game/shared_easyjson.go | 286 +++++++++---------------- 5 files changed, 212 insertions(+), 254 deletions(-) diff --git a/internal/api/v1.go b/internal/api/v1.go index de124545..0feb8b3c 100644 --- a/internal/api/v1.go +++ b/internal/api/v1.go @@ -356,15 +356,10 @@ func (handler *V1Handler) getStats(writer http.ResponseWriter, _ *http.Request) } } -var ( - // CanvasColor is the initially / empty canvas colors value used for - // Lobbydata objects. - CanvasColor = game.RGBColor{R: 255, G: 255, B: 255} - // SuggestedBrushSizes is suggested brush sizes value used for - // Lobbydata objects. A unit test makes sure these values are ordered - // and within the specified bounds. - SuggestedBrushSizes = [4]uint8{8, 16, 24, 32} -) +// SuggestedBrushSizes is suggested brush sizes value used for +// Lobbydata objects. A unit test makes sure these values are ordered +// and within the specified bounds. +var SuggestedBrushSizes = [4]uint8{8, 16, 24, 32} // LobbyData is the data necessary for correctly configuring a lobby. // While unofficial clients will probably need all of these values, the @@ -385,7 +380,7 @@ type LobbyData struct { // MaxBrushSize is the maximum amount of pixels the brush can draw in. MaxBrushSize int `json:"maxBrushSize"` // CanvasColor is the initially (empty) color of the canvas. - CanvasColor game.RGBColor `json:"canvasColor"` + CanvasColor uint8 `json:"canvasColor"` // SuggestedBrushSizes are suggestions for the different brush sizes // that the user can choose between. These brushes are guaranteed to // be ordered from low to high and stay with the bounds. @@ -403,7 +398,7 @@ func CreateLobbyData(lobby *game.Lobby) *LobbyData { DrawingBoardBaseHeight: game.DrawingBoardBaseHeight, MinBrushSize: game.MinBrushSize, MaxBrushSize: game.MaxBrushSize, - CanvasColor: CanvasColor, + CanvasColor: 0, /* White */ SuggestedBrushSizes: SuggestedBrushSizes, } } diff --git a/internal/api/v1_easyjson.go b/internal/api/v1_easyjson.go index 8074cf89..77f5088d 100644 --- a/internal/api/v1_easyjson.go +++ b/internal/api/v1_easyjson.go @@ -255,7 +255,7 @@ func easyjson102f8a2fDecodeGithubComScribbleRsScribbleRsInternalApi2(in *jlexer. case "maxBrushSize": out.MaxBrushSize = int(in.Int()) case "canvasColor": - (out.CanvasColor).UnmarshalEasyJSON(in) + out.CanvasColor = uint8(in.Uint8()) case "suggestedBrushSizes": if in.IsNull() { in.Skip() @@ -336,7 +336,7 @@ func easyjson102f8a2fEncodeGithubComScribbleRsScribbleRsInternalApi2(out *jwrite { const prefix string = ",\"canvasColor\":" out.RawString(prefix) - (in.CanvasColor).MarshalEasyJSON(out) + out.Uint8(uint8(in.CanvasColor)) } { const prefix string = ",\"suggestedBrushSizes\":" diff --git a/internal/frontend/templates/lobby.html b/internal/frontend/templates/lobby.html index ef5af491..3d55d81c 100644 --- a/internal/frontend/templates/lobby.html +++ b/internal/frontend/templates/lobby.html @@ -264,59 +264,59 @@ to prevent hover and active colors to appear. -->
+ onmousedown="setColor(0)" onclick="setColor(0)"> + onmousedown="setColor(1)" onclick="setColor(1)"> + onmousedown="setColor(2)" onclick="setColor(2)"> + onmousedown="setColor(3)" onclick="setColor(3)"> + onmousedown="setColor(4)" onclick="setColor(4)"> + onmousedown="setColor(5)" onclick="setColor(5)"> + onmousedown="setColor(6)" onclick="setColor(6)"> + onmousedown="setColor(7)" onclick="setColor(7)"> + onmousedown="setColor(8)" onclick="setColor(8)"> + onmousedown="setColor(9)" onclick="setColor(9)"> + onmousedown="setColor(10)" onclick="setColor(10)"> + onmousedown="setColor(11)" onclick="setColor(11)"> + onmousedown="setColor(12)" onclick="setColor(12)">
+ onmousedown="setColor(13)" onclick="setColor(13)"> + onmousedown="setColor(14)" onclick="setColor(14)"> + onmousedown="setColor(15)" onclick="setColor(15)"> + onmousedown="setColor(16)" onclick="setColor(16)"> + onmousedown="setColor(17)" onclick="setColor(17)"> + onmousedown="setColor(18)" onclick="setColor(18)"> + onmousedown="setColor(19)" onclick="setColor(19)"> + onmousedown="setColor(20)" onclick="setColor(20)"> + onmousedown="setColor(21)" onclick="setColor(21)"> + onmousedown="setColor(22)" onclick="setColor(22)"> + onmousedown="setColor(23)" onclick="setColor(23)"> + onmousedown="setColor(24)" onclick="setColor(24)"> + onmousedown="setColor(25)" onclick="setColor(25)">