wangwang/js/storage.js

68 lines
1.3 KiB
JavaScript

const STORAGE_KEYS = {
WP: 'wangwang_wp',
UNLOCKED: 'wangwang_unlocked',
SELECTED_DOG: 'wangwang_selected_dog',
TOTAL_WINS: 'wangwang_total_wins'
};
const storage = {
getWP() {
try {
return wx.getStorageSync(STORAGE_KEYS.WP) || 0;
} catch (e) {
return 0;
}
},
addWP(amount) {
const wp = this.getWP() + amount;
wx.setStorageSync(STORAGE_KEYS.WP, wp);
return wp;
},
getUnlocked() {
try {
return wx.getStorageSync(STORAGE_KEYS.UNLOCKED) || ['shiba', 'pomeranian'];
} catch (e) {
return ['shiba', 'pomeranian'];
}
},
unlockDog(dogId) {
const unlocked = this.getUnlocked();
if (!unlocked.includes(dogId)) {
unlocked.push(dogId);
wx.setStorageSync(STORAGE_KEYS.UNLOCKED, unlocked);
}
return unlocked;
},
getSelectedDog() {
try {
return wx.getStorageSync(STORAGE_KEYS.SELECTED_DOG) || 'shiba';
} catch (e) {
return 'shiba';
}
},
setSelectedDog(dogId) {
wx.setStorageSync(STORAGE_KEYS.SELECTED_DOG, dogId);
},
getTotalWins() {
try {
return wx.getStorageSync(STORAGE_KEYS.TOTAL_WINS) || 0;
} catch (e) {
return 0;
}
},
addWin() {
const wins = this.getTotalWins() + 1;
wx.setStorageSync(STORAGE_KEYS.TOTAL_WINS, wins);
return wins;
}
};
export default storage;