Commit 91b790cb authored by hewenhao's avatar hewenhao

feat: 发起pk

parent cc815f9a
...@@ -10,6 +10,14 @@ export const getQuestionList = (stageId: number, normal: boolean = true) => { ...@@ -10,6 +10,14 @@ export const getQuestionList = (stageId: number, normal: boolean = true) => {
})) }))
} }
export const getPkQuestionList = (stageId: number) => {
return requestAfterSilentLogin<QuestionType[], { stageId: number }>(() => ({
url: '/service-idea//pk/personal/stage/generateQuestion',
method: 'GET',
data: { stageId }
}))
}
export const resumeGame = (stageId: number) => { export const resumeGame = (stageId: number) => {
return requestAfterSilentLogin<null, { stageId: number, userId?: number }>(userInfo => ({ return requestAfterSilentLogin<null, { stageId: number, userId?: number }>(userInfo => ({
method: 'GET', method: 'GET',
......
...@@ -63,3 +63,42 @@ export function fetchUserRecords (data: PkParams) { ...@@ -63,3 +63,42 @@ export function fetchUserRecords (data: PkParams) {
} }
})) }))
} }
export type SubmitPkResultParams = {
initiator: number;
initiatorErrNum: number;
initiatorResultTime: number;
recipient: number;
recipientErrNum: number;
recipientResultTime: number;
}
export async function submitPkResult (params: SubmitPkResultParams) {
const result1 = await requestAfterSilentLogin<number, SubmitPkResultParams>(() => ({
url: '/service-idea/pk/personal/stage/complete',
method: 'POST',
data: params
}))
return await getPkResult(result1.data)
}
export type PkResult = SubmitPkResultParams & {
initiatorLevel: string;
initiatorName: string;
initiatorStar: number;
recipientLevel: string;
recipientName: string;
recipientStar: number;
result: string;
stageName: string;
}
export function getPkResult (params: number) {
return requestAfterSilentLogin<PkResult, { id: number }>(() => ({
url: '/service-idea/pk/personal/pkDetail',
method: 'GET',
data: { id: params }
}))
}
...@@ -4,6 +4,7 @@ import { useBoolean } from '@/hooks' ...@@ -4,6 +4,7 @@ import { useBoolean } from '@/hooks'
import { useStoreState, useStoreActions } from '@/store/hooks' import { useStoreState, useStoreActions } from '@/store/hooks'
import assetsPk from '@/assets/oss/assets-pk' import assetsPk from '@/assets/oss/assets-pk'
import assetsMainNext from '@/assets/oss/assets-main-next' import assetsMainNext from '@/assets/oss/assets-main-next'
import { useRef } from 'react'
import { badgeImages } from '../battles/const' import { badgeImages } from '../battles/const'
import styles from './index.module.scss' import styles from './index.module.scss'
import TopHeader from './components/top-header' import TopHeader from './components/top-header'
...@@ -15,6 +16,8 @@ const AcceptPk = () => { ...@@ -15,6 +16,8 @@ const AcceptPk = () => {
const details = useStoreState((state) => state.battlesModel.details) const details = useStoreState((state) => state.battlesModel.details)
const userInfo = useStoreState((state) => state.userModel.userInfo) const userInfo = useStoreState((state) => state.userModel.userInfo)
const getQuestionList = useStoreActions(actions => actions.gameModel.getQuestionList)
const getStageDetail = useStoreActions(actions => actions.gameModel.getStageDetail)
const fetchUserDetails = useStoreActions( const fetchUserDetails = useStoreActions(
(actions) => actions.battlesModel.fetchUserDetails (actions) => actions.battlesModel.fetchUserDetails
...@@ -29,9 +32,21 @@ const AcceptPk = () => { ...@@ -29,9 +32,21 @@ const AcceptPk = () => {
} }
}) })
const isClickRef = useRef(false)
const toGame = () => { const toGame = () => {
if (isClickRef.current) return
isClickRef.current = true
const stageId = +(router.params.stageId ?? -1)
Promise.all([
getQuestionList({ stageId, isBattle: true }),
getStageDetail(stageId)
])
.then(() => {
isClickRef.current = false
navigateTo({ navigateTo({
url: `/pages/game/play/index?id=${router.params.stageId}` url: `/pages/game/play/index?id=${stageId}&isBattle=true`
})
}) })
} }
......
...@@ -32,10 +32,7 @@ const battlesModel: BattlesModel = { ...@@ -32,10 +32,7 @@ const battlesModel: BattlesModel = {
fetchUserDetails: thunk(async (actions, payload) => { fetchUserDetails: thunk(async (actions, payload) => {
try { try {
const res = await fetchUserDetails(payload) const res = await fetchUserDetails(payload)
if (res.code === 200) {
console.log('res', res)
actions.setUserDetails(res.data) actions.setUserDetails(res.data)
}
} catch (err) { } catch (err) {
} }
}), }),
...@@ -43,10 +40,7 @@ const battlesModel: BattlesModel = { ...@@ -43,10 +40,7 @@ const battlesModel: BattlesModel = {
fetchUserRecords: thunk(async (actions, payload) => { fetchUserRecords: thunk(async (actions, payload) => {
try { try {
const res = await fetchUserRecords(payload) const res = await fetchUserRecords(payload)
if (res.code === 200) {
console.log('res', res)
actions.setUserRecords(res.data.records) actions.setUserRecords(res.data.records)
}
} catch (err) { } catch (err) {
} }
}), }),
......
...@@ -3,16 +3,17 @@ import assetsGame from '@/assets/oss/assets-game' ...@@ -3,16 +3,17 @@ import assetsGame from '@/assets/oss/assets-game'
import assetsPrepare from '@/assets/oss/assets-prepare' import assetsPrepare from '@/assets/oss/assets-prepare'
import assetsPk from '@/assets/oss/assets-pk' import assetsPk from '@/assets/oss/assets-pk'
import classNames from 'classnames' import classNames from 'classnames'
import { useStoreState } from '@/store/hooks'
import styles from './battle-complete.module.scss' import styles from './battle-complete.module.scss'
type BattleCompleteProps = { type BattleCompleteProps = {
data?: {}
} }
const BattleComplete = (props: BattleCompleteProps) => { const BattleComplete = (props: BattleCompleteProps) => {
const { data } = props const pkResult = useStoreState(states => states.gameModel.pkResult)
if (!data) {
if (!pkResult) {
return null return null
} }
......
...@@ -2,7 +2,7 @@ import { action, computed, thunk } from 'easy-peasy' ...@@ -2,7 +2,7 @@ import { action, computed, thunk } from 'easy-peasy'
import { import {
getQuestionList, StageLevel, getQuestionList, StageLevel,
getStageDetail, completeStage, StageType, getStageDetail, completeStage, StageType,
resumeGame, fetchUserDetails resumeGame, getPkQuestionList, submitPkResult
} from '@/api' } from '@/api'
import { import {
showLoading, hideLoading, showModal, redirectTo, showLoading, hideLoading, showModal, redirectTo,
...@@ -15,7 +15,7 @@ import { ...@@ -15,7 +15,7 @@ import {
isSelectionQuestion, isSelectionQuestion,
// QuestionOption, // QuestionOption,
} from '../types' } from '../types'
import { buildCompleteStageParam, convertQuestion, determinQuestion } from './utils' import { buildCompleteStageParam, convertQuestion, determinQuestion, buildSubmitPkParams } from './utils'
import { GameModel } from './types' import { GameModel } from './types'
...@@ -38,6 +38,7 @@ const gameModel: GameModel = { ...@@ -38,6 +38,7 @@ const gameModel: GameModel = {
counterId: COUNTER_ID, counterId: COUNTER_ID,
questionList: [], questionList: [],
errList: [], errList: [],
isBattle: false,
gameResult: { gameResult: {
/** 排名档位 */ /** 排名档位 */
gear: 10, gear: 10,
...@@ -66,6 +67,7 @@ const gameModel: GameModel = { ...@@ -66,6 +67,7 @@ const gameModel: GameModel = {
stageDetail: null, stageDetail: null,
stageDetailPre: null, stageDetailPre: null,
pkResult: null,
updateState: action((state, obj) => { updateState: action((state, obj) => {
for (const key in obj) { for (const key in obj) {
...@@ -229,19 +231,28 @@ const gameModel: GameModel = { ...@@ -229,19 +231,28 @@ const gameModel: GameModel = {
} }
}), }),
getQuestionList: thunk(async (actions, round, helpers) => { getQuestionList: thunk(async (actions, { isBattle = false, stageId }, helpers) => {
try { try {
// showLoading() // showLoading()
let questionList
if (!isBattle) {
const stageList = helpers.getStoreState().mainModel.stageList const stageList = helpers.getStoreState().mainModel.stageList
const isFirstStage = stageList.findIndex(c => String(c.id) === String(round)) === 0 const isFirstStage = stageList.findIndex(c => String(c.id) === String(stageId)) === 0
const stageDetail = helpers.getState().stageDetail const stageDetail = helpers.getState().stageDetail
const isNormal = !isFirstStage || !!stageDetail?.level const isNormal = !isFirstStage || !!stageDetail?.level
const { data: questionList } = await getQuestionList(round, isNormal) const { data: list } = await getQuestionList(stageId, isNormal)
questionList = list
} else {
const { data: list } = await getPkQuestionList(stageId)
questionList = list
}
actions.updateState({ actions.updateState({
questionList: questionList.map(convertQuestion), questionList: questionList.map(convertQuestion),
currentInputIndex: 0, currentInputIndex: 0,
currentQuestionIndex: 0, currentQuestionIndex: 0,
errList: [], errList: [],
isBattle,
pkResult: null,
modalContinueVisible: false, modalContinueVisible: false,
bornTimes: 3, bornTimes: 3,
gameStartTime: Date.now(), gameStartTime: Date.now(),
...@@ -273,8 +284,10 @@ const gameModel: GameModel = { ...@@ -273,8 +284,10 @@ const gameModel: GameModel = {
disableAlertBeforeUnload() disableAlertBeforeUnload()
actions.updateState({ isSubmit: true }) actions.updateState({ isSubmit: true })
stopCounter() stopCounter()
const { questionList, stageDetail } = helpers.getState() const { questionList, stageDetail, isBattle } = helpers.getState()
const { errList, errNum, ...rest } = buildCompleteStageParam(questionList, stageDetail!, getCounter()?.getCount() ?? 0) const out = buildCompleteStageParam(questionList, stageDetail!, getCounter()?.getCount() ?? 0)
const { errList, errNum, ...rest } = out
if (!isBattle) {
const { data: gameResult } = await completeStage(rest) const { data: gameResult } = await completeStage(rest)
actions.updateState({ actions.updateState({
errList, errList,
...@@ -289,6 +302,18 @@ const gameModel: GameModel = { ...@@ -289,6 +302,18 @@ const gameModel: GameModel = {
redirectTo({ redirectTo({
url: '/pages/game/complete/index', url: '/pages/game/complete/index',
}) })
} else {
const pkDetails = helpers.getStoreState().battlesModel.details
const userInfo = helpers.getStoreState().userModel.userInfo
if (!pkDetails) {
throw new Error('提交出错')
}
const result = await submitPkResult(buildSubmitPkParams(pkDetails, out, userInfo))
actions.updateState({
pkResult: result.data,
})
}
} catch (err) { } catch (err) {
hideLoading() hideLoading()
showModal({ showModal({
......
...@@ -39,6 +39,8 @@ export interface GameModelState { ...@@ -39,6 +39,8 @@ export interface GameModelState {
counterId: string; counterId: string;
rightIcon: string | null; rightIcon: string | null;
isBattle: boolean;
} }
export interface GameModel extends GameModelState { export interface GameModel extends GameModelState {
...@@ -61,7 +63,7 @@ export interface GameModel extends GameModelState { ...@@ -61,7 +63,7 @@ export interface GameModel extends GameModelState {
canGoNext: Computed<GameModel, boolean>; canGoNext: Computed<GameModel, boolean>;
updateList: Action<GameModel, QuestionType[]>; updateList: Action<GameModel, QuestionType[]>;
getQuestionList: Thunk<GameModel, number, void, AppStoreModel>; getQuestionList: Thunk<GameModel, { stageId: number; isBattle?: boolean }, void, AppStoreModel>;
getStageDetail: Thunk<GameModel, number, void, AppStoreModel>; getStageDetail: Thunk<GameModel, number, void, AppStoreModel>;
handleGoNext: Thunk<GameModel, void, void, AppStoreModel>; handleGoNext: Thunk<GameModel, void, void, AppStoreModel>;
......
import { type CompleteStageParam, type StageDetail, StageLevel, type CompleteQuestion } from '@/api' import { type CompleteStageParam, type StageDetail, StageLevel, type CompleteQuestion, SubmitPkResultParams, PkDetails } from '@/api'
import { getLogManager } from '@tarojs/taro' import { getLogManager } from '@tarojs/taro'
import { getStorage, StorageKeys } from '@/utils/storage' import { getStorage, StorageKeys } from '@/utils/storage'
import { UserInfo } from '@/types'
import { type QuestionType, isBlankQuestion, isVerticalDivisionQuestion, isSelectionQuestion } from '../types' import { type QuestionType, isBlankQuestion, isVerticalDivisionQuestion, isSelectionQuestion } from '../types'
const logger = getLogManager({ level: 1 }) const logger = getLogManager({ level: 1 })
...@@ -155,3 +156,15 @@ export function determinQuestion (question: QuestionType) { ...@@ -155,3 +156,15 @@ export function determinQuestion (question: QuestionType) {
throw new Error('question type not supported') throw new Error('question type not supported')
} }
export function buildSubmitPkParams (pkDetail: PkDetails, out: BuildParamsOut, userInfo: UserInfo): SubmitPkResultParams {
return {
initiator: pkDetail.userId,
initiatorErrNum: 0,
initiatorResultTime: pkDetail.resultTime,
recipient: userInfo.userId!,
recipientErrNum: out.errNum,
recipientResultTime: out.resultTime,
}
}
...@@ -96,7 +96,7 @@ const Prepare = () => { ...@@ -96,7 +96,7 @@ const Prepare = () => {
throttleDelay: 1000, throttleDelay: 1000,
onSuccess: () => { onSuccess: () => {
palyAudio(audio['蹦蹦top出现首页.wav'].u) palyAudio(audio['蹦蹦top出现首页.wav'].u)
getQuestionList(+router.params.id || 1).then(() => { getQuestionList({ stageId: +router.params.id || 1 }).then(() => {
setFalse() setFalse()
navigateTo({ navigateTo({
url: `/pages/game/play/index?id=${router.params.id}` url: `/pages/game/play/index?id=${router.params.id}`
...@@ -128,7 +128,7 @@ const Prepare = () => { ...@@ -128,7 +128,7 @@ const Prepare = () => {
isClickRef.current = true isClickRef.current = true
palyAudio(audio['蹦蹦top出现首页.wav'].u) palyAudio(audio['蹦蹦top出现首页.wav'].u)
getQuestionList(+router.params.id || 1).then(() => { getQuestionList({ stageId: +router.params.id || 1 }).then(() => {
isClickRef.current = false isClickRef.current = false
navigateTo({ navigateTo({
url: `/pages/game/play/index?id=${router.params.id}` url: `/pages/game/play/index?id=${router.params.id}`
......
...@@ -16,4 +16,16 @@ function omit<T extends object, K extends (keyof T)[] = []>(value: T, keys: K): ...@@ -16,4 +16,16 @@ function omit<T extends object, K extends (keyof T)[] = []>(value: T, keys: K):
return ret return ret
} }
export function singular<T extends (...arg: any[]) => any>(fn: T) {
let isRunning = false
return async function (...arg: Parameters<T>) {
if (isRunning) return
try {
return await fn(...arg)
} finally {
isRunning = false
}
}
}
export { omit } export { omit }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment