Skip to content

Commit

Permalink
Entity Records (#86)
Browse files Browse the repository at this point in the history
* Added entity records to keep track of actor info after death.

* Modified max damage.
  • Loading branch information
dmccoystephenson authored May 26, 2022
1 parent f0b7290 commit 0cdf92a
Show file tree
Hide file tree
Showing 18 changed files with 303 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package dansapps.interakt.commands.console;

import dansapps.interakt.commands.abs.InteraktCommand;
import dansapps.interakt.exceptions.NameTakenException;
import dansapps.interakt.factories.ActorFactory;
import dansapps.interakt.factories.WorldFactory;
import preponderous.ponder.system.abs.CommandSender;
Expand Down Expand Up @@ -48,7 +49,12 @@ public boolean execute(CommandSender sender, String[] args) {
String name = doubleQuoteArgs.get(1);

if (type.equalsIgnoreCase("actor")) {
ActorFactory.getInstance().createActorWithRandomName(name);
try {
ActorFactory.getInstance().createActorWithName(name);
} catch (NameTakenException e) {
sender.sendMessage("That name was taken.");
return false;
}
sender.sendMessage("Actor created.");
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dansapps.interakt.commands.abs.InteraktCommand;
import dansapps.interakt.data.PersistentData;
import dansapps.interakt.exceptions.NameTakenException;
import dansapps.interakt.factories.ActorFactory;
import dansapps.interakt.factories.WorldFactory;
import dansapps.interakt.objects.Actor;
Expand Down Expand Up @@ -38,7 +39,11 @@ public boolean execute(CommandSender sender) {
return false;
}
for (String name : names) {
ActorFactory.getInstance().createActorWithRandomName(name);
try {
ActorFactory.getInstance().createActorWithName(name);
} catch (NameTakenException e) {
continue;
}
Actor actor;
try {
actor = PersistentData.getInstance().getActor(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dansapps.interakt.commands.abs.InteraktCommand;
import dansapps.interakt.data.PersistentData;
import dansapps.interakt.exceptions.ZeroFriendshipsExistentException;
import dansapps.interakt.utils.Logger;
import preponderous.ponder.system.abs.CommandSender;

Expand All @@ -23,10 +24,17 @@ public boolean execute(CommandSender sender) {
sender.sendMessage("Number of squares: " + PersistentData.getInstance().getSquares().size());
sender.sendMessage("Number of elapsed time partitions: " + PersistentData.getInstance().getTimePartitions().size());
sender.sendMessage("Number of action records: " + PersistentData.getInstance().getActionRecords().size());
sender.sendMessage("Number of entity records: " + PersistentData.getInstance().getEntityRecords().size());
sender.sendMessage("Most active actor: " + PersistentData.getInstance().getActorWithMostActionRecords().getName());
sender.sendMessage("Least active actor: " + PersistentData.getInstance().getActorWithLeastActionRecords().getName());
sender.sendMessage("Most well travelled: " + PersistentData.getInstance().getMostWellTravelledActor().getName());
sender.sendMessage("Most friendly actor: " + PersistentData.getInstance().getMostFriendlyActor().getName());

try {
sender.sendMessage("Most friendly actor: " + PersistentData.getInstance().getMostFriendlyActor().getName());
} catch(ZeroFriendshipsExistentException e) {
sender.sendMessage("Most friendly actor: N/A");
}

sender.sendMessage("Minutes elapsed: " + PersistentData.getInstance().getSecondsElapsed()/60);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class QuitCommand extends InteraktCommand {

public QuitCommand() {
super(new ArrayList<>(List.of("quit")), new ArrayList<>(List.of("interakt.quit")));
super(new ArrayList<>(List.of("quit", "exit")), new ArrayList<>(List.of("interakt.quit")));
}

@Override
Expand Down
57 changes: 49 additions & 8 deletions src/main/java/dansapps/interakt/data/PersistentData.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
*/
package dansapps.interakt.data;

import dansapps.interakt.exceptions.ActorNotFoundException;
import dansapps.interakt.exceptions.EntityRecordNotFoundException;
import dansapps.interakt.exceptions.ZeroFriendshipsExistentException;
import dansapps.interakt.objects.*;
import preponderous.ponder.system.abs.CommandSender;

Expand All @@ -22,6 +25,7 @@ public class PersistentData {
private Set<Square> squares = new ConcurrentHashMap<Actor, Boolean>().newKeySet();
private ArrayList<TimePartition> timePartitions = new ArrayList<>();
private LinkedList<ActionRecord> actionRecords = new LinkedList<>();
private Set<EntityRecord> entityRecords = new HashSet<>();

private PersistentData() {

Expand Down Expand Up @@ -72,22 +76,22 @@ private void removeActorReferencesFromSquares(Actor actor) {
}
}

public Actor getActor(String name) throws Exception {
public Actor getActor(String name) throws ActorNotFoundException {
for (Actor actor : actors) {
if (actor.getName().equalsIgnoreCase(name)) {
return actor;
}
}
throw new Exception();
throw new ActorNotFoundException();
}

public Actor getActor(UUID entityUUID) {
public Actor getActor(UUID entityUUID) throws ActorNotFoundException {
for (Actor actor : actors) {
if (actor.getUUID().equals(entityUUID)) {
return actor;
}
}
return null;
throw new ActorNotFoundException();
}

public Set<World> getWorlds() {
Expand Down Expand Up @@ -190,13 +194,44 @@ public void addActionRecord(ActionRecord actionRecord) {
actionRecords.add(actionRecord);
}

public Set<EntityRecord> getEntityRecords() {
return entityRecords;
}

public void setEntityRecords(Set<EntityRecord> entityRecords) {
this.entityRecords = entityRecords;
}

public void addEntityRecord(EntityRecord entityRecord) {
entityRecords.add(entityRecord);
}

public EntityRecord getEntityRecord(UUID entityUUID) throws EntityRecordNotFoundException {
for (EntityRecord entityRecord : entityRecords) {
if (entityRecord.getUUID().equals(entityUUID)) {
return entityRecord;
}
}
throw new EntityRecordNotFoundException();
}

public EntityRecord getEntityRecord(String name) throws EntityRecordNotFoundException {
for (EntityRecord entityRecord : entityRecords) {
if (entityRecord.getName().equals(name)) {
return entityRecord;
}
}
throw new EntityRecordNotFoundException();
}

public void clearData() {
actors.clear();
worlds.clear();
regions.clear();
squares.clear();
timePartitions.clear();
actionRecords.clear();
entityRecords.clear();
}

public boolean isWorld(String name) {
Expand All @@ -213,8 +248,14 @@ public boolean isActor(String name) {
getActor(name);
return true;
}
catch (Exception e) {
return false;
catch (ActorNotFoundException actorNotFoundException) {
try {
getEntityRecord(name);
return true;
}
catch(EntityRecordNotFoundException entityRecordNotFoundException) {
return false;
}
}
}

Expand Down Expand Up @@ -294,7 +335,7 @@ public Actor getMostWellTravelledActor() {
return toReturn;
}

public Actor getMostFriendlyActor() {
public Actor getMostFriendlyActor() throws ZeroFriendshipsExistentException {
Actor toReturn = null;
int max = 0;
for (Actor actor : actors) {
Expand All @@ -305,7 +346,7 @@ public Actor getMostFriendlyActor() {
}
}
if (toReturn == null) {
throw new NullPointerException();
throw new ZeroFriendshipsExistentException();
}
return toReturn;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dansapps.interakt.exceptions;

public class ActorNotFoundException extends Exception {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dansapps.interakt.exceptions;

public class EntityRecordNotFoundException extends Exception {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dansapps.interakt.exceptions;

public class NameTakenException extends Exception {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dansapps.interakt.exceptions;

public class ZeroFriendshipsExistentException extends Exception {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dansapps.interakt.actions.abs.Action;
import dansapps.interakt.data.PersistentData;
import dansapps.interakt.exceptions.ActorNotFoundException;
import dansapps.interakt.objects.ActionRecord;
import dansapps.interakt.objects.Actor;

Expand Down Expand Up @@ -31,7 +32,13 @@ public void createActionRecord(Map<String, String> data) {
ActionRecord actionRecord = new ActionRecord(data);
PersistentData.getInstance().addActionRecord(actionRecord);

Actor actor = PersistentData.getInstance().getActor(actionRecord.getEntityUUID());
Actor actor = null;
try {
actor = PersistentData.getInstance().getActor(actionRecord.getEntityUUID());
} catch (ActorNotFoundException actorNotFoundException) {
// fail silently, this actor is likely dead
return;
}
actor.addActionRecord(actionRecord);
}
}
16 changes: 12 additions & 4 deletions src/main/java/dansapps/interakt/factories/ActorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package dansapps.interakt.factories;

import dansapps.interakt.data.PersistentData;
import dansapps.interakt.exceptions.NameTakenException;
import dansapps.interakt.objects.Actor;

import java.util.Map;
Expand All @@ -28,12 +29,13 @@ public static ActorFactory getInstance() {
return instance;
}

public Actor createActorWithRandomName(String name) {
public Actor createActorWithName(String name) throws NameTakenException {
if (isNameTaken(name)) {
return null;
throw new NameTakenException();
}
Actor actor = new Actor(name);
PersistentData.getInstance().addActor(actor);
EntityRecordFactory.getInstance().createEntityRecord(actor);
return actor;
}

Expand All @@ -60,12 +62,18 @@ public Actor createActorWithRandomName() {
do {
name = generateRandomString(5);
} while (isNameTaken(name));
return createActorWithRandomName(name);
try {
return createActorWithName(name);
} catch (NameTakenException e) {
return createActorWithRandomName();
}
}

public void createActorWithRandomName(Map<String, String> data) {
public void createActorWithData(Map<String, String> data) {
Actor actor = new Actor(data);
PersistentData.getInstance().addActor(actor);
EntityRecordFactory.getInstance().createEntityRecord(actor);

}

private boolean isNameTaken(String name) {
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/dansapps/interakt/factories/EntityRecordFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dansapps.interakt.factories;

import dansapps.interakt.data.PersistentData;
import dansapps.interakt.objects.EntityRecord;
import dansapps.interakt.objects.Actor;

import java.util.Map;

public class EntityRecordFactory {
private static EntityRecordFactory instance;

private EntityRecordFactory() {

}

public static EntityRecordFactory getInstance() {
if (instance == null) {
instance = new EntityRecordFactory();
}
return instance;
}

public void createEntityRecord(Actor actor) {
EntityRecord entityRecord = new EntityRecord(actor.getUUID(), actor.getName());
PersistentData.getInstance().addEntityRecord(entityRecord);
}

public void createEntityRecord(Map<String, String> data) {
EntityRecord entityRecord = new EntityRecord(data);
PersistentData.getInstance().addEntityRecord(entityRecord);
}
}
8 changes: 4 additions & 4 deletions src/main/java/dansapps/interakt/misc/CONFIG.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
public class CONFIG {
public static boolean DEBUG_FLAG = false;
public static final int TIME_SLOT_LENGTH_IN_SECONDS = 1;
public static final int GRID_SIZE = 2;
public static final int GRID_SIZE = 4;
public static final int MAX_CHANCE_TO_MOVE = 25;
public static final int MAX_CHANCE_TO_BEFRIEND = 50;
public static final int MAX_CHANCE_TO_ATTACK = 50;
public static final int MAX_CHANCE_TO_REPRODUCE = 10;
public static final int MAX_CHANCE_TO_REPRODUCE = 25;
public static final int SECONDS_BETWEEN_AUTO_SAVES = 300;
public static final double MAX_HEALTH = 100;
public static final double MAX_DAMAGE = 50;
public static final GRID_DISPLAY_TYPE DISPLAY_TYPE = GRID_DISPLAY_TYPE.NUMBER_OF_ENTITIES;
public static final double MAX_DAMAGE = 10;
public static final GRID_DISPLAY_TYPE DISPLAY_TYPE = GRID_DISPLAY_TYPE.CHARACTER_AT_INDEX_ZERO;
public static final boolean SHOW_LINEAGE_INFO = true;
public static final boolean LOG_EVENTS_TO_CONSOLE = false;
}
Loading

0 comments on commit 0cdf92a

Please sign in to comment.