Source code for sidequest_bot.lib.sim_chart

  1"""Draws a gear sim's results as a PNG bar chart that reads at a glance: better to the right, worse to the left.
  2
  3Each :class:`Panel` is one gear option; inside it each :class:`MetricGroup` (threat, damage taken, DPS...) has one
  4:class:`Bar` per fight. Bars are already oriented by the caller (positive = better for the player, so less damage
  5taken points right) and coloured by :data:`Effect`, with grey for changes inside the sim's margin of error. Every
  6option shares one scale, so bar lengths compare across options. Pillow's bundled font has no arrows or dashes, so
  7all chart text must be plain ASCII.
  8"""
  9
 10from dataclasses import dataclass
 11from io import BytesIO
 12from typing import TYPE_CHECKING, Literal
 13
 14from PIL import Image, ImageDraw, ImageFont
 15
 16if TYPE_CHECKING:
 17    from collections.abc import Sequence
 18
 19Effect = Literal["better", "worse", "same"]
 20
 21
 22@dataclass(frozen=True)
[docs] 23class Bar: 24 """One fight's change: ``value`` is the % change oriented so positive is better; ``text`` is drawn beside it.""" 25 26 label: str 27 value: float 28 text: str 29 effect: Effect
30 31 32@dataclass(frozen=True)
[docs] 33class MetricGroup: 34 """One metric's bars, under a title like ``Threat``.""" 35 36 title: str 37 bars: list[Bar]
38 39 40@dataclass(frozen=True)
[docs] 41class Panel: 42 """One gear option: a title like ``Quel'Serrar vs Eater of the Dead`` and its metric groups.""" 43 44 title: str 45 groups: list[MetricGroup]
46 47 48SCALE = 2 49"""Drawn at twice the layout size so the chart stays sharp when Discord scales it down.""" 50WIDTH = 640 51PAD = 20 52LABEL_WIDTH = 150 53TEXT_ROOM = 90 54"""Room kept beside the longest bar for its value text.""" 55BAR_HEIGHT = 18 56ROW_HEIGHT = 28 57GROUP_TITLE_HEIGHT = 26 58PANEL_TITLE_HEIGHT = 30 59AXIS_HEADER_HEIGHT = 22 60PANEL_GAP = 22 61MIN_SCALE_PCT = 2.0 62"""The smallest full-bar scale, so a 0.3% change doesn't fill the chart and look huge.""" 63MIN_BAR_WIDTH = 3 64 65BACKGROUND = (43, 45, 49) 66TEXT = (242, 243, 245) 67MUTED = (181, 186, 193) 68AXIS = (90, 93, 102) 69COLORS: dict[Effect, tuple[int, int, int]] = { 70 "better": (35, 165, 90), 71 "worse": (242, 63, 67), 72 "same": (128, 132, 142), 73} 74 75 76def _font(size: int) -> ImageFont.FreeTypeFont: 77 font = ImageFont.load_default(size=size * SCALE) 78 if not isinstance(font, ImageFont.FreeTypeFont): # pragma: no cover - Pillow built without FreeType 79 msg = "Pillow needs FreeType to draw sim charts" 80 raise TypeError(msg) 81 return font 82 83 84def _height(panels: Sequence[Panel]) -> int: 85 height = PAD * 2 - PANEL_GAP 86 for panel in panels: 87 height += PANEL_TITLE_HEIGHT + AXIS_HEADER_HEIGHT + PANEL_GAP 88 height += sum(GROUP_TITLE_HEIGHT + ROW_HEIGHT * len(group.bars) for group in panel.groups) 89 return height 90 91
[docs] 92def render_chart(panels: Sequence[Panel]) -> bytes: 93 """``panels`` drawn top to bottom as one PNG.""" 94 title_font, group_font, label_font = _font(18), _font(14), _font(14) 95 image = Image.new("RGB", (WIDTH * SCALE, _height(panels) * SCALE), BACKGROUND) 96 draw = ImageDraw.Draw(image) 97 98 bar_left = PAD + LABEL_WIDTH 99 center = (bar_left + WIDTH - PAD) / 2 100 half = (WIDTH - PAD - bar_left) / 2 - TEXT_ROOM 101 scale = max([MIN_SCALE_PCT, *(abs(bar.value) for p in panels for g in p.groups for bar in g.bars)]) 102 103 y = PAD 104 for panel in panels: 105 draw.text((PAD * SCALE, y * SCALE), panel.title, font=title_font, fill=TEXT) 106 y += PANEL_TITLE_HEIGHT 107 header_mid = (y + AXIS_HEADER_HEIGHT / 2) * SCALE 108 draw.text(((center - 8) * SCALE, header_mid), "< worse", font=label_font, fill=MUTED, anchor="rm") 109 draw.text(((center + 8) * SCALE, header_mid), "better >", font=label_font, fill=MUTED, anchor="lm") 110 y += AXIS_HEADER_HEIGHT 111 axis_top = y 112 for group in panel.groups: 113 draw.text((PAD * SCALE, y * SCALE), group.title, font=group_font, fill=MUTED) 114 y += GROUP_TITLE_HEIGHT 115 for bar in group.bars: 116 mid = y + ROW_HEIGHT / 2 - 4 117 draw.text((PAD * SCALE, mid * SCALE), bar.label, font=label_font, fill=TEXT, anchor="lm") 118 length = bar.value / scale * half 119 if abs(length) < MIN_BAR_WIDTH: 120 length = MIN_BAR_WIDTH if bar.value >= 0 else -MIN_BAR_WIDTH 121 left, right = sorted((center, center + length)) 122 top, bottom = mid - BAR_HEIGHT / 2, mid + BAR_HEIGHT / 2 123 draw.rounded_rectangle( 124 (left * SCALE, top * SCALE, right * SCALE, bottom * SCALE), 125 radius=3 * SCALE, 126 fill=COLORS[bar.effect], 127 ) 128 if length >= 0: 129 draw.text(((right + 6) * SCALE, mid * SCALE), bar.text, font=label_font, fill=TEXT, anchor="lm") 130 else: 131 draw.text(((left - 6) * SCALE, mid * SCALE), bar.text, font=label_font, fill=TEXT, anchor="rm") 132 y += ROW_HEIGHT 133 draw.line((center * SCALE, axis_top * SCALE, center * SCALE, (y - 4) * SCALE), fill=AXIS, width=SCALE) 134 y += PANEL_GAP 135 136 buffer = BytesIO() 137 image.save(buffer, format="PNG", optimize=True) 138 return buffer.getvalue()