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
|
import { client } from "../client";
import { safeTags } from "../encoding";
import iniParse from "../iniParse";
import request from "../services/request";
import { AO_HOST } from "./aoHost";
/**
* Lightweight character setup that runs on join. Sets the icon src directly
* (letting the browser handle loading) and stores default character data.
* Does NOT fetch char.ini — that is deferred until needed via ensureCharIni.
*/
export const setupCharacterBasic = (chargs: string[], charid: number) => {
const img = <HTMLImageElement>document.getElementById(`demo_${charid}`);
if (chargs[0]) {
img.alt = chargs[0];
img.title = chargs[0];
const iconExt = client.charicon_extensions[0] || ".webp";
img.src = `${AO_HOST}characters/${encodeURI(
chargs[0],
)}/char_icon${iconExt}`;
const mute_select = <HTMLSelectElement>(
document.getElementById("mute_select")
);
mute_select.add(new Option(safeTags(chargs[0]), String(charid)));
const pair_select = <HTMLSelectElement>(
document.getElementById("pair_select")
);
pair_select.add(new Option(safeTags(chargs[0]), String(charid)));
// Store defaults — these get replaced with actual ini values by ensureCharIni
client.chars[charid] = {
name: safeTags(chargs[0]),
showname: safeTags(chargs[0]),
desc: safeTags(chargs[1]),
blips: "m",
gender: "",
side: "def",
chat: "",
evidence: chargs[3],
icon: "",
muted: false,
};
} else {
console.warn(`missing charid ${charid}`);
img.style.display = "none";
}
};
/**
* Fetches and parses char.ini for a character if not already loaded.
* Replaces default values in client.chars[charid] with actual ini values.
*/
export const ensureCharIni = async (charid: number): Promise<any> => {
const char = client.chars[charid];
if (!char) return {};
if (char.inifile) return char.inifile;
const img = <HTMLImageElement>document.getElementById(`demo_${charid}`);
let cini: any = {};
try {
const cinidata = await request(
`${AO_HOST}characters/${encodeURI(char.name)}/char.ini`,
);
cini = iniParse(cinidata);
} catch (err) {
cini = {};
if (img) img.classList.add("noini");
console.warn(`character ${char.name} is missing from webAO`);
}
const default_options = {
name: char.name,
showname: char.name,
side: "def",
blips: "male",
chat: "",
category: "",
};
cini.options = Object.assign(default_options, cini.options);
const default_emotions = {
number: 0,
};
cini.emotions = Object.assign(default_emotions, cini.emotions);
// Replace defaults with actual ini values
char.showname = safeTags(cini.options.showname);
char.blips = safeTags(cini.options.blips);
char.gender = safeTags(cini.options.gender);
char.side = safeTags(cini.options.side);
char.chat =
cini.options.chat === ""
? safeTags(cini.options.category)
: safeTags(cini.options.chat);
char.icon = img ? img.src : "";
char.inifile = cini;
if (
char.blips === "male" &&
char.gender !== "male" &&
char.gender !== ""
) {
char.blips = char.gender;
}
return cini;
};
/**
* Full character info load (used by iniEdit and handleMS ini-edit path).
* Fetches icon + ini for a single character, replacing any existing data.
*/
export const handleCharacterInfo = async (chargs: string[], charid: number) => {
const img = <HTMLImageElement>document.getElementById(`demo_${charid}`);
if (chargs[0]) {
img.alt = chargs[0];
img.title = chargs[0];
const iconExt = client.charicon_extensions[0] || ".webp";
img.src = `${AO_HOST}characters/${encodeURI(
chargs[0],
)}/char_icon${iconExt}`;
// Reset inifile so ensureCharIni will re-fetch
if (client.chars[charid]) {
client.chars[charid].name = safeTags(chargs[0]);
client.chars[charid].inifile = null;
} else {
setupCharacterBasic(chargs, charid);
}
await ensureCharIni(charid);
} else {
console.warn(`missing charid ${charid}`);
img.style.display = "none";
}
};
|