Skip to content

Commit

Permalink
discard runs
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-vorobiov committed Sep 22, 2024
1 parent 9a292f7 commit f2ed2d2
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 8 deletions.
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.3.4
// @version 0.4.0
// @description Track and see detailed information about your runs
// @author Sneed
// @match https://pmotschmann.github.io/Evolve/
Expand Down
19 changes: 17 additions & 2 deletions src/history.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { saveHistory, loadHistory } from "./database";
import { inferResetType, type LatestRun } from "./runTracking";
import { Subscribable } from "./subscribable";
import { rotateMap } from "./utils"
import type { Game } from "./game";

Expand All @@ -16,15 +17,21 @@ export type RunHistory = {
runs: HistoryEntry[]
}

export class HistoryManager {
export class HistoryManager extends Subscribable {
private game: Game;
private history: RunHistory;
public milestones: Record<number, string>;

constructor(game: Game, history: RunHistory) {
super();

this.game = game;
this.history = history;
this.milestones = rotateMap(history.milestones);

this.on("*", () => {
saveHistory(this.history);
});
}

get milestoneIDs() {
Expand All @@ -35,6 +42,14 @@ export class HistoryManager {
return this.history.runs;
}

discardRun(run: HistoryEntry) {
const idx = this.runs.indexOf(run);
if (idx !== -1) {
this.history.runs.splice(idx, 1);
this.emit("updated", this);
}
}

commitRun(runStats: LatestRun) {
const resetType = inferResetType(runStats, this.game);

Expand All @@ -49,7 +64,7 @@ export class HistoryManager {
milestones
});

saveHistory(this.history);
this.emit("updated", this);
}

getMilestone(id: number): string {
Expand Down
7 changes: 7 additions & 0 deletions src/ui/analyticsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,12 @@ export function buildAnalyticsTab(config: ConfigManager, history: HistoryManager
}
config.on("viewAdded", addViewTab);

// Redraw each view whenever history is updated
history.on("updated", () => {
for (const view of config.views) {
config.emit("viewUpdated", view);
}
});

analyticsPanel.tabs({ active: 0 });
}
11 changes: 10 additions & 1 deletion src/ui/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function* milestoneMarks(plotPoints: PlotPoint[], key: "day" | "segment", histor
yield* pointerMarsk(plotPoints, key, history);
}

export function makeGraph(history: HistoryManager, view: View) {
export function makeGraph(history: HistoryManager, view: View, onSelect: (run: HistoryEntry | null) => void) {
const filteredRuns = applyFilters(history, view);
const plotPoints = asPlotPoints(filteredRuns, history, view);

Expand Down Expand Up @@ -136,6 +136,15 @@ export function makeGraph(history: HistoryManager, view: View) {
marks
});

node.addEventListener("mousedown", () => {
if (node.value) {
onSelect(filteredRuns[node.value.run]);
}
else {
onSelect(null);
}
});

const legendMilestones = $(node).find("> div > span");

legendMilestones
Expand Down
19 changes: 16 additions & 3 deletions src/ui/viewTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { makeGraph } from "./graph";
import { makeViewSettings } from "./viewSettings";
import { makeMilestoneSettings } from "./milestoneSettings";
import type { ConfigManager, View } from "../config";
import type { HistoryManager } from "../history";
import type { HistoryEntry, HistoryManager } from "../history";

function viewTitle(view: View) {
let title = resets[view.resetType];
Expand All @@ -22,15 +22,28 @@ export function makeViewTab(id: string, view: View, config: ConfigManager, histo
const removeViewNode = $(`<button class="button right" style="margin-right: 1em">Delete View</button>`)
.on("click", () => { config.removeView(view); });

let selectedRun: HistoryEntry | null = null;

const discardRunNode = $(`<button class="button" style="margin-right: 1em">Discard Run</button>`)
.on("click", () => { history.discardRun(selectedRun!); })
.hide();

function onRunSelection(run: HistoryEntry | null) {
selectedRun = run;
discardRunNode.toggle(selectedRun !== null);
}

contentNode
.append(makeViewSettings(view).css("margin-bottom", "1em"))
.append(makeMilestoneSettings(view).css("margin-bottom", "1em"))
.append(makeGraph(history, view))
.append(makeGraph(history, view, onRunSelection))
.append(discardRunNode)
.append(removeViewNode);

config.on("viewUpdated", compose([weakFor(view), invokeFor(view)], (updatedView) => {
controlNode.find("> a").text(viewTitle(updatedView));
contentNode.find("figure:last").replaceWith(makeGraph(history, updatedView));
contentNode.find("figure:last").replaceWith(makeGraph(history, updatedView, onRunSelection));
onRunSelection(null);
}));

return [controlNode, contentNode];
Expand Down
22 changes: 21 additions & 1 deletion test/history.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,28 @@ describe("History", () => {
const history = new HistoryManager(game, blankHistory());

history.commitRun({ run: 123, universe: "standard", resets: {}, totalDays: 456, milestones: {} });
history.commitRun({ run: 124, universe: "standard", resets: {}, totalDays: 789, milestones: {} });

expect(loadHistory()).not.toBe(null);
expect(loadHistory()).toEqual({
milestones: {
"reset:bioseed": 0
},
runs: [
{ run: 123, universe: "standard", milestones: [[0, 456]] },
{ run: 124, universe: "standard", milestones: [[0, 789]] }
]
});

history.discardRun(history.runs[0]);

expect(loadHistory()).toEqual({
milestones: {
"reset:bioseed": 0
},
runs: [
{ run: 124, universe: "standard", milestones: [[0, 789]] }
]
});
});

it("should add the reset point as a milestone", () => {
Expand Down

0 comments on commit f2ed2d2

Please sign in to comment.