AotDS, captain log 7

January 29th, 2021
aotdsmicroblogupdux
#aotds, captain log 7: The game state is now contained into a Updux store. It makes things more structured and leverages the Redux browser devtools for sweet visualization of the state and its changes.

If you are curious, the binding of the Updux store to the svelte reactive store and the enabling of Redux devtools goodness look like:

import { writable } from "svelte/store";
import dux from "./battle/dux";
import { compose, applyMiddleware } from "redux";

const composeEnhancers =
  (typeof window !== "undefined" &&
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ||
  compose;

const duxStore = dux.createStore({}, (mw) =>
    composeEnhancers(applyMiddleware(mw))
);

const store = writable(duxStore.getState());

duxStore.subscribe(() => {
    store.set(duxStore.getState());
});

As for the duxStore, where all the logic will reside, it currently looks something like this:

import Updux from "updux";
import u from "@yanick/updeep";
import { action as actionTS, payload } from "ts-action";
import { plot_movement } from "@aotds/aotds-battle";

const action = (name) => actionTS(name, payload());

const dux = new Updux({ initial: {} });

dux.addMutation(action("initBattle"), battle => () => battle);

dux.addMutation(
  action("setFireconTarget"),
  ({ bogey_id, firecon_id, target_id }) =>
    u({
      bogeys: u.mapWhen(
        { id: bogey_id },
        {
          weaponry: {
            firecons: u.mapWhen(
              { id: firecon_id },
              {
                target_id,
              }
            ),
          },
          orders: {
            firecons: u.mapWhenElse(
              { firecon_id },
              { target_id },
              { target_id, firecon_id }
            ),
          },
        }
      ),
    })
);

dux.addMutation(action("setNavigationOrder"), ({ bogey_id: id, order }) =>
  u({
    bogeys: u.mapWhen({ id }, (bogey) => {
      const course = plot_movement(
        u.updateIn("orders.navigation", order, bogey)
      );

      return u(
        {
          navigation: { course },
          orders: { navigation: course.orders },
        },
        bogey
      );
    }),
  })
);

dux.addMutation(
  action("assignWeaponToFirecon"),
  ({ bogey_id, firecon_id, weapon_id }) =>
    u({
      bogeys: u.mapWhen(
        { id: bogey_id },
        {
          weaponry: {
            weapons: u.mapWhen({ id: weapon_id }, { firecon_id }),
          },
          orders: {
            weapons: u.mapWhenElse(
              { weapon_id },
              { firecon_id },
              {
                weapon_id,
                firecon_id,
              }
            ),
          },
        }
      ),
    })
);

export default dux.asDux;