File size: 8,969 Bytes
064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | import type { LocalCommandCall } from '../../types/command.js'
import { companionUserId, getCompanion, roll } from '../../buddy/companion.js'
import type { Species, StoredCompanion } from '../../buddy/types.js'
import { RARITY_STARS } from '../../buddy/types.js'
import { saveGlobalConfig } from '../../utils/config.js'
const NAME_BANK: Record<Species, string[]> = {
duck: ['Pebble', 'Puddle', 'Nib', 'Mochi'],
goose: ['Brass', 'Comet', 'Honk', 'Marble'],
blob: ['Gloop', 'Boba', 'Murmur', 'Pixel'],
cat: ['Juniper', 'Miso', 'Static', 'Velvet'],
dragon: ['Ember', 'Cinder', 'Rune', 'Flare'],
octopus: ['Ink', 'Tangle', 'Coral', 'Nori'],
owl: ['Sumi', 'Orbit', 'Mote', 'Aster'],
penguin: ['Tux', 'Floe', 'Skipper', 'Chill'],
turtle: ['Moss', 'Harbor', 'Slate', 'Ripple'],
snail: ['Spiral', 'Dew', 'Button', 'Lilt'],
ghost: ['Wisp', 'Echo', 'Pale', 'Veil'],
axolotl: ['Bubble', 'Glim', 'Lotus', 'Sprig'],
capybara: ['Loaf', 'Basil', 'Drift', 'Sunny'],
cactus: ['Prickle', 'Agave', 'Dot', 'Needle'],
robot: ['Servo', 'Hex', 'Patch', 'Relay'],
rabbit: ['Thistle', 'Biscuit', 'Hopper', 'Fern'],
mushroom: ['Spore', 'Truffle', 'Pip', 'Toad'],
chonk: ['Boulder', 'Nugget', 'Mallow', 'Crumb'],
}
const PERSONALITY_BANK = [
'calm, observant, and quietly approving',
'chaotic, affectionate, and slightly smug',
'earnest, curious, and proud of tiny victories',
'sleepy, loyal, and unexpectedly insightful',
'dramatic, playful, and obsessed with good refactors',
]
const PET_LINES = [
'leans into the petting and radiates approval.',
'does a tiny bounce and looks extremely pleased.',
'blinks slowly like this is now an official ritual.',
'makes a happy little noise only you can hear.',
]
function pickFrom<T>(items: readonly T[], seed: number): T {
return items[seed % items.length]!
}
function normalizeName(name: string): string {
const trimmed = name.trim().replace(/\s+/g, ' ')
return trimmed.slice(0, 32)
}
function createSoul(customName?: string): StoredCompanion {
const { bones, inspirationSeed } = roll(companionUserId())
return {
name: customName
? normalizeName(customName)
: pickFrom(NAME_BANK[bones.species], inspirationSeed),
personality: pickFrom(PERSONALITY_BANK, Math.floor(inspirationSeed / 7)),
hatchedAt: Date.now(),
}
}
function persistCompanion(soul: StoredCompanion): void {
saveGlobalConfig(current => ({
...current,
companion: soul,
companionMuted: false,
}))
}
function renderCompanionStatus(): string {
const companion = getCompanion()
if (!companion) {
return 'No buddy yet. Run /buddy or /buddy hatch to summon one.'
}
const stats = Object.entries(companion.stats)
.map(([name, value]) => `${name}:${value}`)
.join(' ')
return [
`${RARITY_STARS[companion.rarity]} ${companion.name} the ${companion.species}`,
`eyes ${companion.eye} · hat ${companion.hat} · shiny ${companion.shiny ? 'yes' : 'no'}`,
`personality: ${companion.personality}`,
`stats: ${stats}`,
].join('\n')
}
function setReaction(
context: Parameters<LocalCommandCall>[1],
reaction: string | undefined,
): void {
context.setAppState(prev =>
prev.companionReaction === reaction
? prev
: { ...prev, companionReaction: reaction },
)
}
function hatch(context: Parameters<LocalCommandCall>[1], customName?: string) {
const soul = createSoul(customName)
persistCompanion(soul)
const companion = getCompanion()
const reaction = companion
? `${companion.name} appears with a tiny ${companion.hat === 'none' ? 'flourish' : companion.hat}.`
: undefined
setReaction(context, reaction)
return companion
}
function ensureCompanion(): string | undefined {
if (!getCompanion()) {
return 'No buddy yet. Run /buddy or /buddy hatch first.'
}
}
export const call: LocalCommandCall = async (args, context) => {
const raw = args.trim()
const [action = '', ...rest] = raw.split(/\s+/)
const normalizedAction = action.toLowerCase()
const remainder = raw.slice(action.length).trim()
if (normalizedAction === '' || normalizedAction === 'status') {
if (!getCompanion()) {
const companion = hatch(context)
return {
type: 'text',
value: companion
? `Hatched ${companion.name}.\n${renderCompanionStatus()}`
: 'Buddy hatched.',
}
}
return { type: 'text', value: renderCompanionStatus() }
}
if (normalizedAction === 'hatch') {
const customName = remainder
const existing = getCompanion()
if (existing) {
return {
type: 'text',
value: `Buddy already hatched.\n${renderCompanionStatus()}`,
}
}
const companion = hatch(context, customName || undefined)
return {
type: 'text',
value: companion
? `Hatched ${companion.name}.\n${renderCompanionStatus()}`
: 'Buddy hatched.',
}
}
if (normalizedAction === 'pet') {
const missing = ensureCompanion()
if (missing) return { type: 'text', value: missing }
const companion = getCompanion()!
const petSeed = Date.now()
const line = pickFrom(PET_LINES, petSeed)
context.setAppState(prev => ({
...prev,
companionPetAt: petSeed,
companionReaction: `${companion.name} ${line}`,
}))
return {
type: 'text',
value: `${companion.name} ${line}`,
}
}
if (normalizedAction === 'mute') {
const missing = ensureCompanion()
if (missing) return { type: 'text', value: missing }
saveGlobalConfig(current => ({ ...current, companionMuted: true }))
setReaction(context, undefined)
return { type: 'text', value: 'Buddy muted.' }
}
if (normalizedAction === 'unmute') {
const missing = ensureCompanion()
if (missing) return { type: 'text', value: missing }
saveGlobalConfig(current => ({ ...current, companionMuted: false }))
return { type: 'text', value: 'Buddy unmuted.' }
}
if (normalizedAction === 'rename' || normalizedAction === 'name') {
const missing = ensureCompanion()
if (missing) return { type: 'text', value: missing }
const nextName = normalizeName(remainder)
if (!nextName) {
return { type: 'text', value: 'Usage: /buddy rename <name>' }
}
saveGlobalConfig(current => ({
...current,
companion: current.companion
? { ...current.companion, name: nextName }
: current.companion,
}))
setReaction(context, `${nextName} accepts the rename with theatrical dignity.`)
return { type: 'text', value: `Buddy renamed to ${nextName}.` }
}
if (
normalizedAction === 'bio' ||
normalizedAction === 'persona' ||
normalizedAction === 'personality'
) {
const missing = ensureCompanion()
if (missing) return { type: 'text', value: missing }
if (!remainder) {
return {
type: 'text',
value: 'Usage: /buddy bio <short personality line>',
}
}
const personality = remainder.slice(0, 160)
saveGlobalConfig(current => ({
...current,
companion: current.companion
? { ...current.companion, personality }
: current.companion,
}))
const companion = getCompanion()!
setReaction(context, `${companion.name} seems pleased with the new reputation.`)
return { type: 'text', value: `Buddy personality updated to: ${personality}` }
}
if (normalizedAction === 'react' || normalizedAction === 'say') {
const missing = ensureCompanion()
if (missing) return { type: 'text', value: missing }
if (!remainder) {
return { type: 'text', value: 'Usage: /buddy react <message>' }
}
setReaction(context, remainder.slice(0, 120))
return { type: 'text', value: 'Buddy reaction queued.' }
}
if (normalizedAction === 'dismiss' || normalizedAction === 'hide') {
setReaction(context, undefined)
return { type: 'text', value: 'Buddy bubble dismissed.' }
}
if (normalizedAction === 'reset') {
saveGlobalConfig(current => ({
...current,
companion: undefined,
companionMuted: false,
}))
context.setAppState(prev => ({
...prev,
companionReaction: undefined,
companionPetAt: undefined,
}))
return {
type: 'text',
value: 'Buddy reset. Run /buddy to hatch a fresh companion.',
}
}
if (normalizedAction === 'help') {
return {
type: 'text',
value:
'Usage: /buddy, /buddy status, /buddy hatch [name], /buddy pet, /buddy rename <name>, /buddy bio <text>, /buddy mute, /buddy unmute, /buddy dismiss, /buddy react <text>, /buddy reset',
}
}
if (rest.length === 0 && !getCompanion()) {
const companion = hatch(context, raw)
return {
type: 'text',
value: companion
? `Hatched ${companion.name}.\n${renderCompanionStatus()}`
: 'Buddy hatched.',
}
}
return {
type: 'text',
value:
'Unknown /buddy action. Use /buddy help for available actions.',
}
}
|