Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c89d341651 | |||
| 72fe8d1abb | |||
| cc16e2d9e6 | |||
| b77252b0b1 |
+100
-19
@@ -1,16 +1,21 @@
|
|||||||
window.addEventListener("DOMContentLoaded", () => {
|
window.addEventListener("DOMContentLoaded", () => {
|
||||||
//Create progress bar
|
//Create progress bar
|
||||||
const book_bar = new ProgressBar();
|
const book_bar = new ProgressBar();
|
||||||
|
|
||||||
//Pat Lilac
|
//Pat Lilac
|
||||||
const websocket = new ReconnectingWebSocket("wss://lilac.thewisewolf.dev/tracker/timer/ws");
|
const websocket = new ReconnectingWebSocket("wss://lilac.thewisewolf.dev/tracker/timer/ws");
|
||||||
communicate(websocket, book_bar);
|
communicate(websocket, book_bar);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
class ProgressBar {
|
class ProgressBar {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.current_progress = 0;
|
this.current_progress = 25;
|
||||||
this.max_progress = 45;
|
this.max_progress = 45;
|
||||||
|
this.anim_progress = 0;
|
||||||
|
this.animating = false;
|
||||||
|
this.simming = false;
|
||||||
this.target_element = document.getElementById("progressBar");
|
this.target_element = document.getElementById("progressBar");
|
||||||
this.bar_back = document.getElementById("barBack");
|
this.bar_back = document.getElementById("barBack");
|
||||||
this.bar_front = document.getElementById("barFront");
|
this.bar_front = document.getElementById("barFront");
|
||||||
@@ -26,39 +31,116 @@ class ProgressBar {
|
|||||||
image_cache.push(img_fetcher);
|
image_cache.push(img_fetcher);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
//SET NUMBER OF COLORED BOOKS
|
//SET NUMBER OF COLORED BOOKS
|
||||||
|
|
||||||
|
//Set current campaign progress
|
||||||
setProgress(val) {
|
setProgress(val) {
|
||||||
if (Number.isInteger(val) && (this.target_element != null) && (this.bar_back != null) && (this.bar_front != null)) {
|
if (Number.isInteger(val)
|
||||||
|
&& (val >= 0)
|
||||||
|
&& (val <= this.max_progress)) {
|
||||||
|
this.current_progress = val;
|
||||||
|
} else {
|
||||||
|
console.warn("Progress update failed.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Render current animation state to the screen
|
||||||
|
renderProgress(val) {
|
||||||
|
if (Number.isInteger(val)
|
||||||
|
&& (this.target_element != null)
|
||||||
|
&& (this.bar_back != null)
|
||||||
|
&& (this.bar_front != null)
|
||||||
|
&& (val >= 0)
|
||||||
|
&& (val <= this.current_progress)
|
||||||
|
&& (val <= this.max_progress)) {
|
||||||
let val_string = val.toString();
|
let val_string = val.toString();
|
||||||
if (val_string.length < 2) {
|
if (val_string.length < 2) {
|
||||||
val_string = "0" + val_string;
|
val_string = "0" + val_string;
|
||||||
}
|
}
|
||||||
this.bar_back.style.backgroundImage = "url(src/Books/BookBar_v8_f00" + val_string + ".png)";
|
this.bar_back.style.backgroundImage = "url(src/Books/BookBar_v8_f00" + val_string + ".png)";
|
||||||
setTimeout(this.antiFlash.bind(this), 150, val_string, val);
|
setTimeout(this.antiFlash.bind(this), 150, val_string, val);
|
||||||
|
} else {
|
||||||
|
console.warn("Progress render failed.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Prevent image flicker by pre-loading a version of the image then rendering the top image with a delay
|
||||||
antiFlash(val_string, new_val) {
|
antiFlash(val_string, new_val) {
|
||||||
this.bar_front.style.backgroundImage = "url(src/Books/BookBar_v8_f00" + val_string + ".png)";
|
this.bar_front.style.backgroundImage = "url(src/Books/BookBar_v8_f00" + val_string + ".png)";
|
||||||
if ((new_val >= 0) && (new_val <= this.max_progress)) {
|
//Increment the animation ticker (plus an extra round of safety checks)
|
||||||
this.current_progress = new_val;
|
if ((new_val >= 0)
|
||||||
|
&& (new_val <= this.current_progress)
|
||||||
|
&& (this.anim_progress <= this.max_progress)
|
||||||
|
&& (new_val <= this.max_progress)) {
|
||||||
|
this.anim_progress = new_val;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.warn("Progress update is out of range.");
|
console.warn("Anim update is out of range.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//TEST ANIMATION
|
|
||||||
|
|
||||||
|
//Progress Animation
|
||||||
|
animProgress() {
|
||||||
|
//animate
|
||||||
|
if (this.animating == true) {
|
||||||
|
if ((this.anim_progress >= 0)
|
||||||
|
&& (this.anim_progress < this.current_progress)
|
||||||
|
&& (this.anim_progress < this.max_progress)) {
|
||||||
|
this.renderProgress(this.anim_progress + 1);
|
||||||
|
setTimeout(this.animProgress.bind(this), 500);
|
||||||
|
}
|
||||||
|
//loop
|
||||||
|
if (this.anim_progress == this.current_progress) {
|
||||||
|
setTimeout(() => this.renderProgress(0), 5000);
|
||||||
|
setTimeout(this.animProgress.bind(this), 5500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Toggle animation on
|
||||||
|
animStart() {
|
||||||
|
if (this.animating != true){
|
||||||
|
this.animating = true;
|
||||||
|
this.animProgress();
|
||||||
|
} else {
|
||||||
|
console.warn("Animation already running");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Toggle animation off
|
||||||
|
animEnd() {
|
||||||
|
this.animating = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//TEST ANIMATION
|
||||||
simProgress() {
|
simProgress() {
|
||||||
if (this.current_progress < this.max_progress) {
|
//sim
|
||||||
|
if (this.simming == true) {
|
||||||
|
if (this.current_progress < this.max_progress) {
|
||||||
this.setProgress(this.current_progress + 1);
|
this.setProgress(this.current_progress + 1);
|
||||||
setTimeout(this.simProgress.bind(this), 500);
|
setTimeout(this.simProgress.bind(this), 38500);
|
||||||
}
|
}
|
||||||
if (this.current_progress == this.max_progress) {
|
//loop
|
||||||
setTimeout(() => this.setProgress(0), 5000);
|
if (this.current_progress == this.max_progress) {
|
||||||
setTimeout(this.simProgress.bind(this), 5500);
|
setTimeout(() => this.setProgress(0), 10000);
|
||||||
|
setTimeout(this.simProgress.bind(this), 95500);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
simStart() {
|
||||||
|
console.log("starting");
|
||||||
|
this.simming = true;
|
||||||
|
this.simProgress();
|
||||||
|
console.log("started");
|
||||||
|
}
|
||||||
|
|
||||||
|
simEnd() {
|
||||||
|
console.log("ending");
|
||||||
|
this.simming = false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class ReconnectingWebSocket {
|
class ReconnectingWebSocket {
|
||||||
@@ -98,8 +180,7 @@ class ReconnectingWebSocket {
|
|||||||
console.log('Closing Websocket.')
|
console.log('Closing Websocket.')
|
||||||
} {
|
} {
|
||||||
console.warn('WebSocket closed. Reconnecting... (disabled for now)');
|
console.warn('WebSocket closed. Reconnecting... (disabled for now)');
|
||||||
//this.reconnect();
|
this.reconnect();
|
||||||
//TODO turn that back on ^
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -151,7 +232,7 @@ function communicate(websocket, book_bar) {
|
|||||||
console.log("Communicating");
|
console.log("Communicating");
|
||||||
const params = new URLSearchParams(window.location.search);
|
const params = new URLSearchParams(window.location.search);
|
||||||
// INITIALIZE CLASS HERE
|
// INITIALIZE CLASS HERE
|
||||||
book_bar.simProgress();
|
book_bar.animStart();
|
||||||
|
|
||||||
websocket.onopen = () => {
|
websocket.onopen = () => {
|
||||||
websocket.send(
|
websocket.send(
|
||||||
@@ -176,16 +257,16 @@ function communicate(websocket, book_bar) {
|
|||||||
switch (event.method) {
|
switch (event.method) {
|
||||||
case "setTimer":
|
case "setTimer":
|
||||||
//do something with the new data
|
//do something with the new data
|
||||||
console.log("set timer: " + event);
|
console.log("set timer: " + args.reward_progress);
|
||||||
book_bar.setProgress(event.reward_progress);
|
book_bar.setProgress(args.reward_progress);
|
||||||
break;
|
break;
|
||||||
case "noTimer":
|
case "noTimer":
|
||||||
//do something default
|
//do something default
|
||||||
console.log("no timer: " + event);
|
console.log("no timer: " + args);
|
||||||
break;
|
break;
|
||||||
case "endTimer":
|
case "endTimer":
|
||||||
//do something for the final time
|
//do something for the final time
|
||||||
console.log("end timer: " + event);
|
console.log("end timer: " + args);
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unsupported method requested: ${event.method}.`)
|
throw new Error(`Unsupported method requested: ${event.method}.`)
|
||||||
|
|||||||
Reference in New Issue
Block a user