maoda-commander-tt 0.0.23 → 0.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.js +208 -0
- package/dist/main.js.map +1 -1
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -4096,6 +4096,213 @@ function compareCanvasMatches(a, b) {
|
|
|
4096
4096
|
return a.nodeIndex - b.nodeIndex;
|
|
4097
4097
|
}
|
|
4098
4098
|
|
|
4099
|
+
// src/commands/tool/tool-draw-command.ts
|
|
4100
|
+
var ToolDrawCommand = class extends BaseCommand {
|
|
4101
|
+
_meta() {
|
|
4102
|
+
return {
|
|
4103
|
+
name: "draw",
|
|
4104
|
+
description: "\u6309\u6982\u7387\u5206\u5E03\u62BD\u7B7E"
|
|
4105
|
+
};
|
|
4106
|
+
}
|
|
4107
|
+
_configureArguments(cmd) {
|
|
4108
|
+
cmd.argument("<count>", "\u8981\u62BD\u53D6\u7684\u4E2A\u6570");
|
|
4109
|
+
cmd.argument("<distribution...>", "\u6982\u7387\u5206\u5E03\uFF0C\u652F\u6301\u7B80\u5199\u6216 JSON \u6570\u7EC4");
|
|
4110
|
+
cmd.addHelpText(
|
|
4111
|
+
"after",
|
|
4112
|
+
[
|
|
4113
|
+
"",
|
|
4114
|
+
"\u8F93\u5165\u6848\u4F8B:",
|
|
4115
|
+
' tt tool draw 3 "A A:50,B B:30,C C:20"',
|
|
4116
|
+
` tt tool draw 3 '[{"name":"\u4E2D\u56FD \u6210\u90FD","weight":50},{"name":"Los Angeles","weight":50}]'`
|
|
4117
|
+
].join("\n")
|
|
4118
|
+
);
|
|
4119
|
+
}
|
|
4120
|
+
async _execute(ctx) {
|
|
4121
|
+
const countResult = parseCount(ctx.args[0]);
|
|
4122
|
+
if (!countResult.ok) {
|
|
4123
|
+
return this._makeError(1, countResult.msg);
|
|
4124
|
+
}
|
|
4125
|
+
const distributionText = stringifyDistributionArg(ctx.args[1]);
|
|
4126
|
+
if (!distributionText) {
|
|
4127
|
+
return this._makeError(1, "\u6982\u7387\u5206\u5E03\u4E0D\u80FD\u4E3A\u7A7A");
|
|
4128
|
+
}
|
|
4129
|
+
const distributionResult = parseDistribution(distributionText);
|
|
4130
|
+
if (!distributionResult.ok) {
|
|
4131
|
+
return this._makeError(1, distributionResult.msg);
|
|
4132
|
+
}
|
|
4133
|
+
this._outputJson({
|
|
4134
|
+
items: drawWeightedItems(countResult.value, distributionResult.value)
|
|
4135
|
+
});
|
|
4136
|
+
return this._makeOk();
|
|
4137
|
+
}
|
|
4138
|
+
_outputJson(data) {
|
|
4139
|
+
this._output(JSON.stringify(data, null, 2));
|
|
4140
|
+
}
|
|
4141
|
+
};
|
|
4142
|
+
function parseCount(value) {
|
|
4143
|
+
const count = Number(String(value ?? "").trim());
|
|
4144
|
+
if (!Number.isInteger(count) || count <= 0) {
|
|
4145
|
+
return { ok: false, msg: "\u62BD\u53D6\u4E2A\u6570\u5FC5\u987B\u662F\u6B63\u6574\u6570" };
|
|
4146
|
+
}
|
|
4147
|
+
return { ok: true, value: count };
|
|
4148
|
+
}
|
|
4149
|
+
function stringifyDistributionArg(value) {
|
|
4150
|
+
if (Array.isArray(value)) {
|
|
4151
|
+
return value.map((item) => String(item)).join(" ").trim();
|
|
4152
|
+
}
|
|
4153
|
+
return String(value ?? "").trim();
|
|
4154
|
+
}
|
|
4155
|
+
function parseDistribution(value) {
|
|
4156
|
+
if (value.trimStart().startsWith("[")) {
|
|
4157
|
+
return parseJsonDistribution(value);
|
|
4158
|
+
}
|
|
4159
|
+
return parseShorthandDistribution(value);
|
|
4160
|
+
}
|
|
4161
|
+
function parseJsonDistribution(value) {
|
|
4162
|
+
let parsed;
|
|
4163
|
+
try {
|
|
4164
|
+
parsed = JSON.parse(value);
|
|
4165
|
+
} catch (err) {
|
|
4166
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
4167
|
+
return { ok: false, msg: `JSON \u6982\u7387\u5206\u5E03\u89E3\u6790\u5931\u8D25: ${msg}` };
|
|
4168
|
+
}
|
|
4169
|
+
if (!Array.isArray(parsed)) {
|
|
4170
|
+
return { ok: false, msg: "JSON \u6982\u7387\u5206\u5E03\u5FC5\u987B\u662F\u6570\u7EC4" };
|
|
4171
|
+
}
|
|
4172
|
+
const entries = [];
|
|
4173
|
+
for (let index = 0; index < parsed.length; index += 1) {
|
|
4174
|
+
const entryResult = parseJsonDistributionEntry(parsed[index], index);
|
|
4175
|
+
if (!entryResult.ok) {
|
|
4176
|
+
return entryResult;
|
|
4177
|
+
}
|
|
4178
|
+
entries.push(entryResult.value);
|
|
4179
|
+
}
|
|
4180
|
+
return validateDistribution(entries);
|
|
4181
|
+
}
|
|
4182
|
+
function parseJsonDistributionEntry(value, index) {
|
|
4183
|
+
const record = asRecord2(value);
|
|
4184
|
+
if (!record) {
|
|
4185
|
+
return { ok: false, msg: `\u7B2C ${index + 1} \u9879\u5FC5\u987B\u662F\u5BF9\u8C61` };
|
|
4186
|
+
}
|
|
4187
|
+
const nameResult = parseEntryName(record.name, `\u7B2C ${index + 1} \u9879`);
|
|
4188
|
+
if (!nameResult.ok) {
|
|
4189
|
+
return nameResult;
|
|
4190
|
+
}
|
|
4191
|
+
const weightResult = parseWeight(
|
|
4192
|
+
getJsonWeightValue(record),
|
|
4193
|
+
`\u7B2C ${index + 1} \u9879`
|
|
4194
|
+
);
|
|
4195
|
+
if (!weightResult.ok) {
|
|
4196
|
+
return weightResult;
|
|
4197
|
+
}
|
|
4198
|
+
return {
|
|
4199
|
+
ok: true,
|
|
4200
|
+
value: {
|
|
4201
|
+
name: nameResult.value,
|
|
4202
|
+
weight: weightResult.value
|
|
4203
|
+
}
|
|
4204
|
+
};
|
|
4205
|
+
}
|
|
4206
|
+
function getJsonWeightValue(record) {
|
|
4207
|
+
if ("weight" in record) {
|
|
4208
|
+
return record.weight;
|
|
4209
|
+
}
|
|
4210
|
+
if ("probability" in record) {
|
|
4211
|
+
return record.probability;
|
|
4212
|
+
}
|
|
4213
|
+
if ("prob" in record) {
|
|
4214
|
+
return record.prob;
|
|
4215
|
+
}
|
|
4216
|
+
return record.p;
|
|
4217
|
+
}
|
|
4218
|
+
function parseShorthandDistribution(value) {
|
|
4219
|
+
const parts = value.split(/[,,]/);
|
|
4220
|
+
const entries = [];
|
|
4221
|
+
for (let index = 0; index < parts.length; index += 1) {
|
|
4222
|
+
const part = parts[index].trim();
|
|
4223
|
+
if (!part) {
|
|
4224
|
+
return { ok: false, msg: `\u7B2C ${index + 1} \u9879\u4E3A\u7A7A` };
|
|
4225
|
+
}
|
|
4226
|
+
const separatorIndex = findLastWeightSeparator(part);
|
|
4227
|
+
if (separatorIndex < 0) {
|
|
4228
|
+
return {
|
|
4229
|
+
ok: false,
|
|
4230
|
+
msg: `\u7B2C ${index + 1} \u9879\u7F3A\u5C11\u6982\u7387\u5206\u9694\u7B26 ":"`
|
|
4231
|
+
};
|
|
4232
|
+
}
|
|
4233
|
+
const nameResult = parseEntryName(
|
|
4234
|
+
part.slice(0, separatorIndex),
|
|
4235
|
+
`\u7B2C ${index + 1} \u9879`
|
|
4236
|
+
);
|
|
4237
|
+
if (!nameResult.ok) {
|
|
4238
|
+
return nameResult;
|
|
4239
|
+
}
|
|
4240
|
+
const weightResult = parseWeight(
|
|
4241
|
+
part.slice(separatorIndex + 1),
|
|
4242
|
+
`\u7B2C ${index + 1} \u9879`
|
|
4243
|
+
);
|
|
4244
|
+
if (!weightResult.ok) {
|
|
4245
|
+
return weightResult;
|
|
4246
|
+
}
|
|
4247
|
+
entries.push({
|
|
4248
|
+
name: nameResult.value,
|
|
4249
|
+
weight: weightResult.value
|
|
4250
|
+
});
|
|
4251
|
+
}
|
|
4252
|
+
return validateDistribution(entries);
|
|
4253
|
+
}
|
|
4254
|
+
function findLastWeightSeparator(value) {
|
|
4255
|
+
return Math.max(value.lastIndexOf(":"), value.lastIndexOf("\uFF1A"));
|
|
4256
|
+
}
|
|
4257
|
+
function parseEntryName(value, label) {
|
|
4258
|
+
if (typeof value !== "string") {
|
|
4259
|
+
return { ok: false, msg: `${label}\u7684\u540D\u79F0\u5FC5\u987B\u662F\u5B57\u7B26\u4E32` };
|
|
4260
|
+
}
|
|
4261
|
+
const name = value.trim();
|
|
4262
|
+
if (!name) {
|
|
4263
|
+
return { ok: false, msg: `${label}\u7684\u540D\u79F0\u4E0D\u80FD\u4E3A\u7A7A` };
|
|
4264
|
+
}
|
|
4265
|
+
return { ok: true, value: name };
|
|
4266
|
+
}
|
|
4267
|
+
function parseWeight(value, label) {
|
|
4268
|
+
const rawValue = typeof value === "string" ? value.trim().replace(/[%%]$/, "") : value;
|
|
4269
|
+
const weight = Number(rawValue);
|
|
4270
|
+
if (!Number.isFinite(weight) || weight <= 0) {
|
|
4271
|
+
return { ok: false, msg: `${label}\u7684\u6982\u7387\u5FC5\u987B\u662F\u5927\u4E8E 0 \u7684\u6570\u5B57` };
|
|
4272
|
+
}
|
|
4273
|
+
return { ok: true, value: weight };
|
|
4274
|
+
}
|
|
4275
|
+
function validateDistribution(entries) {
|
|
4276
|
+
if (entries.length === 0) {
|
|
4277
|
+
return { ok: false, msg: "\u6982\u7387\u5206\u5E03\u81F3\u5C11\u9700\u8981 1 \u9879" };
|
|
4278
|
+
}
|
|
4279
|
+
return { ok: true, value: entries };
|
|
4280
|
+
}
|
|
4281
|
+
function drawWeightedItems(count, entries) {
|
|
4282
|
+
const totalWeight = entries.reduce((sum, entry) => sum + entry.weight, 0);
|
|
4283
|
+
const items = [];
|
|
4284
|
+
for (let index = 0; index < count; index += 1) {
|
|
4285
|
+
items.push(drawWeightedItem(entries, totalWeight));
|
|
4286
|
+
}
|
|
4287
|
+
return items;
|
|
4288
|
+
}
|
|
4289
|
+
function drawWeightedItem(entries, totalWeight) {
|
|
4290
|
+
let cursor = Math.random() * totalWeight;
|
|
4291
|
+
for (const entry of entries) {
|
|
4292
|
+
cursor -= entry.weight;
|
|
4293
|
+
if (cursor < 0) {
|
|
4294
|
+
return entry.name;
|
|
4295
|
+
}
|
|
4296
|
+
}
|
|
4297
|
+
return entries[entries.length - 1].name;
|
|
4298
|
+
}
|
|
4299
|
+
function asRecord2(value) {
|
|
4300
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
4301
|
+
return void 0;
|
|
4302
|
+
}
|
|
4303
|
+
return value;
|
|
4304
|
+
}
|
|
4305
|
+
|
|
4099
4306
|
// src/commands/tool/tool-command.ts
|
|
4100
4307
|
var ToolCommand = class extends BaseSubcommandHost {
|
|
4101
4308
|
_meta() {
|
|
@@ -4106,6 +4313,7 @@ var ToolCommand = class extends BaseSubcommandHost {
|
|
|
4106
4313
|
}
|
|
4107
4314
|
_registerSubcommands() {
|
|
4108
4315
|
this._addSubcommand(new ToolCanvasCommand());
|
|
4316
|
+
this._addSubcommand(new ToolDrawCommand());
|
|
4109
4317
|
}
|
|
4110
4318
|
};
|
|
4111
4319
|
|