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.
 
 
 
hexmap/client/src/reducers/ClientReducer.ts

38 lines
1.7 KiB

import {CLIENT_HELLO, CLIENT_PENDING, CLIENT_REFRESH, CLIENT_SENT, ClientAction} from "../actions/NetworkAction";
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_SENT:
if (!action.nested.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.nested.length,
sentActions: [...oldState.sentActions, ...action.nested],
pendingActions: oldState.pendingActions.slice(action.nested.length),
}
}
}