You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
2.0 KiB
51 lines
2.0 KiB
import {
|
|
CLIENT_ACT,
|
|
CLIENT_GOODBYE,
|
|
CLIENT_HELLO,
|
|
CLIENT_PENDING,
|
|
CLIENT_REFRESH,
|
|
ClientAction
|
|
} from "../../../common/src/actions/ClientAction";
|
|
import {NetworkState, ServerConnectionState} from "../state/NetworkState";
|
|
|
|
// TODO: Verify that only one special message exists at a time.
|
|
export function clientReducer(oldState: NetworkState, action: ClientAction): NetworkState {
|
|
switch (action.type) {
|
|
case CLIENT_HELLO:
|
|
return {
|
|
...oldState,
|
|
specialMessage: action,
|
|
connectionState: ServerConnectionState.AWAITING_HELLO,
|
|
}
|
|
case CLIENT_REFRESH:
|
|
return {
|
|
...oldState,
|
|
specialMessage: action,
|
|
connectionState: ServerConnectionState.AWAITING_REFRESH,
|
|
}
|
|
case CLIENT_PENDING:
|
|
// This happens when an action is successfully applied locally, so we prepare to send it to the server;
|
|
// we don't have to actually do anything because it was already done by the time we got here.
|
|
return {
|
|
...oldState,
|
|
pendingActions: [...oldState.pendingActions, action.pending],
|
|
}
|
|
case CLIENT_ACT:
|
|
if (!action.actions.every((innerAction, index) =>
|
|
innerAction.id === oldState.nextID + index && innerAction.action === oldState.pendingActions[index])) {
|
|
throw Error("Only the next actions can be sent, and only with the next IDs.")
|
|
}
|
|
return {
|
|
...oldState,
|
|
nextID: oldState.nextID + action.actions.length,
|
|
sentActions: [...oldState.sentActions, ...action.actions],
|
|
pendingActions: oldState.pendingActions.slice(action.actions.length),
|
|
}
|
|
case CLIENT_GOODBYE:
|
|
return {
|
|
...oldState,
|
|
specialMessage: action,
|
|
connectionState: ServerConnectionState.AWAITING_GOODBYE,
|
|
}
|
|
}
|
|
} |