feat(gameplay): add crazy dave casino and bank, and zombie mutation lab
This commit is contained in:
@@ -40,6 +40,7 @@ window.LeaderboardSystem = class LeaderboardSystem {
|
||||
};
|
||||
|
||||
this.loadFromAuth();
|
||||
this.fetchAllLeaderboards();
|
||||
}
|
||||
|
||||
loadFromAuth() {
|
||||
@@ -59,15 +60,77 @@ window.LeaderboardSystem = class LeaderboardSystem {
|
||||
}
|
||||
}
|
||||
|
||||
async fetchAllLeaderboards() {
|
||||
try {
|
||||
const resp = await fetch('/api/leaderboard');
|
||||
if (resp.ok) {
|
||||
const json = await resp.json();
|
||||
if (json && json.success && json.records) {
|
||||
for (const [mode, rows] of Object.entries(json.records)) {
|
||||
if (Array.isArray(rows) && rows.length > 0) {
|
||||
this.mergeServerRecords(mode, rows);
|
||||
}
|
||||
}
|
||||
if (window._pvzEngine && typeof window._pvzEngine.renderLeaderboardUI === 'function') {
|
||||
window._pvzEngine.renderLeaderboardUI(this.selectedMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
async fetchLeaderboard(mode) {
|
||||
try {
|
||||
const resp = await fetch(`/api/leaderboard?mode=${encodeURIComponent(mode)}`);
|
||||
if (resp.ok) {
|
||||
const json = await resp.json();
|
||||
if (json && json.success && Array.isArray(json.records)) {
|
||||
this.mergeServerRecords(mode, json.records);
|
||||
if (window._pvzEngine && typeof window._pvzEngine.renderLeaderboardUI === 'function') {
|
||||
window._pvzEngine.renderLeaderboardUI(mode);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
mergeServerRecords(mode, serverRows) {
|
||||
if (!this.records[mode]) {
|
||||
this.records[mode] = [];
|
||||
}
|
||||
const combined = [...this.records[mode]];
|
||||
for (const sr of serverRows) {
|
||||
const existing = combined.find(r => r.name === sr.name);
|
||||
if (existing) {
|
||||
if (sr.score > existing.score) {
|
||||
existing.score = sr.score;
|
||||
existing.detail = sr.detail;
|
||||
existing.date = sr.date;
|
||||
}
|
||||
} else {
|
||||
combined.push({
|
||||
name: sr.name,
|
||||
score: sr.score,
|
||||
detail: sr.detail,
|
||||
date: sr.date
|
||||
});
|
||||
}
|
||||
}
|
||||
combined.sort((a, b) => b.score - a.score);
|
||||
this.records[mode] = combined.slice(0, 10);
|
||||
}
|
||||
|
||||
submitScore(mode, score, detail = '') {
|
||||
if (!this.records[mode]) {
|
||||
this.records[mode] = [];
|
||||
}
|
||||
|
||||
let playerName = 'Игрок';
|
||||
let token = null;
|
||||
if (window.PvZAuth) {
|
||||
playerName = window.PvZAuth.getCurrentUser();
|
||||
if (window.PvZAuth.isGuest()) playerName = 'Гость';
|
||||
token = window.PvZAuth.getToken();
|
||||
}
|
||||
|
||||
const today = new Date().toISOString().slice(0, 10);
|
||||
@@ -94,6 +157,23 @@ window.LeaderboardSystem = class LeaderboardSystem {
|
||||
// Keep top 10
|
||||
this.records[mode] = this.records[mode].slice(0, 10);
|
||||
this.saveToAuth();
|
||||
|
||||
// Submit to server leaderboard asynchronously
|
||||
try {
|
||||
const headers = { 'Content-Type': 'application/json' };
|
||||
if (token) headers['Authorization'] = `Bearer ${token}`;
|
||||
fetch('/api/leaderboard', {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
mode,
|
||||
score,
|
||||
detail: detail || `${score} очков`,
|
||||
name: playerName,
|
||||
token
|
||||
})
|
||||
}).then(r => r.json()).catch(() => {});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
getEntries(mode) {
|
||||
|
||||
Reference in New Issue
Block a user