-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
122 lines (100 loc) · 3.86 KB
/
script.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const featureTypes = {
Race: "featureOptions/race.txt",
Class: "featureOptions/class.txt",
Background: "featureOptions/background.txt",
Flaws: "featureOptions/flaws.txt",
Personality: "featureOptions/personality.txt"
};
let quotes = [];
let characterFeatures = {};
function generateCharacter() {
const characterContainer = document.getElementById("characterContainer");
characterContainer.innerHTML = "";
characterFeatures = {}; // Clear existing character features
for (const featureType in featureTypes) {
const featureFile = featureTypes[featureType];
fetchFeatureOptions(featureFile)
.then(options => {
const randomOption = getRandomOption(options);
characterFeatures[featureType] = randomOption;
const featureBox = createFeatureBox(featureType, randomOption);
characterContainer.appendChild(featureBox);
})
.catch(error => {
console.error(`Failed to fetch ${featureType} options. Error: ${error}`);
});
}
}
function fetchFeatureOptions(featureFile) {
return fetch(featureFile)
.then(response => response.text())
.then(text => text.trim().split("\n"))
.catch(error => {
console.error(`Failed to fetch ${featureFile}. Error: ${error}`);
return [];
});
}
function getRandomOption(options) {
return options[Math.floor(Math.random() * options.length)];
}
function createFeatureBox(featureType, randomOption) {
const featureBox = document.createElement("div");
featureBox.classList.add("featureBox");
featureBox.setAttribute("data-feature", featureType);
const titleContainer = document.createElement("div");
titleContainer.classList.add("titleContainer");
const redoIcon = document.createElement("img");
redoIcon.src = "d20light.svg";
redoIcon.classList.add("redoIcon");
redoIcon.addEventListener("click", (event) => {
regenerateFeature(event.target.closest(".featureBox").getAttribute("data-feature"));
});
titleContainer.appendChild(redoIcon);
const titleElement = document.createElement("p");
titleElement.textContent = featureType;
titleElement.classList.add("featureTitle");
titleContainer.appendChild(titleElement);
featureBox.appendChild(titleContainer);
const valueElement = document.createElement("p");
valueElement.textContent = randomOption;
valueElement.classList.add("featureValue");
featureBox.appendChild(valueElement);
return featureBox;
}
function regenerateFeature(featureType) {
const characterContainer = document.getElementById("characterContainer");
const featureBox = characterContainer.querySelector(`[data-feature="${featureType}"]`);
if (!featureBox) return;
const featureFile = featureTypes[featureType];
fetchFeatureOptions(featureFile)
.then(options => {
const randomOption = getRandomOption(options);
characterFeatures[featureType] = randomOption;
const valueElement = featureBox.querySelector(".featureValue");
valueElement.textContent = randomOption;
})
.catch(error => {
console.error(`Failed to fetch ${featureType} options. Error: ${error}`);
});
}
function getRandomQuote() {
const randomIndex = Math.floor(Math.random() * quotes.length);
return quotes[randomIndex];
}
async function fetchRandomQuote() {
try {
const response = await fetch("quotes.txt");
const quotesText = await response.text();
quotes = quotesText.trim().split("\n");
const randomQuote = getRandomQuote();
const quoteContainer = document.getElementById("quoteContainer");
quoteContainer.textContent = randomQuote;
} catch (error) {
console.error("Failed to fetch random quote. Error:", error);
}
}
window.addEventListener("DOMContentLoaded", fetchRandomQuote);
const headerElement = document.getElementById("appLogo");
headerElement.addEventListener("click", () => {
location.reload();
});