From 6d8684619bbd3b7c9236b84d795fbdef2aa5bd70 Mon Sep 17 00:00:00 2001
From: zkldi <20380519+zkldi@users.noreply.github.com>
Date: Thu, 19 Jan 2023 00:46:31 +0000
Subject: [PATCH] fix incorrect goal migrations
---
.../tables/cells/RatingSystemPart.tsx | 6 ++++-
.../src/lib/migration/migrations/v3-props.ts | 25 ++++++++++---------
server/src/lib/targets/goal-utils.ts | 9 +++----
server/src/lib/targets/goals.ts | 5 ++--
server/src/utils/misc.ts | 15 +++++++++++
5 files changed, 39 insertions(+), 21 deletions(-)
diff --git a/client/src/components/tables/cells/RatingSystemPart.tsx b/client/src/components/tables/cells/RatingSystemPart.tsx
index 316c4bea1..b009a07f1 100644
--- a/client/src/components/tables/cells/RatingSystemPart.tsx
+++ b/client/src/components/tables/cells/RatingSystemPart.tsx
@@ -1,3 +1,4 @@
+import { IsNotNullish } from "util/misc";
import QuickTooltip from "components/layout/misc/QuickTooltip";
import Icon from "components/util/Icon";
import Muted from "components/util/Muted";
@@ -60,7 +61,10 @@ export default function RatingSystemPart({ chart, game }: { chart: ChartDocument
>
- {ratingSystems.map((r) => r.toString(chart as any)).join(" / ")}
+ {ratingSystems
+ .map((r) => r.toString(chart as any))
+ .filter((e) => IsNotNullish(e))
+ .join(" / ")}
diff --git a/server/src/lib/migration/migrations/v3-props.ts b/server/src/lib/migration/migrations/v3-props.ts
index 1b87eadee..a27ad0e09 100644
--- a/server/src/lib/migration/migrations/v3-props.ts
+++ b/server/src/lib/migration/migrations/v3-props.ts
@@ -11,18 +11,19 @@ async function UpdateGoals(
) {
const goals = await db.goals.find(query);
- return Promise.all(
- goals.map((oldGoal) =>
- EditGoal(oldGoal, {
- ...oldGoal,
- criteria: {
- ...oldGoal.criteria,
- key: newCriteriaKey,
- value: newCriteriaValue?.(oldGoal) ?? oldGoal.criteria.value,
- },
- })
- )
- );
+ for (const oldGoal of goals) {
+ // too lazy to parallelise this wrt. the quest changes.
+ // ah well
+ // eslint-disable-next-line no-await-in-loop
+ await EditGoal(oldGoal, {
+ ...oldGoal,
+ criteria: {
+ ...oldGoal.criteria,
+ key: newCriteriaKey,
+ value: newCriteriaValue?.(oldGoal) ?? oldGoal.criteria.value,
+ },
+ });
+ }
}
function perToScore(goal: GoalDocument) {
diff --git a/server/src/lib/targets/goal-utils.ts b/server/src/lib/targets/goal-utils.ts
index 907fc47a6..f1008dc71 100644
--- a/server/src/lib/targets/goal-utils.ts
+++ b/server/src/lib/targets/goal-utils.ts
@@ -9,11 +9,8 @@ import {
} from "tachi-common";
import { GetFolderForIDGuaranteed, HumaniseChartID } from "utils/db";
import { GetFolderChartIDs } from "utils/folder";
-import { NumToDP, HumanisedJoinArray } from "utils/misc";
-import type {
- ChartSpecificMetricValidator,
- GoalCriteriaFormatter,
-} from "game-implementations/types";
+import { HumanisedJoinArray, OnlyFloatToDP } from "utils/misc";
+import type { GoalCriteriaFormatter } from "game-implementations/types";
import type { GPTString, Game, GoalDocument, Playtype } from "tachi-common";
export async function CreateGoalTitle(
@@ -74,7 +71,7 @@ export async function CreateGoalTitle(
// See above about switch exhaustivity
// eslint-disable-next-line no-fallthrough
case "proportion": {
- const propFormat = NumToDP(criteria.countNum * 100);
+ const propFormat = OnlyFloatToDP(criteria.countNum * 100);
switch (charts.type) {
case "multi":
diff --git a/server/src/lib/targets/goals.ts b/server/src/lib/targets/goals.ts
index b720a7772..3a7cb7fe5 100644
--- a/server/src/lib/targets/goals.ts
+++ b/server/src/lib/targets/goals.ts
@@ -712,6 +712,9 @@ export async function EditGoal(oldGoal: GoalDocument, newGoal: GoalDocument) {
newGoal.playtype
);
+ // eslint-disable-next-line require-atomic-updates
+ newGoal.goalID = newGoalID;
+
await db["goal-subs"].update(
{
goalID: oldGoal.goalID,
@@ -754,8 +757,6 @@ export async function EditGoal(oldGoal: GoalDocument, newGoal: GoalDocument) {
await db.goals.remove({ goalID: oldGoal.goalID });
- // eslint-disable-next-line require-atomic-updates
- newGoal.goalID = newGoalID;
// eslint-disable-next-line require-atomic-updates
newGoal.name = await CreateGoalName(
newGoal.charts,
diff --git a/server/src/utils/misc.ts b/server/src/utils/misc.ts
index cf08c9fb5..7da7c47f3 100644
--- a/server/src/utils/misc.ts
+++ b/server/src/utils/misc.ts
@@ -236,6 +236,21 @@ export function NumToDP(num: number, points = 2) {
return num.toFixed(points);
}
+/**
+ * If the input is a decimal, format it to N decimal places, otherwise,
+ * format it to 0dp, i.e.
+ *
+ * 10.053 -> 10.05
+ * 10 -> 10
+ */
+export function OnlyFloatToDP(num: number, points = 2) {
+ if (Number.isInteger(num)) {
+ return num.toFixed(0);
+ }
+
+ return num.toFixed(points);
+}
+
/**
* Returns whether this game is supported by this instance of tachi or not.
*/