mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-09 16:46:00 +00:00
Added analytics
This commit is contained in:
44
assets/Scripts/AppRoot/Analytics.ts
Normal file
44
assets/Scripts/AppRoot/Analytics.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { Y8 } from "../../Plugins/Y8/Scripts/Y8";
|
||||
|
||||
export class Analytics {
|
||||
private totalTime = 0;
|
||||
private minutesInGame = -1; // Track the 0 minute as well
|
||||
private gamesPerSession = 0;
|
||||
|
||||
public constructor(private y8: Y8) {}
|
||||
|
||||
public update(deltaTime: number): void {
|
||||
this.totalTime += deltaTime;
|
||||
this.trySendTotalTime();
|
||||
}
|
||||
|
||||
public gameStart(): void {
|
||||
this.y8.sendCustomEvent(EventName.GAMES_PER_SESSION, ++this.gamesPerSession);
|
||||
}
|
||||
|
||||
public gameEnd(time: number): void {
|
||||
this.y8.sendCustomEvent(EventName.GAME_TIME, Math.floor(time));
|
||||
}
|
||||
|
||||
public gameExit(time: number): void {
|
||||
this.y8.sendCustomEvent(EventName.GAME_EXIT, Math.floor(time));
|
||||
}
|
||||
|
||||
public goldPerRun(goldEarned: number): void {
|
||||
this.y8.sendCustomEvent(EventName.GOLD_PER_RUN, Math.floor(goldEarned));
|
||||
}
|
||||
|
||||
private trySendTotalTime(): void {
|
||||
if (this.minutesInGame < Math.floor(this.totalTime / 60)) {
|
||||
this.y8.sendCustomEvent(EventName.TOTAL_TIME, ++this.minutesInGame);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum EventName {
|
||||
TOTAL_TIME = "Minutes_total",
|
||||
GOLD_PER_RUN = "Gold_per_run",
|
||||
GAMES_PER_SESSION = "Games_per_session",
|
||||
GAME_TIME = "Game_time_seconds",
|
||||
GAME_EXIT = "Game_exit"
|
||||
}
|
9
assets/Scripts/AppRoot/Analytics.ts.meta
Normal file
9
assets/Scripts/AppRoot/Analytics.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "58a3e586-6c96-4f84-8682-7a73b1b72b9f",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -7,6 +7,8 @@ import { AudioPlayer } from "../Services/AudioPlayer/AudioPlayer";
|
||||
import { SaveSystem } from "./SaveSystem";
|
||||
import { ModalWindowManager } from "../Services/ModalWindowSystem/ModalWindowManager";
|
||||
import { OpenCloseAnimator } from "../Utils/OpenCloseAnimator";
|
||||
import { Y8 } from "../../Plugins/Y8/Scripts/Y8";
|
||||
import { Analytics } from "./Analytics";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("AppRoot")
|
||||
@@ -18,12 +20,14 @@ export class AppRoot extends Component {
|
||||
@property(Camera) private mainCamera: Camera;
|
||||
@property(ModalWindowManager) private modalWindowManager: ModalWindowManager;
|
||||
@property(OpenCloseAnimator) private screenFader: OpenCloseAnimator;
|
||||
@property(Y8) private y8: Y8;
|
||||
|
||||
private static instance: AppRoot;
|
||||
private saveSystem: SaveSystem;
|
||||
|
||||
private liveUserData: UserData;
|
||||
private gameAssets: GameAssets;
|
||||
private analytics: Analytics;
|
||||
|
||||
public static get Instance(): AppRoot {
|
||||
return this.instance;
|
||||
@@ -61,6 +65,14 @@ export class AppRoot extends Component {
|
||||
return this.screenFader;
|
||||
}
|
||||
|
||||
public get Y8(): Y8 {
|
||||
return this.y8;
|
||||
}
|
||||
|
||||
public get Analytics(): Analytics {
|
||||
return this.analytics;
|
||||
}
|
||||
|
||||
public saveUserData(): void {
|
||||
this.saveSystem.save(this.liveUserData);
|
||||
}
|
||||
@@ -75,7 +87,11 @@ export class AppRoot extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
private init(): void {
|
||||
public update(deltaTime: number): void {
|
||||
if (this.analytics) this.analytics.update(deltaTime);
|
||||
}
|
||||
|
||||
private async init(): Promise<void> {
|
||||
this.saveSystem = new SaveSystem();
|
||||
this.liveUserData = this.saveSystem.load();
|
||||
|
||||
@@ -88,5 +104,9 @@ export class AppRoot extends Component {
|
||||
|
||||
this.screenFader.init();
|
||||
this.screenFader.node.active = false;
|
||||
|
||||
await this.y8.init();
|
||||
|
||||
this.analytics = new Analytics(this.y8);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user