Files
shoot-miniprograms/src/pages/practise-one.vue

163 lines
4.5 KiB
Vue
Raw Normal View History

2025-05-08 22:05:53 +08:00
<script setup>
2025-08-25 13:47:32 +08:00
import { ref, onMounted, onBeforeUnmount } from "vue";
2025-11-13 21:23:45 +08:00
import { onShow } from "@dcloudio/uni-app";
2025-06-05 22:21:40 +08:00
import Container from "@/components/Container.vue";
2025-05-10 16:57:36 +08:00
import ShootProgress from "@/components/ShootProgress.vue";
import BowTarget from "@/components/BowTarget.vue";
import ScorePanel2 from "@/components/ScorePanel2.vue";
2025-05-16 15:56:54 +08:00
import ScoreResult from "@/components/ScoreResult.vue";
2025-05-29 23:45:44 +08:00
import SButton from "@/components/SButton.vue";
2025-06-17 12:50:59 +08:00
import Avatar from "@/components/Avatar.vue";
import BowPower from "@/components/BowPower.vue";
2025-07-05 14:52:41 +08:00
import TestDistance from "@/components/TestDistance.vue";
2025-07-12 16:01:49 +08:00
import BubbleTip from "@/components/BubbleTip.vue";
2025-11-13 11:58:16 +08:00
import audioManager from "@/audioManager";
2026-01-07 15:12:30 +08:00
2026-02-07 18:30:16 +08:00
import {
createPractiseAPI,
startPractiseAPI,
endPractiseAPI,
getPractiseAPI,
} from "@/apis";
2026-01-08 10:30:41 +08:00
import { sharePractiseData } from "@/canvas";
import { wxShare, debounce } from "@/util";
2026-02-07 18:30:16 +08:00
import { MESSAGETYPESV2, MESSAGETYPES, roundsName } from "@/constants";
2026-01-07 15:12:30 +08:00
2025-06-15 15:53:57 +08:00
import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user } = storeToRefs(store);
2026-01-07 15:12:30 +08:00
2025-07-13 11:38:54 +08:00
const start = ref(false);
2025-05-29 23:45:44 +08:00
const scores = ref([]);
2025-05-29 23:56:42 +08:00
const total = 12;
2025-06-17 12:50:59 +08:00
const currentRound = ref(0);
2025-05-31 14:57:25 +08:00
const practiseResult = ref({});
2025-06-25 18:41:30 +08:00
const practiseId = ref("");
2025-07-12 16:01:49 +08:00
const showGuide = ref(false);
2025-11-13 11:58:16 +08:00
const tips = ref("");
2025-05-10 16:57:36 +08:00
2025-05-29 23:45:44 +08:00
const onReady = async () => {
2026-02-07 18:30:16 +08:00
await startPractiseAPI();
2025-06-17 19:35:21 +08:00
currentRound.value = 0;
2025-06-15 15:53:57 +08:00
scores.value = [];
2025-06-19 01:55:40 +08:00
start.value = true;
2025-11-14 15:51:45 +08:00
audioManager.play("练习开始");
2025-06-05 21:32:15 +08:00
};
2025-05-29 23:45:44 +08:00
2026-01-07 15:12:30 +08:00
const onOver = async () => {
practiseResult.value = await getPractiseAPI(practiseId.value);
2026-02-07 18:30:16 +08:00
start.value = false;
2026-01-07 15:12:30 +08:00
};
2026-02-07 18:30:16 +08:00
async function onReceiveMessage(msg) {
if (msg.type === MESSAGETYPESV2.ShootResult) {
scores.value = msg.details;
} else if (msg.type === MESSAGETYPESV2.BattleEnd) {
setTimeout(onOver, 1500);
}
2025-06-05 21:32:15 +08:00
}
2025-06-17 21:34:41 +08:00
async function onComplete() {
2026-02-07 18:30:16 +08:00
const validArrows = (practiseResult.value.details || []).filter(
2025-11-17 17:14:51 +08:00
(a) => a.x !== -30 && a.y !== -30
);
if (validArrows.length === total) {
2025-07-05 13:12:58 +08:00
uni.navigateBack();
} else {
2025-07-16 17:04:57 +08:00
practiseId.value = "";
2025-07-05 13:12:58 +08:00
practiseResult.value = {};
start.value = false;
scores.value = [];
currentRound.value = 0;
2026-02-07 18:30:16 +08:00
const result = await createPractiseAPI(total, 360);
if (result) practiseId.value = result.id;
2025-07-05 13:12:58 +08:00
}
2025-06-05 22:21:40 +08:00
}
2025-11-11 10:13:14 +08:00
const onClickShare = debounce(async () => {
2026-01-08 10:30:41 +08:00
await sharePractiseData("shareCanvas", 2, user.value, practiseResult.value);
2025-11-11 10:13:14 +08:00
await wxShare("shareCanvas");
});
2026-02-07 18:30:16 +08:00
onMounted(async () => {
// audioManager.play("第一轮");
2025-07-13 11:21:19 +08:00
uni.setKeepScreenOn({
keepScreenOn: true,
});
2025-06-05 21:32:15 +08:00
uni.$on("socket-inbox", onReceiveMessage);
2025-11-11 10:13:14 +08:00
uni.$on("share-image", onClickShare);
2026-02-07 18:30:16 +08:00
const result = await createPractiseAPI(total, 120);
if (result) practiseId.value = result.id;
2025-06-05 21:32:15 +08:00
});
2025-05-29 23:45:44 +08:00
2025-08-25 13:47:32 +08:00
onBeforeUnmount(() => {
2025-07-13 11:21:19 +08:00
uni.setKeepScreenOn({
keepScreenOn: false,
});
2025-06-05 21:32:15 +08:00
uni.$off("socket-inbox", onReceiveMessage);
2025-11-11 10:13:14 +08:00
uni.$off("share-image", onClickShare);
2025-11-13 11:58:16 +08:00
audioManager.stopAll();
2026-02-07 18:30:16 +08:00
endPractiseAPI();
2025-05-29 23:45:44 +08:00
});
2025-05-08 22:05:53 +08:00
</script>
<template>
2026-02-05 18:06:55 +08:00
<Container
:bgType="1"
title="个人单组练习"
:showBottom="!start && !scores.length"
>
2025-06-05 22:21:40 +08:00
<view>
2026-02-07 18:30:16 +08:00
<TestDistance v-if="!start && !practiseResult.id" />
<block v-else>
2025-07-05 14:52:41 +08:00
<ShootProgress
:tips="`${
!start || scores.length === 12
? ''
: `请开始射箭第${
roundsName[Math.ceil((scores.length + 1) / 3)]
}`
}`"
:start="start"
2026-01-07 15:12:30 +08:00
:onStop="onOver"
2025-07-05 14:52:41 +08:00
/>
2025-07-11 00:47:34 +08:00
<view class="user-row">
2025-07-05 14:52:41 +08:00
<Avatar :src="user.avatar" :size="35" />
2025-07-13 11:38:54 +08:00
<BubbleTip v-if="showGuide" type="normal2">
2025-07-12 16:01:49 +08:00
<text>还有两场坚持</text>
<text>就是胜利💪</text>
</BubbleTip>
2025-10-29 17:26:27 +08:00
<BowPower />
2025-07-05 14:52:41 +08:00
</view>
<BowTarget
:totalRound="start ? total / 4 : 0"
:currentRound="currentRound"
:scores="scores"
/>
2026-02-09 17:27:44 +08:00
<ScorePanel2 :arrows="scores" />
2025-07-05 14:52:41 +08:00
<ScoreResult
2026-02-07 18:30:16 +08:00
v-if="practiseResult.details"
2025-07-05 14:52:41 +08:00
:rowCount="6"
:total="total"
:onClose="onComplete"
:result="practiseResult"
:tipSrc="`../static/${
2026-02-07 18:30:16 +08:00
practiseResult.details.filter(
2025-11-17 17:14:51 +08:00
(arrow) => arrow.x !== -30 && arrow.y !== -30
).length < total
? 'un'
: ''
2025-07-05 14:52:41 +08:00
}finish-tip.png`"
/>
2026-01-08 10:30:41 +08:00
<canvas class="share-canvas" id="shareCanvas" type="2d"></canvas>
2025-07-05 14:52:41 +08:00
</block>
2025-06-05 22:21:40 +08:00
</view>
2026-01-09 18:12:27 +08:00
<template #bottom>
2026-01-12 10:09:36 +08:00
<SButton :onClick="onReady">准备好了直接开始</SButton>
2026-01-09 18:12:27 +08:00
</template>
2025-06-05 22:21:40 +08:00
</Container>
2025-05-08 22:05:53 +08:00
</template>
2025-07-11 22:21:34 +08:00
<style scoped></style>