{
  "schemaVersion": 1,
  "type": "concurrent",
  "questName": "roulette_game",
  "timers": ["roulette_spin"],
  "positions": {
    "roulette_start": {
      "type": "random_room",
      "not_in_secret": true,
      "not_in_vault": true
    }
  },
  "npcs": {
    "roulette_wheel": {
      "display_name": "Roulette Wheel",
      "initialState": {
        "bet_color": { "type": "string", "default": "none", "keyed": true },
        "opposite_color": { "type": "string", "default": "none", "keyed": true },
        "round_in_progress": { "type": "string", "default": "inactive", "keyed": true }
      },
      "solid": true,
      "opaque": false,
      "sprites": {
        "idle": "roulette_wheel_idle",
        "spin": "roulette_wheel_spin"
      },
      "dialogs": {
        "place_bet": {
          "text": "It's 10 gold to play. What do you want to do?",
          "portrait": "placeholder",
          "onAbort": "noop",
          "choices": [
            {
              "text": "Bet on White",
              "action": {
                "action": "call_function",
                "functionName": "bet_white"
              }
            },
            {
              "text": "Bet on Black",
              "action": {
                "action": "call_function",
                "functionName": "bet_black"
              }
            },
            {
              "text": "No Thanks",
              "action": {
                "action": "call_function",
                "functionName": "noop"
              }
            }
          ]
        },
        "wheel_busy": {
          "text": "The roulette wheel is already in use.",
          "portrait": "placeholder",
          "onAbort": "noop"
        },
        "cannot_afford": {
          "text": "You need 10 gold to play.",
          "portrait": "placeholder",
          "onAbort": "noop"
        },
        "house_result": {
          "text": "The ball lands on the house. The house wins.",
          "portrait": "placeholder",
          "onAbort": "finish_loss",
          "choices": [
            {
              "text": "OK",
              "action": {
                "action": "call_function",
                "functionName": "finish_loss"
              }
            }
          ]
        },
        "other_color_result": {
          "text": "The ball lands on [[npc.opposite_color.[[interactor]]]]. The house wins.",
          "portrait": "placeholder",
          "onAbort": "finish_loss",
          "choices": [
            {
              "text": "OK",
              "action": {
                "action": "call_function",
                "functionName": "finish_loss"
              }
            }
          ]
        },
        "winning_result": {
          "text": "The ball lands on [[npc.bet_color.[[interactor]]]]. You win 20 gold.",
          "portrait": "placeholder",
          "onAbort": "finish_win",
          "choices": [
            {
              "text": "OK",
              "action": {
                "action": "call_function",
                "functionName": "finish_win"
              }
            }
          ]
        }
      },
      "conditions": {
        "round_is_active": {
          "type": "compare",
          "opA": "round_in_progress.[[interactor]]",
          "opB": "active",
          "operation": "eq"
        }
      },
      "functions": {
        "on_interact": [
          {
            "action": "check_condition",
            "conditionName": "round_is_active",
            "onTrue": "show_wheel_busy",
            "onFalse": "show_place_bet"
          }
        ],
        "on_busy": [
          {
            "action": "dialog",
            "dialogId": "wheel_busy"
          }
        ],
        "show_place_bet": [
          {
            "action": "dialog",
            "dialogId": "place_bet"
          }
        ],
        "show_wheel_busy": [
          {
            "action": "dialog",
            "dialogId": "wheel_busy"
          }
        ],
        "show_cannot_afford": [
          {
            "action": "dialog",
            "dialogId": "cannot_afford"
          }
        ],
        "bet_white": [
          {
            "action": "set_state",
            "stateRef": "bet_color.[[interactor]]",
            "value": "White"
          },
          {
            "action": "set_state",
            "stateRef": "opposite_color.[[interactor]]",
            "value": "Black"
          },
          {
            "action": "call_function",
            "functionName": "begin_spin"
          }
        ],
        "bet_black": [
          {
            "action": "set_state",
            "stateRef": "bet_color.[[interactor]]",
            "value": "Black"
          },
          {
            "action": "set_state",
            "stateRef": "opposite_color.[[interactor]]",
            "value": "White"
          },
          {
            "action": "call_function",
            "functionName": "begin_spin"
          }
        ],
        "begin_spin": [
          {
            "action": "take_gold",
            "sourceRef": "interactor",
            "amount": 10,
            "onFailure": "show_cannot_afford"
          },
          {
            "action": "set_state",
            "stateRef": "round_in_progress.[[interactor]]",
            "value": "active"
          },
          {
            "action": "set_npc_sprite",
            "targetName": "npc",
            "mode": "one_shot",
            "sprite": "roulette_wheel_spin"
          },
          {
            "action": "set_timeout",
            "timerName": "roulette_spin",
            "functionName": "resolve_spin",
            "ticks": 4
          }
        ],
        "resolve_spin": [
          {
            "action": "random_function",
            "outcomes": [
              {
                "weight": 1,
                "functionName": "show_house_result"
              },
              {
                "weight": 18,
                "functionName": "show_other_color_result"
              },
              {
                "weight": 18,
                "functionName": "show_winning_result"
              }
            ]
          }
        ],
        "show_house_result": [
          {
            "action": "dialog",
            "dialogId": "house_result"
          }
        ],
        "show_other_color_result": [
          {
            "action": "dialog",
            "dialogId": "other_color_result"
          }
        ],
        "show_winning_result": [
          {
            "action": "dialog",
            "dialogId": "winning_result"
          }
        ],
        "finish_loss": [
          {
            "action": "call_function",
            "functionName": "reset_wheel"
          },
          {
            "action": "complete_quest"
          }
        ],
        "finish_win": [
          {
            "action": "give_gold",
            "targetRef": "interactor",
            "amount": 20
          },
          {
            "action": "call_function",
            "functionName": "reset_wheel"
          },
          {
            "action": "complete_quest"
          }
        ],
        "reset_wheel": [
          {
            "action": "set_state",
            "stateRef": "bet_color.[[interactor]]",
            "value": "none"
          },
          {
            "action": "set_state",
            "stateRef": "opposite_color.[[interactor]]",
            "value": "none"
          },
          {
            "action": "set_state",
            "stateRef": "round_in_progress.[[interactor]]",
            "value": "inactive"
          }
        ]
      }
    }
  },
  "functions": {
    "on_start": [
      {
        "action": "spawn",
        "targetName": "quest.roulette_wheel",
        "position": "quest.roulette_start"
      }
    ],
    "on_complete": [
      {
        "action": "call_function",
        "functionName": "noop"
      }
    ]
  }
}
