Files
shoot-miniprograms/src/store.js

168 lines
4.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { defineStore } from "pinia";
const defaultUser = {
id: "",
nickName: "",
avatar: "../static/user-icon.png",
trio: 0, // 大于1表示完成了新手引导
lvlName: "",
};
const getLvlName = (rankLvl, rankList = []) => {
if (!rankList) return;
let lvlName = "";
rankList.some((r, index) => {
lvlName = rankList[index].name;
if (r.rank_id === rankLvl) {
lvlName = rankList[index].name;
return true;
}
return false;
});
return lvlName;
};
const getLvlNameByScore = (score, rankList = []) => {
if (!rankList) return;
let lvlName = "";
rankList.some((r, index) => {
if (r.upgrade_scores && r.upgrade_scores < score) {
lvlName = rankList[index].name;
return true;
}
return false;
});
return lvlName;
};
const getLvlImage = (rankLvl, rankList = []) => {
if (!rankList) return;
let lvlImage = "";
rankList.some((r, index) => {
if (r.rank_id === rankLvl) {
lvlImage = rankList[index].icoin;
return true;
}
return false;
});
return lvlImage;
};
const getLvlImageByScore = (score, rankList = []) => {
if (!rankList) return;
let lvlImage = "";
rankList.some((r, index) => {
if (r.upgrade_scores && r.upgrade_scores < score) {
lvlImage = rankList[index].icoin;
return true;
}
return false;
});
return lvlImage;
};
// 定义游戏相关的 store
export default defineStore("store", {
// 状态
state: () => ({
user: defaultUser,
device: {
deviceId: "",
deviceName: "",
},
config: {},
rankData: {
rank: [],
ringRank: [],
},
online: false,
game: {
roomID: "",
inBattle: false,
roomNumber: "", // 当前房间号,供 Header 展示房号胶囊
currentShot: 0, // 当前已射箭数(用于 HeaderProgress 恢复状态)
totalShot: 0, // 轮次总箭数(用于 HeaderProgress 恢复状态)
tips: "", // 当前提示文案(用于 HeaderProgress 恢复状态,替代 uni.$emit 避免时序问题)
},
}),
// 计算属性
getters: {
getUsername: (state) => {
return state.user.username;
},
},
// 方法
actions: {
getLvlNameByScore(score) {
return getLvlNameByScore(score, this.config.randInfos);
},
getLvlName(rankLvl) {
return getLvlName(rankLvl, this.config.randInfos);
},
getLvlImage(rankLvl) {
return getLvlImage(rankLvl, this.config.randInfos);
},
updateRank(data = {}) {
this.rankData = { ...(data || {}) };
},
updateOnline(online) {
this.online = online;
},
async updateUser(user = {}) {
this.user = { ...defaultUser, ...user };
this.user.lvlName = getLvlNameByScore(this.user.scores, this.config.randInfos)
this.user.lvlImage = getLvlImageByScore(
this.user.scores,
this.config.randInfos
);
},
updateDevice(deviceId, deviceName) {
this.device.deviceId = deviceId;
this.device.deviceName = deviceName;
},
async updateConfig(config) {
this.config = config;
if (this.user.scores !== undefined) {
this.user.lvlName = getLvlName(
this.user.rankLvl,
this.config.randInfos
);
}
this.user.lvlImage = getLvlImage(
this.user.rankLvl,
this.config.randInfos
);
},
updateGame(inBattle = false, roomID = "") {
this.game.roomID = roomID;
this.game.inBattle = inBattle;
},
/** 更新当前射箭进度(用于 HeaderProgress 恢复状态,替代 uni.$emit 避免时序问题) */
updateShotInfo(currentShot = 0, totalShot = 0) {
this.game.currentShot = currentShot;
this.game.totalShot = totalShot;
},
/** 更新当前提示文案(用于 HeaderProgress 恢复状态,替代 uni.$emit 避免时序问题) */
updateTips(tips = "") {
this.game.tips = tips;
},
/** 更新当前房间号,供 Header 组件展示房号胶囊 */
updateRoomNumber(number) {
this.game.roomNumber = number;
},
},
// 数据持久化via pinia-plugin-persistedstate
// 仅持久化 user 和 device身份凭证需在冷启动时恢复如从分享链接进入
// config、game 等运行时状态不持久化,每次联网后重新拉取
persist: {
storage: {
getItem: (key) => uni.getStorageSync(key),
setItem: (key, value) => uni.setStorageSync(key, value),
},
paths: ['user', 'device'],
},
});