-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
138 lines (122 loc) · 4.29 KB
/
Copy pathscript.js
File metadata and controls
138 lines (122 loc) · 4.29 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
// Character amounts mapping for different player counts
const characterAmounts = {
5: [3, 0, 1, 1],
6: [3, 1, 1, 1],
7: [5, 0, 1, 1],
8: [5, 1, 1, 1],
9: [5, 2, 1, 1],
10: [7, 0, 2, 1],
11: [7, 1, 2, 1],
12: [7, 2, 2, 1],
13: [9, 0, 3, 1],
14: [9, 1, 3, 1],
15: [9, 2, 3, 1],
};
// Default values
const DEFAULT_VALUES = {
PLAYERS: 10,
TRAVELLERS: 0,
};
// Storage keys
const STORAGE_KEYS = {
PLAYERS: 'botc-player-count',
TRAVELLERS: 'botc-traveller-count',
};
// Function to update the title with player count and traveller subtitle
function updateTitle(playerCount, travellerCount) {
const titleElement = document.getElementById('player-count-title');
const subtitleElement = document.getElementById('traveller-subtitle');
titleElement.textContent = `${playerCount} Players`;
if (travellerCount > 0) {
subtitleElement.textContent = `+${travellerCount} ${
travellerCount === 1 ? 'Traveller' : 'Travellers'
}`;
subtitleElement.style.display = 'block';
} else {
subtitleElement.style.display = 'none';
}
}
// Function to update character counts based on player count
function updateCharacterCounts(playerCount) {
const [townsfolk, outsiders, minions, demons] = characterAmounts[playerCount];
// Update the display for each character type
document.querySelector('.townsfolk .number').textContent = townsfolk;
document.querySelector('.outsiders .number').textContent = outsiders;
document.querySelector('.minions .number').textContent = minions;
document.querySelector('.demons .number').textContent = demons;
// Update the title with current traveller count
const travellerCount = parseInt(
document.getElementById('travellers-value').textContent
);
updateTitle(playerCount, travellerCount);
}
// Function to save values to localStorage
function saveToStorage(key, value) {
try {
localStorage.setItem(key, value.toString());
} catch (e) {
console.warn('Failed to save to localStorage:', e);
}
}
// Function to load values from localStorage
function loadFromStorage(key, defaultKey) {
try {
const value = localStorage.getItem(key);
if (value === null) {
// If no value found, save and return the default
const defaultValue = DEFAULT_VALUES[defaultKey];
saveToStorage(key, defaultValue);
return defaultValue;
}
return parseInt(value, 10);
} catch (e) {
console.warn('Failed to load from localStorage:', e);
return DEFAULT_VALUES[defaultKey];
}
}
// Handle the controls
function adjustValue(type, change) {
const element = document.getElementById(`${type}-value`);
let value = parseInt(element.textContent);
// Apply minimum and maximum values
if (type === 'players') {
value = Math.min(15, Math.max(5, value + change)); // Between 5 and 15 players
updateCharacterCounts(value);
saveToStorage(STORAGE_KEYS.PLAYERS, value);
} else if (type === 'travellers') {
value = Math.min(5, Math.max(0, value + change)); // Between 0 and 5 travellers
saveToStorage(STORAGE_KEYS.TRAVELLERS, value);
// Update the title with new traveller count
const playerCount = parseInt(
document.getElementById('players-value').textContent
);
updateTitle(playerCount, value);
}
element.textContent = value;
}
// Get initial player count from URL ?p=N or localStorage
function getInitialPlayerCount() {
const params = new URLSearchParams(window.location.search);
const p = params.get('p');
if (p !== null) {
const value = parseInt(p, 10);
if (!isNaN(value) && value >= 5 && value <= 15) {
return value;
}
window.location.replace(`${window.location.pathname}?p=10`);
return 10;
}
return loadFromStorage(STORAGE_KEYS.PLAYERS, 'PLAYERS');
}
// Initialize values from URL/localStorage when the page loads
document.addEventListener('DOMContentLoaded', () => {
// Load and set player count (URL ?p=N overrides storage)
const playerCount = getInitialPlayerCount();
document.getElementById('players-value').textContent = playerCount;
saveToStorage(STORAGE_KEYS.PLAYERS, playerCount);
updateCharacterCounts(playerCount);
// Load and set traveller count
const travellerCount = loadFromStorage(STORAGE_KEYS.TRAVELLERS, 'TRAVELLERS');
document.getElementById('travellers-value').textContent = travellerCount;
updateTitle(playerCount, travellerCount);
});