Skip to content

Commit

Permalink
change milestone format
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-vorobiov committed Sep 20, 2024
1 parent 4e75eb8 commit 36c995c
Show file tree
Hide file tree
Showing 35 changed files with 2,659 additions and 1,470 deletions.
4 changes: 2 additions & 2 deletions demo/config.3.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const config = {
"mode": "Total (filled)",
"resetType": "Ascension",
"universe": "heavy",
"numRuns": "50"
"numRuns": 50
},
{
"milestones": [
Expand All @@ -35,7 +35,7 @@ const config = {
"universe": "heavy"
}
]
}
};

function resetConfig() {
localStorage.setItem("sneed.analytics.config", JSON.stringify(config));
Expand Down
2 changes: 1 addition & 1 deletion demo/history.3.js
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ const history = {
"milestones": [[8, 78], [0, 97], [1, 200], [2, 268], [3, 381], [4, 520], [5, 564], [6, 576], [7, 599]]
}
]
}
};

function resetHistory() {
localStorage.setItem("sneed.analytics.history", JSON.stringify(history));
Expand Down
2 changes: 2 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="./config.3.js"></script>
<script type="text/javascript" src="./history.3.js"></script>
<script type="text/javascript" src="./latest.3.js"></script>
<script type="text/javascript" src="../build/evolve_analytics.user.js"></script>
<script lang="js">
window.evolve = {
Expand Down Expand Up @@ -79,6 +80,7 @@
<div class="settings sticky" role="tabpanel" id="settings" tabindex="0">
<button class="button" style="margin-right: 1em" onclick="resetConfig()">Reset views</button>
<button class="button" style="margin-right: 1em" onclick="resetHistory()">Reset History</button>
<button class="button" style="margin-right: 1em" onclick="resetLatestRun()">Reset Latest Run</button>
</div>
<div class="tab-item" role="tabpanel" id="128-content" tabindex="-1" style="display: none;"></div>
</section>
Expand Down
26 changes: 26 additions & 0 deletions demo/latest.3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const latestRun = {
run: 626,
universe: "heavy",
totalDays: 179,
resets: {
"MAD": 234,
"Bioseed": 47,
"Cataclysm": 2,
"Black Hole": 17,
"Ascension": 317,
"Demonic Infusion": 3,
"AI Apocalypse": 1,
"Matrix": 1,
"Retirement": 1,
"Garden of Eden": 1,
"Terraform": 1
},
milestones: {
"Womlings arrival": 10,
"Launch Facility": 96
}
};

function resetLatestRun() {
localStorage.setItem("sneed.analytics.latest", JSON.stringify(latestRun));
}
2 changes: 1 addition & 1 deletion evolve_analytics.meta.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name Evolve Analytics
// @namespace http://tampermonkey.net/
// @version 0.2.1
// @version 0.3.0
// @description Track and see detailed information about your runs
// @author Sneed
// @match https://pmotschmann.github.io/Evolve/
Expand Down
77 changes: 36 additions & 41 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { saveConfig, loadConfig } from "./database";
import { milestoneEnabled, milestoneName, milestoneType, type Milestone } from "./milestones";
import { Subscribable } from "./subscribable";
import type { resets, universes, viewModes } from "./enums";
import type { Game } from "./game";

export type ViewConfig = {
resetType: string,
universe?: string,
mode: string,
resetType: keyof typeof resets,
universe?: keyof typeof universes,
mode: keyof typeof viewModes,
daysScale?: number,
numRuns?: number,
milestones: Milestone[]
milestones: Record<string, boolean>
}

export type View = ViewConfig & {
toggleMilestone(milestone: string): void;
addMilestone(milestone: Milestone): void;
removeMilestone(milestone: Milestone): void;
addMilestone(milestone: string): void;
removeMilestone(milestone: string): void;
}

export type Config = {
Expand All @@ -27,26 +27,24 @@ function makeViewProxy(config: ConfigManager, view: ViewConfig): View {
return <View> new Proxy(view, {
get(obj, prop, receiver) {
if (prop === "toggleMilestone") {
return (name: string) => {
const milestone = view.milestones.find(m => milestoneName(m) === name);
if (milestone !== undefined) {
milestone[milestone.length - 1] = !milestone[milestone.length - 1];
return (milestone: string) => {
const enabled = view.milestones[milestone];
if (enabled !== undefined) {
view.milestones[milestone] = !enabled;
config.emit("viewUpdated", receiver);
}
};
}
else if (prop === "addMilestone") {
return (milestone: Milestone) => {
view.milestones.push(milestone);
return (milestone: string) => {
view.milestones[milestone] = true;
config.emit("viewUpdated", receiver);
};
}
else if (prop === "removeMilestone") {
return (milestone: Milestone) => {
const signature = milestone.slice(0, -1).join(":");
const idx = view.milestones.findIndex(m => m.slice(0, -1).join(":") === signature);
if (idx !== -1) {
view.milestones.splice(idx, 1);
return (milestone: string) => {
if (milestone in view.milestones) {
delete view.milestones[milestone];
config.emit("viewUpdated", receiver);
}
};
Expand All @@ -56,15 +54,17 @@ function makeViewProxy(config: ConfigManager, view: ViewConfig): View {
}
},
set(obj, prop, value, receiver) {
const ret = Reflect.set(obj, prop, value, receiver);
if (value === view[prop as keyof ViewConfig]) {
return true;
}

if (prop === "resetType") {
const milestone = obj.milestones.find(m => milestoneType(m) === "Reset");
if (milestone !== undefined) {
milestone[1] = value;
}
delete view.milestones[`reset:${view.resetType}`];
view.milestones[`reset:${value}`] = true;
}

const ret = Reflect.set(obj, prop, value, receiver);

config.emit("viewUpdated", receiver);

return ret;
Expand All @@ -73,7 +73,7 @@ function makeViewProxy(config: ConfigManager, view: ViewConfig): View {
}

export class ConfigManager extends Subscribable {
milestones: Milestone[];
milestones: string[];
views: View[];

constructor(private game: Game, private config: Config) {
Expand All @@ -95,10 +95,10 @@ export class ConfigManager extends Subscribable {

addView() {
const view: ViewConfig = {
resetType: "Ascension",
resetType: "ascend",
universe: this.game.universe,
mode: "Total (filled)",
milestones: [["Reset", "Ascension", true]]
mode: "filled",
milestones: { "reset:ascend": true }
};

const proxy = makeViewProxy(this, view);
Expand All @@ -119,23 +119,18 @@ export class ConfigManager extends Subscribable {
}

private collectMilestones() {
const milestones = this.config.views.flatMap(v => {
return v.milestones
.filter(milestoneEnabled)
.filter(m => milestoneType(m) !== "Reset");
});

const uniqueMilestones: Map<string, Milestone> = new Map();

for (const milestone of milestones) {
uniqueMilestones.set(milestone.join(":"), milestone);
}

return [...uniqueMilestones.values()];
const uniqueMilestones = new Set(this.config.views.flatMap(v => {
return Object.entries(v.milestones)
.filter(([, enabled]) => enabled)
.filter(([milestone]) => !milestone.startsWith("reset:"))
.map(([milestone]) => milestone);
}));

return Array.from(uniqueMilestones);
}
}

export function getConfig(game: Game) {
const config = loadConfig() ?? { version: 3, views: [] };
const config = loadConfig() ?? { version: 4, views: [] };
return new ConfigManager(game, config);
}
Loading

0 comments on commit 36c995c

Please sign in to comment.