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

412 lines
11 KiB
Vue
Raw Normal View History

2025-05-16 15:56:54 +08:00
<script setup>
2025-07-28 09:02:02 +08:00
import { ref, watch, onMounted, onUnmounted, nextTick } from "vue";
import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
2025-05-30 16:14:17 +08:00
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";
import SButton from "@/components/SButton.vue";
2025-06-17 16:42:53 +08:00
import SModal from "@/components/SModal.vue";
2025-08-12 18:33:39 +08:00
import Avatar from "@/components/Avatar.vue";
2025-07-28 13:54:37 +08:00
import { getRoomAPI, destroyRoomAPI, exitRoomAPI, startRoomAPI } from "@/apis";
import { MESSAGETYPES } 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);
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 players = ref([]);
2025-07-28 13:54:37 +08:00
2025-06-17 16:42:53 +08:00
const showModal = ref(false);
2025-07-16 12:09:27 +08:00
const battleType = ref(0);
2025-07-28 09:02:02 +08:00
const refreshRoomTimer = ref(null);
2025-07-25 16:47:07 +08:00
async function refreshRoomData() {
if (!roomNumber.value) return;
const result = await getRoomAPI(roomNumber.value);
room.value = result;
battleType.value = result.battleType;
2025-07-28 09:02:02 +08:00
(result.members || []).some((m) => {
2025-07-25 16:47:07 +08:00
if (m.userInfo.id === result.creator) {
owner.value = {
id: m.userInfo.id,
name: m.userInfo.name,
avatar: m.userInfo.avatar,
};
return true;
}
return false;
});
if (result.battleType === 1) {
if (user.value.id !== owner.value.id) {
opponent.value = {
id: user.value.id,
name: user.value.nickName,
avatar: user.value.avatar,
};
} 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
});
2025-06-13 14:05:30 +08:00
}
2025-07-25 16:47:07 +08:00
} else if (result.battleType === 2) {
2025-07-26 19:35:31 +08:00
players.value = [];
2025-07-25 16:47:07 +08:00
const ownerIndex = result.members.findIndex(
(m) => m.userInfo.id === result.creator
);
if (ownerIndex !== -1) {
players.value.push(result.members[ownerIndex].userInfo);
} else {
players.value.push({});
}
result.members.forEach((m, index) => {
if (ownerIndex !== index) players.value.push(m.userInfo);
});
2025-05-30 16:14:17 +08:00
}
2025-07-25 16:47:07 +08:00
}
2025-07-15 17:10:41 +08:00
2025-05-30 16:14:17 +08:00
const startGame = async () => {
const result = await startRoomAPI(room.value.number);
2025-06-05 21:32:51 +08:00
};
2025-05-30 16:14:17 +08:00
2025-06-19 21:03:33 +08:00
async function onReceiveMessage(messages = []) {
2025-06-05 21:32:51 +08:00
messages.forEach((msg) => {
2025-06-13 14:05:30 +08:00
if (msg.roomNumber === roomNumber.value) {
if (msg.constructor === MESSAGETYPES.UserEnterRoom) {
2025-07-16 12:09:27 +08:00
if (battleType.value === 1) {
2025-06-26 01:27:23 +08:00
if (msg.userId === room.value.creator) {
owner.value = {
id: msg.userId,
name: msg.name,
avatar: msg.avatar,
};
} else {
opponent.value = {
id: msg.userId,
name: msg.name,
avatar: msg.avatar,
};
}
2025-06-13 14:05:30 +08:00
}
2025-07-16 12:09:27 +08:00
if (battleType.value === 2) {
2025-07-15 17:10:41 +08:00
if (room.value.creator === msg.userId) {
players.value[0] = {
id: msg.userId,
name: msg.name,
avatar: msg.avatar,
};
} else {
players.value.push({
id: msg.userId,
name: msg.name,
avatar: msg.avatar,
});
}
2025-06-16 22:43:39 +08:00
}
2025-06-13 14:05:30 +08:00
}
2025-07-28 13:54:37 +08:00
if (msg.constructor === MESSAGETYPES.UserExitRoom) {
2025-07-16 12:09:27 +08:00
if (battleType.value === 1) {
2025-06-26 01:27:23 +08:00
if (msg.userId === room.value.creator) {
owner.value = {
id: "",
};
} else {
opponent.value = {
id: "",
};
}
2025-06-13 14:05:30 +08:00
}
2025-07-16 12:09:27 +08:00
if (battleType.value === 2) {
2025-06-16 22:43:39 +08:00
players.value = players.value.filter((p) => p.id !== msg.userId);
}
2025-06-13 14:05:30 +08:00
}
2025-07-28 13:54:37 +08:00
if (msg.constructor === MESSAGETYPES.RoomDestroy) {
2025-06-13 14:05:30 +08:00
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);
}
}
2025-07-28 13:54:37 +08:00
if (msg.constructor === MESSAGETYPES.WaitForAllReady) {
if (msg.groupUserStatus) {
uni.setStorageSync("red-team", msg.groupUserStatus.redTeam);
uni.setStorageSync("blue-team", msg.groupUserStatus.blueTeam);
uni.setStorageSync("melee-players", [
2025-07-16 18:18:02 +08:00
...msg.groupUserStatus.redTeam,
...msg.groupUserStatus.blueTeam,
2025-07-28 13:54:37 +08:00
]);
uni.removeStorageSync("current-battle");
if (msg.groupUserStatus.config.mode == 1) {
uni.redirectTo({
url: `/pages/team-match?battleId=${msg.id}&gameMode=1`,
});
} else if (msg.groupUserStatus.config.mode == 2) {
uni.redirectTo({
url: `/pages/melee-match?battleId=${msg.id}&gameMode=1`,
});
2025-07-16 16:09:10 +08:00
}
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
}
2025-06-17 16:42:53 +08:00
const destroyRoom = async () => {
2025-06-22 15:04:10 +08:00
if (roomNumber.value) await destroyRoomAPI(roomNumber.value);
2025-06-17 16:42:53 +08:00
};
const exitRoom = async () => {
uni.navigateBack();
};
2025-07-07 14:39:11 +08:00
const setClipboardData = () => {
uni.setClipboardData({
data: roomNumber.value,
success() {
uni.showToast({ title: "复制成功" });
},
});
};
2025-07-11 00:47:34 +08:00
const onBack = () => {
2025-07-28 13:54:37 +08:00
showModal.value = true;
2025-07-11 00:47:34 +08:00
};
2025-07-25 16:47:07 +08:00
onLoad(async (options) => {
2025-07-28 13:54:37 +08:00
if (options.roomNumber) {
2025-07-25 16:47:07 +08:00
roomNumber.value = options.roomNumber;
refreshRoomData();
2025-07-28 09:02:02 +08:00
refreshRoomTimer.value = setInterval(refreshRoomData, 2000);
2025-07-25 16:47:07 +08:00
}
});
2025-06-05 21:32:51 +08:00
onMounted(() => {
2025-07-13 11:21:19 +08:00
uni.setKeepScreenOn({
keepScreenOn: true,
});
2025-06-05 21:32:51 +08:00
uni.$on("socket-inbox", onReceiveMessage);
});
onUnmounted(() => {
2025-07-28 09:02:02 +08:00
if (refreshRoomTimer.value) clearInterval(refreshRoomTimer.value);
2025-07-13 11:21:19 +08:00
uni.setKeepScreenOn({
keepScreenOn: false,
});
2025-06-05 21:32:51 +08:00
uni.$off("socket-inbox", onReceiveMessage);
2025-07-28 13:54:37 +08:00
if (roomNumber.value && owner.value.id !== user.value.id) {
2025-06-15 20:55:34 +08:00
exitRoomAPI(roomNumber.value);
2025-06-05 21:32:51 +08:00
}
});
2025-07-23 20:47:31 +08:00
onShow(async () => {
2025-07-28 13:54:37 +08:00
refreshRoomData();
});
2025-07-28 13:54:37 +08:00
onHide(() => {});
2025-05-16 15:56:54 +08:00
</script>
<template>
2025-07-28 13:54:37 +08:00
<Container :title="`好友约战 - ${roomNumber}`" :onBack="onBack">
<view class="standby-phase">
2025-05-16 15:56:54 +08:00
<Guide>
2025-07-07 14:39:11 +08:00
<view class="battle-guide">
<view :style="{ display: 'flex', flexDirection: 'column' }">
<text :style="{ color: '#fed847' }">弓箭手们人都到齐了吗?</text>
2025-07-16 12:09:27 +08:00
<text v-if="battleType === 1">1v1比赛即将开始! </text>
<text v-if="battleType === 2">大乱斗即将开始! </text>
2025-07-07 14:39:11 +08:00
</view>
2025-07-25 15:18:46 +08:00
<view @click="setClipboardData">复制房间号</view>
2025-05-16 15:56:54 +08:00
</view>
</Guide>
2025-07-16 12:09:27 +08:00
<view v-if="battleType === 1" class="team-mode">
2025-08-05 15:17:19 +08:00
<image
src="https://static.shelingxingqiu.com/attachment/2025-08-05/dbua9nuf5fyeph7cxi.png"
mode="widthFix"
/>
2025-05-30 16:14:17 +08:00
<view>
2025-06-13 14:05:30 +08:00
<view class="player" :style="{ transform: 'translateY(-60px)' }">
2025-08-12 18:33:39 +08:00
<Avatar :rankLvl="user.rankLvl" :src="user.avatar" :size="60" />
2025-06-13 14:05:30 +08:00
<text>{{ owner.name }}</text>
2025-08-12 18:33:39 +08:00
<text>创建者</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)' }">
2025-07-11 22:21:34 +08:00
<image
:src="opponent.avatar || '../static/user-icon.png'"
mode="widthFix"
/>
2025-06-13 14:05:30 +08:00
<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-07-16 12:09:27 +08:00
v-if="battleType === 2"
2025-06-16 22:43:39 +08:00
: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-07-16 12:09:27 +08:00
v-if="user.id === owner.id && 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-07-16 12:09:27 +08:00
v-if="user.id === owner.id && battleType === 2"
2025-07-15 17:10:41 +08:00
:disabled="players.length < 3"
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-17 16:42:53 +08:00
<SModal :show="showModal" :onClose="() => (showModal = false)">
<view class="btns">
2025-07-28 13:54:37 +08:00
<SButton :onClick="exitRoom" width="200px" :rounded="20">
暂时离开
</SButton>
<block v-if="owner.id === user.id">
<view :style="{ height: '20px' }"></view>
<SButton :onClick="destroyRoom" width="200px" :rounded="20">
解散房间
2025-06-28 12:46:41 +08:00
</SButton>
</block>
2025-06-17 16:42:53 +08:00
</view>
</SModal>
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);
2025-07-07 14:39:11 +08:00
height: 125vw;
2025-05-30 16:14:17 +08:00
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);
2025-08-12 18:33:39 +08:00
color: #fff;
2025-06-13 14:05:30 +08:00
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-17 13:47:33 +08:00
.player > text {
2025-08-12 18:33:39 +08:00
max-width: 100px;
2025-06-17 13:47:33 +08:00
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
text-align: center;
}
2025-08-12 18:33:39 +08:00
.player > text:last-child {
color: #000;
background-color: #fed847;
font-size: 8px;
border-radius: 10px;
padding: 2px 5px;
}
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-06-17 16:42:53 +08:00
.btns {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
2025-07-07 14:39:11 +08:00
.battle-guide {
display: flex;
align-items: center;
justify-content: space-between;
}
.battle-guide > view:last-child {
color: #fed847;
border: 1px solid #fed847;
margin-right: 10px;
padding: 5px 12px;
border-radius: 20px;
position: relative;
}
2025-05-16 15:56:54 +08:00
</style>