2024-12-11 00:30:21 +00:00
|
|
|
import { Constants } from '@/constants/Constants'
|
2024-12-06 20:49:28 +00:00
|
|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
|
|
|
import { LocationObject } from 'expo-location'
|
|
|
|
|
2024-12-13 00:18:55 +00:00
|
|
|
export type PlayerLocation = {
|
2024-12-17 00:06:23 +00:00
|
|
|
id?: number
|
2024-12-13 00:18:55 +00:00
|
|
|
playerId: number
|
|
|
|
longitude: number
|
|
|
|
latitude: number
|
|
|
|
speed: number
|
|
|
|
accuracy: number
|
|
|
|
altitude: number
|
|
|
|
altitudeAccuracy: number
|
|
|
|
timestamp: string
|
|
|
|
}
|
|
|
|
|
2024-12-06 20:49:28 +00:00
|
|
|
interface LocationState {
|
2024-12-11 00:30:21 +00:00
|
|
|
lastOwnLocation: LocationObject | null
|
|
|
|
lastSentLocation: LocationObject | null
|
|
|
|
queuedLocations: LocationObject[]
|
2024-12-13 00:18:55 +00:00
|
|
|
lastPlayerLocations: PlayerLocation[]
|
2024-12-06 20:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const initialState: LocationState = {
|
2024-12-11 00:30:21 +00:00
|
|
|
lastOwnLocation: null,
|
|
|
|
lastSentLocation: null,
|
2024-12-13 00:18:55 +00:00
|
|
|
queuedLocations: [],
|
|
|
|
lastPlayerLocations: []
|
2024-12-06 20:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const locationSlice = createSlice({
|
|
|
|
name: 'location',
|
|
|
|
initialState: initialState,
|
|
|
|
reducers: {
|
2024-12-11 00:30:21 +00:00
|
|
|
setLastLocation: (state, action: PayloadAction<LocationObject>) => {
|
|
|
|
const location: LocationObject = action.payload
|
|
|
|
state.lastOwnLocation = location
|
|
|
|
if (state.lastSentLocation === null || (location.timestamp - state.lastSentLocation.timestamp) >= Constants.MIN_DELAY_LOCATION_SENT * 1000) {
|
|
|
|
state.lastSentLocation = location
|
|
|
|
state.queuedLocations.push(location)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unqueueLocation: (state, action: PayloadAction<LocationObject>) => {
|
2024-12-12 23:32:52 +00:00
|
|
|
const sentLoc = action.payload
|
|
|
|
state.queuedLocations = state.queuedLocations
|
|
|
|
.filter(loc => new Date(loc.timestamp).getTime() !== sentLoc.timestamp
|
2024-12-15 19:42:21 +00:00
|
|
|
&& loc.coords.latitude !== sentLoc.coords.latitude
|
|
|
|
&& loc.coords.longitude !== sentLoc.coords.longitude)
|
2024-12-06 20:49:28 +00:00
|
|
|
},
|
2024-12-13 00:18:55 +00:00
|
|
|
setLastPlayerLocations: (state, action: PayloadAction<PlayerLocation[]>) => {
|
|
|
|
state.lastPlayerLocations = action.payload
|
2024-12-17 00:06:23 +00:00
|
|
|
},
|
|
|
|
setLastPlayerLocation: (state, action: PayloadAction<PlayerLocation>) => {
|
|
|
|
state.lastPlayerLocations = state.lastPlayerLocations.filter(playerLoc => playerLoc.playerId !== action.payload.playerId)
|
|
|
|
state.lastPlayerLocations.push(action.payload)
|
2024-12-13 00:18:55 +00:00
|
|
|
}
|
2024-12-06 20:49:28 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-12-17 00:06:23 +00:00
|
|
|
export const { setLastLocation, unqueueLocation, setLastPlayerLocation, setLastPlayerLocations } = locationSlice.actions
|
2024-12-06 20:49:28 +00:00
|
|
|
|
|
|
|
export default locationSlice.reducer
|