Allow challenge instances to persist through the database

This commit is contained in:
Melledy
2023-10-29 15:58:17 -07:00
parent ac1751a749
commit e5d7eefea5
4 changed files with 54 additions and 24 deletions

View File

@@ -1,5 +1,6 @@
package emu.lunarcore.game.challenge;
import dev.morphia.annotations.Entity;
import emu.lunarcore.data.GameData;
import emu.lunarcore.data.config.GroupInfo;
import emu.lunarcore.data.config.MonsterInfo;
@@ -20,30 +21,35 @@ import emu.lunarcore.util.Position;
import lombok.Getter;
import lombok.Setter;
@Getter
@Getter @Entity(useDiscriminator = false)
public class ChallengeInstance {
private final Player player;
private final ChallengeExcel excel;
private transient Player player;
private transient ChallengeExcel excel;
private Position startPos;
private Position startRot;
private boolean hasAvatarDied;
private int challengeId;
private int currentStage;
private ExtraLineupType currentExtraLineup;
private ChallengeStatus status;
private int currentExtraLineup;
private int status;
private boolean hasAvatarDied;
@Setter private int roundsLeft;
@Setter private int stars;
@Deprecated // Morphia only
public ChallengeInstance() {}
public ChallengeInstance(Player player, ChallengeExcel excel) {
this.player = player;
this.excel = excel;
this.challengeId = excel.getId();
this.startPos = new Position();
this.startRot = new Position();
this.currentStage = 1;
this.roundsLeft = excel.getChallengeCountDown();
this.status = ChallengeStatus.CHALLENGE_DOING;
this.currentExtraLineup = ExtraLineupType.LINEUP_CHALLENGE;
this.setStatus(ChallengeStatus.CHALLENGE_DOING);
this.setCurrentExtraLineup(ExtraLineupType.LINEUP_CHALLENGE);
}
private Scene getScene() {
@@ -54,6 +60,14 @@ public class ChallengeInstance {
return this.getExcel().getId();
}
private void setStatus(ChallengeStatus status) {
this.status = status.getNumber();
}
private void setCurrentExtraLineup(ExtraLineupType type) {
this.currentExtraLineup = type.getNumber();
}
protected void setupStage1() {
this.setupStage(
excel.getMazeGroupID1(),
@@ -110,7 +124,7 @@ public class ChallengeInstance {
}
public boolean isWin() {
return status == ChallengeStatus.CHALLENGE_FINISH;
return status == ChallengeStatus.CHALLENGE_FINISH_VALUE;
}
public void onBattleStart(Battle battle) {
@@ -133,7 +147,7 @@ public class ChallengeInstance {
// Progress to the next stage
if (this.currentStage >= excel.getStageNum()) {
// Last stage
this.status = ChallengeStatus.CHALLENGE_FINISH;
this.setStatus(ChallengeStatus.CHALLENGE_FINISH);
this.stars = this.calculateStars();
// Save history
player.getChallengeManager().addHistory(this.getChallengeId(), this.getStars());
@@ -144,7 +158,7 @@ public class ChallengeInstance {
this.currentStage++;
this.setupStage2();
// Change player line up
this.currentExtraLineup = ExtraLineupType.LINEUP_CHALLENGE_2;
this.setCurrentExtraLineup(ExtraLineupType.LINEUP_CHALLENGE_2);
player.getLineupManager().setCurrentExtraLineup(this.getCurrentExtraLineup(), true);
player.sendPacket(new PacketChallengeLineupNotify(this.getCurrentExtraLineup()));
// Move player
@@ -156,13 +170,13 @@ public class ChallengeInstance {
this.roundsLeft = Math.min(Math.max(this.roundsLeft - stats.getRoundCnt(), 0), this.roundsLeft);
} else {
// Fail challenge
this.status = ChallengeStatus.CHALLENGE_FAILED;
this.setStatus(ChallengeStatus.CHALLENGE_FAILED);
}
}
public void onUpdate() {
// End challenge if its done
if (status != ChallengeStatus.CHALLENGE_DOING) {
if (status != ChallengeStatus.CHALLENGE_DOING_VALUE) {
getPlayer().setChallengeInstance(null);
}
}
@@ -194,12 +208,21 @@ public class ChallengeInstance {
return Math.min(stars, 7);
}
public boolean validate(Player player) {
if (this.player == null) {
this.player = player;
}
this.excel = GameData.getChallengeExcelMap().get(this.challengeId);
return this.excel != null;
}
public ChallengeInfo toProto() {
var proto = ChallengeInfo.newInstance()
.setChallengeId(this.getExcel().getId())
.setStatus(this.getStatus())
.setStatusValue(this.getStatus())
.setRoundCount(this.getRoundsElapsed())
.setExtraLineupType(this.getCurrentExtraLineup());
.setExtraLineupTypeValue(this.getCurrentExtraLineup());
return proto;
}

View File

@@ -58,13 +58,13 @@ public class ChallengeManager extends BasePlayerManager {
lineup.setMp(5);
}
// Set first lineup before we enter scenes
getPlayer().getLineupManager().setCurrentExtraLineup(ExtraLineupType.LINEUP_CHALLENGE, false);
// Set challenge data for player
ChallengeInstance instance = new ChallengeInstance(getPlayer(), excel);
getPlayer().setChallengeInstance(instance);
// Set first lineup before we enter scenes
getPlayer().getLineupManager().setCurrentExtraLineup(instance.getCurrentExtraLineup(), false);
// Enter scene
boolean success = getPlayer().enterScene(excel.getMapEntranceID(), 0, false);
if (!success) {

View File

@@ -98,13 +98,15 @@ public class Player {
private LineupManager lineupManager;
private PlayerGachaInfo gachaInfo;
// Instances
@Setter private ChallengeInstance challengeInstance;
@Setter private transient RogueInstance rogueInstance;
// Etc
private transient boolean inAnchorRange;
private transient int nextBattleId;
@Setter private transient boolean paused;
@Setter private transient ChallengeInstance challengeInstance;
@Setter private transient RogueInstance rogueInstance;
@Deprecated // Morphia only
public Player() {
@@ -554,6 +556,12 @@ public class Player {
// Post database load
this.getAvatars().setupHeroPaths();
// Check instances
if (this.getChallengeInstance() != null && !this.getChallengeInstance().validate(this)) {
// Delete instance if it failed to validate (example: missing an excel)
this.challengeInstance = null;
}
// Load into saved scene (should happen after everything else loads)
this.loadScene(planeId, floorId, entryId, this.getPos(), this.getRot());

View File

@@ -1,17 +1,16 @@
package emu.lunarcore.server.packet.send;
import emu.lunarcore.proto.ChallengeLineupNotifyOuterClass.ChallengeLineupNotify;
import emu.lunarcore.proto.ExtraLineupTypeOuterClass.ExtraLineupType;
import emu.lunarcore.server.packet.BasePacket;
import emu.lunarcore.server.packet.CmdId;
public class PacketChallengeLineupNotify extends BasePacket {
public PacketChallengeLineupNotify(ExtraLineupType type) {
public PacketChallengeLineupNotify(int type) {
super(CmdId.ChallengeLineupNotify);
var data = ChallengeLineupNotify.newInstance()
.setExtraLineupType(type);
.setExtraLineupTypeValue(type);
this.setData(data);
}