Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New rendering package, partially replaced v1 #137

Merged
merged 27 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/cufee/aftermath/internal/database"
"github.com/cufee/aftermath/internal/external/wargaming"
"github.com/cufee/aftermath/internal/realtime"
stats "github.com/cufee/aftermath/internal/stats/client/v1"
stats "github.com/cufee/aftermath/internal/stats/client/v2"
"github.com/cufee/aftermath/internal/stats/fetch/v1"
"golang.org/x/text/language"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/discord/commands/public/career.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/cufee/aftermath/internal/log"
"github.com/cufee/aftermath/internal/logic"
"github.com/cufee/aftermath/internal/permissions"
stats "github.com/cufee/aftermath/internal/stats/client/v1"
stats "github.com/cufee/aftermath/internal/stats/client/common"
"github.com/cufee/aftermath/internal/utils"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/discord/commands/public/interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"golang.org/x/sync/errgroup"
"golang.org/x/text/language"

stats "github.com/cufee/aftermath/internal/stats/client/v1"
stats "github.com/cufee/aftermath/internal/stats/client/common"

"github.com/cufee/aftermath/internal/log"
"github.com/pkg/errors"
Expand Down
2 changes: 1 addition & 1 deletion cmd/discord/commands/public/my.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/cufee/aftermath/internal/log"
"github.com/cufee/aftermath/internal/logic"
"github.com/cufee/aftermath/internal/permissions"
stats "github.com/cufee/aftermath/internal/stats/client/v1"
stats "github.com/cufee/aftermath/internal/stats/client/common"
"github.com/cufee/aftermath/internal/stats/fetch/v1"
"github.com/cufee/aftermath/internal/utils"
"github.com/pkg/errors"
Expand Down
2 changes: 1 addition & 1 deletion cmd/discord/commands/public/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/cufee/aftermath/cmd/discord/common"
"github.com/cufee/aftermath/cmd/discord/middleware"
"github.com/cufee/aftermath/internal/permissions"
stats "github.com/cufee/aftermath/internal/stats/client/v1"
stats "github.com/cufee/aftermath/internal/stats/client/common"
"github.com/cufee/aftermath/internal/stats/fetch/v1/replay"
"github.com/pkg/errors"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/discord/commands/public/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/cufee/aftermath/internal/log"
"github.com/cufee/aftermath/internal/logic"
"github.com/cufee/aftermath/internal/permissions"
stats "github.com/cufee/aftermath/internal/stats/client/v1"
stats "github.com/cufee/aftermath/internal/stats/client/common"
"github.com/cufee/aftermath/internal/stats/fetch/v1"
"github.com/cufee/aftermath/internal/utils"
"github.com/pkg/errors"
Expand Down
2 changes: 1 addition & 1 deletion cmd/frontend/routes/api/widget/account.templ
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/cufee/aftermath/cmd/frontend/components/widget"
"github.com/cufee/aftermath/cmd/frontend/handler"
"github.com/cufee/aftermath/internal/database/models"
"github.com/cufee/aftermath/internal/stats/client/v1"
client "github.com/cufee/aftermath/internal/stats/client/common"
"golang.org/x/text/language"
"net/http"
"slices"
Expand Down
4 changes: 2 additions & 2 deletions cmd/frontend/routes/widget/live.templ
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
"github.com/cufee/aftermath/internal/log"
backend "github.com/cufee/aftermath/internal/logic"
"github.com/cufee/aftermath/internal/realtime"
"github.com/cufee/aftermath/internal/stats/client/v1"
client "github.com/cufee/aftermath/internal/stats/client/common"
"github.com/cufee/aftermath/internal/stats/fetch/v1"
"github.com/cufee/aftermath/internal/stats/prepare/session/v1"
"github.com/pkg/errors"
"golang.org/x/text/language"
"strconv"
"time"

"net/http"
"time"
)

