mirror of
https://github.com/Melledy/Nebula.git
synced 2025-12-14 21:34:52 +01:00
Implement saving tower records
This commit is contained in:
@@ -2,11 +2,14 @@ package emu.nebula.game.tower;
|
||||
|
||||
import dev.morphia.annotations.Entity;
|
||||
import dev.morphia.annotations.Id;
|
||||
import emu.nebula.Nebula;
|
||||
import emu.nebula.data.GameData;
|
||||
import emu.nebula.database.GameDatabaseObject;
|
||||
import emu.nebula.game.player.Player;
|
||||
import emu.nebula.game.player.PlayerManager;
|
||||
import emu.nebula.proto.StarTowerApply.StarTowerApplyReq;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@@ -14,9 +17,10 @@ import lombok.Getter;
|
||||
public class StarTowerManager extends PlayerManager implements GameDatabaseObject {
|
||||
@Id
|
||||
private int uid;
|
||||
private int lastBuildId;
|
||||
|
||||
private transient Int2ObjectMap<StarTowerBuild> builds;
|
||||
private transient StarTowerInstance instance;
|
||||
private transient StarTowerBuild lastBuild;
|
||||
|
||||
@Deprecated // Morphia only
|
||||
public StarTowerManager() {
|
||||
@@ -30,8 +34,12 @@ public class StarTowerManager extends PlayerManager implements GameDatabaseObjec
|
||||
this.save();
|
||||
}
|
||||
|
||||
public int getNextBuildId() {
|
||||
return ++this.lastBuildId;
|
||||
public Int2ObjectMap<StarTowerBuild> getBuilds() {
|
||||
if (this.builds == null) {
|
||||
this.loadFromDatabase();
|
||||
}
|
||||
|
||||
return builds;
|
||||
}
|
||||
|
||||
public StarTowerInstance apply(StarTowerApplyReq req) {
|
||||
@@ -47,6 +55,11 @@ public class StarTowerManager extends PlayerManager implements GameDatabaseObjec
|
||||
return null;
|
||||
}
|
||||
|
||||
// Make sure player has at least 3 chars and 3 discs
|
||||
if (formation.getCharCount() != 3 || formation.getDiscCount() < 3) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Create instance
|
||||
this.instance = new StarTowerInstance(this, data, formation, req);
|
||||
|
||||
@@ -55,12 +68,57 @@ public class StarTowerManager extends PlayerManager implements GameDatabaseObjec
|
||||
}
|
||||
|
||||
public StarTowerInstance giveUp() {
|
||||
// Cache instance
|
||||
var instance = this.instance;
|
||||
|
||||
if (instance != null) {
|
||||
// Set last build
|
||||
this.lastBuild = instance.getBuild();
|
||||
|
||||
// Clear instance
|
||||
this.instance = null;
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public boolean saveBuild(boolean delete, String name, boolean lock) {
|
||||
// Sanity check
|
||||
if (this.getLastBuild() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Cache build and clear reference
|
||||
var build = this.lastBuild;
|
||||
this.lastBuild = null;
|
||||
|
||||
// Check if the player wants this build or not
|
||||
if (delete) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check limit
|
||||
if (this.getBuilds().size() >= 50) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add to builds
|
||||
this.getBuilds().put(build.getUid(), build);
|
||||
|
||||
// Save build to database
|
||||
build.save();
|
||||
|
||||
//
|
||||
return true;
|
||||
}
|
||||
|
||||
// Database
|
||||
|
||||
private void loadFromDatabase() {
|
||||
this.builds = new Int2ObjectOpenHashMap<>();
|
||||
|
||||
Nebula.getGameDatabase().getObjects(StarTowerBuild.class, "playerUid", getPlayerUid()).forEach(build -> {
|
||||
this.builds.put(build.getUid(), build);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user