Commit 91b790cb authored by hewenhao's avatar hewenhao

feat: 发起pk

parent cc815f9a
......@@ -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) => {
return requestAfterSilentLogin<null, { stageId: number, userId?: number }>(userInfo => ({
method: 'GET',
......
......@@ -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'
import { useStoreState, useStoreActions } from '@/store/hooks'
import assetsPk from '@/assets/oss/assets-pk'
import assetsMainNext from '@/assets/oss/assets-main-next'
import { useRef } from 'react'
import { badgeImages } from '../battles/const'
import styles from './index.module.scss'
import TopHeader from './components/top-header'
......@@ -15,6 +16,8 @@ const AcceptPk = () => {
const details = useStoreState((state) => state.battlesModel.details)
const userInfo = useStoreState((state) => state.userModel.userInfo)
const getQuestionList = useStoreActions(actions => actions.gameModel.getQuestionList)
const getStageDetail = useStoreActions(actions => actions.gameModel.getStageDetail)
const fetchUserDetails = useStoreActions(
(actions) => actions.battlesModel.fetchUserDetails
......@@ -29,9 +32,21 @@ const AcceptPk = () => {
}
})
const isClickRef = useRef(false)
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({
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 = {
fetchUserDetails: thunk(async (actions, payload) => {
try {
const res = await fetchUserDetails(payload)
if (res.code === 200) {
console.log('res', res)
actions.setUserDetails(res.data)
}
} catch (err) {
}
}),
......@@ -43,10 +40,7 @@ const battlesModel: BattlesModel = {
fetchUserRecords: thunk(async (actions, payload) => {
try {
const res = await fetchUserRecords(payload)
if (res.code === 200) {
console.log('res', res)
actions.setUserRecords(res.data.records)
}
} catch (err) {
}
}),
......
......@@ -3,16 +3,17 @@ import assetsGame from '@/assets/oss/assets-game'
import assetsPrepare from '@/assets/oss/assets-prepare'
import assetsPk from '@/assets/oss/assets-pk'
import classNames from 'classnames'
import { useStoreState } from '@/store/hooks'
import styles from './battle-complete.module.scss'
type BattleCompleteProps = {
data?: {}
}
const BattleComplete = (props: BattleCompleteProps) => {
const { data } = props
if (!data) {
const pkResult = useStoreState(states => states.gameModel.pkResult)
if (!pkResult) {
return null
}
......
......@@ -2,7 +2,7 @@ import { action, computed, thunk } from 'easy-peasy'
import {
getQuestionList, StageLevel,
getStageDetail, completeStage, StageType,
resumeGame, fetchUserDetails
resumeGame, getPkQuestionList, submitPkResult
} from '@/api'
import {
showLoading, hideLoading, showModal, redirectTo,
......@@ -15,7 +15,7 @@ import {
isSelectionQuestion,
// QuestionOption,
} from '../types'
import { buildCompleteStageParam, convertQuestion, determinQuestion } from './utils'
import { buildCompleteStageParam, convertQuestion, determinQuestion, buildSubmitPkParams } from './utils'
import { GameModel } from './types'
......@@ -38,6 +38,7 @@ const gameModel: GameModel = {
counterId: COUNTER_ID,
questionList: [],
errList: [],
isBattle: false,
gameResult: {
/** 排名档位 */
gear: 10,
......@@ -66,6 +67,7 @@ const gameModel: GameModel = {
stageDetail: null,
stageDetailPre: null,
pkResult: null,
updateState: action((state, obj) => {
for (const key in obj) {
......@@ -229,19 +231,28 @@ const gameModel: GameModel = {
}
}),
getQuestionList: thunk(async (actions, round, helpers) => {
getQuestionList: thunk(async (actions, { isBattle = false, stageId }, helpers) => {
try {
// showLoading()
let questionList
if (!isBattle) {
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 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({
questionList: questionList.map(convertQuestion),
currentInputIndex: 0,
currentQuestionIndex: 0,
errList: [],
isBattle,
pkResult: null,
modalContinueVisible: false,
bornTimes: 3,
gameStartTime: Date.now(),
......@@ -273,8 +284,10 @@ const gameModel: GameModel = {
disableAlertBeforeUnload()
actions.updateState({ isSubmit: true })
stopCounter()
const { questionList, stageDetail } = helpers.getState()
const { errList, errNum, ...rest } = buildCompleteStageParam(questionList, stageDetail!, getCounter()?.getCount() ?? 0)
const { questionList, stageDetail, isBattle } = helpers.getState()
const out = buildCompleteStageParam(questionList, stageDetail!, getCounter()?.getCount() ?? 0)
const { errList, errNum, ...rest } = out
if (!isBattle) {
const { data: gameResult } = await completeStage(rest)
actions.updateState({
errList,
......@@ -289,6 +302,18 @@ const gameModel: GameModel = {
redirectTo({
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) {
hideLoading()
showModal({
......
......@@ -39,6 +39,8 @@ export interface GameModelState {
counterId: string;
rightIcon: string | null;
isBattle: boolean;
}
export interface GameModel extends GameModelState {
......@@ -61,7 +63,7 @@ export interface GameModel extends GameModelState {
canGoNext: Computed<GameModel, boolean>;
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>;
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 { getStorage, StorageKeys } from '@/utils/storage'
import { UserInfo } from '@/types'
import { type QuestionType, isBlankQuestion, isVerticalDivisionQuestion, isSelectionQuestion } from '../types'
const logger = getLogManager({ level: 1 })
......@@ -155,3 +156,15 @@ export function determinQuestion (question: QuestionType) {
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 = () => {
throttleDelay: 1000,
onSuccess: () => {
palyAudio(audio['蹦蹦top出现首页.wav'].u)
getQuestionList(+router.params.id || 1).then(() => {
getQuestionList({ stageId: +router.params.id || 1 }).then(() => {
setFalse()
navigateTo({
url: `/pages/game/play/index?id=${router.params.id}`
......@@ -128,7 +128,7 @@ const Prepare = () => {
isClickRef.current = true
palyAudio(audio['蹦蹦top出现首页.wav'].u)
getQuestionList(+router.params.id || 1).then(() => {
getQuestionList({ stageId: +router.params.id || 1 }).then(() => {
isClickRef.current = false
navigateTo({
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):
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 }
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