character service singleton
Some checks reported errors
studiorailgun/Renderer/pipeline/head Something is wrong with the build of this commit

This commit is contained in:
austin 2025-05-10 16:34:52 -04:00
parent b320bc3e06
commit bc2ecadf64
8 changed files with 33 additions and 26 deletions

View File

@ -1705,6 +1705,7 @@ Move all character to database instead of macro data object
Major work on CharacterService Major work on CharacterService
Move character utils classe to macro data Move character utils classe to macro data
Move CharacterService to service package Move CharacterService to service package
Convert character service to singleton

View File

@ -85,6 +85,7 @@ import electrosphere.server.datacell.RealmManager;
import electrosphere.server.db.DatabaseController; import electrosphere.server.db.DatabaseController;
import electrosphere.server.entity.poseactor.PoseModel; import electrosphere.server.entity.poseactor.PoseModel;
import electrosphere.server.saves.Save; import electrosphere.server.saves.Save;
import electrosphere.server.service.CharacterService;
import electrosphere.server.service.StructureScanningService; import electrosphere.server.service.StructureScanningService;
import electrosphere.server.simulation.MacroSimulation; import electrosphere.server.simulation.MacroSimulation;
import electrosphere.server.simulation.MicroSimulation; import electrosphere.server.simulation.MicroSimulation;
@ -387,11 +388,10 @@ public class Globals {
public static ElementService elementService; public static ElementService elementService;
public static int openInventoriesCount = 0; public static int openInventoriesCount = 0;
//file service //services
public static FileWatcherService fileWatcherService; public static FileWatcherService fileWatcherService;
//structure scanning service
public static StructureScanningService structureScanningService; public static StructureScanningService structureScanningService;
public static CharacterService characterService;
//collision world data //collision world data
public static CollisionWorldData commonWorldData; public static CollisionWorldData commonWorldData;
@ -553,6 +553,7 @@ public class Globals {
Globals.mainThreadSignalService = (MainThreadSignalService)serviceManager.registerService(new MainThreadSignalService()); Globals.mainThreadSignalService = (MainThreadSignalService)serviceManager.registerService(new MainThreadSignalService());
Globals.fileWatcherService = (FileWatcherService)serviceManager.registerService(new FileWatcherService()); Globals.fileWatcherService = (FileWatcherService)serviceManager.registerService(new FileWatcherService());
Globals.structureScanningService = (StructureScanningService)serviceManager.registerService(new StructureScanningService()); Globals.structureScanningService = (StructureScanningService)serviceManager.registerService(new StructureScanningService());
Globals.characterService = (CharacterService)serviceManager.registerService(new CharacterService());
serviceManager.instantiate(); serviceManager.instantiate();
// //
//End service manager //End service manager

View File

@ -29,7 +29,6 @@ import electrosphere.server.datacell.ServerWorldData;
import electrosphere.server.entity.serialization.ContentSerialization; import electrosphere.server.entity.serialization.ContentSerialization;
import electrosphere.server.macro.character.Character; import electrosphere.server.macro.character.Character;
import electrosphere.server.macro.character.PlayerCharacterCreation; import electrosphere.server.macro.character.PlayerCharacterCreation;
import electrosphere.server.service.CharacterService;
import electrosphere.util.Utilities; import electrosphere.util.Utilities;
/** /**
@ -47,7 +46,7 @@ public class CharacterProtocol implements ServerProtocolTemplate<CharacterMessag
switch(message.getMessageSubtype()){ switch(message.getMessageSubtype()){
case REQUESTCHARACTERLIST: { case REQUESTCHARACTERLIST: {
Gson gson = new Gson(); Gson gson = new Gson();
List<CharacterDescriptionDTO>characters = CharacterService.getCharacters(connectionHandler.getPlayer().getDBID()).stream().map((Character chara) -> { List<CharacterDescriptionDTO>characters = Globals.characterService.getCharacters(connectionHandler.getPlayer().getDBID()).stream().map((Character chara) -> {
CharacterDescriptionDTO dtoObj = new CharacterDescriptionDTO(); CharacterDescriptionDTO dtoObj = new CharacterDescriptionDTO();
dtoObj.setId(chara.getId() + ""); dtoObj.setId(chara.getId() + "");
dtoObj.setTemplate(chara.getCreatureTemplate()); dtoObj.setTemplate(chara.getCreatureTemplate());
@ -70,7 +69,7 @@ public class CharacterProtocol implements ServerProtocolTemplate<CharacterMessag
case REQUESTCREATECHARACTER: { case REQUESTCREATECHARACTER: {
CreatureTemplate template = Utilities.deserialize(message.getdata(), CreatureTemplate.class); CreatureTemplate template = Utilities.deserialize(message.getdata(), CreatureTemplate.class);
if(template != null){ if(template != null){
Character charaData = CharacterService.createCharacter(template, connectionHandler.getPlayer().getDBID()); Character charaData = Globals.characterService.createCharacter(template, connectionHandler.getPlayer().getDBID());
charaData.setPos(Globals.realmManager.first().getSpawnPoint()); charaData.setPos(Globals.realmManager.first().getSpawnPoint());
connectionHandler.setCreatureTemplate(Utilities.deserialize(message.getdata(), CreatureTemplate.class)); connectionHandler.setCreatureTemplate(Utilities.deserialize(message.getdata(), CreatureTemplate.class));
connectionHandler.setCharacterId(charaData.getId()); connectionHandler.setCharacterId(charaData.getId());

View File

@ -13,7 +13,6 @@ import electrosphere.server.datacell.ServerDataCell;
import electrosphere.server.entity.serialization.ContentSerialization; import electrosphere.server.entity.serialization.ContentSerialization;
import electrosphere.server.macro.MacroData; import electrosphere.server.macro.MacroData;
import electrosphere.server.saves.SaveUtils; import electrosphere.server.saves.SaveUtils;
import electrosphere.server.service.CharacterService;
import electrosphere.util.FileUtils; import electrosphere.util.FileUtils;
import electrosphere.util.math.HashUtils; import electrosphere.util.math.HashUtils;
import electrosphere.server.macro.character.Character; import electrosphere.server.macro.character.Character;
@ -106,7 +105,7 @@ public class ServerContentManager {
//store all character entities in database //store all character entities in database
for(Entity entity : entities){ for(Entity entity : entities){
if(ServerCharacterData.hasServerCharacterDataTree(entity)){ if(ServerCharacterData.hasServerCharacterDataTree(entity)){
CharacterService.saveCharacter(entity); Globals.characterService.saveCharacter(entity);
} }
} }
} }

View File

@ -17,7 +17,6 @@ import electrosphere.server.macro.spatial.MacroAreaObject;
import electrosphere.server.macro.structure.Structure; import electrosphere.server.macro.structure.Structure;
import electrosphere.server.macro.symbolism.Symbol; import electrosphere.server.macro.symbolism.Symbol;
import electrosphere.server.macro.town.Town; import electrosphere.server.macro.town.Town;
import electrosphere.server.service.CharacterService;
import electrosphere.util.FileUtils; import electrosphere.util.FileUtils;
import java.util.Random; import java.util.Random;
@ -246,7 +245,7 @@ public class MacroData {
LoggerInterface.loggerEngine.WARNING(race.getName()); LoggerInterface.loggerEngine.WARNING(race.getName());
int numCharsOfRace = 0; int numCharsOfRace = 0;
//n*m complexity - yikes! - as long as we're not making a million chars at start this should be _ok_ //n*m complexity - yikes! - as long as we're not making a million chars at start this should be _ok_
for(Character chara : CharacterService.getAllCharacters()){ for(Character chara : Globals.characterService.getAllCharacters()){
if(chara.containsKey(CharacterDataStrings.RACE)){ if(chara.containsKey(CharacterDataStrings.RACE)){
if(Race.getRace(chara).equals(race)){ if(Race.getRace(chara).equals(race)){
numCharsOfRace++; numCharsOfRace++;
@ -267,7 +266,7 @@ public class MacroData {
*/ */
public List<Character> getCharacters(Vector3i worldPos){ public List<Character> getCharacters(Vector3i worldPos){
List<Character> rVal = new LinkedList<Character>(); List<Character> rVal = new LinkedList<Character>();
for(Character character : CharacterService.getAllCharacters()){ for(Character character : Globals.characterService.getAllCharacters()){
if(ServerWorldData.convertRealToChunkSpace(character.getPos()).equals(worldPos.x, worldPos.y, worldPos.z)){ if(ServerWorldData.convertRealToChunkSpace(character.getPos()).equals(worldPos.x, worldPos.y, worldPos.z)){
rVal.add(character); rVal.add(character);
} }
@ -291,7 +290,7 @@ public class MacroData {
* @return The character if it exists, null otherwise * @return The character if it exists, null otherwise
*/ */
public Character getCharacter(int id){ public Character getCharacter(int id){
for(Character character : CharacterService.getAllCharacters()){ for(Character character : Globals.characterService.getAllCharacters()){
if(character.getId() == id){ if(character.getId() == id){
return character; return character;
} }

View File

@ -2,13 +2,13 @@ package electrosphere.server.macro.character;
import org.joml.Vector3d; import org.joml.Vector3d;
import electrosphere.engine.Globals;
import electrosphere.server.datacell.Realm; import electrosphere.server.datacell.Realm;
import electrosphere.server.macro.character.data.CharacterDataStrings; import electrosphere.server.macro.character.data.CharacterDataStrings;
import electrosphere.server.macro.character.diety.Diety; import electrosphere.server.macro.character.diety.Diety;
import electrosphere.server.macro.race.Race; import electrosphere.server.macro.race.Race;
import electrosphere.server.macro.structure.Structure; import electrosphere.server.macro.structure.Structure;
import electrosphere.server.macro.town.Town; import electrosphere.server.macro.town.Town;
import electrosphere.server.service.CharacterService;
/** /**
* Utility functions for dealing with characters * Utility functions for dealing with characters
@ -55,7 +55,7 @@ public class CharacterUtils {
* @return The character * @return The character
*/ */
public static Character spawnCharacter(Realm realm, Vector3d position){ public static Character spawnCharacter(Realm realm, Vector3d position){
Character rVal = CharacterService.createCharacter(null, 0); Character rVal = Globals.characterService.createCharacter(null, 0);
rVal.setPos(position); rVal.setPos(position);
Race.setRace(rVal, Race.create("human", "human")); Race.setRace(rVal, Race.create("human", "human"));
realm.getDataCellManager().evaluateMacroObject(rVal); realm.getDataCellManager().evaluateMacroObject(rVal);

View File

@ -15,7 +15,6 @@ import electrosphere.net.server.player.Player;
import electrosphere.net.server.protocol.CharacterProtocol; import electrosphere.net.server.protocol.CharacterProtocol;
import electrosphere.server.datacell.Realm; import electrosphere.server.datacell.Realm;
import electrosphere.server.datacell.ServerWorldData; import electrosphere.server.datacell.ServerWorldData;
import electrosphere.server.service.CharacterService;
/** /**
* Deals with spawning player characters * Deals with spawning player characters
@ -32,7 +31,7 @@ public class PlayerCharacterCreation {
// //
//get template //get template
Character charaData = CharacterService.getCharacter(connectionHandler.getPlayer().getDBID(), connectionHandler.getCharacterId()); Character charaData = Globals.characterService.getCharacter(connectionHandler.getPlayer().getDBID(), connectionHandler.getCharacterId());
CreatureTemplate template = charaData.getCreatureTemplate(); CreatureTemplate template = charaData.getCreatureTemplate();
if(connectionHandler.getCharacterId() == CharacterProtocol.SPAWN_EXISTING_TEMPLATE){ if(connectionHandler.getCharacterId() == CharacterProtocol.SPAWN_EXISTING_TEMPLATE){
template = connectionHandler.getCurrentCreatureTemplate(); template = connectionHandler.getCurrentCreatureTemplate();
@ -91,7 +90,7 @@ public class PlayerCharacterCreation {
*/ */
static void addPlayerServerBTrees(Entity entity, ServerConnectionHandler serverConnectionHandler){ static void addPlayerServerBTrees(Entity entity, ServerConnectionHandler serverConnectionHandler){
ServerPlayerViewDirTree.attachServerPlayerViewDirTree(entity); ServerPlayerViewDirTree.attachServerPlayerViewDirTree(entity);
ServerCharacterData.attachServerCharacterData(entity, CharacterService.getCharacter(serverConnectionHandler.getPlayer().getDBID(), serverConnectionHandler.getCharacterId())); ServerCharacterData.attachServerCharacterData(entity, Globals.characterService.getCharacter(serverConnectionHandler.getPlayer().getDBID(), serverConnectionHandler.getCharacterId()));
} }
/** /**
@ -101,7 +100,7 @@ public class PlayerCharacterCreation {
* @return The spawn point for the player * @return The spawn point for the player
*/ */
public static Vector3d solveSpawnPoint(Realm realm, ServerConnectionHandler connectionHandler){ public static Vector3d solveSpawnPoint(Realm realm, ServerConnectionHandler connectionHandler){
Vector3d spawnPoint = CharacterService.getCharacter(connectionHandler.getPlayer().getDBID(), connectionHandler.getCharacterId()).getPos(); Vector3d spawnPoint = Globals.characterService.getCharacter(connectionHandler.getPlayer().getDBID(), connectionHandler.getCharacterId()).getPos();
if(spawnPoint == null){ if(spawnPoint == null){
spawnPoint = realm.getSpawnPoint(); spawnPoint = realm.getSpawnPoint();
} }

View File

@ -9,6 +9,8 @@ import java.util.concurrent.locks.ReentrantLock;
import com.google.gson.Gson; import com.google.gson.Gson;
import electrosphere.engine.Globals; import electrosphere.engine.Globals;
import electrosphere.engine.signal.Signal.SignalType;
import electrosphere.engine.signal.SignalServiceImpl;
import electrosphere.entity.Entity; import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils; import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.server.ServerCharacterData; import electrosphere.entity.state.server.ServerCharacterData;
@ -23,24 +25,31 @@ import electrosphere.util.SerializationUtils;
/** /**
* Service for interacting with macro-level characters * Service for interacting with macro-level characters
*/ */
public class CharacterService { public class CharacterService extends SignalServiceImpl {
/** /**
* Map that stores the characters currently loaded into memory * Map that stores the characters currently loaded into memory
*/ */
static Map<Integer,Character> loadedCharacterMap = new HashMap<Integer,Character>(); Map<Integer,Character> loadedCharacterMap = new HashMap<Integer,Character>();
/** /**
* Lock for thread-safe-ing the service * Lock for thread-safe-ing the service
*/ */
static ReentrantLock lock = new ReentrantLock(); ReentrantLock lock = new ReentrantLock();
/**
* Constructor
*/
public CharacterService(){
super("CharacterService", new SignalType[]{});
}
/** /**
* Creates a character in the database * Creates a character in the database
* @param template The creature template for the character * @param template The creature template for the character
* @param playerId The player's id * @param playerId The player's id
*/ */
public static Character createCharacter(CreatureTemplate template, int playerId){ public Character createCharacter(CreatureTemplate template, int playerId){
lock.lock(); lock.lock();
Character toStore = new Character(template); Character toStore = new Character(template);
DatabaseResult result = Globals.dbController.executePreparedQuery( DatabaseResult result = Globals.dbController.executePreparedQuery(
@ -65,7 +74,7 @@ public class CharacterService {
* @param characterId The character's id * @param characterId The character's id
* @return The character if it exists, null otherwise * @return The character if it exists, null otherwise
*/ */
public static Character getCharacter(int playerId, int characterId){ public Character getCharacter(int playerId, int characterId){
lock.lock(); lock.lock();
if(loadedCharacterMap.containsKey(characterId)){ if(loadedCharacterMap.containsKey(characterId)){
Character rVal = loadedCharacterMap.get(characterId); Character rVal = loadedCharacterMap.get(characterId);
@ -93,7 +102,7 @@ public class CharacterService {
* @param playerId The player's id * @param playerId The player's id
* @return The list of characters that player has * @return The list of characters that player has
*/ */
public static List<Character> getCharacters(int playerId){ public List<Character> getCharacters(int playerId){
lock.lock(); lock.lock();
DatabaseResult result = Globals.dbController.executePreparedQuery("SELECT id, dataVal FROM charaData WHERE playerId=?;",playerId); DatabaseResult result = Globals.dbController.executePreparedQuery("SELECT id, dataVal FROM charaData WHERE playerId=?;",playerId);
List<Character> rVal = new LinkedList<Character>(); List<Character> rVal = new LinkedList<Character>();
@ -119,7 +128,7 @@ public class CharacterService {
* Saves a character from an entity * Saves a character from an entity
* @param characterEntity The entity * @param characterEntity The entity
*/ */
public static void saveCharacter(Entity characterEntity){ public void saveCharacter(Entity characterEntity){
lock.lock(); lock.lock();
if(!ServerCharacterData.hasServerCharacterDataTree(characterEntity)){ if(!ServerCharacterData.hasServerCharacterDataTree(characterEntity)){
throw new Error("Trying to save entity hat does not contain character data!"); throw new Error("Trying to save entity hat does not contain character data!");
@ -145,7 +154,7 @@ public class CharacterService {
* Gets all characters * Gets all characters
* @return The list of all characters * @return The list of all characters
*/ */
public static List<Character> getAllCharacters(){ public List<Character> getAllCharacters(){
lock.lock(); lock.lock();
DatabaseResult result = Globals.dbController.executePreparedQuery("SELECT id, dataVal FROM charaData"); DatabaseResult result = Globals.dbController.executePreparedQuery("SELECT id, dataVal FROM charaData");
List<Character> rVal = new LinkedList<Character>(); List<Character> rVal = new LinkedList<Character>();