KoogleApp/KoogleApp/Store/Game/ThrowPanel/Reducers.cs

127 lines
4.4 KiB
C#

using Fluxor;
using KoogleApp.Model;
namespace KoogleApp.Store.Game.ThrowPanel
{
// Reducer
public static class ThrowPanelStateReducer
{
[ReducerMethod]
public static ThrowPanelState ReceiveStateFromServer(ThrowPanelState state, ReceiveStateFromServer action)
{
return action.State;
}
[ReducerMethod]
public static ThrowPanelState OnToggleDevice(ThrowPanelState state, TogglePinValueAction action)
{
switch (action.PinNumber)
{
case 1:
return state with { Pin1Value = action.IsOn };
case 2:
return state with { Pin2Value = action.IsOn };
case 3:
return state with { Pin3Value = action.IsOn };
case 4:
return state with { Pin4Value = action.IsOn };
case 5:
return state with { Pin5Value = action.IsOn };
case 6:
return state with { Pin6Value = action.IsOn };
case 7:
return state with { Pin7Value = action.IsOn };
case 8:
return state with { Pin8Value = action.IsOn };
case 9:
return state with { Pin9Value = action.IsOn };
}
return state;
}
[ReducerMethod]
public static ThrowPanelState OnStart(ThrowPanelState state, StartStopAction stopAction)
{
return state with { IsStated = !state.IsStated };
}
[ReducerMethod]
public static ThrowPanelState OnToggleAllPins(ThrowPanelState state, ToggleAllPinsAction action)
{
return state with
{
Pin1Value = !state.Pin1Disabled && !state.Pin1Value,
Pin2Value = !state.Pin1Disabled && !state.Pin2Value,
Pin3Value = !state.Pin1Disabled && !state.Pin3Value,
Pin4Value = !state.Pin1Disabled && !state.Pin4Value,
Pin5Value = !state.Pin1Disabled && !state.Pin5Value,
Pin6Value = !state.Pin1Disabled && !state.Pin6Value,
Pin7Value = !state.Pin1Disabled && !state.Pin7Value,
Pin8Value = !state.Pin1Disabled && !state.Pin8Value,
Pin9Value = !state.Pin1Disabled && !state.Pin9Value,
};
}
class PinState(bool value, bool disabled)
{
public bool Disabled { get; set; } = disabled;
public bool Value { get; set; } = value;
}
[ReducerMethod]
public static ThrowPanelState OnUpdatePinStateByNumber(ThrowPanelState state, UpdatePinStateByNumber action)
{
var chgCnt = 0;
var _pins = new List<PinState>
{
new(state.Pin1Value, state.Pin1Disabled),
new(state.Pin2Value, state.Pin2Disabled),
new(state.Pin3Value, state.Pin3Disabled),
new(state.Pin4Value, state.Pin4Disabled),
new(state.Pin5Value, state.Pin5Disabled),
new(state.Pin6Value, state.Pin6Disabled),
new(state.Pin7Value, state.Pin7Disabled),
new(state.Pin8Value, state.Pin8Disabled),
new(state.Pin9Value, state.Pin9Disabled),
};
foreach (var pin in _pins)
{
var nr = _pins.IndexOf(pin) + 1;
if (!pin.Disabled)
{
pin.Value = chgCnt < action.Number;
chgCnt++;
}
}
return state with
{
Pin1Value = _pins[8].Value,
Pin2Value = _pins[7].Value,
Pin3Value = _pins[6].Value,
Pin4Value = _pins[5].Value,
Pin5Value = _pins[4].Value,
Pin6Value = _pins[3].Value,
Pin7Value = _pins[2].Value,
Pin8Value = _pins[1].Value,
Pin9Value = _pins[0].Value,
};
}
[ReducerMethod]
public static ThrowPanelState OnUpdatePinStateByNumber(ThrowPanelState state, ToggleBellAction action)
{
return state with { BellValue = !state.BellValue };
}
[ReducerMethod]
public static ThrowPanelState OnLoadState(ThrowPanelState state, StateLoadedAction action)
{
return action.State;
}
}
}