Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/apis/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import { EventEmitter } from '../core/event_emitter';
* Events:
* - frame: Emitted every frame. Can be used for things that should be executed on every frame
* - frame_end: Emitted after `frame` and is mainly used internally to update position variables
* - state => (state): Emitted whenever the game changes its state: 'home', 'game', 'stats', 'loading', 'captcha
* - s_home: Emitted when the game changes its state to home
* - s_game: Emitted when the game changes its state to game
* - s_stats: Emitted when the game changes its state to stats
* - s_loading: Emitted when the game changes its state to loading
* - s_captcha: Emitted when the game changes its state to captcha
*/
class Game extends EventEmitter {
#ready = false;
#shadowRoot: ShadowRoot;

constructor() {
super();
Expand All @@ -27,6 +34,43 @@ class Game extends EventEmitter {

#onready(): void {
setTimeout(() => super.emit('ready'), 100);

this.#shadowRoot = document.querySelector('d-base').shadowRoot;
new MutationObserver((mutationList, observer) => {
mutationList.forEach((mutation) => {
if (mutation.addedNodes.length === 0) {
return;
}

super.emit('state', this.state);
super.emit(`s_${this.state}`);
return;
});
}).observe(this.#shadowRoot, { childList: true });
}

get state(): string {
return this.#shadowRoot.querySelector('.screen').tagName.slice(2).toLowerCase();
}

get inHome(): boolean {
return this.state == 'home';
}

get inGame(): boolean {
return this.state == 'game';
}

get inStats(): boolean {
return this.state == 'stats';
}

get inLoading(): boolean {
return this.state == 'loading';
}

get isCaptcha(): boolean {
return this.state == 'captcha';
}
}

Expand Down