1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00
tobspr_shapez.io/src/ts/game/achievement_proxy.ts

105 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-11-18 01:33:12 +00:00
/* typehints:start */
import type { Entity } from "./entity";
import type { GameRoot } from "./root";
/* typehints:end */
import { globalConfig } from "../core/config";
import { createLogger } from "../core/logging";
import { ACHIEVEMENTS } from "../platform/achievement_provider";
import { getBuildingDataFromCode } from "./building_codes";
2022-11-18 15:20:54 +00:00
const logger = createLogger("achievement_proxy");
const ROTATER = "rotater";
const DEFAULT = "default";
2022-11-18 01:33:12 +00:00
export class AchievementProxy {
public root = root;
public provider = this.root.app.achievementProvider;
public disabled = true;
public sliceTime = 0;
constructor(root) {
if (G_IS_DEV && globalConfig.debug.testAchievements) {
// still enable the proxy
}
else if (!this.provider.hasAchievements()) {
return;
}
this.root.signals.postLoadHook.add(this.onLoad, this);
}
2022-11-18 15:20:54 +00:00
onLoad() {
2022-11-18 01:33:12 +00:00
if (!this.root.gameMode.hasAchievements()) {
logger.log("Disabling achievements because game mode does not have achievements");
this.disabled = true;
return;
}
this.provider
.onLoad(this.root)
2022-11-18 15:20:54 +00:00
.then(() => {
2022-11-18 01:33:12 +00:00
this.disabled = false;
logger.log("Recieving achievement signals");
this.initialize();
})
2022-11-18 15:20:54 +00:00
.catch(err => {
2022-11-18 01:33:12 +00:00
this.disabled = true;
logger.error("Ignoring achievement signals", err);
});
}
2022-11-18 15:20:54 +00:00
initialize() {
2022-11-18 01:33:12 +00:00
this.root.signals.achievementCheck.dispatch(ACHIEVEMENTS.darkMode, null);
if (this.has(ACHIEVEMENTS.mam)) {
this.root.signals.entityAdded.add(this.onMamFailure, this);
this.root.signals.entityDestroyed.add(this.onMamFailure, this);
this.root.signals.storyGoalCompleted.add(this.onStoryGoalCompleted, this);
}
if (this.has(ACHIEVEMENTS.noInverseRotater)) {
this.root.signals.entityAdded.add(this.onEntityAdded, this);
}
this.startSlice();
}
2022-11-18 15:20:54 +00:00
startSlice() {
2022-11-18 01:33:12 +00:00
this.sliceTime = this.root.time.now();
this.root.signals.bulkAchievementCheck.dispatch(ACHIEVEMENTS.storeShape, this.sliceTime, ACHIEVEMENTS.throughputBp25, this.sliceTime, ACHIEVEMENTS.throughputBp50, this.sliceTime, ACHIEVEMENTS.throughputLogo25, this.sliceTime, ACHIEVEMENTS.throughputLogo50, this.sliceTime, ACHIEVEMENTS.throughputRocket10, this.sliceTime, ACHIEVEMENTS.throughputRocket20, this.sliceTime, ACHIEVEMENTS.play1h, this.sliceTime, ACHIEVEMENTS.play10h, this.sliceTime, ACHIEVEMENTS.play20h, this.sliceTime);
}
2022-11-18 15:20:54 +00:00
update() {
2022-11-18 01:33:12 +00:00
if (this.disabled) {
return;
}
if (this.root.time.now() - this.sliceTime > globalConfig.achievementSliceDuration) {
this.startSlice();
}
}
/**
* {}
*/
has(key: string): boolean {
if (!this.provider.collection) {
return false;
}
return this.provider.collection.map.has(key);
}
2022-11-18 15:20:54 +00:00
onEntityAdded(entity: Entity) {
2022-11-18 01:33:12 +00:00
if (!entity.components.StaticMapEntity) {
return;
}
2022-11-18 15:20:54 +00:00
const building = getBuildingDataFromCode(entity.components.StaticMapEntity.code);
2022-11-18 01:33:12 +00:00
if (building.metaInstance.id !== ROTATER) {
return;
}
if (building.variant === DEFAULT) {
return;
}
this.root.savegame.currentData.stats.usedInverseRotater = true;
this.root.signals.entityAdded.remove(this.onEntityAdded);
}
2022-11-18 15:20:54 +00:00
onStoryGoalCompleted(level: number) {
2022-11-18 01:33:12 +00:00
if (level > 26) {
this.root.signals.entityAdded.add(this.onMamFailure, this);
this.root.signals.entityDestroyed.add(this.onMamFailure, this);
}
this.root.signals.achievementCheck.dispatch(ACHIEVEMENTS.mam, null);
// reset on every level
this.root.savegame.currentData.stats.failedMam = false;
}
2022-11-18 15:20:54 +00:00
onMamFailure() {
2022-11-18 01:33:12 +00:00
this.root.savegame.currentData.stats.failedMam = true;
}
}