update:提交我的设备改版
This commit is contained in:
+54
-3
@@ -17,6 +17,15 @@ const getDefaultDevice = () => ({
|
||||
deviceName: "",
|
||||
});
|
||||
|
||||
const getDefaultDeviceStatus = () => ({
|
||||
battery: null,
|
||||
version: "",
|
||||
online: false,
|
||||
netType: "",
|
||||
onlineDuration: null,
|
||||
receivedAt: 0,
|
||||
});
|
||||
|
||||
const getDefaultGame = () => ({
|
||||
roomID: "",
|
||||
inBattle: false,
|
||||
@@ -102,6 +111,8 @@ export default defineStore("store", {
|
||||
online: false,
|
||||
// 设备电量属于运行时状态,null 表示当前没有有效数据。
|
||||
deviceBattery: null,
|
||||
// WebSocket 实时推送的设备状态,不参与持久化。
|
||||
deviceStatus: getDefaultDeviceStatus(),
|
||||
game: {
|
||||
roomID: "",
|
||||
inBattle: false,
|
||||
@@ -136,7 +147,7 @@ export default defineStore("store", {
|
||||
this.rankData = { ...(data || {}) };
|
||||
},
|
||||
updateOnline(online) {
|
||||
this.online = online;
|
||||
this.setDeviceOnline(online);
|
||||
},
|
||||
updateDeviceBattery(value) {
|
||||
if (value === null || value === undefined || value === "") {
|
||||
@@ -149,6 +160,43 @@ export default defineStore("store", {
|
||||
? Math.min(100, Math.max(0, battery))
|
||||
: null;
|
||||
},
|
||||
updateDeviceStatus(status = {}) {
|
||||
const battery = Number(status.battery ?? status.power);
|
||||
const onlineDuration = Number(status.onlineDuration);
|
||||
const online = status.online === true;
|
||||
const nextStatus = {
|
||||
battery: Number.isFinite(battery)
|
||||
? Math.min(100, Math.max(0, battery))
|
||||
: null,
|
||||
version: String(status.version ?? "").trim(),
|
||||
online,
|
||||
netType: String(status.netType ?? "").trim().toLowerCase(),
|
||||
onlineDuration:
|
||||
Number.isFinite(onlineDuration) && onlineDuration >= 0
|
||||
? onlineDuration
|
||||
: null,
|
||||
receivedAt: Date.now(),
|
||||
};
|
||||
|
||||
this.deviceStatus = nextStatus;
|
||||
this.online = online;
|
||||
this.deviceBattery = online ? nextStatus.battery : null;
|
||||
},
|
||||
setDeviceOnline(online) {
|
||||
const nextOnline = online === true;
|
||||
this.online = nextOnline;
|
||||
if (nextOnline) {
|
||||
this.deviceStatus = { ...this.deviceStatus, online: true };
|
||||
return;
|
||||
}
|
||||
this.deviceBattery = null;
|
||||
this.deviceStatus = getDefaultDeviceStatus();
|
||||
},
|
||||
clearDeviceStatus() {
|
||||
this.online = false;
|
||||
this.deviceBattery = null;
|
||||
this.deviceStatus = getDefaultDeviceStatus();
|
||||
},
|
||||
async updateUser(user = {}) {
|
||||
this.user = { ...getDefaultUser(), ...user };
|
||||
this.user.lvlName = getLvlNameByScore(this.user.scores, this.config.randInfos)
|
||||
@@ -158,13 +206,15 @@ export default defineStore("store", {
|
||||
);
|
||||
},
|
||||
updateDevice(deviceId, deviceName) {
|
||||
if (String(this.device.deviceId || "") !== String(deviceId || "")) {
|
||||
this.clearDeviceStatus();
|
||||
}
|
||||
this.device.deviceId = deviceId;
|
||||
this.device.deviceName = deviceName;
|
||||
},
|
||||
clearDevice() {
|
||||
this.device = getDefaultDevice();
|
||||
this.online = false;
|
||||
this.deviceBattery = null;
|
||||
this.clearDeviceStatus();
|
||||
},
|
||||
async updateConfig(config) {
|
||||
this.config = config;
|
||||
@@ -214,6 +264,7 @@ export default defineStore("store", {
|
||||
device: getDefaultDevice(),
|
||||
online: false,
|
||||
deviceBattery: null,
|
||||
deviceStatus: getDefaultDeviceStatus(),
|
||||
game: getDefaultGame(),
|
||||
dailyCount: getDefaultDailyCount(),
|
||||
deviceChargingDialogVisible: false,
|
||||
|
||||
Reference in New Issue
Block a user