Repository: sunflower-farmers/sunflower-farmers
File: src/features/game/lib/gameMachine.ts (TypeScript)

Code before:
  | { type: "EXPAND" }
  | { type: "SAVE_SUCCESS" }
  | { type: "UPGRADE" }
  | { type: "CLOSE" }
  | { type: "RANDOMISE" }
  | DepositFlowerFromLinkedWalletEvent
  | { type: StateMachineVisitEffectName }
  | Effect; // Test only

const playingEventHandler = (eventName: string) => {
  return {
    [eventName]: [
      {
        target: "hoarding",
        cond: (context: Context, event: PlayingEvent) => {
          const { valid } = checkProgress({
            state: context.state as GameState,
            action: event,
            farmId: context.farmId,
          });

          return !valid;
        },
        actions: assign((context: Context, event: PlayingEvent) => {
          const { maxedItem } = checkProgress({
            state: context.state as GameState,
            action: event,
            farmId: context.farmId,
          });

          return { maxedItem };
        }),
      },
      {
        actions: assign((context: Context, event: PlayingEvent) => {
          const result = processEvent({
            state: context.state,
            action: event,
            announcements: context.announcements,
            farmId: context.farmId,
            visitorState: context.visitorState,
          });

          const actions = [
            ...context.actions,
            {
              ...event,
              createdAt: new Date(),
            },
          ];


Review comment (bug):
The `gameMachine` updates have caused bugs in the effects again. Unable to craft, fish, airdrop players etc. 👀 

The game freezes or errors when trying to perform these actions

Code after:
  | UpdateUsernameEvent
  | PostEffectEvent
  | { type: "EXPAND" }
  | { type: "SAVE_SUCCESS" }
  | { type: "UPGRADE" }
  | { type: "CLOSE" }
  | { type: "RANDOMISE" }
  | DepositFlowerFromLinkedWalletEvent
  | { type: StateMachineVisitEffectName }
  | Effect; // Test only

const playingEventHandler = (eventName: string) => {
  return {
    [eventName]: [
      {
        target: "hoarding",
        cond: (context: Context, event: PlayingEvent | VisitingEvent) => {
          const { valid } = checkProgress({
            state: context.state as GameState,
            action: event,
            farmId: context.farmId,
          });

          return !valid;
        },
        actions: assign(
          (context: Context, event: PlayingEvent | VisitingEvent) => {
            const { maxedItem } = checkProgress({
              state: context.state as GameState,
              action: event,
              farmId: context.farmId,
            });

            return { maxedItem };
          },
        ),
      },
      {
        actions: assign(
          (context: Context, event: PlayingEvent | VisitingEvent) => {
            const result = processEvent({
              state: context.state,
              action: event,
              announcements: context.announcements,
              farmId: context.farmId,
              visitorState: context.visitorState,
            });

            const actions = [
              ...context.actions,
              {