91 lines
2.1 KiB
Vue
91 lines
2.1 KiB
Vue
<script setup>
|
||
import { ref, onMounted, onBeforeUnmount } from "vue";
|
||
import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
|
||
import Container from "@/components/Container.vue";
|
||
import Matching from "@/components/Matching.vue";
|
||
import { matchGameAPI } from "@/apis";
|
||
import { MESSAGETYPESV2 } from "@/constants";
|
||
|
||
const gameType = ref(0);
|
||
const teamSize = ref(0);
|
||
const onComplete = ref(null);
|
||
|
||
async function stopMatch() {
|
||
uni.$showHint(3);
|
||
}
|
||
|
||
async function cancelMatch() {
|
||
if (gameType.value && teamSize.value) {
|
||
await matchGameAPI(false, gameType.value, teamSize.value);
|
||
}
|
||
uni.navigateBack()
|
||
}
|
||
|
||
async function onReceiveMessage(msg) {
|
||
if (msg.type === MESSAGETYPESV2.MatchSuccess) {
|
||
onComplete.value = () => {}
|
||
}
|
||
if (msg.type === MESSAGETYPESV2.AboutToStart) {
|
||
// 使用后端下发的 mode 字段判断跳转目标,与好友约战(battle-room.vue)保持一致
|
||
// mode <= 3 为团队对抗,mode > 3 为大乱斗,覆盖全部 gameType(1~5),不再遗漏
|
||
if (msg.mode <= 3) {
|
||
uni.redirectTo({
|
||
url: `/pages/team-battle?battleId=${msg.id}`,
|
||
});
|
||
} else {
|
||
uni.redirectTo({
|
||
url: `/pages/melee-battle?battleId=${msg.id}`,
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
onLoad(async (options) => {
|
||
if (options.gameType && options.teamSize) {
|
||
gameType.value = options.gameType;
|
||
teamSize.value = options.teamSize;
|
||
}
|
||
});
|
||
|
||
onMounted(() => {
|
||
uni.setKeepScreenOn({
|
||
keepScreenOn: true,
|
||
});
|
||
uni.$on("socket-inbox", onReceiveMessage);
|
||
uni.$on("cancelMatching", cancelMatch);
|
||
});
|
||
|
||
onBeforeUnmount(() => {
|
||
uni.setKeepScreenOn({
|
||
keepScreenOn: false,
|
||
});
|
||
uni.$off("socket-inbox", onReceiveMessage);
|
||
uni.$off("cancelMatching", cancelMatch);
|
||
});
|
||
|
||
onShow(async () => {
|
||
if (gameType.value && teamSize.value) {
|
||
matchGameAPI(true, gameType.value, teamSize.value);
|
||
}
|
||
});
|
||
|
||
onHide(() => {
|
||
|
||
});
|
||
</script>
|
||
|
||
<template>
|
||
<Container title="搜索对手..." :bgType="1" :onBack="stopMatch">
|
||
<view class="container">
|
||
<Matching :stopMatch="stopMatch" :onComplete="onComplete" />
|
||
</view>
|
||
</Container>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.container {
|
||
width: 100%;
|
||
height: 100%;
|
||
}
|
||
</style>
|