增加天梯排行榜
This commit is contained in:
144
ladder/ladder.service.js
Normal file
144
ladder/ladder.service.js
Normal file
@@ -0,0 +1,144 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
// Load ladder configuration
|
||||
const ladderConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'ladder-config.json')));
|
||||
|
||||
class LadderService {
|
||||
constructor() {
|
||||
this.rankConfigs = new Map();
|
||||
this.initConfig();
|
||||
}
|
||||
|
||||
initConfig() {
|
||||
ladderConfig.forEach(config => {
|
||||
this.rankConfigs.set(config.Id, config);
|
||||
});
|
||||
}
|
||||
|
||||
// Get rank configuration by ID
|
||||
getRankConfig(rankId) {
|
||||
return this.rankConfigs.get(rankId);
|
||||
}
|
||||
|
||||
// Get all rank configurations
|
||||
getAllRankConfigs() {
|
||||
return Array.from(this.rankConfigs.values());
|
||||
}
|
||||
|
||||
// Handle player win
|
||||
async handleWin(player, opponent) {
|
||||
const config = this.getRankConfig(player.rankId);
|
||||
|
||||
if (config.RankScore === 1) {
|
||||
// 王者分数 mechanism
|
||||
this.updateRankScore(player, opponent, true);
|
||||
} else {
|
||||
// Star-based mechanism
|
||||
this.updateStars(player, config, true);
|
||||
}
|
||||
|
||||
player.totalWins++;
|
||||
player.winStreak++;
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
// Handle player loss
|
||||
async handleLoss(player, opponent) {
|
||||
const config = this.getRankConfig(player.rankId);
|
||||
|
||||
if (config.RankScore === 1) {
|
||||
// 王者分数 mechanism
|
||||
this.updateRankScore(player, opponent, false);
|
||||
} else {
|
||||
// Star-based mechanism
|
||||
this.updateStars(player, config, false);
|
||||
}
|
||||
|
||||
player.winStreak = 0;
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
updateStars(player, config, isWin) {
|
||||
if (isWin) {
|
||||
let starsToAdd = config.WinGetStar;
|
||||
// Add extra stars for win streak
|
||||
if (player.winStreak >= 3) {
|
||||
starsToAdd += config.ExtraGetStar;
|
||||
}
|
||||
player.stars += starsToAdd;
|
||||
|
||||
// Level up check
|
||||
if (player.stars > config.MaxStar) {
|
||||
this.levelUp(player);
|
||||
}
|
||||
} else {
|
||||
if (config.LoseLostStar === 1) {
|
||||
player.stars = Math.max(0, player.stars - 1);
|
||||
|
||||
// Level down check
|
||||
if (player.stars === 0) {
|
||||
this.levelDown(player, config);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
updateRankScore(player, opponent, isWin) {
|
||||
if (isWin) {
|
||||
let scoreToAdd = 20;
|
||||
// If opponent has a higher rank score, calculate bonus
|
||||
if (opponent.rankScore > player.rankScore) {
|
||||
const ratio = Math.min(opponent.rankScore / player.rankScore, 3);
|
||||
scoreToAdd = Math.round(ratio * 20);
|
||||
}
|
||||
player.rankScore += scoreToAdd;
|
||||
} else {
|
||||
// Deduct points on loss
|
||||
if (player.rankScore > 0) {
|
||||
player.rankScore = Math.max(0, player.rankScore - 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
levelUp(player) {
|
||||
const nextConfig = this.getRankConfig(player.rankId + 1);
|
||||
if (nextConfig) {
|
||||
player.rankId++;
|
||||
player.stars = nextConfig.BeginStar;
|
||||
}
|
||||
}
|
||||
|
||||
levelDown(player, currentConfig) {
|
||||
if (currentConfig.LoseRankDown === 1 && player.rankId > 1) {
|
||||
player.rankId--;
|
||||
const prevConfig = this.getRankConfig(player.rankId);
|
||||
player.stars = prevConfig.RankDownStar;
|
||||
}
|
||||
}
|
||||
|
||||
// Get player's rank display info
|
||||
getPlayerRankInfo(player) {
|
||||
const config = this.getRankConfig(player.rankId);
|
||||
if (config.RankScore === 1) {
|
||||
return {
|
||||
rankName: config.RankName,
|
||||
level: config.Level,
|
||||
score: player.rankScore,
|
||||
isRankScore: true
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
rankName: config.RankName,
|
||||
level: config.Level,
|
||||
stars: player.stars,
|
||||
maxStars: config.MaxStar,
|
||||
isRankScore: false
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new LadderService();
|
||||
Reference in New Issue
Block a user