2024-12-11 18:35:36 +00:00
|
|
|
import { AuthState } from "@/utils/features/auth/authSlice"
|
2024-12-11 20:33:51 +00:00
|
|
|
import { GamePayload, GameState } from "@/utils/features/game/gameSlice"
|
2024-12-11 18:35:36 +00:00
|
|
|
import { useMutation } from "@tanstack/react-query"
|
|
|
|
|
|
|
|
type ErrorResponse = {
|
|
|
|
error: string
|
|
|
|
message: string
|
|
|
|
statusCode: number
|
|
|
|
}
|
|
|
|
|
|
|
|
type onPostSuccessFunc = () => void
|
|
|
|
type ErrorFuncProps = { response?: ErrorResponse, error?: Error }
|
|
|
|
type onErrorFunc = (props: ErrorFuncProps) => void
|
|
|
|
|
|
|
|
type GameProps = {
|
2024-12-11 20:33:51 +00:00
|
|
|
updateGameState: (payload: GamePayload) => { payload: GamePayload, type: "game/updateGameState" }
|
2024-12-11 18:35:36 +00:00
|
|
|
auth: AuthState
|
|
|
|
onPostSuccess?: onPostSuccessFunc
|
|
|
|
onError?: onErrorFunc
|
|
|
|
}
|
|
|
|
|
2024-12-11 20:33:51 +00:00
|
|
|
export const useGameStartMutation = ({ auth, updateGameState, onPostSuccess, onError }: GameProps) => {
|
2024-12-11 18:35:36 +00:00
|
|
|
return useMutation({
|
|
|
|
mutationFn: async () => {
|
|
|
|
return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/start/`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Authorization": `Bearer ${auth.token}`,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
}).then(resp => resp.json())
|
|
|
|
},
|
|
|
|
onSuccess: async (data) => {
|
2024-12-12 14:21:58 +00:00
|
|
|
if (data.statusCode) {
|
2024-12-11 18:35:36 +00:00
|
|
|
if (onError)
|
|
|
|
onError({ response: data })
|
|
|
|
return
|
|
|
|
}
|
2024-12-11 20:33:51 +00:00
|
|
|
updateGameState(data)
|
2024-12-11 18:35:36 +00:00
|
|
|
if (onPostSuccess)
|
|
|
|
onPostSuccess()
|
|
|
|
},
|
|
|
|
onError: async (error: Error) => {
|
|
|
|
if (onError)
|
|
|
|
onError({ error: error })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-12-11 20:33:51 +00:00
|
|
|
export const useGameStopMutation = ({ auth, updateGameState, onPostSuccess, onError }: GameProps) => {
|
2024-12-11 18:35:36 +00:00
|
|
|
return useMutation({
|
|
|
|
mutationFn: async () => {
|
|
|
|
return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/stop/`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Authorization": `Bearer ${auth.token}`,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
}).then(resp => resp.json())
|
|
|
|
},
|
|
|
|
onSuccess: async (data) => {
|
2024-12-12 14:21:58 +00:00
|
|
|
if (data.statusCode) {
|
2024-12-11 18:35:36 +00:00
|
|
|
if (onError)
|
|
|
|
onError({ response: data })
|
|
|
|
return
|
|
|
|
}
|
2024-12-11 20:33:51 +00:00
|
|
|
updateGameState(data)
|
2024-12-11 18:35:36 +00:00
|
|
|
if (onPostSuccess)
|
|
|
|
onPostSuccess()
|
|
|
|
},
|
|
|
|
onError: async (error: Error) => {
|
|
|
|
if (onError)
|
|
|
|
onError({ error: error })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-12-11 20:33:51 +00:00
|
|
|
export const useGameSwitchPlayerMutation = ({ auth, updateGameState, onPostSuccess, onError }: GameProps) => {
|
2024-12-11 18:35:36 +00:00
|
|
|
return useMutation({
|
|
|
|
mutationFn: async () => {
|
|
|
|
return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/switch-running-player/`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Authorization": `Bearer ${auth.token}`,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
}).then(resp => resp.json())
|
|
|
|
},
|
|
|
|
onSuccess: async (data) => {
|
2024-12-12 14:21:58 +00:00
|
|
|
if (data.statusCode) {
|
2024-12-11 18:35:36 +00:00
|
|
|
if (onError)
|
|
|
|
onError({ response: data })
|
|
|
|
return
|
|
|
|
}
|
2024-12-11 20:33:51 +00:00
|
|
|
updateGameState(data)
|
2024-12-11 18:35:36 +00:00
|
|
|
if (onPostSuccess)
|
|
|
|
onPostSuccess()
|
|
|
|
},
|
|
|
|
onError: async (error: Error) => {
|
|
|
|
if (onError)
|
|
|
|
onError({ error: error })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-12-14 14:57:14 +00:00
|
|
|
export const useGameRepairMutation = ({ auth, onPostSuccess, onError }: Omit<GameProps, 'updateGameState'>) => {
|
2024-12-11 18:35:36 +00:00
|
|
|
return useMutation({
|
|
|
|
mutationFn: async () => {
|
|
|
|
return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/repair/`, {
|
|
|
|
method: "PUT",
|
|
|
|
headers: {
|
|
|
|
"Authorization": `Bearer ${auth.token}`,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
}).then(resp => resp.json())
|
|
|
|
},
|
|
|
|
onSuccess: async (data) => {
|
2024-12-12 14:21:58 +00:00
|
|
|
if (data.statusCode) {
|
2024-12-11 18:35:36 +00:00
|
|
|
if (onError)
|
|
|
|
onError({ response: data })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (onPostSuccess)
|
|
|
|
onPostSuccess()
|
|
|
|
},
|
|
|
|
onError: async (error: Error) => {
|
|
|
|
if (onError)
|
|
|
|
onError({ error: error })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-12-11 20:33:51 +00:00
|
|
|
export const useGameResetMutation = ({ auth, updateGameState, onPostSuccess, onError }: GameProps) => {
|
2024-12-11 18:35:36 +00:00
|
|
|
return useMutation({
|
|
|
|
mutationFn: async () => {
|
|
|
|
return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/reset/`, {
|
|
|
|
method: "DELETE",
|
|
|
|
headers: {
|
|
|
|
"Authorization": `Bearer ${auth.token}`,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
}).then(resp => resp.json())
|
|
|
|
},
|
|
|
|
onSuccess: async (data) => {
|
2024-12-12 14:21:58 +00:00
|
|
|
if (data.statusCode) {
|
2024-12-11 18:35:36 +00:00
|
|
|
if (onError)
|
|
|
|
onError({ response: data })
|
|
|
|
return
|
|
|
|
}
|
2024-12-11 20:33:51 +00:00
|
|
|
updateGameState(data)
|
2024-12-11 18:35:36 +00:00
|
|
|
if (onPostSuccess)
|
|
|
|
onPostSuccess()
|
|
|
|
},
|
|
|
|
onError: async (error: Error) => {
|
|
|
|
if (onError)
|
|
|
|
onError({ error: error })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|