-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.js
More file actions
305 lines (255 loc) · 10.3 KB
/
logic.js
File metadata and controls
305 lines (255 loc) · 10.3 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
let gameData = null;
let currentLang = 'zh'; // 默认中文
// UI 文本字典
const uiText = {
zh: {
btn: "生成随机配装",
module: "生成模块",
weap: "随机武器 (必选)",
tool: "随机工具",
cons: "随机消耗品",
slot_config: "允许的槽位组合 (多选)",
opt_31: "3 + 1 (大槽 + 小槽)",
opt_32: "3 + 2 (大槽 + 中槽 / 军需官)",
opt_22: "2 + 2 (中槽 + 中槽)",
opt_21: "2 + 1 (中槽 + 小槽)",
opt_11: "1 + 1 (小槽 + 小槽)",
rule: "其他规则",
med: "工具必带急救包",
mel: "工具必带近战",
h_trait: "特质 / 状态",
h_weap: "武器",
h_tool: "工具",
h_cons: "消耗品",
no_trait: "无特殊特质要求",
qm_trait: "★ 必须携带:军需官 (Quartermaster) ★",
slot_large: "主武器 (Large Slot)",
slot_medium: "中槽武器 (Medium Slot)",
slot_small: "副武器 (Small Slot)",
lbl_total: "总花费",
disabled: "- 未启用 -"
},
en: {
btn: "GENERATE LOADOUT",
module: "Modules",
weap: "Random Weapons",
tool: "Random Tools",
cons: "Random Consumables",
slot_config: "Allowed Configurations (Multi-select)",
opt_31: "3 + 1 (Large + Small)",
opt_32: "3 + 2 (Large + Medium / Quartermaster)",
opt_22: "2 + 2 (Medium + Medium)",
opt_21: "2 + 1 (Medium + Small)",
opt_11: "1 + 1 (Small + Small)",
rule: "Rules",
med: "Force Medkit",
mel: "Force Melee Tool",
h_trait: "Trait / Status",
h_weap: "Weapons",
h_tool: "Tools",
h_cons: "Consumables",
no_trait: "No specific traits required",
qm_trait: "★ Required: Quartermaster ★",
slot_large: "PRIMARY (Large Slot)",
slot_medium: "MEDIUM SLOT",
slot_small: "SECONDARY (Small Slot)",
lbl_total: "TOTAL COST",
disabled: "- Disabled -"
}
};
async function loadData() {
try {
const response = await fetch('./data.json');
if (!response.ok) throw new Error("无法读取 data.json");
gameData = await response.json();
console.log("Data Loaded");
document.getElementById('generateBtn').disabled = false;
updateUIText();
} catch (error) {
console.error(error);
alert("数据加载失败,请确保使用 Live Server 运行。");
}
}
function toggleLanguage() {
currentLang = currentLang === 'zh' ? 'en' : 'zh';
updateUIText();
if(document.getElementById('results').style.display !== 'none'){
generateLoadout();
}
}
function updateUIText() {
const t = uiText[currentLang];
document.getElementById('generateBtn').innerText = t.btn;
document.getElementById('lbl_module').innerText = t.module;
document.getElementById('lbl_weap').innerText = t.weap;
document.getElementById('lbl_tool').innerText = t.tool;
document.getElementById('lbl_cons').innerText = t.cons;
document.getElementById('lbl_slot_config').innerText = t.slot_config;
document.getElementById('opt_31').innerText = t.opt_31;
document.getElementById('opt_32').innerText = t.opt_32;
document.getElementById('opt_22').innerText = t.opt_22;
document.getElementById('opt_21').innerText = t.opt_21;
document.getElementById('opt_11').innerText = t.opt_11;
document.getElementById('lbl_rule').innerText = t.rule;
document.getElementById('lbl_med').innerText = t.med;
document.getElementById('lbl_mel').innerText = t.mel;
document.getElementById('h_trait').innerText = t.h_trait;
document.getElementById('h_weap').innerText = t.h_weap;
document.getElementById('h_tool').innerText = t.h_tool;
document.getElementById('h_cons').innerText = t.h_cons;
document.getElementById('lbl_total').innerText = t.lbl_total;
}
function getRandomItem(arr) {
if (!arr || arr.length === 0) return { en: "None", zh: "无", price: 0 };
return arr[Math.floor(Math.random() * arr.length)];
}
function shuffleArray(array) {
let arr = [...array];
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
// 辅助函数:生成带价格的HTML
function renderItem(item) {
const name = item[currentLang];
const price = item.price || 0;
return `<span>${name}</span><span class="price-tag">$${price}</span>`;
}
function generateLoadout() {
if (!gameData) return;
const t = uiText[currentLang];
// 获取规则
const forceMed = document.getElementById('forceMedkit').checked;
const forceMel = document.getElementById('forceMelee').checked;
const enableTools = document.getElementById('doTools').checked;
const enableCons = document.getElementById('doConsumables').checked;
// 分类总价初始化
let subCostWeap = 0;
let subCostTool = 0;
let subCostCons = 0;
// 1. 武器逻辑 (支持多选)
// 收集所有勾选的配置
let allowedConfigs = [];
if(document.getElementById('cfg_31').checked) allowedConfigs.push("3+1");
if(document.getElementById('cfg_32').checked) allowedConfigs.push("3+2");
if(document.getElementById('cfg_22').checked) allowedConfigs.push("2+2");
if(document.getElementById('cfg_21').checked) allowedConfigs.push("2+1");
if(document.getElementById('cfg_11').checked) allowedConfigs.push("1+1");
// 如果一个都没选,默认给一个 3+1
if (allowedConfigs.length === 0) {
allowedConfigs.push("3+1");
}
// 从允许的配置里随机抽一个
const chosenConfig = getRandomItem(allowedConfigs);
let primaryObj = null;
let secondaryObj = null;
let traitDisplay = t.no_trait;
let labelPrimary = "";
let labelSecondary = "";
switch (chosenConfig) {
case "3+1":
primaryObj = getRandomItem(gameData.weaponsLarge);
secondaryObj = getRandomItem(gameData.weaponsSmall);
labelPrimary = t.slot_large;
labelSecondary = t.slot_small;
break;
case "3+2":
primaryObj = getRandomItem(gameData.weaponsLarge);
secondaryObj = getRandomItem(gameData.weaponsMedium);
traitDisplay = `<span class='quartermaster'>${t.qm_trait}</span>`;
labelPrimary = t.slot_large;
labelSecondary = t.slot_medium;
break;
case "2+2":
primaryObj = getRandomItem(gameData.weaponsMedium);
secondaryObj = getRandomItem(gameData.weaponsMedium);
labelPrimary = t.slot_medium;
labelSecondary = t.slot_medium;
// 2+2 不需要军需官,这里保持 no_trait
break;
case "2+1":
primaryObj = getRandomItem(gameData.weaponsMedium);
secondaryObj = getRandomItem(gameData.weaponsSmall);
labelPrimary = t.slot_medium;
labelSecondary = t.slot_small;
break;
case "1+1":
primaryObj = getRandomItem(gameData.weaponsSmall);
secondaryObj = getRandomItem(gameData.weaponsSmall);
labelPrimary = t.slot_small;
labelSecondary = t.slot_small;
break;
}
// 累加武器价格
subCostWeap += (primaryObj.price || 0);
subCostWeap += (secondaryObj.price || 0);
// 2. 工具逻辑
const toolsDiv = document.getElementById('toolsList');
if (enableTools) {
let toolsPool = [...gameData.tools];
let selectedTools = [];
if (forceMed) {
const medIndex = toolsPool.findIndex(t => t.en === "First Aid Kit");
if (medIndex > -1) {
selectedTools.push(toolsPool[medIndex]);
toolsPool.splice(medIndex, 1);
}
}
if (forceMel) {
const meleeOptions = toolsPool.filter(t =>
t.en === "Knife" || t.en === "Dusters" || t.en === "Knuckle Knife" || t.en === "Heavy Knife"
);
if (meleeOptions.length > 0) {
const choice = getRandomItem(meleeOptions);
selectedTools.push(choice);
toolsPool = toolsPool.filter(t => t.en !== choice.en);
}
}
toolsPool = shuffleArray(toolsPool);
while (selectedTools.length < 4 && toolsPool.length > 0) {
selectedTools.push(toolsPool.pop());
}
// 渲染并累加价格
toolsDiv.innerHTML = selectedTools.map(item => {
subCostTool += (item.price || 0);
return `<div class="item">${renderItem(item)}</div>`;
}).join('');
} else {
toolsDiv.innerHTML = `<div class="sub-text">${t.disabled}</div>`;
subCostTool = 0;
}
// 3. 消耗品逻辑
const consDiv = document.getElementById('consumablesList');
if (enableCons) {
let selectedCons = [];
for (let i = 0; i < 4; i++) {
selectedCons.push(getRandomItem(gameData.consumables));
}
// 渲染并累加价格
consDiv.innerHTML = selectedCons.map(item => {
subCostCons += (item.price || 0);
return `<div class="item">${renderItem(item)}</div>`;
}).join('');
} else {
consDiv.innerHTML = `<div class="sub-text">${t.disabled}</div>`;
subCostCons = 0;
}
// 4. 计算总价
const totalCost = subCostWeap + subCostTool + subCostCons;
// 5. 渲染结果
document.getElementById('traitResult').innerHTML = traitDisplay;
// 更新分类价格显示
document.getElementById('weapCost').innerText = `$${subCostWeap}`;
document.getElementById('toolCost').innerText = `$${subCostTool}`;
document.getElementById('consCost').innerText = `$${subCostCons}`;
document.getElementById('primarySlotLabel').innerText = labelPrimary;
document.getElementById('primaryWeapon').innerHTML = renderItem(primaryObj);
document.getElementById('secondarySlotLabel').innerText = labelSecondary;
document.getElementById('secondaryWeapon').innerHTML = renderItem(secondaryObj);
// 显示总价格
document.getElementById('totalCostValue').innerText = `$${totalCost}`;
document.getElementById('results').style.display = 'grid';
}
window.onload = loadData;