All files / src/lib/project/migration/versions 410to420.ts

100% Statements 47/47
95.45% Branches 21/22
100% Functions 8/8
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 1399x               9x     6x 5x 5x 80x 80x 80x   80x   80x         5x         1x     9x           9x     9x 5x   5x       5x         5x         4x     9x           9x     7x         4x   4x 4x         3x     9x 9x 7x   4x 1x           3x       7x 14x     7x       14x 13x             7x 7x   7x                 9x                
import {
  ScriptEventMigrationFn,
  ProjectResourcesMigration,
  createScriptEventsMigrator,
  ProjectResourcesMigrationFn,
  pipeMigrationFns,
} from "lib/project/migration/helpers";
 
export const migrateFrom410r1To420r1Event: ScriptEventMigrationFn = (
  scriptEvent,
) => {
  if (scriptEvent.args && scriptEvent.command === "EVENT_SWITCH") {
    const args: Record<string, unknown> = { ...scriptEvent.args };
    for (let i = 0; i < 16; i++) {
      const key = `value${i}`;
      const defaultValue = i + 1;
      const storedValue = args[key];
      const value =
        typeof storedValue === "number" ? storedValue : defaultValue;
      // Convert to constvalue
      args[key] = {
        type: "number",
        value,
      };
    }
    return {
      ...scriptEvent,
      args,
    };
  }
  return scriptEvent;
};
 
export const migrate410r1To420r1: ProjectResourcesMigration = {
  from: { version: "4.1.0", release: "1" },
  to: { version: "4.2.0", release: "1" },
  migrationFn: createScriptEventsMigrator(migrateFrom410r1To420r1Event),
};
 
export const migrateFrom420r1To420r2Event: ScriptEventMigrationFn = (
  scriptEvent,
) => {
  if (scriptEvent.args && scriptEvent.command === "EVENT_WAIT") {
    const args: Record<string, unknown> = { ...scriptEvent.args };
    // Convert to constvalue
    args["frames"] = {
      type: "number",
      value: typeof args["frames"] === "number" ? args["frames"] : 30,
    };
    args["time"] = {
      type: "number",
      value: typeof args["time"] === "number" ? args["time"] : 0.5,
    };
 
    return {
      ...scriptEvent,
      args,
    };
  }
  return scriptEvent;
};
 
export const migrate420r1To420r2: ProjectResourcesMigration = {
  from: { version: "4.2.0", release: "1" },
  to: { version: "4.2.0", release: "2" },
  migrationFn: createScriptEventsMigrator(migrateFrom420r1To420r2Event),
};
 
export const migrateFrom420r2To420r3Event: ScriptEventMigrationFn = (
  scriptEvent,
) => {
  if (
    scriptEvent.args &&
    (scriptEvent.command === "EVENT_ACTOR_MOVE_TO" ||
      scriptEvent.command === "EVENT_ACTOR_MOVE_RELATIVE")
  ) {
    const args: Record<string, unknown> = { ...scriptEvent.args };
    // If useCollisions was set default to all collisions
    args["collideWith"] = args["useCollisions"] ? ["walls", "actors"] : [];
    return {
      ...scriptEvent,
      args,
    };
  }
  return scriptEvent;
};
 
export const migrateFrom420r2To420r3EngineFields: ProjectResourcesMigrationFn =
  (resources) => {
    const engineFieldValues = resources.engineFieldValues.engineFieldValues.map(
      (fieldValue) => {
        if (fieldValue.id === "shooter_scroll_speed") {
          return {
            ...fieldValue,
            value:
              typeof fieldValue.value === "number" ? fieldValue.value * 2 : 0,
          };
        }
        return fieldValue;
      },
    );
 
    const hasFieldValue = (id: string) => {
      return engineFieldValues.some((fieldValue) => fieldValue.id === id);
    };
 
    const setDefaultFieldValue = (
      id: string,
      value: string | number | undefined,
    ) => {
      if (!hasFieldValue(id)) {
        engineFieldValues.push({
          id: id,
          value,
        });
      }
    };
 
    setDefaultFieldValue("FEAT_PLATFORM_COYOTE_TIME", 0);
    setDefaultFieldValue("FEAT_PLATFORM_DROP_THROUGH", 0);
 
    return {
      ...resources,
      engineFieldValues: {
        ...resources.engineFieldValues,
        engineFieldValues,
      },
    };
  };
 
export const migrate420r2To420r3: ProjectResourcesMigration = {
  from: { version: "4.2.0", release: "2" },
  to: { version: "4.2.0", release: "3" },
  migrationFn: pipeMigrationFns([
    createScriptEventsMigrator(migrateFrom420r2To420r3Event),
    migrateFrom420r2To420r3EngineFields,
  ]),
};