Files
shoot-miniprograms/src/pages/battle-room.vue

418 lines
12 KiB
Vue
Raw Normal View History

2025-05-16 15:56:54 +08:00
<script setup>
2025-06-05 21:32:51 +08:00
import { ref, onMounted, onUnmounted } from "vue";
2025-05-30 16:14:17 +08:00
import { onLoad } from "@dcloudio/uni-app";
import Container from "@/components/Container.vue";
2025-06-04 16:26:07 +08:00
import PlayerSeats from "@/components/PlayerSeats.vue";
2025-05-16 15:56:54 +08:00
import Guide from "@/components/Guide.vue";
2025-06-13 14:05:30 +08:00
import Timer from "@/components/Timer.vue";
2025-05-16 15:56:54 +08:00
import SButton from "@/components/SButton.vue";
import BowTarget from "@/components/BowTarget.vue";
import BattleHeader from "@/components/BattleHeader.vue";
import BattleFooter from "@/components/BattleFooter.vue";
import BowPower from "@/components/BowPower.vue";
import ShootProgress from "@/components/ShootProgress.vue";
import PlayersRow from "@/components/PlayersRow.vue";
2025-05-30 16:14:17 +08:00
import { getRoomAPI, destroyRoomAPI, exitRoomAPI, startRoomAPI } from "@/apis";
2025-06-13 14:05:30 +08:00
import { MESSAGETYPES, roundsName } from "@/constants";
2025-05-30 16:14:17 +08:00
import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user } = storeToRefs(store);
2025-06-11 23:57:17 +08:00
const step = ref(1);
2025-06-13 14:05:30 +08:00
const seq = ref(0);
const timerSeq = ref(0);
const battleId = ref("");
2025-05-30 16:14:17 +08:00
const room = ref({});
const roomNumber = ref("");
2025-06-13 14:05:30 +08:00
const owner = ref({});
2025-06-11 23:57:17 +08:00
const opponent = ref({});
2025-06-13 14:05:30 +08:00
const redTeam = ref([]);
const blueTeam = ref([]);
const currentShooterId = ref(0);
const players = ref([]);
const currentRound = ref(1);
const totalRounds = ref(0);
2025-06-11 23:57:17 +08:00
const start = ref(false);
const power = ref(0);
const scores = ref([]);
2025-06-13 14:05:30 +08:00
const tips = ref("即将开始...");
const roundResults = ref([]);
const redPoints = ref(0);
const bluePoints = ref(0);
const playersScores = ref({});
2025-05-30 16:14:17 +08:00
onLoad(async (options) => {
2025-06-13 14:05:30 +08:00
if (options.roomNumber) {
2025-05-30 16:14:17 +08:00
roomNumber.value = options.roomNumber;
2025-06-13 14:05:30 +08:00
const result = await getRoomAPI(options.roomNumber);
2025-05-30 16:14:17 +08:00
room.value = result;
2025-06-13 14:05:30 +08:00
result.members.some((m) => {
if (m.userInfo.id === result.creator) {
owner.value = {
id: m.userInfo.id,
name: m.userInfo.name,
avatar: m.userInfo.avatar,
};
return true;
}
return false;
});
2025-06-16 22:43:39 +08:00
if (result.battleType === 1) {
2025-06-13 14:05:30 +08:00
if (user.value.id !== owner.value.id) {
opponent.value = {
id: user.value.id,
name: user.value.nickName,
avatar: user.value.avatarUrl,
};
} else if (result.members.length > 1) {
result.members.some((m) => {
if (m.userInfo.id !== owner.value.id) {
opponent.value = {
id: m.userInfo.id,
name: m.userInfo.name,
avatar: m.userInfo.avatar,
};
return true;
}
return false;
});
}
2025-06-16 22:43:39 +08:00
} else if (result.battleType === 2) {
result.members.forEach((m) => {
players.value.push(m.userInfo);
});
2025-06-13 14:05:30 +08:00
}
2025-05-30 16:14:17 +08:00
}
});
const startGame = async () => {
const result = await startRoomAPI(room.value.number);
2025-06-13 14:05:30 +08:00
timerSeq.value += 1;
2025-05-30 16:14:17 +08:00
step.value = 2;
2025-06-05 21:32:51 +08:00
};
2025-05-30 16:14:17 +08:00
2025-06-05 21:32:51 +08:00
async function onReceiveMessage(content) {
const messages = JSON.parse(content).data.updates || [];
messages.forEach((msg) => {
2025-06-13 14:05:30 +08:00
if (
msg.roomNumber === roomNumber.value ||
2025-06-16 12:01:11 +08:00
(battleId.value && msg.id === battleId.value) ||
2025-06-13 14:05:30 +08:00
msg.constructor === MESSAGETYPES.WaitForAllReady
) {
console.log("收到消息:", msg);
}
if (!start.value && msg.constructor === MESSAGETYPES.ShootSyncMeArrowID) {
2025-06-05 21:32:51 +08:00
scores.value.push(msg.target);
2025-06-13 14:05:30 +08:00
power.value = msg.target.battery;
}
if (msg.constructor === MESSAGETYPES.WaitForAllReady) {
// 这里会掉多次;
timerSeq.value += 1;
battleId.value = msg.id;
2025-06-16 22:43:39 +08:00
if (room.value.battleType === 1) {
redTeam.value = msg.groupUserStatus.redTeam;
blueTeam.value = msg.groupUserStatus.blueTeam;
}
2025-06-16 12:01:11 +08:00
step.value = 2;
2025-06-13 14:05:30 +08:00
}
if (msg.roomNumber === roomNumber.value) {
if (msg.constructor === MESSAGETYPES.UserEnterRoom) {
2025-06-16 22:43:39 +08:00
if (room.value.battleType === 1) {
2025-06-13 14:05:30 +08:00
opponent.value = {
id: msg.userId,
name: msg.name,
avatar: msg.avatar,
};
}
2025-06-16 22:43:39 +08:00
if (room.value.battleType === 2) {
players.value.push({
id: msg.userId,
name: msg.name,
avatar: msg.avatar,
});
}
2025-06-13 14:05:30 +08:00
}
2025-06-16 12:01:11 +08:00
if (!start.value && msg.constructor === MESSAGETYPES.UserExitRoom) {
2025-06-16 22:43:39 +08:00
if (room.value.battleType === 1) {
2025-06-13 14:05:30 +08:00
opponent.value = {
id: "",
};
}
2025-06-16 22:43:39 +08:00
if (room.value.battleType === 2) {
players.value = players.value.filter((p) => p.id !== msg.userId);
}
2025-06-13 14:05:30 +08:00
}
if (msg.constructor === MESSAGETYPES.RoomDestroy) {
uni.showToast({
title: "房间已解散",
icon: "none",
});
2025-06-16 12:01:11 +08:00
roomNumber.value = "";
2025-06-13 14:05:30 +08:00
setTimeout(() => {
uni.navigateBack();
}, 1000);
}
}
if (msg.id === battleId.value) {
if (msg.constructor === MESSAGETYPES.AllReady) {
start.value = true;
timerSeq.value = 0;
scores.value = [];
totalRounds.value = msg.groupUserStatus.config.maxRounds;
step.value = 3;
2025-06-16 22:43:39 +08:00
if (room.value.battleType === 2) {
tips.value = "请在90秒内射完12支箭";
seq.value += 1;
}
2025-06-13 14:05:30 +08:00
}
if (msg.constructor === MESSAGETYPES.ToSomeoneShoot) {
2025-06-16 22:43:39 +08:00
if (room.value.battleType === 1) {
scores.value = [];
seq.value += 1;
currentShooterId.value = msg.userId;
if (owner.id !== currentShooterId.value) {
tips.value = `请红队射箭-第${roundsName[currentRound.value]}`;
} else {
tips.value = `请蓝队射箭-第${roundsName[currentRound.value]}`;
}
2025-06-13 14:05:30 +08:00
}
}
if (msg.constructor === MESSAGETYPES.ShootResult) {
2025-06-16 22:43:39 +08:00
if (room.value.battleType === 1) {
scores.value = [msg.target];
}
if (room.value.battleType === 2) {
scores.value.push(msg.target);
power.value = msg.target.battery;
}
playersScores.value[msg.userId].push(msg.target);
2025-06-13 14:05:30 +08:00
}
if (msg.constructor === MESSAGETYPES.RoundPoint) {
bluePoints.value += msg.blueScore;
redPoints.value += msg.redScore;
}
if (msg.constructor === MESSAGETYPES.CurrentRoundEnded) {
const result = msg.preRoundResult;
scores.value = [];
currentShooterId.value = 0;
if (
result.currentRound > 0 &&
result.currentRound < totalRounds.value
) {
// 开始下一轮;
roundResults.value = result.roundResults;
currentRound.value = result.currentRound + 1;
}
}
if (msg.constructor === MESSAGETYPES.MatchOver) {
uni.redirectTo({
url: `/pages/battle-result?battleId=${msg.id}&winner=${msg.endStatus.winner}`,
});
2025-05-30 16:14:17 +08:00
}
2025-06-05 21:32:51 +08:00
}
2025-05-30 16:14:17 +08:00
});
2025-06-05 21:32:51 +08:00
}
onMounted(() => {
uni.$on("socket-inbox", onReceiveMessage);
});
onUnmounted(() => {
uni.$off("socket-inbox", onReceiveMessage);
2025-06-16 12:01:11 +08:00
if (owner.value.id === user.value.id) {
2025-06-15 20:55:34 +08:00
destroyRoomAPI(roomNumber.value);
2025-06-16 12:01:11 +08:00
} else if (roomNumber.value) {
2025-06-15 20:55:34 +08:00
exitRoomAPI(roomNumber.value);
2025-06-05 21:32:51 +08:00
}
});
2025-05-16 15:56:54 +08:00
</script>
<template>
2025-05-30 16:14:17 +08:00
<Container title="对战">
<view class="standby-phase" v-if="step === 1">
2025-05-16 15:56:54 +08:00
<Guide>
<view :style="{ display: 'flex', flexDirection: 'column' }">
<text :style="{ color: '#fed847' }">人都到齐了吗</text>
<text>天赋异禀的弓箭手们比赛即将开始</text>
</view>
</Guide>
2025-06-16 22:43:39 +08:00
<view v-if="room.battleType === 1" class="team-mode">
2025-05-30 16:14:17 +08:00
<image src="../static/1v1-bg.png" mode="widthFix" />
<view>
2025-06-13 14:05:30 +08:00
<view class="player" :style="{ transform: 'translateY(-60px)' }">
<image :src="owner.avatar" mode="widthFix" />
<text>{{ owner.name }}</text>
2025-05-30 16:14:17 +08:00
</view>
2025-06-13 14:05:30 +08:00
<image src="../static/versus.png" mode="widthFix" />
<block v-if="opponent.id">
<view class="player" :style="{ transform: 'translateY(60px)' }">
<image :src="opponent.avatar" mode="widthFix" />
<text v-if="opponent.name">{{ opponent.name }}</text>
</view>
</block>
<block v-else>
<view class="no-player">
<image src="../static/question-mark.png" mode="widthFix" />
</view>
</block>
2025-05-30 16:14:17 +08:00
</view>
</view>
2025-06-04 16:26:07 +08:00
<PlayerSeats
2025-06-16 22:43:39 +08:00
v-if="room.battleType === 2"
:total="room.count || 10"
2025-06-04 16:26:07 +08:00
:players="players"
/>
2025-06-13 14:05:30 +08:00
<view>
2025-05-30 16:14:17 +08:00
<SButton
2025-06-16 22:43:39 +08:00
v-if="user.id === owner.id && room.battleType === 1"
2025-06-16 12:01:11 +08:00
:disabled="!opponent.id"
2025-05-30 16:14:17 +08:00
:onClick="startGame"
>进入对战</SButton
>
<SButton
2025-06-16 22:43:39 +08:00
v-if="user.id === owner.id && room.battleType === 2"
:disabled="players.length < 2"
2025-05-30 16:14:17 +08:00
:onClick="startGame"
>进入大乱斗</SButton
>
2025-06-13 14:05:30 +08:00
<SButton v-if="user.id !== owner.id" disabled>等待房主开启对战</SButton>
2025-05-30 16:14:17 +08:00
<text class="tips">创建者点击下一步所有人即可进入游戏</text>
</view>
2025-05-16 15:56:54 +08:00
</view>
2025-06-13 14:05:30 +08:00
<view v-if="step === 2" :style="{ width: '100%' }">
<BattleHeader
:blueTeam="blueTeam"
:redTeam="redTeam"
:players="players"
/>
2025-05-16 15:56:54 +08:00
<Guide noBg>
<view :style="{ display: 'flex', justifyContent: 'space-between' }">
<view :style="{ display: 'flex', flexDirection: 'column' }">
<text :style="{ color: '#fed847' }">请预先射几箭测试</text>
<text>请确保射击距离只有5米</text>
</view>
2025-06-13 14:05:30 +08:00
<BowPower :power="power" />
2025-05-16 15:56:54 +08:00
</view>
</Guide>
2025-06-11 23:57:17 +08:00
<BowTarget
2025-06-13 14:05:30 +08:00
:scores="scores"
2025-06-11 23:57:17 +08:00
:tips="
!start && scores.length > 0
? `本次射程${scores[scores.length - 1].dst / 100}米,${
scores[scores.length - 1].dst / 100 >= 5 ? '已' : '未'
}达到距离要求`
: ''
"
/>
2025-05-16 15:56:54 +08:00
</view>
<view v-if="step === 3">
2025-06-13 14:05:30 +08:00
<ShootProgress :tips="tips" :seq="seq" :start="start" />
<PlayersRow
2025-06-16 22:43:39 +08:00
v-if="room.battleType === 1"
2025-06-13 14:05:30 +08:00
:blueTeam="blueTeam"
:redTeam="redTeam"
:currentShooterId="currentShooterId"
/>
<BowTarget
:power="power"
:currentRound="currentRound"
:totalRound="totalRounds"
:scores="scores"
/>
<BattleFooter
2025-06-16 22:43:39 +08:00
v-if="room.battleType === 1"
2025-06-13 14:05:30 +08:00
:roundResults="roundResults"
:redPoints="redPoints"
:bluePoints="bluePoints"
/>
<PlayerScore
2025-06-16 22:43:39 +08:00
v-if="room.battleType === 2"
2025-06-13 14:05:30 +08:00
v-for="(player, index) in players"
:key="index"
:name="player.name"
:avatar="player.avatar"
:scores="playersScores[player.id]"
/>
2025-05-16 15:56:54 +08:00
</view>
2025-06-13 14:05:30 +08:00
<Timer :seq="timerSeq" />
2025-05-30 16:14:17 +08:00
</Container>
2025-05-16 15:56:54 +08:00
</template>
<style scoped>
2025-05-30 16:14:17 +08:00
.standby-phase {
width: 100%;
height: calc(100% - 40px);
overflow-x: hidden;
2025-05-16 15:56:54 +08:00
}
.tips {
color: #fff9;
width: 100%;
text-align: center;
display: block;
margin-top: 10px;
2025-06-16 22:43:39 +08:00
font-size: 12px;
2025-05-16 15:56:54 +08:00
}
.player-unknow {
width: 40px;
height: 40px;
margin: 0 10px;
border: 1px solid #fff3;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
background-color: #69686866;
}
.player-unknow > image {
width: 40%;
}
2025-06-11 23:57:17 +08:00
.team-mode {
2025-05-30 16:14:17 +08:00
width: calc(100vw - 30px);
height: 120vw;
margin: 15px;
}
2025-06-11 23:57:17 +08:00
.team-mode > image:first-child {
2025-05-30 16:14:17 +08:00
position: absolute;
width: calc(100vw - 30px);
z-index: -1;
}
2025-06-11 23:57:17 +08:00
.team-mode > view {
2025-05-30 16:14:17 +08:00
display: flex;
justify-content: center;
align-items: center;
height: 95%;
}
2025-06-13 14:05:30 +08:00
.player {
2025-05-30 16:14:17 +08:00
width: 70px;
2025-06-13 14:05:30 +08:00
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
transform: translateY(-60px);
color: #fff9;
font-size: 14px;
}
.player > image {
width: 70px;
height: 70px;
2025-05-30 16:14:17 +08:00
border-radius: 50%;
2025-06-13 14:05:30 +08:00
background-color: #ccc;
margin-bottom: 5px;
2025-05-30 16:14:17 +08:00
}
2025-06-11 23:57:17 +08:00
.team-mode > view > image:nth-child(2) {
2025-05-30 16:14:17 +08:00
width: 120px;
}
2025-06-13 14:05:30 +08:00
.no-player {
2025-05-30 16:14:17 +08:00
width: 70px;
height: 70px;
border-radius: 50%;
background-color: #ccc;
display: flex;
justify-content: center;
align-items: center;
2025-06-13 14:05:30 +08:00
transform: translateY(60px);
2025-05-30 16:14:17 +08:00
}
2025-06-13 14:05:30 +08:00
.no-player > image {
width: 20px;
2025-05-30 16:14:17 +08:00
margin-right: 2px;
}
2025-05-16 15:56:54 +08:00
</style>