mirror of
https://github.com/Melledy/Nebula.git
synced 2026-09-20 01:29:54 +02:00
Implement achievements properly
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
package emu.nebula.game.achievement;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
import lombok.Getter;
|
||||
|
||||
public enum AchievementCondition {
|
||||
AchievementSpecific (1),
|
||||
AchievementTotal (2),
|
||||
BattleTotal (3),
|
||||
CharacterAcquire (5),
|
||||
CharacterAcquireQuantityRarityAndAdvancement (6),
|
||||
CharacterAdvanceTotal (7),
|
||||
CharacterSkillUpTotal (8),
|
||||
CharacterSkillWithSpecificUpTotal (9),
|
||||
CharacterSpecific (10),
|
||||
CharacterUpLevel (11),
|
||||
CharacterUpTotal (12),
|
||||
CharacterWithSpecificAdvance (13),
|
||||
CharacterWithSpecificAffinity (14),
|
||||
CharacterWithSpecificUpLevel (15),
|
||||
CharactersWithSpecificLevelAndQuantity (16),
|
||||
CharactersWithSpecificNumberLevelAndAttributes (17),
|
||||
CharactersWithSpecificPlot (18),
|
||||
CharactersWithSpecificQuantityAdvancementCountAndAttribute (19),
|
||||
CharactersWithSpecificQuantityAndRarity (20),
|
||||
CharactersWithSpecificQuantityRarityAndAdvancement (21),
|
||||
CharactersWithSpecificQuantityRarityAndLevel (22),
|
||||
ChatTotal (23),
|
||||
DailyInstanceClearSpecificDifficultyAndTotal (24),
|
||||
DailyInstanceClearSpecificTypeAndTotal (25),
|
||||
DailyInstanceClearTotal (26),
|
||||
DateSpecific (27),
|
||||
DiscAcquire (28),
|
||||
DiscAcquireSpecificQuantityAndRarity (29),
|
||||
DiscAcquireQuantityLevelAndRarity (30),
|
||||
DiscAcquireQuantityPhaseAndRarity (31),
|
||||
DiscAcquireQuantityStarAndRarity (32),
|
||||
DiscLimitBreakTotal (33),
|
||||
DiscPromoteTotal (34),
|
||||
DiscStrengthenTotal (35),
|
||||
DiscWithSpecificQuantityLevelAndRarity (36),
|
||||
DiscWithSpecificQuantityPhaseAndRarity (37),
|
||||
DiscWithSpecificQuantityStarAndRarity (38),
|
||||
GachaCharacterNotSSRTotal (40),
|
||||
GachaCharacterTenModeSSRTotal (41),
|
||||
GachaCharacterTotal (42),
|
||||
GachaTenModeAcquireQuantityAndRarityItems (43),
|
||||
GachaTotal (44),
|
||||
GiftGiveTotal (45),
|
||||
InfinityTowerClearSpecificFloor (46),
|
||||
InfinityTowerClearTotal (47),
|
||||
ItemsAdd (48),
|
||||
ItemsDeplete (49),
|
||||
ItemsProductTotal (50),
|
||||
LoginTotal (51),
|
||||
QuestTravelerDuelChallengeTotal (52),
|
||||
QuestTourGuideSpecific (53),
|
||||
QuestTravelerDuelSpecific (54),
|
||||
QuestWithSpecificType (55),
|
||||
RegionBossClearSpecificFullStarWithBossIdAndDifficulty (56),
|
||||
RegionBossClearSpecificLevelWithDifficultyAndTotal (57),
|
||||
RegionBossClearSpecificTotal (58),
|
||||
RegionBossClearTotal (59),
|
||||
SkillsWithSpecificQuantityAndLevel (60),
|
||||
SkinAcquire (61),
|
||||
StageClearSpecificStars (62),
|
||||
StoryClear (63),
|
||||
TravelerDuelChallengeSpecificBoosLevelWithDifficultyAndTotal (64),
|
||||
TravelerDuelClearBossTotal (65),
|
||||
TravelerDuelClearSpecificBossIdAndDifficulty (66),
|
||||
TravelerDuelChallengeClearSpecificBossLevelAndAffix (67),
|
||||
TravelerDuelClearSpecificBossLevelWithDifficultyAndTotal (68),
|
||||
TravelerDuelClearSpecificBossTotal (69),
|
||||
TravelerDuelChallengeRankUploadTotal (70),
|
||||
WorldClassSpecific (71),
|
||||
RegionBossClearSpecificTypeWithTotal (72),
|
||||
CharactersWithSpecificDatingCount (73),
|
||||
CharactersDatingTotal (74),
|
||||
VampireSurvivorScoreTotal (75),
|
||||
VampireSurvivorSpecificLevelWithSpecificScore (76),
|
||||
VampireSurvivorPassedSpecificLevel (77),
|
||||
CharacterParticipateTowerNumber (78),
|
||||
CharacterAllSkillReachSpecificLevel (79),
|
||||
TravelerDuelPlayTotal (80),
|
||||
VampireClearTotal (81),
|
||||
VampireWithSpecificClearTotal (82),
|
||||
AgentFinishTotal (83),
|
||||
AgentWithSpecificFinishTotal (84),
|
||||
ActivityMiningEnterLayer (86),
|
||||
ActivityMiningDestroyGrid (87),
|
||||
BossRushTotalStars (88),
|
||||
InfinityTowerClearSpecificDifficultyAndTotal (89),
|
||||
SkillInstanceClearTotal (90),
|
||||
VampireSurvivorSpecificPassedLevel (91),
|
||||
WeekBoosClearSpecificDifficultyAndTotal (92),
|
||||
NpcAffinityWithSpecificLevel (93),
|
||||
CharacterPassedWithSpecificTowerAndCount (94),
|
||||
JointDrillScoreTotal (95),
|
||||
CharGemInstanceClearTotal (104),
|
||||
DailyShopReceiveShopTotal (105),
|
||||
AgentApplyTotal (106),
|
||||
DiscSpecific (114),
|
||||
ClientReport (200),
|
||||
TowerBattleTimes (501),
|
||||
TowerBossChallengeSpecificHighRewardWithTotal (502),
|
||||
TowerBuildSpecificCharacter (503),
|
||||
TowerBuildSpecificScoreWithTotal (504),
|
||||
TowerClearSpecificCharacterTypeWithTotal (505),
|
||||
TowerClearSpecificGroupIdAndDifficulty (506),
|
||||
TowerClearSpecificLevelWithDifficultyAndTotal (507),
|
||||
TowerClearTotal (508),
|
||||
TowerEnterRoom (509),
|
||||
TowerEventTimes (511),
|
||||
TowerFateTimes (512),
|
||||
TowerItemsGet (513),
|
||||
TowerSpecificDifficultyShopBuyTimes (514),
|
||||
TowerGrowthSpecificNote (515),
|
||||
TowerClearSpecificLevelWithDifficultyAndTotalHistory (516),
|
||||
TowerBookWithSpecificEvent (517),
|
||||
TowerBookWithSpecificFateCard (518),
|
||||
TowerBookWithSpecificPotential (520),
|
||||
TowerBuildSpecificDifficultyAndScoreWithTotal (521),
|
||||
TowerSpecificDifficultyStrengthenMachineTotal (522),
|
||||
TowerSpecificDifficultyKillBossTotal (524),
|
||||
TowerBookSpecificCharWithPotentialTotal (525),
|
||||
TowerBuildSpecificCharSpecificScoreWithTotal (526),
|
||||
TowerGrowthWithSpecificNote (527),
|
||||
TowerSpecificFateCardReRollTotal (528),
|
||||
TowerSpecificPotentialReRollTotal (529),
|
||||
TowerSpecificShopReRollTotal (530),
|
||||
TowerSpecificNoteActivateTotal (531),
|
||||
TowerSpecificNoteLevelTotal (532),
|
||||
TowerSpecificPotentialBonusTotal (533),
|
||||
TowerSpecificPotentialLuckyTotal (534),
|
||||
TowerSpecificShopBuyDiscountTotal (535),
|
||||
TowerSpecificSecondarySkillActivateTotal (536),
|
||||
TowerSpecificGetExtraNoteLvTotal (537),
|
||||
TowerSweepTimes (539),
|
||||
TowerSweepTotal (540);
|
||||
|
||||
@Getter
|
||||
private final int value;
|
||||
private final static Int2ObjectMap<AchievementCondition> map = new Int2ObjectOpenHashMap<>();
|
||||
|
||||
static {
|
||||
for (AchievementCondition type : AchievementCondition.values()) {
|
||||
map.put(type.getValue(), type);
|
||||
}
|
||||
}
|
||||
|
||||
private AchievementCondition(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static AchievementCondition getByValue(int value) {
|
||||
return map.get(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package emu.nebula.game.achievement;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import emu.nebula.data.GameData;
|
||||
import emu.nebula.data.resources.AchievementDef;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
import lombok.Getter;
|
||||
|
||||
// Because achievements in the data files do not have params, we will hardcode them here
|
||||
public class AchievementHelper {
|
||||
// Achievement cache
|
||||
|
||||
@Getter
|
||||
private static Int2ObjectMap<List<AchievementDef>> cache = new Int2ObjectOpenHashMap<>();
|
||||
|
||||
public static List<AchievementDef> getAchievementsByCondition(AchievementCondition condition) {
|
||||
return cache.get(condition.getValue());
|
||||
}
|
||||
|
||||
// Fix params
|
||||
|
||||
public static void fixParams() {
|
||||
addParam(78, -1, 2);
|
||||
addParam(79, -1, 4);
|
||||
addParam(498, -1, 1);
|
||||
}
|
||||
|
||||
private static void addParam(int achievementId, int param1, int param2) {
|
||||
var data = GameData.getAchievementDataTable().get(achievementId);
|
||||
if (data == null) return;
|
||||
|
||||
data.setParams(param1, param2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
package emu.nebula.game.achievement;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import dev.morphia.annotations.Entity;
|
||||
import dev.morphia.annotations.Id;
|
||||
import emu.nebula.Nebula;
|
||||
import emu.nebula.data.GameData;
|
||||
import emu.nebula.data.resources.AchievementDef;
|
||||
import emu.nebula.database.GameDatabaseObject;
|
||||
import emu.nebula.game.inventory.ItemParamMap;
|
||||
import emu.nebula.game.player.Player;
|
||||
import emu.nebula.game.player.PlayerChangeInfo;
|
||||
import emu.nebula.game.player.PlayerManager;
|
||||
import emu.nebula.net.NetMsgId;
|
||||
import emu.nebula.proto.Public.Achievements;
|
||||
import emu.nebula.proto.Public.Events;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import us.hebi.quickbuf.RepeatedInt;
|
||||
|
||||
@Getter
|
||||
@Entity(value = "achievements", useDiscriminator = false)
|
||||
public class AchievementManager extends PlayerManager implements GameDatabaseObject {
|
||||
@Id
|
||||
private int uid;
|
||||
|
||||
// Achievement data
|
||||
private Map<Integer, GameAchievement> achievements;
|
||||
|
||||
@Setter
|
||||
private transient boolean queueSave;
|
||||
|
||||
@Deprecated // Morphia only
|
||||
public AchievementManager() {
|
||||
|
||||
}
|
||||
|
||||
public AchievementManager(Player player) {
|
||||
super(player);
|
||||
this.uid = player.getUid();
|
||||
this.achievements = new HashMap<>();
|
||||
|
||||
this.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if there are any unclaimed achievements
|
||||
*/
|
||||
public synchronized boolean hasNewAchievements() {
|
||||
for (var achievement : this.getAchievements().values()) {
|
||||
if (achievement.isClaimable()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public synchronized GameAchievement getAchievement(AchievementDef data) {
|
||||
// Try and get achievement normally
|
||||
var achievement = this.getAchievements().get(data.getId());
|
||||
|
||||
// Create achievement if it doesnt exist
|
||||
if (achievement == null) {
|
||||
achievement = new GameAchievement(data);
|
||||
|
||||
this.getAchievements().put(achievement.getId(), achievement);
|
||||
}
|
||||
|
||||
return achievement;
|
||||
}
|
||||
|
||||
public synchronized void handleClientEvents(Events events) {
|
||||
// Parse events
|
||||
for (var event : events.getList()) {
|
||||
// Check id
|
||||
if (event.getId() != 200) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Sanity check
|
||||
if (event.getData().length() < 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get achievement id and progress
|
||||
int id = event.getData().get(1);
|
||||
int progress = event.getData().get(0);
|
||||
|
||||
if (progress <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get achievement data
|
||||
var data = GameData.getAchievementDataTable().get(id);
|
||||
if (data == null) continue;
|
||||
|
||||
// Make sure achivement can be completed by the client
|
||||
if (data.getCompleteCond() != AchievementCondition.ClientReport.getValue()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get achievement
|
||||
var achievement = this.getAchievement(data);
|
||||
|
||||
// Update achievement
|
||||
boolean changed = achievement.trigger(true, progress, 0, 0);
|
||||
|
||||
// Only save/update on client if achievement was changed
|
||||
if (changed) {
|
||||
// Sync
|
||||
this.syncAchievement(achievement);
|
||||
|
||||
// Set save flag
|
||||
this.queueSave = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void trigger(AchievementCondition condition, int progress, int param1, int param2) {
|
||||
// Sanity check
|
||||
if (progress <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get achievements to trigger
|
||||
var triggerList = AchievementHelper.getAchievementsByCondition(condition);
|
||||
|
||||
if (triggerList == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check what type of achievement condition this is
|
||||
boolean isTotal = condition.name().endsWith("Total");
|
||||
|
||||
// Parse achievements
|
||||
for (var data : triggerList) {
|
||||
// Get achievement
|
||||
var achievement = this.getAchievement(data);
|
||||
|
||||
// Update achievement
|
||||
boolean changed = achievement.trigger(isTotal, progress, param1, param2);
|
||||
|
||||
// Only save/update on client if achievement was changed
|
||||
if (changed) {
|
||||
// Sync
|
||||
this.syncAchievement(achievement);
|
||||
|
||||
// Set save flag
|
||||
this.queueSave = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update this achievement on the player client
|
||||
*/
|
||||
private void syncAchievement(GameAchievement achievement) {
|
||||
if (!getPlayer().hasSession()) {
|
||||
return;
|
||||
}
|
||||
|
||||
getPlayer().addNextPackage(
|
||||
NetMsgId.achievement_change_notify,
|
||||
achievement.toProto()
|
||||
);
|
||||
}
|
||||
|
||||
public synchronized PlayerChangeInfo recvRewards(RepeatedInt ids) {
|
||||
// Sanity check
|
||||
if (ids.length() <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Init variables
|
||||
var rewards = new ItemParamMap();
|
||||
|
||||
// Claim achievements
|
||||
for (int id : ids) {
|
||||
// Get achievement
|
||||
var achievement = this.getAchievements().get(id);
|
||||
if (achievement == null) continue;
|
||||
|
||||
// Check if we can claim this achievement
|
||||
if (achievement.isClaimable()) {
|
||||
// Claim
|
||||
achievement.setClaimed(true);
|
||||
|
||||
// Add rewards
|
||||
rewards.add(achievement.getData().getTid1(), achievement.getData().getQty1());
|
||||
|
||||
// Save
|
||||
this.queueSave = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Success
|
||||
return this.getPlayer().getInventory().addItems(rewards);
|
||||
}
|
||||
|
||||
// Proto
|
||||
|
||||
public synchronized Achievements toProto() {
|
||||
var proto = Achievements.newInstance();
|
||||
|
||||
for (var achievement : this.getAchievements().values()) {
|
||||
proto.addList(achievement.toProto());
|
||||
}
|
||||
|
||||
return proto;
|
||||
}
|
||||
|
||||
// Database
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
Nebula.getGameDatabase().save(this);
|
||||
this.queueSave = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package emu.nebula.game.achievement;
|
||||
|
||||
import dev.morphia.annotations.Entity;
|
||||
import emu.nebula.Nebula;
|
||||
import emu.nebula.data.GameData;
|
||||
import emu.nebula.data.resources.AchievementDef;
|
||||
import emu.nebula.proto.Public.Achievement;
|
||||
import emu.nebula.proto.Public.QuestProgress;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@Entity(useDiscriminator = false)
|
||||
public class GameAchievement {
|
||||
private int id;
|
||||
private int curProgress;
|
||||
private long completed;
|
||||
private boolean claimed;
|
||||
|
||||
private transient AchievementDef data;
|
||||
|
||||
@Deprecated // Morphia only
|
||||
public GameAchievement() {
|
||||
|
||||
}
|
||||
|
||||
public GameAchievement(AchievementDef data) {
|
||||
this.id = data.getId();
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public AchievementDef getData() {
|
||||
if (this.data == null) {
|
||||
this.data = GameData.getAchievementDataTable().get(this.getId());
|
||||
}
|
||||
|
||||
return this.data;
|
||||
}
|
||||
|
||||
public int getMaxProgress() {
|
||||
return this.getData().getAimNumShow();
|
||||
}
|
||||
|
||||
public boolean isComplete() {
|
||||
return this.completed > 0;
|
||||
}
|
||||
|
||||
public boolean isClaimable() {
|
||||
return !this.isClaimed() && this.isComplete();
|
||||
}
|
||||
|
||||
public void setClaimed(boolean claimed) {
|
||||
this.claimed = claimed;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
if (this.isClaimed()) {
|
||||
return 2;
|
||||
} else if (this.isComplete()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if achievement was updated
|
||||
*/
|
||||
public boolean trigger(boolean isTotal, int progress, int param1, int param2) {
|
||||
// Sanity check
|
||||
if (this.isComplete()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check param conditions
|
||||
var data = this.getData();
|
||||
if (data == null) return false;
|
||||
|
||||
if (data.hasParam1() && data.getParam1() != param1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (data.hasParam2() && data.getParam2() != param2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set previous progress
|
||||
int prevProgress = this.curProgress;
|
||||
|
||||
// Update progress
|
||||
if (isTotal) {
|
||||
this.curProgress += progress;
|
||||
} else {
|
||||
this.curProgress = progress;
|
||||
}
|
||||
|
||||
// Check if completed
|
||||
if (this.getCurProgress() >= this.getMaxProgress()) {
|
||||
this.curProgress = this.getMaxProgress();
|
||||
this.completed = Nebula.getCurrentTime();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if progress was changed
|
||||
return prevProgress != this.curProgress;
|
||||
}
|
||||
|
||||
// Proto
|
||||
|
||||
public Achievement toProto() {
|
||||
var progress = QuestProgress.newInstance()
|
||||
.setCur(this.getCurProgress())
|
||||
.setMax(this.getMaxProgress());
|
||||
|
||||
var proto = Achievement.newInstance()
|
||||
.setId(this.getId())
|
||||
.setCompleted(this.getCompleted())
|
||||
.setStatus(this.getStatus())
|
||||
.addProgress(progress);
|
||||
|
||||
return proto;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user