Handle record rename/delete/lock/favorite

This commit is contained in:
Melledy
2025-10-29 22:31:31 -07:00
parent 0f4f56a96b
commit 08520493ed
8 changed files with 217 additions and 20 deletions

View File

@@ -3,6 +3,7 @@ package emu.nebula.game.tower;
import dev.morphia.annotations.Entity; import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id; import dev.morphia.annotations.Id;
import dev.morphia.annotations.Indexed; import dev.morphia.annotations.Indexed;
import emu.nebula.Nebula;
import emu.nebula.data.GameData; import emu.nebula.data.GameData;
import emu.nebula.database.GameDatabaseObject; import emu.nebula.database.GameDatabaseObject;
import emu.nebula.proto.Public.ItemTpl; 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.uid = Snowflake.newUid();
this.playerUid = instance.getPlayer().getUid(); this.playerUid = instance.getPlayer().getUid();
this.name = ""; 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 // Proto
@@ -129,4 +149,10 @@ public class StarTowerBuild implements GameDatabaseObject {
return proto; return proto;
} }
// Database
public void delete() {
Nebula.getGameDatabase().delete(this);
}
} }

View File

@@ -26,7 +26,7 @@ import lombok.SneakyThrows;
@Getter @Getter
@Entity(useDiscriminator = false) @Entity(useDiscriminator = false)
public class StarTowerInstance { public class StarTowerGame {
private transient StarTowerManager manager; private transient StarTowerManager manager;
private transient StarTowerDef data; private transient StarTowerDef data;
@@ -64,11 +64,11 @@ public class StarTowerInstance {
private transient StarTowerBuild build; private transient StarTowerBuild build;
@Deprecated // Morphia only @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.manager = manager;
this.data = data; this.data = data;

View File

@@ -6,10 +6,11 @@ import emu.nebula.Nebula;
import emu.nebula.data.GameData; import emu.nebula.data.GameData;
import emu.nebula.database.GameDatabaseObject; import emu.nebula.database.GameDatabaseObject;
import emu.nebula.game.player.Player; import emu.nebula.game.player.Player;
import emu.nebula.game.player.PlayerChangeInfo;
import emu.nebula.game.player.PlayerManager; import emu.nebula.game.player.PlayerManager;
import emu.nebula.proto.StarTowerApply.StarTowerApplyReq; import emu.nebula.proto.StarTowerApply.StarTowerApplyReq;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import lombok.Getter; import lombok.Getter;
@Getter @Getter
@@ -18,8 +19,13 @@ public class StarTowerManager extends PlayerManager implements GameDatabaseObjec
@Id @Id
private int uid; private int uid;
private transient Int2ObjectMap<StarTowerBuild> builds; // TODO add tower talents here
private transient StarTowerInstance instance;
// Tower game instance
private transient StarTowerGame game;
// Tower builds
private transient Long2ObjectMap<StarTowerBuild> builds;
private transient StarTowerBuild lastBuild; private transient StarTowerBuild lastBuild;
@Deprecated // Morphia only @Deprecated // Morphia only
@@ -34,7 +40,7 @@ public class StarTowerManager extends PlayerManager implements GameDatabaseObjec
this.save(); this.save();
} }
public Int2ObjectMap<StarTowerBuild> getBuilds() { public Long2ObjectMap<StarTowerBuild> getBuilds() {
if (this.builds == null) { if (this.builds == null) {
this.loadFromDatabase(); this.loadFromDatabase();
} }
@@ -42,7 +48,11 @@ public class StarTowerManager extends PlayerManager implements GameDatabaseObjec
return builds; return builds;
} }
public StarTowerInstance apply(StarTowerApplyReq req) { public StarTowerBuild getBuildById(long id) {
return this.getBuilds().get(id);
}
public StarTowerGame apply(StarTowerApplyReq req) {
// Sanity checks // Sanity checks
var data = GameData.getStarTowerDataTable().get(req.getId()); var data = GameData.getStarTowerDataTable().get(req.getId());
if (data == null) { if (data == null) {
@@ -60,23 +70,23 @@ public class StarTowerManager extends PlayerManager implements GameDatabaseObjec
return null; return null;
} }
// Create instance // Create game
this.instance = new StarTowerInstance(this, data, formation, req); this.game = new StarTowerGame(this, data, formation, req);
// Success // Success
return this.instance; return this.game;
} }
public StarTowerInstance giveUp() { public StarTowerGame giveUp() {
// Cache instance // Cache instance
var instance = this.instance; var instance = this.game;
if (instance != null) { if (instance != null) {
// Set last build // Set last build
this.lastBuild = instance.getBuild(); this.lastBuild = instance.getBuild();
// Clear instance // Clear instance
this.instance = null; this.game = null;
} }
return instance; return instance;
@@ -112,10 +122,27 @@ public class StarTowerManager extends PlayerManager implements GameDatabaseObjec
return true; 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 // Database
private void loadFromDatabase() { private void loadFromDatabase() {
this.builds = new Int2ObjectOpenHashMap<>(); this.builds = new Long2ObjectOpenHashMap<>();
Nebula.getGameDatabase().getObjects(StarTowerBuild.class, "playerUid", getPlayerUid()).forEach(build -> { Nebula.getGameDatabase().getObjects(StarTowerBuild.class, "playerUid", getPlayerUid()).forEach(build -> {
this.builds.put(build.getUid(), build); this.builds.put(build.getUid(), build);

View File

@@ -0,0 +1,35 @@
package emu.nebula.server.handlers;
import emu.nebula.net.NetHandler;
import emu.nebula.net.NetMsgId;
import emu.nebula.proto.StarTowerBuildDelete.StarTowerBuildDeleteReq;
import emu.nebula.proto.StarTowerBuildDelete.StarTowerBuildDeleteResp;
import emu.nebula.net.HandlerId;
import emu.nebula.game.player.PlayerChangeInfo;
import emu.nebula.net.GameSession;
@HandlerId(NetMsgId.star_tower_build_delete_req)
public class HandlerStarTowerBuildDeleteReq extends NetHandler {
@Override
public byte[] handle(GameSession session, byte[] message) throws Exception {
// Parse request
var req = StarTowerBuildDeleteReq.parseFrom(message);
//
var changes = new PlayerChangeInfo();
// Delete
for (var id : req.getBuildIds()) {
session.getPlayer().getStarTowerManager().deleteBuild(id, changes);
}
// Build response
var rsp = StarTowerBuildDeleteResp.newInstance()
.setChange(changes.toProto());
// Encode packet
return this.encodeMsg(NetMsgId.star_tower_build_delete_succeed_ack, rsp);
}
}

View File

@@ -0,0 +1,30 @@
package emu.nebula.server.handlers;
import emu.nebula.net.NetHandler;
import emu.nebula.net.NetMsgId;
import emu.nebula.proto.StarTowerBuildLockUnlock.StarTowerBuildLockUnlockReq;
import emu.nebula.net.HandlerId;
import emu.nebula.net.GameSession;
@HandlerId(NetMsgId.star_tower_build_lock_unlock_req)
public class HandlerStarTowerBuildLockUnlockReq extends NetHandler {
@Override
public byte[] handle(GameSession session, byte[] message) throws Exception {
// Parse request
var req = StarTowerBuildLockUnlockReq.parseFrom(message);
// Get build
var build = session.getPlayer().getStarTowerManager().getBuildById(req.getBuildId());
if (build == null) {
return this.encodeMsg(NetMsgId.star_tower_build_lock_unlock_failed_ack);
}
build.setLock(req.getLock());
// Encode packet
return this.encodeMsg(NetMsgId.star_tower_build_lock_unlock_succeed_ack);
}
}

View File

@@ -0,0 +1,36 @@
package emu.nebula.server.handlers;
import emu.nebula.net.NetHandler;
import emu.nebula.net.NetMsgId;
import emu.nebula.proto.StarTowerBuildNameSet.StarTowerBuildNameSetReq;
import emu.nebula.net.HandlerId;
import emu.nebula.net.GameSession;
@HandlerId(NetMsgId.star_tower_build_name_set_req)
public class HandlerStarTowerBuildNameSetReq extends NetHandler {
@Override
public byte[] handle(GameSession session, byte[] message) throws Exception {
// Parse request
var req = StarTowerBuildNameSetReq.parseFrom(message);
// Sanity
if (req.getName() == null || req.getName().isEmpty()) {
return this.encodeMsg(NetMsgId.star_tower_build_name_set_failed_ack);
}
// Get build
var build = session.getPlayer().getStarTowerManager().getBuildById(req.getBuildId());
if (build == null) {
return this.encodeMsg(NetMsgId.star_tower_build_name_set_failed_ack);
}
// Set name
build.setName(req.getName());
// Encode packet
return this.encodeMsg(NetMsgId.star_tower_build_name_set_succeed_ack);
}
}

View File

@@ -0,0 +1,43 @@
package emu.nebula.server.handlers;
import emu.nebula.net.NetHandler;
import emu.nebula.net.NetMsgId;
import emu.nebula.proto.StarTowerBuildPreferenceSet.StarTowerBuildPreferenceSetReq;
import emu.nebula.net.HandlerId;
import emu.nebula.net.GameSession;
@HandlerId(NetMsgId.star_tower_build_preference_set_req)
public class HandlerStarTowerBuildPreferenceSetReq extends NetHandler {
@Override
public byte[] handle(GameSession session, byte[] message) throws Exception {
// Parse request
var req = StarTowerBuildPreferenceSetReq.parseFrom(message);
for (long id : req.getCheckInIds()) {
// Get build
var build = session.getPlayer().getStarTowerManager().getBuildById(id);
if (build == null) {
continue;
}
build.setPreference(true);
}
for (long id : req.getCheckOutIds()) {
// Get build
var build = session.getPlayer().getStarTowerManager().getBuildById(id);
if (build == null) {
continue;
}
build.setPreference(false);
}
// Encode packet
return this.encodeMsg(NetMsgId.star_tower_build_preference_set_succeed_ack);
}
}

View File

@@ -12,9 +12,9 @@ public class HandlerStarTowerInteractReq extends NetHandler {
@Override @Override
public byte[] handle(GameSession session, byte[] message) throws Exception { public byte[] handle(GameSession session, byte[] message) throws Exception {
// Get star tower instance // Get star tower instance
var instance = session.getPlayer().getStarTowerManager().getInstance(); var game = session.getPlayer().getStarTowerManager().getGame();
if (instance == null) { if (game == null) {
return this.encodeMsg(NetMsgId.star_tower_interact_failed_ack); return this.encodeMsg(NetMsgId.star_tower_interact_failed_ack);
} }
@@ -22,7 +22,7 @@ public class HandlerStarTowerInteractReq extends NetHandler {
var req = StarTowerInteractReq.parseFrom(message); var req = StarTowerInteractReq.parseFrom(message);
// Handle interaction // Handle interaction
var rsp = instance.handleInteract(req); var rsp = game.handleInteract(req);
// Template // Template
return this.encodeMsg(NetMsgId.star_tower_interact_succeed_ack, rsp); return this.encodeMsg(NetMsgId.star_tower_interact_succeed_ack, rsp);