2024-12-13 22:07:37 +00:00
|
|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
|
|
|
import { PaginationMeta } from '../common'
|
|
|
|
|
|
|
|
export interface MoneyUpdate {
|
|
|
|
id: number
|
|
|
|
playerId: number
|
2024-12-13 23:27:30 +00:00
|
|
|
amount: number
|
2024-12-14 12:30:26 +00:00
|
|
|
reason: 'START' | 'NEW_RUN' | 'BUY_TRAIN' | 'CHALLENGE'
|
2024-12-13 22:07:37 +00:00
|
|
|
timestamp: number
|
|
|
|
runId: number | null
|
|
|
|
actionId: number | null
|
2024-12-13 23:27:30 +00:00
|
|
|
tripId: string | null
|
2024-12-13 22:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface MoneyUpdatesState {
|
|
|
|
moneyUpdates: MoneyUpdate[]
|
|
|
|
}
|
|
|
|
|
|
|
|
const initialState: MoneyUpdatesState = {
|
|
|
|
moneyUpdates: []
|
|
|
|
}
|
|
|
|
|
2024-12-13 23:27:30 +00:00
|
|
|
export interface MoneyUpdatePayload {
|
|
|
|
id: number
|
|
|
|
playerId: number
|
|
|
|
amount: number
|
2024-12-14 12:30:26 +00:00
|
|
|
reason: 'START' | 'NEW_RUN' | 'BUY_TRAIN' | 'CHALLENGE'
|
2024-12-13 23:27:30 +00:00
|
|
|
timestamp: string
|
|
|
|
runId: number | null
|
|
|
|
actionId: number | null
|
|
|
|
tripId: string | null
|
|
|
|
}
|
|
|
|
|
2024-12-13 22:07:37 +00:00
|
|
|
export interface MoneyUpdatesPayload {
|
2024-12-13 23:27:30 +00:00
|
|
|
data: MoneyUpdatePayload[]
|
2024-12-13 22:07:37 +00:00
|
|
|
meta: PaginationMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
export const moneyUpdatesSlice = createSlice({
|
|
|
|
name: 'moneyUpdates',
|
|
|
|
initialState: initialState,
|
|
|
|
reducers: {
|
|
|
|
downloadMoneyUpdates(state, action: PayloadAction<MoneyUpdatesPayload>) {
|
|
|
|
state.moneyUpdates = state.moneyUpdates.filter(moneyUpdate => action.payload.data.filter(dlMU => dlMU.id === moneyUpdate.id) === null)
|
|
|
|
for (const dlMU of action.payload.data) {
|
2024-12-13 23:27:30 +00:00
|
|
|
state.moneyUpdates.push({
|
|
|
|
id: dlMU.id,
|
|
|
|
playerId: dlMU.playerId,
|
|
|
|
amount: dlMU.amount,
|
|
|
|
reason: dlMU.reason,
|
|
|
|
timestamp: new Date(dlMU.timestamp).getTime(),
|
|
|
|
runId: dlMU.runId,
|
|
|
|
actionId: dlMU.actionId,
|
|
|
|
tripId: dlMU.tripId,
|
|
|
|
})
|
2024-12-13 22:07:37 +00:00
|
|
|
}
|
|
|
|
state.moneyUpdates.sort((mu1, mu2) => mu2.id - mu1.id)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
export const { downloadMoneyUpdates } = moneyUpdatesSlice.actions
|
|
|
|
|
|
|
|
export default moneyUpdatesSlice.reducer
|