1package server 2 3import ( 4 "bytes" 5 "embed" 6 "fmt" 7 "image" 8 "image/color" 9 "image/draw" 10 "image/png" 11 "strings" 12 "sync" 13 14 "golang.org/x/image/font" 15 "golang.org/x/image/font/opentype" 16 "golang.org/x/image/math/fixed" 17 18 "tinyadventure/internal/model" 19) 20 21//go:embed fonts/*.ttf 22var fontFiles embed.FS 23 24var ( 25 cardBg = color.RGBA{0x14, 0x11, 0x0e, 0xff} 26 cardRaised = color.RGBA{0x2a, 0x25, 0x1f, 0xff} 27 cardFg = color.RGBA{0xef, 0xe6, 0xd2, 0xff} 28 cardMuted = color.RGBA{0xb3, 0xa7, 0x8f, 0xff} 29 cardFaint = color.RGBA{0x7c, 0x72, 0x5f, 0xff} 30 cardAccent = color.RGBA{0xff, 0xc9, 0x4d, 0xff} 31 cardDanger = color.RGBA{0xe2, 0x55, 0x3f, 0xff} 32) 33 34var ( 35 fontsOnce sync.Once 36 fontsErr error 37 display *opentype.Font 38 body *opentype.Font 39) 40 41func loadCardFonts() error { 42 fontsOnce.Do(func() { 43 parse := func(name string) *opentype.Font { 44 b, err := fontFiles.ReadFile("fonts/" + name) 45 if err == nil { 46 var f *opentype.Font 47 if f, err = opentype.Parse(b); err == nil { 48 return f 49 } 50 } 51 fontsErr = fmt.Errorf("font %s: %w", name, err) 52 return nil 53 } 54 display, body = parse("MedievalSharp.ttf"), parse("Alegreya.ttf") 55 }) 56 return fontsErr 57} 58 59type pen struct { 60 img *image.RGBA 61 faces map[string]font.Face 62} 63 64func (p pen) face(f *opentype.Font, size float64) font.Face { 65 key := fmt.Sprintf("%p %v", f, size) 66 if fc, ok := p.faces[key]; ok { 67 return fc 68 } 69 fc, _ := opentype.NewFace(f, &opentype.FaceOptions{Size: size, DPI: 72, Hinting: font.HintingFull}) 70 p.faces[key] = fc 71 return fc 72} 73 74func (p pen) text(f *opentype.Font, size float64, c color.Color, x, y int, s string) int { 75 d := font.Drawer{Dst: p.img, Src: image.NewUniform(c), Face: p.face(f, size), Dot: fixed.P(x, y)} 76 d.DrawString(s) 77 return d.Dot.X.Round() 78} 79 80func (p pen) width(f *opentype.Font, size float64, s string) int { 81 return font.MeasureString(p.face(f, size), s).Round() 82} 83 84func (p pen) rect(x, y, w, h int, c color.Color) { 85 draw.Draw(p.img, image.Rect(x, y, x+w, y+h), image.NewUniform(c), image.Point{}, draw.Src) 86} 87 88func card(run model.Run, host string) ([]byte, error) { 89 if err := loadCardFonts(); err != nil { 90 return nil, err 91 } 92 const W, H, pad = 1200, 630, 72 93 p := pen{img: image.NewRGBA(image.Rect(0, 0, W, H)), faces: map[string]font.Face{}} 94 p.rect(0, 0, W, H, cardBg) 95 tone := cardAccent 96 if run.Phase != "victory" { 97 tone = cardDanger 98 } 99 p.rect(0, 0, 12, H, tone) 100 title, _ := headline(run) 101 p.text(display, 34, cardMuted, pad, 104, "Tiny Adventure") 102 p.text(display, 84, tone, pad, 206, title) 103 p.text(body, 34, cardFg, pad, 262, fitText(p, body, 34, run.Mission, W-2*pad)) 104 sum := run.Summary 105 stats := [][2]string{ 106 {fmt.Sprint(sum.Battles), "battles"}, 107 {fmt.Sprint(sum.Kills), "foes"}, 108 {fmt.Sprintf("%d/%d", run.Keys, run.KeysOf), "keys"}, 109 {fmt.Sprint(sum.Hexes), "hexes"}, 110 {fmt.Sprint(sum.Best), "best hit"}, 111 {fmt.Sprint(sum.Fallen), "fallen"}, 112 } 113 tw := (W - 2*pad - 5*16) / 6 114 for i, st := range stats { 115 x := pad + i*(tw+16) 116 p.rect(x, 306, tw, 128, cardRaised) 117 p.text(body, 56, cardFg, x+(tw-p.width(body, 56, st[0]))/2, 378, st[0]) 118 p.text(body, 26, cardMuted, x+(tw-p.width(body, 26, st[1]))/2, 416, st[1]) 119 } 120 heroes := make([]string, len(run.Party)) 121 for i, h := range run.Party { 122 heroes[i] = fmt.Sprintf("%s, level %d %s", h.Name, h.Level, strings.ToLower(h.Class)) 123 } 124 p.text(body, 28, cardMuted, pad, 492, fitText(p, body, 28, strings.Join(heroes[:min(2, len(heroes))], " "), W-2*pad)) 125 if len(heroes) > 2 { 126 p.text(body, 28, cardMuted, pad, 532, fitText(p, body, 28, strings.Join(heroes[2:], " "), W-2*pad)) 127 } 128 foot := fmt.Sprintf("Seed %d", run.Seed) 129 p.text(body, 26, cardFaint, pad, 590, foot) 130 p.text(body, 26, cardFaint, W-pad-p.width(body, 26, host), 590, host) 131 var buf bytes.Buffer 132 if err := png.Encode(&buf, p.img); err != nil { 133 return nil, err 134 } 135 return buf.Bytes(), nil 136} 137 138func fitText(p pen, f *opentype.Font, size float64, s string, w int) string { 139 if p.width(f, size, s) <= w { 140 return s 141 } 142 r := []rune(s) 143 for len(r) > 0 && p.width(f, size, string(r)+"…") > w { 144 r = r[:len(r)-1] 145 } 146 return string(r) + "…" 147}