forked from cirion02/bingo-variants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
searcher.js
108 lines (88 loc) · 3.07 KB
/
searcher.js
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
function calcPlayerCounts(variant, target, allowUnbalanced){
if (variant.player_count_override){
return variant.player_count_override == target;
}
if (variant.min_teams * variant.min_players_per_team > target) return false;
if (allowUnbalanced){
if (variant.max_players_per_team == null) return true;
// Math stuff
let t = Math.floor(target/variant.min_players_per_team);
let r = target % variant.min_players_per_team;
if (variant.max_teams != null && variant.max_teams < t){
r += (t-variant.max_teams) * variant.min_players_per_team;
t = variant.max_teams;
}
return r <= t * (variant.max_players_per_team - variant.min_players_per_team);
}
let playerCap = variant.max_players_per_team == null ? target : Math.min(target, variant.max_players_per_team)
for (let i=variant.min_players_per_team; i<=playerCap; i++){
if (target % i != 0) continue;
console.log(target / i)
if (target / i < variant.min_teams) continue;
if (variant.max_teams != null && target / i > variant.max_teams) continue;
return true;
}
return false;
}
function getVariantList(){
queryString = window.location.search;
urlParams = new URLSearchParams(queryString);
query = urlParams.get("q");
conditions = query.split(" ").filter(n => n);
filters = [];
random = false;
for (let condition of conditions){
let temp = condition.split(":");
let key = temp[0];
let val = temp[1];
console.log(`${key} : ${val}`)
if (key == "is") {
filters.push((variant) => (variant.tags.includes(val)));
}
if (key == "not") {
filters.push((variant) => (!variant.tags.includes(val)));
}
if (key == "teams") {
filters.push((variant) => (variant.min_teams <= val && (variant.max_teams == null || variant.max_teams >= val)));
}
if (key == "c") {
filters.push((variant) => (val.includes(variant.color.charAt(0).toLowerCase())));
}
if (key == "players") {
filters.push((variant) => (calcPlayerCounts(variant, val, false)));
}
if (key == "playersun") {
filters.push((variant) => (calcPlayerCounts(variant, val, true)));
}
if (key == "random") {
random = true;
}
if (key == "n") {
filters.push((variant) => (variant.name.toLowerCase().includes(val.toLowerCase())));
}
}
let result = [];
for (let variant of variant_list_data) {
let accept = true;
for (let filter of filters){
if (!filter(variant)){
accept = false;
break;
}
}
if (accept) {
result.push(variant);
}
}
return result;
}
function fillSearch(){
result = getVariantList();
if (random) {
result = [result[Math.floor(Math.random()*result.length)]];
}
else {
result.sort(orderVariants)
}
renderVariants(result);
}