-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfarmListConfig.js
More file actions
152 lines (141 loc) · 4.49 KB
/
Copy pathfarmListConfig.js
File metadata and controls
152 lines (141 loc) · 4.49 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
'use strict';
const { loadConfig } = require('./auth');
/** @param {string|{name:string,enabled?:boolean}} entry */
function normalizeFarmListEntry(entry) {
if (typeof entry === 'string') {
const name = entry.trim();
return name ? { name, enabled: true } : null;
}
if (entry && typeof entry === 'object') {
const name = String(entry.name || '').trim();
if (!name) return null;
return { name, enabled: entry.enabled !== false };
}
return null;
}
/** @param {Array<string|{name:string,enabled?:boolean}>} raw */
function normalizeFarmListsFromConfig(raw) {
const list = Array.isArray(raw) ? raw : [];
const out = [];
const seen = new Set();
for (const item of list) {
const e = normalizeFarmListEntry(item);
if (!e) continue;
const key = e.name.toLowerCase();
if (seen.has(key)) continue;
seen.add(key);
out.push(e);
}
return out;
}
/**
* Reorder list entries to match the game page (village → list DOM order).
* Lists only in config are appended at the end in their previous order.
* @param {Array<{name:string,enabled?:boolean}>} lists
* @param {string[]} gameOrderNames
*/
function orderFarmListsByGame(lists, gameOrderNames) {
const items = normalizeFarmListsFromConfig(lists);
const order = (gameOrderNames || [])
.map(n => String(n || '').trim())
.filter(Boolean);
if (!order.length) return items;
const byKey = new Map(items.map(e => [e.name.toLowerCase(), e]));
const out = [];
const placed = new Set();
for (const name of order) {
const key = name.toLowerCase();
if (!byKey.has(key) || placed.has(key)) continue;
out.push(byKey.get(key));
placed.add(key);
}
for (const entry of items) {
const key = entry.name.toLowerCase();
if (!placed.has(key)) out.push(entry);
}
return out;
}
/**
* Merge lists from game discovery with saved config (keeps enabled flags).
* Result order matches the game page; config-only lists stay at the end.
* @param {Array<string|{name:string,enabled?:boolean}>} existing
* @param {string[]} discoveredNames
*/
function mergeFarmLists(existing, discoveredNames) {
const byKey = new Map();
for (const entry of normalizeFarmListsFromConfig(existing)) {
byKey.set(entry.name.toLowerCase(), { ...entry });
}
const discovered = (discoveredNames || [])
.map(n => String(n || '').trim())
.filter(Boolean);
const out = [];
for (const name of discovered) {
const key = name.toLowerCase();
if (byKey.has(key)) {
out.push(byKey.get(key));
} else {
out.push({ name, enabled: true });
}
}
for (const entry of byKey.values()) {
const key = entry.name.toLowerCase();
if (!discovered.some(n => n.toLowerCase() === key)) out.push(entry);
}
return out;
}
/** @param {string[]} activeNames @param {string[]} gameOrderNames */
function orderActiveNamesByGame(activeNames, gameOrderNames) {
const names = (activeNames || []).map(n => String(n || '').trim()).filter(Boolean);
const order = (gameOrderNames || []).map(n => String(n || '').trim()).filter(Boolean);
if (!order.length) return names;
const want = new Set(names.map(n => n.toLowerCase()));
const out = [];
const placed = new Set();
for (const name of order) {
const key = name.toLowerCase();
if (!want.has(key) || placed.has(key)) continue;
const exact = names.find(n => n.toLowerCase() === key);
if (exact) {
out.push(exact);
placed.add(key);
}
}
for (const name of names) {
const key = name.toLowerCase();
if (!placed.has(key)) out.push(name);
}
return out;
}
function farmListSettings(cfg = loadConfig(), options = {}) {
const fl = cfg.farmList || {};
let allLists = normalizeFarmListsFromConfig(fl.lists);
if (options.gameOrder?.length) {
allLists = orderFarmListsByGame(allLists, options.gameOrder);
}
const activeLists = allLists.filter(l => l.enabled).map(l => l.name);
const min = Math.max(1, Number(fl.intervalMinutesMin) || 5);
const max = Math.max(min, Number(fl.intervalMinutesMax) || 15);
return {
enabled: !!fl.enabled,
sendAllMode: !!fl.sendAllMode,
allLists,
lists: activeLists,
activeCount: activeLists.length,
totalCount: allLists.length,
intervalMinutesMin: min,
intervalMinutesMax: max,
};
}
function farmListTargetCount(fl) {
return fl.sendAllMode ? fl.totalCount : fl.activeCount;
}
module.exports = {
farmListSettings,
farmListTargetCount,
normalizeFarmListEntry,
normalizeFarmListsFromConfig,
mergeFarmLists,
orderFarmListsByGame,
orderActiveNamesByGame,
};