major character data reorganization
This commit is contained in:
parent
f07ea76129
commit
3355e65a8c
@ -9,13 +9,6 @@ INSERT INTO mainTable (propName, propValue) VALUES ("ver","1");
|
||||
|
||||
|
||||
-- CHARACTERS
|
||||
-- positions
|
||||
CREATE TABLE charaWorldPositions (playerId INTEGER PRIMARY KEY, id INTEGER, posX INTEGER, posY INTEGER);
|
||||
CREATE INDEX charaWorldPositionsIDIndex ON charaWorldPositions (id);
|
||||
CREATE INDEX charaWorldPositionsPosIndex ON charaWorldPositions (posX, posY);
|
||||
|
||||
-- real positions of characters (ie player's characters)
|
||||
CREATE TABLE charaRealPos (id INTEGER PRIMARY KEY NOT NULL, x REAL NOT NULL, y REAL NOT NULL, z REAL NOT NULL);
|
||||
|
||||
-- data
|
||||
CREATE TABLE charaData (id INTEGER PRIMARY KEY AUTOINCREMENT, playerId INTEGER, dataVal VARCHAR);
|
||||
|
||||
@ -1701,6 +1701,8 @@ Debug ability to send characters off map
|
||||
More windows for debugging work
|
||||
GriddedDataCellManager simulates cells that contain creatures
|
||||
Characters can pursue goal to leave sim range and the real entity is unloaded when they leave range
|
||||
Move all character to database instead of macro data object
|
||||
Major work on CharacterService
|
||||
|
||||
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@ import electrosphere.entity.types.creature.CreatureTemplate;
|
||||
/**
|
||||
* Describes a character
|
||||
*/
|
||||
public class CharacterDescription {
|
||||
public class CharacterDescriptionDTO {
|
||||
|
||||
/**
|
||||
* The id of the character
|
||||
@ -10,13 +10,13 @@ public class ClientCharacterListDTO {
|
||||
/**
|
||||
* The list of characters stored in the DTO
|
||||
*/
|
||||
List<CharacterDescription> characters;
|
||||
List<CharacterDescriptionDTO> characters;
|
||||
|
||||
/**
|
||||
* Gets the list of characters
|
||||
* @return The list of characters
|
||||
*/
|
||||
public List<CharacterDescription> getCharacters() {
|
||||
public List<CharacterDescriptionDTO> getCharacters() {
|
||||
return characters;
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ public class ClientCharacterListDTO {
|
||||
* Sets the list of characters
|
||||
* @param characters The list of characters
|
||||
*/
|
||||
public void setCharacters(List<CharacterDescription> characters) {
|
||||
public void setCharacters(List<CharacterDescriptionDTO> characters) {
|
||||
this.characters = characters;
|
||||
}
|
||||
|
||||
|
||||
@ -93,7 +93,7 @@ public class ImGuiAI {
|
||||
Entity entity = ai.getParent();
|
||||
ServerCharacterData serverCharacterData = ServerCharacterData.getServerCharacterData(entity);
|
||||
MacroData macroData = Globals.realmManager.getEntityRealm(entity).getServerContentManager().getMacroData();
|
||||
Character character = macroData.getCharacter(serverCharacterData.getCharacterId());
|
||||
Character character = macroData.getCharacter(serverCharacterData.getCharacterData().getId());
|
||||
CharacterGoal.setCharacterGoal(character, new CharacterGoal(CharacterGoalType.LEAVE_SIM_RANGE));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package electrosphere.client.ui.menu.mainmenu;
|
||||
|
||||
import electrosphere.client.entity.character.CharacterDescription;
|
||||
import electrosphere.client.entity.character.CharacterDescriptionDTO;
|
||||
import electrosphere.client.ui.components.CharacterCustomizer;
|
||||
import electrosphere.client.ui.menu.WindowUtils;
|
||||
import electrosphere.engine.Globals;
|
||||
@ -38,7 +38,7 @@ public class MenuCharacterCreation {
|
||||
//the list of characters
|
||||
Div selectContainer = Div.createCol();
|
||||
if(Globals.clientCharacterManager.getCharacterList() != null){
|
||||
for(CharacterDescription description : Globals.clientCharacterManager.getCharacterList().getCharacters()){
|
||||
for(CharacterDescriptionDTO description : Globals.clientCharacterManager.getCharacterList().getCharacters()){
|
||||
String buttonTitle = "Character " + description.getId();
|
||||
Div charNameContainer = Div.createRow(Button.createButton(buttonTitle, () -> {
|
||||
Globals.clientConnection.queueOutgoingMessage(CharacterMessage.constructRequestSpawnCharacterMessage(description.getId()));
|
||||
|
||||
@ -2,6 +2,7 @@ package electrosphere.entity.state.server;
|
||||
|
||||
import electrosphere.entity.Entity;
|
||||
import electrosphere.entity.EntityDataStrings;
|
||||
import electrosphere.server.macro.character.Character;
|
||||
|
||||
/**
|
||||
* Stores data that associated an entity to a character in the character database
|
||||
@ -9,9 +10,9 @@ import electrosphere.entity.EntityDataStrings;
|
||||
public class ServerCharacterData {
|
||||
|
||||
/**
|
||||
* The character id
|
||||
* The character data
|
||||
*/
|
||||
private int characterId;
|
||||
private Character charaData;
|
||||
|
||||
/**
|
||||
* The associated entity
|
||||
@ -23,17 +24,18 @@ public class ServerCharacterData {
|
||||
* @param parent
|
||||
* @param characterId
|
||||
*/
|
||||
private ServerCharacterData(Entity parent, int characterId){
|
||||
private ServerCharacterData(Entity parent, Character charaData){
|
||||
this.parent = parent;
|
||||
this.characterId = characterId;
|
||||
this.charaData = charaData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches a ServerCharacterData to a given entity
|
||||
* @param entity The entity to add to
|
||||
* @param charaData The character data
|
||||
*/
|
||||
public static void attachServerCharacterData(Entity entity, int characterId){
|
||||
ServerCharacterData tree = new ServerCharacterData(entity, characterId);
|
||||
public static void attachServerCharacterData(Entity entity, Character charaData){
|
||||
ServerCharacterData tree = new ServerCharacterData(entity, charaData);
|
||||
entity.putData(EntityDataStrings.TREE_SERVERCHARACTERDATA, tree);
|
||||
}
|
||||
|
||||
@ -59,8 +61,8 @@ public class ServerCharacterData {
|
||||
* Gets the associated character id for this entity
|
||||
* @return The id
|
||||
*/
|
||||
public int getCharacterId() {
|
||||
return characterId;
|
||||
public Character getCharacterData() {
|
||||
return charaData;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -21,7 +21,7 @@ public class CreatureTemplate {
|
||||
/**
|
||||
* The attribute map for visual variants
|
||||
*/
|
||||
Map<String,CreatureTemplateAttributeValue> attributeMap = new HashMap<String,CreatureTemplateAttributeValue>();
|
||||
private Map<String,CreatureTemplateAttributeValue> attributeMap = new HashMap<String,CreatureTemplateAttributeValue>();
|
||||
|
||||
/**
|
||||
* The equip data for the creature
|
||||
|
||||
@ -2,12 +2,13 @@ package electrosphere.net.server.protocol;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.joml.Vector3d;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import electrosphere.client.entity.character.CharacterDescription;
|
||||
import electrosphere.client.entity.character.CharacterDescriptionDTO;
|
||||
import electrosphere.client.entity.character.ClientCharacterListDTO;
|
||||
import electrosphere.engine.Globals;
|
||||
import electrosphere.engine.loadingthreads.LoadingUtils;
|
||||
@ -28,6 +29,7 @@ import electrosphere.server.character.PlayerCharacterCreation;
|
||||
import electrosphere.server.datacell.Realm;
|
||||
import electrosphere.server.datacell.ServerWorldData;
|
||||
import electrosphere.server.entity.serialization.ContentSerialization;
|
||||
import electrosphere.server.macro.character.Character;
|
||||
import electrosphere.util.Utilities;
|
||||
|
||||
/**
|
||||
@ -45,7 +47,12 @@ public class CharacterProtocol implements ServerProtocolTemplate<CharacterMessag
|
||||
switch(message.getMessageSubtype()){
|
||||
case REQUESTCHARACTERLIST: {
|
||||
Gson gson = new Gson();
|
||||
List<CharacterDescription> characters = CharacterService.getCharacters(connectionHandler.getPlayer().getDBID());
|
||||
List<CharacterDescriptionDTO>characters = CharacterService.getCharacters(connectionHandler.getPlayer().getDBID()).stream().map((Character chara) -> {
|
||||
CharacterDescriptionDTO dtoObj = new CharacterDescriptionDTO();
|
||||
dtoObj.setId(chara.getId() + "");
|
||||
dtoObj.setTemplate(chara.getCreatureTemplate());
|
||||
return dtoObj;
|
||||
}).collect(Collectors.toList());
|
||||
ClientCharacterListDTO dto = new ClientCharacterListDTO();
|
||||
dto.setCharacters(characters);
|
||||
connectionHandler.addMessagetoOutgoingQueue(CharacterMessage.constructResponseCharacterListMessage(gson.toJson(dto)));
|
||||
@ -63,8 +70,10 @@ public class CharacterProtocol implements ServerProtocolTemplate<CharacterMessag
|
||||
case REQUESTCREATECHARACTER: {
|
||||
CreatureTemplate template = Utilities.deserialize(message.getdata(), CreatureTemplate.class);
|
||||
if(template != null){
|
||||
CharacterService.createCharacter(template, connectionHandler.getPlayer().getDBID());
|
||||
Character charaData = CharacterService.createCharacter(template, connectionHandler.getPlayer().getDBID());
|
||||
charaData.setPos(Globals.realmManager.first().getSpawnPoint());
|
||||
connectionHandler.setCreatureTemplate(Utilities.deserialize(message.getdata(), CreatureTemplate.class));
|
||||
connectionHandler.setCharacterId(charaData.getId());
|
||||
connectionHandler.addMessagetoOutgoingQueue(CharacterMessage.constructResponseCreateCharacterSuccessMessage());
|
||||
} else {
|
||||
connectionHandler.addMessagetoOutgoingQueue(CharacterMessage.constructResponseCreateCharacterFailureMessage());
|
||||
@ -96,7 +105,9 @@ public class CharacterProtocol implements ServerProtocolTemplate<CharacterMessag
|
||||
* @return THe player's entity
|
||||
*/
|
||||
static Entity spawnEntityForClient(ServerConnectionHandler connectionHandler, int id){
|
||||
connectionHandler.setCharacterId(id);
|
||||
if(id != SPAWN_EXISTING_TEMPLATE){
|
||||
connectionHandler.setCharacterId(id);
|
||||
}
|
||||
Entity rVal = PlayerCharacterCreation.spawnPlayerCharacter(connectionHandler);
|
||||
Realm realm = Globals.playerManager.getPlayerRealm(connectionHandler.getPlayer());
|
||||
Vector3d spawnPoint = PlayerCharacterCreation.solveSpawnPoint(realm, connectionHandler);
|
||||
|
||||
@ -1,12 +1,9 @@
|
||||
package electrosphere.server.ai.nodes.checks.macro;
|
||||
|
||||
import electrosphere.engine.Globals;
|
||||
import electrosphere.entity.Entity;
|
||||
import electrosphere.entity.state.server.ServerCharacterData;
|
||||
import electrosphere.server.ai.blackboard.Blackboard;
|
||||
import electrosphere.server.ai.nodes.AITreeNode;
|
||||
import electrosphere.server.datacell.Realm;
|
||||
import electrosphere.server.macro.MacroData;
|
||||
import electrosphere.server.macro.character.Character;
|
||||
import electrosphere.server.macro.character.CharacterUtils;
|
||||
import electrosphere.server.macro.structure.Structure;
|
||||
@ -18,10 +15,8 @@ public class HasShelter implements AITreeNode {
|
||||
|
||||
@Override
|
||||
public AITreeNodeResult evaluate(Entity entity, Blackboard blackboard) {
|
||||
Realm entityRealm = Globals.realmManager.getEntityRealm(entity);
|
||||
MacroData macroData = entityRealm.getServerContentManager().getMacroData();
|
||||
ServerCharacterData serverCharacterData = ServerCharacterData.getServerCharacterData(entity);
|
||||
Character character = macroData.getCharacter(serverCharacterData.getCharacterId());
|
||||
Character character = serverCharacterData.getCharacterData();
|
||||
if(character == null){
|
||||
throw new Error("Character is null");
|
||||
}
|
||||
|
||||
@ -2,7 +2,6 @@ package electrosphere.server.ai.nodes.macro;
|
||||
|
||||
import org.joml.Vector3d;
|
||||
|
||||
import electrosphere.engine.Globals;
|
||||
import electrosphere.entity.Entity;
|
||||
import electrosphere.entity.EntityUtils;
|
||||
import electrosphere.entity.state.server.ServerCharacterData;
|
||||
@ -10,7 +9,6 @@ import electrosphere.server.ai.blackboard.Blackboard;
|
||||
import electrosphere.server.ai.blackboard.BlackboardKeys;
|
||||
import electrosphere.server.ai.nodes.AITreeNode;
|
||||
import electrosphere.server.macro.character.Character;
|
||||
import electrosphere.server.macro.MacroData;
|
||||
import electrosphere.server.macro.character.goal.CharacterGoal;
|
||||
import electrosphere.server.macro.character.goal.CharacterGoal.CharacterGoalType;
|
||||
|
||||
@ -48,8 +46,7 @@ public class MacroCharacterGoalNode implements AITreeNode {
|
||||
return AITreeNodeResult.FAILURE;
|
||||
}
|
||||
ServerCharacterData serverCharacterData = ServerCharacterData.getServerCharacterData(entity);
|
||||
MacroData macroData = Globals.realmManager.getEntityRealm(entity).getServerContentManager().getMacroData();
|
||||
Character character = macroData.getCharacter(serverCharacterData.getCharacterId());
|
||||
Character character = serverCharacterData.getCharacterData();
|
||||
CharacterGoal goal = CharacterGoal.getCharacterGoal(character);
|
||||
if(goal == null){
|
||||
return AITreeNodeResult.FAILURE;
|
||||
|
||||
@ -1,24 +1,23 @@
|
||||
package electrosphere.server.character;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.joml.Vector3d;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import electrosphere.client.entity.character.CharacterDescription;
|
||||
import electrosphere.engine.Globals;
|
||||
import electrosphere.entity.Entity;
|
||||
import electrosphere.entity.EntityUtils;
|
||||
import electrosphere.entity.state.server.ServerCharacterData;
|
||||
import electrosphere.entity.state.server.ServerPlayerViewDirTree;
|
||||
import electrosphere.entity.types.creature.CreatureTemplate;
|
||||
import electrosphere.entity.types.creature.CreatureUtils;
|
||||
import electrosphere.logger.LoggerInterface;
|
||||
import electrosphere.server.db.DatabaseResult;
|
||||
import electrosphere.server.db.DatabaseResultRow;
|
||||
import electrosphere.server.entity.serialization.ContentSerialization;
|
||||
import electrosphere.server.entity.serialization.EntitySerialization;
|
||||
import electrosphere.server.macro.character.Character;
|
||||
import electrosphere.util.SerializationUtils;
|
||||
|
||||
/**
|
||||
@ -26,36 +25,67 @@ import electrosphere.util.SerializationUtils;
|
||||
*/
|
||||
public class CharacterService {
|
||||
|
||||
/**
|
||||
* Map that stores the characters currently loaded into memory
|
||||
*/
|
||||
static Map<Integer,Character> loadedCharacterMap = new HashMap<Integer,Character>();
|
||||
|
||||
/**
|
||||
* Lock for thread-safe-ing the service
|
||||
*/
|
||||
static ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
/**
|
||||
* Creates a character in the database
|
||||
* @param template The creature template for the character
|
||||
* @param playerId The player's id
|
||||
*/
|
||||
public static void createCharacter(CreatureTemplate template, int playerId){
|
||||
Globals.dbController.executePreparedStatement(
|
||||
"INSERT INTO charaData (playerId,dataVal) VALUES (?,?);",
|
||||
public static Character createCharacter(CreatureTemplate template, int playerId){
|
||||
lock.lock();
|
||||
Character toStore = new Character(template);
|
||||
DatabaseResult result = Globals.dbController.executePreparedQuery(
|
||||
"INSERT INTO charaData (playerId,dataVal) VALUES (?,?) RETURNING id;",
|
||||
playerId,
|
||||
new Gson().toJson(template)
|
||||
new Gson().toJson(toStore)
|
||||
);
|
||||
if(!result.hasResult()){
|
||||
throw new Error("Failed to insert character!");
|
||||
}
|
||||
for(DatabaseResultRow row : result){
|
||||
toStore.setId(row.getAsInteger("id"));
|
||||
}
|
||||
loadedCharacterMap.put(toStore.getId(),toStore);
|
||||
lock.unlock();
|
||||
return toStore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the template for a character
|
||||
* Gets the character
|
||||
* @param playerId The player's id
|
||||
* @param characterId The character's id
|
||||
* @return The template if it exists, null otherwise
|
||||
* @return The character if it exists, null otherwise
|
||||
*/
|
||||
public static CreatureTemplate getTemplate(int playerId, int characterId){
|
||||
DatabaseResult result = Globals.dbController.executePreparedQuery("SELECT id, dataVal FROM charaData WHERE playerId=? AND id=?;",playerId, characterId);
|
||||
if(result.hasResult()){
|
||||
//if we get a valid response from the database, check that it actually matches hashes
|
||||
for(DatabaseResultRow row : result){
|
||||
CreatureTemplate template = SerializationUtils.deserialize(row.getAsString("dataVal"),CreatureTemplate.class);
|
||||
return template;
|
||||
}
|
||||
public static Character getCharacter(int playerId, int characterId){
|
||||
lock.lock();
|
||||
if(loadedCharacterMap.containsKey(characterId)){
|
||||
Character rVal = loadedCharacterMap.get(characterId);
|
||||
lock.unlock();
|
||||
return rVal;
|
||||
}
|
||||
LoggerInterface.loggerDB.WARNING("Failed to locate creature template for playerId=" + playerId + " characterId=" + characterId);
|
||||
return null;
|
||||
Character charData = null;
|
||||
DatabaseResult result = Globals.dbController.executePreparedQuery("SELECT id, dataVal FROM charaData WHERE playerId=? AND id=?;",playerId, characterId);
|
||||
if(!result.hasResult()){
|
||||
LoggerInterface.loggerDB.WARNING("Failed to locate creature template for playerId=" + playerId + " characterId=" + characterId);
|
||||
lock.unlock();
|
||||
return null;
|
||||
}
|
||||
for(DatabaseResultRow row : result){
|
||||
charData = SerializationUtils.deserialize(row.getAsString("dataVal"),Character.class);
|
||||
charData.setId(row.getAsInteger("id"));
|
||||
}
|
||||
loadedCharacterMap.put(charData.getId(),charData);
|
||||
lock.unlock();
|
||||
return charData;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -63,56 +93,25 @@ public class CharacterService {
|
||||
* @param playerId The player's id
|
||||
* @return The list of characters that player has
|
||||
*/
|
||||
public static List<CharacterDescription> getCharacters(int playerId){
|
||||
public static List<Character> getCharacters(int playerId){
|
||||
lock.lock();
|
||||
DatabaseResult result = Globals.dbController.executePreparedQuery("SELECT id, dataVal FROM charaData WHERE playerId=?;",playerId);
|
||||
List<CharacterDescription> rVal = new LinkedList<CharacterDescription>();
|
||||
List<Character> rVal = new LinkedList<Character>();
|
||||
if(result.hasResult()){
|
||||
//if we get a valid response from the database, check that it actually matches hashes
|
||||
for(DatabaseResultRow row : result){
|
||||
CharacterDescription description = new CharacterDescription();
|
||||
CreatureTemplate template = SerializationUtils.deserialize(row.getAsString("dataVal"),CreatureTemplate.class);
|
||||
description.setTemplate(template);
|
||||
description.setId(row.getAsInteger("id") + "");
|
||||
rVal.add(description);
|
||||
}
|
||||
}
|
||||
return rVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the position of a character
|
||||
* @param characterId The character's Id
|
||||
* @return The position if it is stored in the DB, null otherwise
|
||||
*/
|
||||
public static Vector3d getCharacterPosition(int characterId){
|
||||
DatabaseResult result = Globals.dbController.executePreparedQuery("SELECT x, y, z FROM charaRealPos WHERE id=?;",characterId);
|
||||
if(result.hasResult()){
|
||||
//if we get a valid response from the database, return the position
|
||||
for(DatabaseResultRow row : result){
|
||||
double x = row.getAsDouble("x");
|
||||
double y = row.getAsDouble("y");
|
||||
double z = row.getAsDouble("z");
|
||||
return new Vector3d(x,y,z);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the serialized entity data for the given character
|
||||
* @param characterId The character's ID
|
||||
* @return the serialziation if it exists, null otherwise
|
||||
*/
|
||||
public static EntitySerialization getSerialization(int characterId){
|
||||
EntitySerialization rVal = null;
|
||||
DatabaseResult result = Globals.dbController.executePreparedQuery("SELECT id, serialization FROM charaSerializations WHERE id=?;",characterId);
|
||||
if(result.hasResult()){
|
||||
//if we get a valid response from the database, check that it actually matches hashes
|
||||
for(DatabaseResultRow row : result){
|
||||
String jsonString = row.getAsString("serialization");
|
||||
rVal = SerializationUtils.deserialize(jsonString, EntitySerialization.class);
|
||||
int id = row.getAsInteger("id");
|
||||
if(loadedCharacterMap.containsKey(id)){
|
||||
rVal.add(loadedCharacterMap.get(id));
|
||||
} else {
|
||||
Character description = SerializationUtils.deserialize(row.getAsString("dataVal"),Character.class);
|
||||
description.setId(id);
|
||||
loadedCharacterMap.put(description.getId(),description);
|
||||
rVal.add(description);
|
||||
}
|
||||
}
|
||||
}
|
||||
lock.unlock();
|
||||
return rVal;
|
||||
}
|
||||
|
||||
@ -121,50 +120,50 @@ public class CharacterService {
|
||||
* @param characterEntity The entity
|
||||
*/
|
||||
public static void saveCharacter(Entity characterEntity){
|
||||
lock.lock();
|
||||
if(!ServerCharacterData.hasServerCharacterDataTree(characterEntity)){
|
||||
throw new Error("Trying to save entity hat does not contain character data!");
|
||||
}
|
||||
ServerCharacterData characterData = ServerCharacterData.getServerCharacterData(characterEntity);
|
||||
int characterId = characterData.getCharacterId();
|
||||
Character charaData = characterData.getCharacterData();
|
||||
|
||||
//store exact position if it's a player's entity
|
||||
if(ServerPlayerViewDirTree.hasTree(characterEntity)){
|
||||
Vector3d realPos = EntityUtils.getPosition(characterEntity);
|
||||
Globals.dbController.executePreparedStatement(
|
||||
"INSERT OR REPLACE INTO charaRealPos (id, x, y, z) VALUES (?,?,?,?);",
|
||||
characterId,
|
||||
realPos.x,
|
||||
realPos.y,
|
||||
realPos.z
|
||||
);
|
||||
}
|
||||
//serialize
|
||||
charaData.setCreatureTemplate(CreatureUtils.getCreatureTemplate(characterEntity));
|
||||
charaData.setPos(EntityUtils.getPosition(characterEntity));
|
||||
String toStore = SerializationUtils.serialize(charaData);
|
||||
|
||||
//store a serialization to associate with the character
|
||||
EntitySerialization characterEntitySerialization = ContentSerialization.constructEntitySerialization(characterEntity);
|
||||
Globals.dbController.executePreparedStatement(
|
||||
"UPDATE charaData SET dataVal=? WHERE id=?;",
|
||||
characterEntitySerialization.getTemplate(),
|
||||
characterId
|
||||
toStore,
|
||||
charaData.getId()
|
||||
);
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all characters
|
||||
* @return The list of all characters
|
||||
*/
|
||||
public static List<CharacterDescription> getAllCharacters(){
|
||||
public static List<Character> getAllCharacters(){
|
||||
lock.lock();
|
||||
DatabaseResult result = Globals.dbController.executePreparedQuery("SELECT id, dataVal FROM charaData");
|
||||
List<CharacterDescription> rVal = new LinkedList<CharacterDescription>();
|
||||
List<Character> rVal = new LinkedList<Character>();
|
||||
if(result.hasResult()){
|
||||
//if we get a valid response from the database, check that it actually matches hashes
|
||||
for(DatabaseResultRow row : result){
|
||||
CharacterDescription description = new CharacterDescription();
|
||||
CreatureTemplate template = SerializationUtils.deserialize(row.getAsString("dataVal"),CreatureTemplate.class);
|
||||
description.setTemplate(template);
|
||||
description.setId(row.getAsInteger("id") + "");
|
||||
rVal.add(description);
|
||||
int id = row.getAsInteger("id");
|
||||
if(loadedCharacterMap.containsKey(id)){
|
||||
rVal.add(loadedCharacterMap.get(id));
|
||||
} else {
|
||||
Character description = SerializationUtils.deserialize(row.getAsString("dataVal"),Character.class);
|
||||
description.setId(id);
|
||||
loadedCharacterMap.put(description.getId(),description);
|
||||
rVal.add(description);
|
||||
}
|
||||
}
|
||||
}
|
||||
lock.unlock();
|
||||
return rVal;
|
||||
}
|
||||
|
||||
|
||||
@ -15,6 +15,7 @@ import electrosphere.net.server.player.Player;
|
||||
import electrosphere.net.server.protocol.CharacterProtocol;
|
||||
import electrosphere.server.datacell.Realm;
|
||||
import electrosphere.server.datacell.ServerWorldData;
|
||||
import electrosphere.server.macro.character.Character;
|
||||
|
||||
/**
|
||||
* Deals with spawning player characters
|
||||
@ -31,7 +32,8 @@ public class PlayerCharacterCreation {
|
||||
|
||||
//
|
||||
//get template
|
||||
CreatureTemplate template = CharacterService.getTemplate(connectionHandler.getPlayer().getDBID(), connectionHandler.getCharacterId());
|
||||
Character charaData = CharacterService.getCharacter(connectionHandler.getPlayer().getDBID(), connectionHandler.getCharacterId());
|
||||
CreatureTemplate template = charaData.getCreatureTemplate();
|
||||
if(connectionHandler.getCharacterId() == CharacterProtocol.SPAWN_EXISTING_TEMPLATE){
|
||||
template = connectionHandler.getCurrentCreatureTemplate();
|
||||
}
|
||||
@ -89,7 +91,7 @@ public class PlayerCharacterCreation {
|
||||
*/
|
||||
static void addPlayerServerBTrees(Entity entity, ServerConnectionHandler serverConnectionHandler){
|
||||
ServerPlayerViewDirTree.attachServerPlayerViewDirTree(entity);
|
||||
ServerCharacterData.attachServerCharacterData(entity, serverConnectionHandler.getCharacterId());
|
||||
ServerCharacterData.attachServerCharacterData(entity, CharacterService.getCharacter(serverConnectionHandler.getPlayer().getDBID(), serverConnectionHandler.getCharacterId()));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -99,7 +101,7 @@ public class PlayerCharacterCreation {
|
||||
* @return The spawn point for the player
|
||||
*/
|
||||
public static Vector3d solveSpawnPoint(Realm realm, ServerConnectionHandler connectionHandler){
|
||||
Vector3d spawnPoint = CharacterService.getCharacterPosition(connectionHandler.getCharacterId());
|
||||
Vector3d spawnPoint = CharacterService.getCharacter(connectionHandler.getPlayer().getDBID(), connectionHandler.getCharacterId()).getPos();
|
||||
if(spawnPoint == null){
|
||||
spawnPoint = realm.getSpawnPoint();
|
||||
}
|
||||
|
||||
@ -136,7 +136,7 @@ public class ServerContentManager {
|
||||
}
|
||||
//place macro object
|
||||
Entity characterEntity = CreatureUtils.serverSpawnBasicCreature(realm, object.getPos(), creatureName, null);
|
||||
ServerCharacterData.attachServerCharacterData(characterEntity, ((Character)object).getId());
|
||||
ServerCharacterData.attachServerCharacterData(characterEntity, (Character)object);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -48,7 +48,7 @@ public class ContentSerialization {
|
||||
continue;
|
||||
}
|
||||
if(type != null){
|
||||
EntitySerialization serializedEntity = constructEntitySerialization(entity);
|
||||
EntitySerialization serializedEntity = ContentSerialization.constructEntitySerialization(entity);
|
||||
rVal.serializedEntities.add(serializedEntity);
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ import java.util.List;
|
||||
|
||||
import electrosphere.engine.Globals;
|
||||
import electrosphere.logger.LoggerInterface;
|
||||
import electrosphere.server.character.CharacterService;
|
||||
import electrosphere.server.datacell.ServerWorldData;
|
||||
import electrosphere.server.macro.character.Character;
|
||||
import electrosphere.server.macro.character.CharacterUtils;
|
||||
@ -43,16 +44,6 @@ public class MacroData {
|
||||
*/
|
||||
List<Race> races = new LinkedList<Race>();
|
||||
|
||||
/**
|
||||
* List of characters
|
||||
*/
|
||||
List<Character> characters = new LinkedList<Character>();
|
||||
|
||||
/**
|
||||
* List of alive characters
|
||||
*/
|
||||
List<Character> aliveCharacters = new LinkedList<Character>();
|
||||
|
||||
/**
|
||||
* List of civilizations
|
||||
*/
|
||||
@ -78,12 +69,13 @@ public class MacroData {
|
||||
MacroData rVal = new MacroData();
|
||||
|
||||
//generate initial dieties
|
||||
int numDieties = 3 + Math.abs(random.nextInt()) % 7;
|
||||
for(int i = 0; i < numDieties; i++){
|
||||
Character diety = generateInitialDiety(random.nextLong());
|
||||
rVal.initialDieties.add(diety);
|
||||
rVal.characters.add(diety);
|
||||
}
|
||||
// int numDieties = 3 + Math.abs(random.nextInt()) % 7;
|
||||
// for(int i = 0; i < numDieties; i++){
|
||||
// Character diety = generateInitialDiety(random.nextLong());
|
||||
// rVal.initialDieties.add(diety);
|
||||
// rVal.characters.add(diety);
|
||||
// CharacterService.createCharacter(null, i);
|
||||
// }
|
||||
|
||||
//generate initial races
|
||||
if(Globals.gameConfigCurrent.getRaceMap().getSymbolismMap().size() < 3){
|
||||
@ -168,29 +160,21 @@ public class MacroData {
|
||||
FileUtils.serializeObjectToSavePath(saveName, "./macro.json", this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an initial diety
|
||||
* @param seed The seed
|
||||
* @return The character for the diety
|
||||
*/
|
||||
static Character generateInitialDiety(long seed){
|
||||
Character rVal = new Character();
|
||||
// /**
|
||||
// * Generates an initial diety
|
||||
// * @param seed The seed
|
||||
// * @return The character for the diety
|
||||
// */
|
||||
// static Character generateInitialDiety(long seed){
|
||||
// Character rVal = new Character();
|
||||
|
||||
|
||||
Diety diety = Diety.generateDiety(seed);
|
||||
CharacterUtils.addDiety(rVal, diety);
|
||||
// Diety diety = Diety.generateDiety(seed);
|
||||
// CharacterUtils.addDiety(rVal, diety);
|
||||
|
||||
|
||||
return rVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all alive characters
|
||||
* @return The list of all characters
|
||||
*/
|
||||
public List<Character> getAliveCharacters(){
|
||||
return aliveCharacters;
|
||||
}
|
||||
// return rVal;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Gets the list of civilizations
|
||||
@ -239,14 +223,6 @@ public class MacroData {
|
||||
public void addStructure(Structure structure){
|
||||
structures.add(structure);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a character to the macro data
|
||||
* @param character The character
|
||||
*/
|
||||
public void addCharacter(Character character){
|
||||
characters.add(character);
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes the world
|
||||
@ -270,7 +246,7 @@ public class MacroData {
|
||||
LoggerInterface.loggerEngine.WARNING(race.getName());
|
||||
int numCharsOfRace = 0;
|
||||
//n*m complexity - yikes! - as long as we're not making a million chars at start this should be _ok_
|
||||
for(Character chara : characters){
|
||||
for(Character chara : CharacterService.getAllCharacters()){
|
||||
if(chara.containsKey(CharacterDataStrings.RACE)){
|
||||
if(Race.getRace(chara).equals(race)){
|
||||
numCharsOfRace++;
|
||||
@ -291,7 +267,7 @@ public class MacroData {
|
||||
*/
|
||||
public List<Character> getCharacters(Vector3i worldPos){
|
||||
List<Character> rVal = new LinkedList<Character>();
|
||||
for(Character character : this.characters){
|
||||
for(Character character : CharacterService.getAllCharacters()){
|
||||
if(ServerWorldData.convertRealToChunkSpace(character.getPos()).equals(worldPos.x, worldPos.y, worldPos.z)){
|
||||
rVal.add(character);
|
||||
}
|
||||
@ -315,7 +291,7 @@ public class MacroData {
|
||||
* @return The character if it exists, null otherwise
|
||||
*/
|
||||
public Character getCharacter(int id){
|
||||
for(Character character : this.characters){
|
||||
for(Character character : CharacterService.getAllCharacters()){
|
||||
if(character.getId() == id){
|
||||
return character;
|
||||
}
|
||||
|
||||
@ -1,12 +1,8 @@
|
||||
package electrosphere.server.macro;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import electrosphere.client.entity.character.CharacterDescription;
|
||||
import electrosphere.game.data.block.BlockFab;
|
||||
import electrosphere.server.character.CharacterService;
|
||||
import electrosphere.server.macro.character.Character;
|
||||
import electrosphere.server.macro.structure.Structure;
|
||||
import electrosphere.util.FileUtils;
|
||||
|
||||
@ -37,18 +33,6 @@ public class MacroDataLoader {
|
||||
structure.setFab(fab);
|
||||
}
|
||||
|
||||
//inject characters from character service
|
||||
List<CharacterDescription> characters = CharacterService.getAllCharacters();
|
||||
for(CharacterDescription desc : characters){
|
||||
int targetId = Integer.parseInt(desc.getId());
|
||||
Character macroCharacter = rVal.getCharacter(targetId);
|
||||
if(macroCharacter == null){
|
||||
macroCharacter = new Character();
|
||||
macroCharacter.setId(targetId);
|
||||
rVal.characters.add(macroCharacter);
|
||||
}
|
||||
}
|
||||
|
||||
return rVal;
|
||||
}
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@ import java.util.Map;
|
||||
|
||||
import org.joml.Vector3d;
|
||||
|
||||
import electrosphere.entity.types.creature.CreatureTemplate;
|
||||
import electrosphere.server.macro.character.data.CharacterData;
|
||||
import electrosphere.server.macro.spatial.MacroObject;
|
||||
|
||||
@ -12,12 +13,25 @@ import electrosphere.server.macro.spatial.MacroObject;
|
||||
* A character
|
||||
*/
|
||||
public class Character implements MacroObject {
|
||||
static int character_id_iterator = 0;
|
||||
|
||||
|
||||
/**
|
||||
* The id of the character object
|
||||
*/
|
||||
int id;
|
||||
|
||||
/**
|
||||
* Data stored on the character
|
||||
*/
|
||||
Map<String,CharacterData> data = new HashMap<String,CharacterData>();
|
||||
|
||||
/**
|
||||
* The creature template
|
||||
*/
|
||||
CreatureTemplate creatureTemplate;
|
||||
|
||||
/**
|
||||
* The position of the character object
|
||||
*/
|
||||
Vector3d pos = new Vector3d();
|
||||
|
||||
|
||||
@ -41,10 +55,29 @@ public class Character implements MacroObject {
|
||||
public CharacterData getData(String key){
|
||||
return data.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param template
|
||||
*/
|
||||
public Character(CreatureTemplate template){
|
||||
this.creatureTemplate = template;
|
||||
}
|
||||
|
||||
public Character(){
|
||||
this.id = character_id_iterator;
|
||||
character_id_iterator++;
|
||||
/**
|
||||
* Gets the creature template for the character
|
||||
* @return The template
|
||||
*/
|
||||
public CreatureTemplate getCreatureTemplate() {
|
||||
return creatureTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the creature template for the character
|
||||
* @param creatureTemplate The template
|
||||
*/
|
||||
public void setCreatureTemplate(CreatureTemplate creatureTemplate) {
|
||||
this.creatureTemplate = creatureTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -53,7 +86,7 @@ public class Character implements MacroObject {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPos(Vector3d pos) {
|
||||
public void setPos(Vector3d pos){
|
||||
this.pos.set(pos);
|
||||
}
|
||||
|
||||
|
||||
@ -2,8 +2,8 @@ package electrosphere.server.macro.character;
|
||||
|
||||
import org.joml.Vector3d;
|
||||
|
||||
import electrosphere.server.character.CharacterService;
|
||||
import electrosphere.server.datacell.Realm;
|
||||
import electrosphere.server.macro.MacroData;
|
||||
import electrosphere.server.macro.character.data.CharacterDataStrings;
|
||||
import electrosphere.server.macro.character.diety.Diety;
|
||||
import electrosphere.server.macro.race.Race;
|
||||
@ -55,11 +55,9 @@ public class CharacterUtils {
|
||||
* @return The character
|
||||
*/
|
||||
public static Character spawnCharacter(Realm realm, Vector3d position){
|
||||
Character rVal = new Character();
|
||||
Character rVal = CharacterService.createCharacter(null, 0);
|
||||
rVal.setPos(position);
|
||||
Race.setRace(rVal, Race.create("human", "human"));
|
||||
MacroData macroData = realm.getServerContentManager().getMacroData();
|
||||
macroData.addCharacter(rVal);
|
||||
realm.getDataCellManager().evaluateMacroObject(rVal);
|
||||
return rVal;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user