Files
SnipzilandClaude Opus 5.5 83c2fc3b0b feat(chat): custom chat bubbles
Players choose how their chat bubble looks, and everyone sees it.

- style: a gradient of 2 to 5 colour stops (linear at any 45° step,
  radial, or solid), its strength, text and border colours, outline
  thickness 0-4, opacity 25-100%, and an animation (still, pulse, shimmer,
  flow, rainbow) at 40-300% speed. lib/chatstyles.js keeps the one
  canonical form: forgiving for saved accounts, strict for requests.
- in the game: Settings > Chat Bubble > Customize opens an editor with
  presets (Ocean, Sunset, Forest, Midnight, Candy, Rainbow), a palette,
  Swap, Random, Reset and Save, and a live preview. "Custom bubbles: Off"
  draws everyone's bubbles normally on this computer only.
- on the web: the account page has a Chat bubble section with the same
  choices.
- wire: xcs with no fields asks for the saved style, with twelve (or an
  older client's 7, 10 or 11) saves it; the reply is the canonical twelve.
  A typed line ("uc") carries its sender's style after the stock extra
  field, only when it is not the default, so message and type stay where
  the game reads them and unpatched clients are unaffected.

ajclient.swf is built with this and the six client commits before it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-21 22:42:11 -04:00

80 lines
3.7 KiB
JavaScript

"use strict";
// The style travels with a chat line, rather than with an avatar-create
// packet, so a new choice takes effect immediately and is seen by everybody
// who sees that line. Keep it small and entirely server-generated: clients
// never get to choose the fields sent to other players.
const DEFAULT = Object.freeze({background: "ffffff", accent: "dfe8ff", text: "445559", border: "9aabb0", opacity: 75,
animation: "none", speed: 100, gradient: "linear", angle: 45, strength: 100, outline: 1,
stops: "ffffff:0;dfe8ff:100"});
const ANIMATIONS = new Set(["none", "pulse", "shimmer", "flow", "rainbow"]);
const GRADIENTS = new Set(["solid", "linear", "radial"]);
const COLOR = /^[0-9a-f]{6}$/i;
const color = (value, fallback) => typeof value === "string" && COLOR.test(value) ? value.toLowerCase() : fallback;
const number = (value, fallback, low, high) => Number.isInteger(value) && value >= low && value <= high ? value : fallback;
const direction = value => {
const angle = number(value, DEFAULT.angle, 0, 315);
return angle % 45 === 0 ? angle : DEFAULT.angle;
};
const stops = (value, background, accent) => {
if (typeof value === "string") {
const parsed = value.split(";").map(stop => {
const match = /^([0-9a-f]{6}):(100|[1-9]?\d)$/i.exec(stop);
return match ? {color: match[1].toLowerCase(), position: Number(match[2])} : null;
});
if (parsed.length >= 2 && parsed.length <= 5 && parsed.every(Boolean)
&& parsed[0].position === 0 && parsed.at(-1).position === 100
&& parsed.every((stop, index) => index === 0 || stop.position > parsed[index - 1].position)) {
return parsed.map(stop => `${stop.color}:${stop.position}`).join(";");
}
}
return `${background}:0;${accent}:100`;
};
const normalize = value => {
const source = value && typeof value === "object" && !Array.isArray(value) ? value : {};
let background = color(source.background, DEFAULT.background);
let accent = color(source.accent, DEFAULT.accent);
const gradientStops = stops(source.stops, background, accent);
const stopParts = gradientStops.split(";");
background = stopParts[0].slice(0, 6);
accent = stopParts.at(-1).slice(0, 6);
return {
background,
accent,
text: color(source.text, DEFAULT.text),
border: color(source.border, DEFAULT.border),
opacity: number(source.opacity, DEFAULT.opacity, 25, 100),
animation: ANIMATIONS.has(source.animation) ? source.animation : DEFAULT.animation,
speed: number(source.speed, DEFAULT.speed, 40, 300),
gradient: GRADIENTS.has(source.gradient) ? source.gradient : DEFAULT.gradient,
angle: direction(source.angle),
strength: number(source.strength, DEFAULT.strength, 0, 100),
outline: number(source.outline, DEFAULT.outline, 0, 4),
stops: gradientStops,
};
};
const same = (one, two) => Object.keys(DEFAULT).every(key => one[key] === two[key]);
// `normalize` is intentionally forgiving when old account data is loaded.
// Incoming editor requests are not: every field must already be canonical.
const valid = value => Boolean(value) && typeof value === "object" && !Array.isArray(value)
&& Object.keys(DEFAULT).every(key => value[key] === normalize(value)[key]);
const fields = value => {
const style = normalize(value);
return [style.background, style.accent, style.text, style.border, style.opacity, style.animation, style.speed,
style.gradient, style.angle, style.strength, style.outline, style.stops];
};
// Twelve comma-separated, URL-safe fields; empty means the original game bubble.
const packet = value => {
const style = normalize(value);
return same(style, DEFAULT) ? "" : fields(style).join(",");
};
module.exports = {DEFAULT, normalize, valid, fields, packet};