mirror of
https://github.com/Melledy/Nebula.git
synced 2025-12-15 05:44:36 +01:00
Handle record rename/delete/lock/favorite
This commit is contained in:
@@ -3,6 +3,7 @@ package emu.nebula.game.tower;
|
||||
import dev.morphia.annotations.Entity;
|
||||
import dev.morphia.annotations.Id;
|
||||
import dev.morphia.annotations.Indexed;
|
||||
import emu.nebula.Nebula;
|
||||
import emu.nebula.data.GameData;
|
||||
import emu.nebula.database.GameDatabaseObject;
|
||||
import emu.nebula.proto.Public.ItemTpl;
|
||||
@@ -41,7 +42,7 @@ public class StarTowerBuild implements GameDatabaseObject {
|
||||
|
||||
}
|
||||
|
||||
public StarTowerBuild(StarTowerInstance instance) {
|
||||
public StarTowerBuild(StarTowerGame instance) {
|
||||
this.uid = Snowflake.newUid();
|
||||
this.playerUid = instance.getPlayer().getUid();
|
||||
this.name = "";
|
||||
@@ -74,6 +75,25 @@ public class StarTowerBuild implements GameDatabaseObject {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setName(String newName) {
|
||||
if (newName.length() > 32) {
|
||||
newName = newName.substring(0, 31);
|
||||
}
|
||||
|
||||
this.name = newName;
|
||||
Nebula.getGameDatabase().update(this, this.getUid(), "name", this.getName());
|
||||
}
|
||||
|
||||
public void setLock(boolean state) {
|
||||
this.lock = state;
|
||||
Nebula.getGameDatabase().update(this, this.getUid(), "lock", this.isLock());
|
||||
}
|
||||
|
||||
public void setPreference(boolean state) {
|
||||
this.preference = state;
|
||||
Nebula.getGameDatabase().update(this, this.getUid(), "preference", this.isPreference());
|
||||
}
|
||||
|
||||
// Proto
|
||||
|
||||
@@ -129,4 +149,10 @@ public class StarTowerBuild implements GameDatabaseObject {
|
||||
|
||||
return proto;
|
||||
}
|
||||
|
||||
// Database
|
||||
|
||||
public void delete() {
|
||||
Nebula.getGameDatabase().delete(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import lombok.SneakyThrows;
|
||||
|
||||
@Getter
|
||||
@Entity(useDiscriminator = false)
|
||||
public class StarTowerInstance {
|
||||
public class StarTowerGame {
|
||||
private transient StarTowerManager manager;
|
||||
private transient StarTowerDef data;
|
||||
|
||||
@@ -64,11 +64,11 @@ public class StarTowerInstance {
|
||||
private transient StarTowerBuild build;
|
||||
|
||||
@Deprecated // Morphia only
|
||||
public StarTowerInstance() {
|
||||
public StarTowerGame() {
|
||||
|
||||
}
|
||||
|
||||
public StarTowerInstance(StarTowerManager manager, StarTowerDef data, Formation formation, StarTowerApplyReq req) {
|
||||
public StarTowerGame(StarTowerManager manager, StarTowerDef data, Formation formation, StarTowerApplyReq req) {
|
||||
this.manager = manager;
|
||||
this.data = data;
|
||||
|
||||
@@ -6,10 +6,11 @@ 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.PlayerChangeInfo;
|
||||
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 it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@@ -18,8 +19,13 @@ public class StarTowerManager extends PlayerManager implements GameDatabaseObjec
|
||||
@Id
|
||||
private int uid;
|
||||
|
||||
private transient Int2ObjectMap<StarTowerBuild> builds;
|
||||
private transient StarTowerInstance instance;
|
||||
// TODO add tower talents here
|
||||
|
||||
// Tower game instance
|
||||
private transient StarTowerGame game;
|
||||
|
||||
// Tower builds
|
||||
private transient Long2ObjectMap<StarTowerBuild> builds;
|
||||
private transient StarTowerBuild lastBuild;
|
||||
|
||||
@Deprecated // Morphia only
|
||||
@@ -34,7 +40,7 @@ public class StarTowerManager extends PlayerManager implements GameDatabaseObjec
|
||||
this.save();
|
||||
}
|
||||
|
||||
public Int2ObjectMap<StarTowerBuild> getBuilds() {
|
||||
public Long2ObjectMap<StarTowerBuild> getBuilds() {
|
||||
if (this.builds == null) {
|
||||
this.loadFromDatabase();
|
||||
}
|
||||
@@ -42,7 +48,11 @@ public class StarTowerManager extends PlayerManager implements GameDatabaseObjec
|
||||
return builds;
|
||||
}
|
||||
|
||||
public StarTowerInstance apply(StarTowerApplyReq req) {
|
||||
public StarTowerBuild getBuildById(long id) {
|
||||
return this.getBuilds().get(id);
|
||||
}
|
||||
|
||||
public StarTowerGame apply(StarTowerApplyReq req) {
|
||||
// Sanity checks
|
||||
var data = GameData.getStarTowerDataTable().get(req.getId());
|
||||
if (data == null) {
|
||||
@@ -60,23 +70,23 @@ public class StarTowerManager extends PlayerManager implements GameDatabaseObjec
|
||||
return null;
|
||||
}
|
||||
|
||||
// Create instance
|
||||
this.instance = new StarTowerInstance(this, data, formation, req);
|
||||
// Create game
|
||||
this.game = new StarTowerGame(this, data, formation, req);
|
||||
|
||||
// Success
|
||||
return this.instance;
|
||||
return this.game;
|
||||
}
|
||||
|
||||
public StarTowerInstance giveUp() {
|
||||
public StarTowerGame giveUp() {
|
||||
// Cache instance
|
||||
var instance = this.instance;
|
||||
var instance = this.game;
|
||||
|
||||
if (instance != null) {
|
||||
// Set last build
|
||||
this.lastBuild = instance.getBuild();
|
||||
|
||||
// Clear instance
|
||||
this.instance = null;
|
||||
this.game = null;
|
||||
}
|
||||
|
||||
return instance;
|
||||
@@ -112,10 +122,27 @@ public class StarTowerManager extends PlayerManager implements GameDatabaseObjec
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO give rewards to player
|
||||
public PlayerChangeInfo deleteBuild(long buildId, PlayerChangeInfo changes) {
|
||||
// Create change info
|
||||
if (changes == null) {
|
||||
changes = new PlayerChangeInfo();
|
||||
}
|
||||
|
||||
// Get build
|
||||
var build = this.getBuilds().remove(buildId);
|
||||
|
||||
if (build != null) {
|
||||
build.delete();
|
||||
}
|
||||
|
||||
return changes;
|
||||
}
|
||||
|
||||
// Database
|
||||
|
||||
private void loadFromDatabase() {
|
||||
this.builds = new Int2ObjectOpenHashMap<>();
|
||||
this.builds = new Long2ObjectOpenHashMap<>();
|
||||
|
||||
Nebula.getGameDatabase().getObjects(StarTowerBuild.class, "playerUid", getPlayerUid()).forEach(build -> {
|
||||
this.builds.put(build.getUid(), build);
|
||||
|
||||
Reference in New Issue
Block a user