type widgetEndpointResponse struct {
Expand Down
13 changes: 6 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/cufee/aftermath

go 1.23

toolchain go1.23.4
go 1.23.5

require github.com/go-jet/jet/v2 v2.12.0

Expand All @@ -13,6 +11,7 @@ require (
github.com/bwmarrin/discordgo v0.28.1
github.com/cufee/aftermath-assets v0.1.0
github.com/cufee/am-wg-proxy-next/v2 v2.2.6
github.com/cufee/facepaint v0.0.5
github.com/fogleman/gg v1.3.0
github.com/go-co-op/gocron v1.37.0
github.com/goccy/go-json v0.10.4
Expand All @@ -33,7 +32,7 @@ require (
github.com/rs/zerolog v1.33.0
github.com/servusdei2018/shards/v2 v2.5.0
github.com/stretchr/testify v1.10.0
github.com/tdewolff/minify/v2 v2.21.2
github.com/tdewolff/minify/v2 v2.21.3
go.dedis.ch/protobuf v1.0.11
golang.org/x/image v0.23.0
golang.org/x/sync v0.10.0
Expand All @@ -52,14 +51,14 @@ require (
github.com/jackc/pgtype v1.14.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.61.0 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rs/xid v1.6.0 // indirect
Expand All @@ -68,5 +67,5 @@ require (
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/sys v0.29.0 // indirect
google.golang.org/protobuf v1.36.1 // indirect
google.golang.org/protobuf v1.36.3 // indirect
)
17 changes: 10 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ github.com/cufee/aftermath-assets v0.1.0 h1:r8p2mUN+h/cw1T6/oEX7bntD+lrL9Nz27GXO
github.com/cufee/aftermath-assets v0.1.0/go.mod h1:6yCITCiz7POJnUMn1oohvadLA4z5YrFNo3p9EKgRdGU=
github.com/cufee/am-wg-proxy-next/v2 v2.2.6 h1:6RAnPuYbPGtaLzOPhTk/N2Hx4KJx14x/c/cIik668xA=
github.com/cufee/am-wg-proxy-next/v2 v2.2.6/go.mod h1:x6fkRfYry3l4Ykxl+v6pJAw5ISw+CuGzJzSkc5y5SYs=
github.com/cufee/facepaint v0.0.5 h1:ae83MF7+gsgeJ+zr76aDmuzc2rqnVx/XGjAVC7e2kTM=
github.com/cufee/facepaint v0.0.5/go.mod h1:7zR5lQMN3EO3qNtff0J8nzIhDb258UoYbRzhRToLQdg=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -147,8 +149,9 @@ github.com/matryer/is v1.4.1 h1:55ehd8zaGABKLXQUe2awZ99BD/PTc2ls+KV/dXphgEQ=
github.com/matryer/is v1.4.1/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
Expand Down Expand Up @@ -179,8 +182,8 @@ github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.61.0 h1:3gv/GThfX0cV2lpO7gkTUwZru38mxevy90Bj8YFSRQQ=
github.com/prometheus/common v0.61.0/go.mod h1:zr29OCN/2BsJRaFwG8QOBr41D6kkchKbpeNH7pAjb/s=
github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io=
github.com/prometheus/common v0.62.0/go.mod h1:vyBcEuLSvWos9B1+CyL7JZ2up+uFzXhkqml0W5zIY1I=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
Expand Down Expand Up @@ -221,8 +224,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tdewolff/minify/v2 v2.21.2 h1:VfTvmGVtBYhMTlUAeHtXM7XOsW0JT/6uMwUPPqgUs9k=
github.com/tdewolff/minify/v2 v2.21.2/go.mod h1:Olje3eHdBnrMjINKffDsil/3NV98Iv7MhWf7556WQVg=
github.com/tdewolff/minify/v2 v2.21.3 h1:KmhKNGrN/dGcvb2WDdB5yA49bo37s+hcD8RiF+lioV8=
github.com/tdewolff/minify/v2 v2.21.3/go.mod h1:iGxHaGiONAnsYuo8CRyf8iPUcqRJVB/RhtEcTpqS7xw=
github.com/tdewolff/parse/v2 v2.7.19 h1:7Ljh26yj+gdLFEq/7q9LT4SYyKtwQX4ocNrj45UCePg=
github.com/tdewolff/parse/v2 v2.7.19/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA=
github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
Expand Down Expand Up @@ -371,8 +374,8 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
google.golang.org/protobuf v1.36.3 h1:82DV7MYdb8anAVi3qge1wSnMDrnKK7ebr+I0hHRN1BU=
google.golang.org/protobuf v1.36.3/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Expand Down
160 changes: 160 additions & 0 deletions internal/render/common/background.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package common

import (
"image"
"image/color"
"math"
"math/rand"
"sync"

"github.com/fogleman/gg"
"github.com/nao1215/imaging"
)

var DefaultBackgroundBlur float64 = 5
var GlassEffectBackgroundBlur float64 = DefaultBackgroundBlur * 5

var globalLogoCacheMx sync.Mutex
var globalLogoCache = make(map[color.Color]image.Image)

func AddDefaultBrandedOverlay(background image.Image, colors []color.Color, seed int, colorChance float32) image.Image {
if len(colors) < 1 {
colors = DefaultLogoColorOptions
}

source := rand.NewSource(int64(seed))
r := rand.New(source)
for i := range colors {
if r.Float32() > colorChance {
colors[i] = TextSecondary
}
}

size := 15
overlay := NewBrandedBackground(background.Bounds().Dx()*2, background.Bounds().Dy()*2, size, -size/2, colors, seed)
return imaging.OverlayCenter(background, overlay, 0.5)
}

func NewBrandedBackground(width, height, logoSize, padding int, colors []color.Color, hashSeed int) image.Image {
// 1/3 of the image should be left for logos
rows := max((height-padding*2)/3/logoSize, 2)
cols := max((width-padding*2)/3/logoSize, 2)
// the rest is gaps
xGapsTotal := width - padding*2 - (cols)*logoSize
yGapsTotal := height - padding*2 - (rows)*logoSize
xGap := xGapsTotal / (cols - 1)
yGap := yGapsTotal / (rows - 1)

localLogoCache := make(map[color.Color]image.Image)
getLogo := func(color color.Color) image.Image {
if img := localLogoCache[color]; img != nil {
return img
}

logo, ok := globalLogoCache[color]
if !ok {
logo = AftermathLogo(color, SmallLogoOptions())
globalLogoCacheMx.Lock()
globalLogoCache[color] = logo
globalLogoCacheMx.Unlock()
}
logo = imaging.Fill(logo, logoSize, logoSize, imaging.Center, imaging.Lanczos)
localLogoCache[color] = logo // mx is already locked inside the caller
return logo
}

ctx := gg.NewContext(width, height)
var makeFn []func()
var mx sync.Mutex
var wg sync.WaitGroup

for c := range cols {
for r := range rows {
wg.Add(1)

makeFn = append(makeFn, func() {
defer wg.Done()

posX := float64(padding + c*(logoSize+xGap))
posY := float64(padding + r*(logoSize+yGap))
source := rand.NewSource(int64(hashSeed) + int64(posX)*51 + int64(posY)*37)
rnd := rand.New(source)

if n := rnd.Float32(); n < 0.5 {
return
}

clr := pickColor(colors, rnd)
scale := pickScaleFactor(rnd)
rotation := pickRotationRad(rnd)

mx.Lock()
logo := getLogo(clr)
mx.Unlock()

logoAdjusted := imaging.New(logoSize, logoSize, color.Transparent)
logoAdjusted = imaging.PasteCenter(logoAdjusted, logo)
logoAdjusted = imaging.Rotate(logoAdjusted, rotation, color.Transparent)
logoAdjusted = imaging.Resize(logoAdjusted, int(float64(logoSize)*scale), int(float64(logoSize)*scale), imaging.Linear)

xJ, yJ := pickPositionJitter(rnd)
posX += xJ
posY += yJ

mx.Lock()
ctx.DrawImage(logoAdjusted, int(posX), int(posY))
mx.Unlock()
})
}
}
for _, fn := range makeFn {
go fn() // wg.Done is called inside already, wg.Add was called already
}
wg.Wait()

return ctx.Image()
}

// pickColor function that includes hashSeed in the hash calculation
func pickColor(colors []color.Color, r *rand.Rand) color.Color {
if len(colors) < 1 {
return color.White
}

index := r.Intn(len(colors))
return colors[index]
}

// pickScaleFactor function that generates a scale factor clamped between 0.8 and 1.2, influenced by image size
func pickScaleFactor(r *rand.Rand) float64 {
// Clamp the scale factor between 0.5 and 1.5
scaleFactor := 0.5 + (r.Float64())
return scaleFactor
}

// pickPositionJitter function that generates an x,y position offset based on the hash seed
func pickPositionJitter(r *rand.Rand) (float64, float64) {
// Clamp between 0.5 and 1.5
xJitter := -0.5 + r.Float64()
yJitter := -0.5 + r.Float64()
return xJitter, yJitter
}

func pickRotationRad(r *rand.Rand) float64 {
// Clamp the rotation angle between -2.5π and 2.5π radians
rotationRad := -2.5*math.Pi + (r.Float64() * (5 * math.Pi))
return rotationRad
}

func BlurWithMask(content image.Image, mask *image.Alpha, blur, maskBlur float64) (image.Image, error) {
ctx := gg.NewContext(content.Bounds().Dx(), content.Bounds().Dy())
err := ctx.SetMask(mask)
if err != nil {
return nil, err
}
ctx.DrawImage(imaging.Blur(content, maskBlur), 0, 0)

content = imaging.Blur(content, blur)
content = imaging.OverlayCenter(content, ctx.Image(), 1) // paste masked content on top of content
return content, nil
}
10 changes: 10 additions & 0 deletions internal/render/common/border-radius.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package common

var (
BorderRadius2XL = 42.5
BorderRadiusXL = 30.0
BorderRadiusLG = 25.0
BorderRadiusMD = 20.0
BorderRadiusSM = 15.0
BorderRadiusXS = 10.0
)
24 changes: 24 additions & 0 deletions internal/render/common/colors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package common

import "image/color"

var DiscordBackgroundColor = color.NRGBA{49, 51, 56, 255}

var DefaultLogoColorOptions = []color.Color{color.NRGBA{50, 50, 50, 180}, color.NRGBA{200, 200, 200, 180}}

var (
TextPrimary = color.NRGBA{255, 255, 255, 255}
TextSecondary = color.NRGBA{204, 204, 204, 255}
TextAlt = color.NRGBA{150, 150, 150, 255}

TextSubscriptionPlus = color.NRGBA{72, 167, 250, 255}
TextSubscriptionPremium = color.NRGBA{255, 223, 0, 255}

DefaultCardColor = color.NRGBA{10, 10, 10, 150}
DefaultCardColorNoAlpha = color.NRGBA{10, 10, 10, 255}
ClanTagBackgroundColor = color.NRGBA{10, 10, 10, 100}

ColorAftermathRed = color.NRGBA{255, 0, 120, 255}
ColorAftermathBlue = color.NRGBA{72, 167, 250, 255}
ColorAftermathYellow = color.NRGBA{255, 223, 0, 255}
)
Loading
Loading