diff --git a/src/main/java/com/projectswg/holocore/resources/gameplay/crafting/resource/galactic/GalacticResource.java b/src/main/java/com/projectswg/holocore/resources/gameplay/crafting/resource/galactic/GalacticResource.java index a612d1865..15f1ae1d4 100644 --- a/src/main/java/com/projectswg/holocore/resources/gameplay/crafting/resource/galactic/GalacticResource.java +++ b/src/main/java/com/projectswg/holocore/resources/gameplay/crafting/resource/galactic/GalacticResource.java @@ -26,11 +26,13 @@ ***********************************************************************************/ package com.projectswg.holocore.resources.gameplay.crafting.resource.galactic; +import com.projectswg.common.data.encodables.mongo.MongoData; +import com.projectswg.common.data.encodables.mongo.MongoPersistable; import com.projectswg.common.network.NetBufferStream; import com.projectswg.common.persistable.Persistable; import com.projectswg.holocore.resources.gameplay.crafting.resource.raw.RawResource; -public class GalacticResource implements Persistable { +public class GalacticResource implements Persistable, MongoPersistable { private final GalacticResourceStats stats; @@ -79,6 +81,15 @@ public class GalacticResource implements Persistable { this.rawResource = rawResource; } + @Override + public void read(NetBufferStream stream) { + stream.getByte(); + id = stream.getLong(); + name = stream.getAscii(); + rawId = stream.getLong(); + stats.read(stream); + } + @Override public void save(NetBufferStream stream) { stream.addByte(0); @@ -89,12 +100,19 @@ public class GalacticResource implements Persistable { } @Override - public void read(NetBufferStream stream) { - stream.getByte(); - id = stream.getLong(); - name = stream.getAscii(); - rawId = stream.getLong(); - stats.read(stream); + public void readMongo(MongoData data) { + id = data.getLong("id", id); + name = data.getString("name", name); + rawId = data.getLong("rawId", rawId); + data.getDocument("stats", stats); + } + + @Override + public void saveMongo(MongoData data) { + data.putLong("id", id); + data.putString("name", name); + data.putLong("rawId", rawId); + data.putDocument("stats", stats); } @Override diff --git a/src/main/java/com/projectswg/holocore/resources/gameplay/crafting/resource/galactic/GalacticResourceSpawn.java b/src/main/java/com/projectswg/holocore/resources/gameplay/crafting/resource/galactic/GalacticResourceSpawn.java index 6762eb7b1..8ae81b050 100644 --- a/src/main/java/com/projectswg/holocore/resources/gameplay/crafting/resource/galactic/GalacticResourceSpawn.java +++ b/src/main/java/com/projectswg/holocore/resources/gameplay/crafting/resource/galactic/GalacticResourceSpawn.java @@ -26,24 +26,27 @@ ***********************************************************************************/ package com.projectswg.holocore.resources.gameplay.crafting.resource.galactic; +import com.projectswg.common.data.encodables.mongo.MongoData; +import com.projectswg.common.data.encodables.mongo.MongoPersistable; import com.projectswg.common.data.location.Terrain; import com.projectswg.common.network.NetBufferStream; import com.projectswg.common.persistable.Persistable; -import com.projectswg.common.utilities.TimeUtilities; import com.projectswg.holocore.resources.support.data.config.ConfigFile; import com.projectswg.holocore.resources.support.data.server_info.DataManager; +import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.Random; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.ThreadLocalRandom; -public class GalacticResourceSpawn implements Persistable { +public class GalacticResourceSpawn implements Persistable, MongoPersistable { private static final int MAP_SIZE = 16384; private static final int MUST_MAP_SIZE = 8000; private static final int KASH_MAP_SIZE = 8192; - private static final double POSITION_GAUSSIAN_FACTOR = MAP_SIZE / 2 * Math.sqrt(2); - private static final double POSITION_MUST_GAUSSIAN_FACTOR = MUST_MAP_SIZE / 2 * Math.sqrt(2); - private static final double POSITION_KASH_GAUSSIAN_FACTOR = KASH_MAP_SIZE / 2 * Math.sqrt(2); + private static final double POSITION_GAUSSIAN_FACTOR = MAP_SIZE / 2d * Math.sqrt(2); + private static final double POSITION_MUST_GAUSSIAN_FACTOR = MUST_MAP_SIZE / 2d * Math.sqrt(2); + private static final double POSITION_KASH_GAUSSIAN_FACTOR = KASH_MAP_SIZE / 2d * Math.sqrt(2); // Resource-based private long resourceId; @@ -55,19 +58,28 @@ public class GalacticResourceSpawn implements Persistable { private int z; private int radius; // Time-based - private long startTime; - private long endTime; + private Instant startTime; + private Instant endTime; public GalacticResourceSpawn() { - + this.resourceId = 0; + this.minConcentration = 0; + this.maxConcentration = 0; + this.terrain = null; + this.x = 0; + this.z = 0; + this.radius = 0; + this.startTime = Instant.EPOCH; + this.endTime = Instant.EPOCH; } public GalacticResourceSpawn(long resourceId) { + this(); this.resourceId = resourceId; } public void setRandomValues(Terrain terrain) { - Random random = new Random(); + ThreadLocalRandom random = ThreadLocalRandom.current(); this.minConcentration = random.nextInt(50); this.maxConcentration = calculateRandomMaxConcentration(random, minConcentration); @@ -78,8 +90,8 @@ public class GalacticResourceSpawn implements Persistable { int minSpawnTime = getMinSpawnTime(); int maxSpawnTime = getMaxSpawnTime(); - this.startTime = TimeUtilities.getTime(); - this.endTime = startTime + TimeUnit.DAYS.toMillis(random.nextInt(maxSpawnTime - minSpawnTime) + minSpawnTime); + this.startTime = Instant.now(); + this.endTime = startTime.plus(random.nextInt(minSpawnTime, maxSpawnTime), ChronoUnit.DAYS); } public long getResourceId() { @@ -110,11 +122,11 @@ public class GalacticResourceSpawn implements Persistable { return radius; } - public long getStartTime() { + public Instant getStartTime() { return startTime; } - public long getEndTime() { + public Instant getEndTime() { return endTime; } @@ -128,7 +140,7 @@ public class GalacticResourceSpawn implements Persistable { } public boolean isExpired() { - return TimeUtilities.getTime() > endTime; + return Instant.now().isAfter(endTime); } private int getMinSpawnTime() { @@ -188,23 +200,6 @@ public class GalacticResourceSpawn implements Persistable { return Math.max(-MAP_SIZE/2, Math.min(MAP_SIZE/2, x)); } - @Override - public void save(NetBufferStream stream) { - stream.addByte(0); - // Resource - stream.addLong(resourceId); - stream.addInt(minConcentration); - stream.addInt(maxConcentration); - // Location - stream.addAscii(terrain.name()); - stream.addInt(x); - stream.addInt(z); - stream.addInt(radius); - // Time - stream.addLong(startTime); - stream.addLong(endTime); - } - @Override public void read(NetBufferStream stream) { stream.getByte(); @@ -218,10 +213,71 @@ public class GalacticResourceSpawn implements Persistable { this.z = stream.getInt(); this.radius = stream.getInt(); // Time - this.startTime = stream.getLong(); - this.endTime = stream.getLong(); + this.startTime = Instant.ofEpochMilli(stream.getLong()); + this.endTime = Instant.ofEpochMilli(stream.getLong()); } - + + @Override + public void save(NetBufferStream stream) { + stream.addByte(0); + // Resource + stream.addLong(resourceId); + stream.addInt(minConcentration); + stream.addInt(maxConcentration); + // Location + stream.addAscii(terrain.name()); + stream.addInt(x); + stream.addInt(z); + stream.addInt(radius); + // Time + stream.addLong(startTime.toEpochMilli()); + stream.addLong(endTime.toEpochMilli()); + } + + @Override + public void readMongo(MongoData data) { + { + MongoData resource = data.getDocument("resource"); + resourceId = resource.getLong("id", resourceId); + minConcentration = resource.getInteger("minConcentration", minConcentration); + maxConcentration = resource.getInteger("maxConcentration", maxConcentration); + } + { + MongoData location = data.getDocument("location"); + terrain = Terrain.valueOf(location.getString("terrain", Terrain.TATOOINE.name())); + x = location.getInteger("x", x); + z = location.getInteger("z", z); + radius = location.getInteger("radius", radius); + } + { + MongoData time = data.getDocument("time"); + startTime = time.getDate("start", Instant.EPOCH); + endTime = time.getDate("end", Instant.EPOCH); + } + } + + @Override + public void saveMongo(MongoData data) { + { + MongoData resource = data.getDocument("resource"); + resource.putLong("id", resourceId); + resource.putInteger("minConcentration", minConcentration); + resource.putInteger("maxConcentration", maxConcentration); + } + { + MongoData location = data.getDocument("location"); + location.putString("terrain", terrain.name()); + location.putInteger("x", x); + location.putInteger("z", z); + location.putInteger("radius", radius); + } + { + MongoData time = data.getDocument("time"); + time.putDate("start", startTime); + time.putDate("end", endTime); + } + } + private double getDistance(Terrain terrain, double x, double z) { if (this.terrain != terrain) return Double.MAX_VALUE; diff --git a/src/main/java/com/projectswg/holocore/resources/gameplay/crafting/resource/galactic/GalacticResourceStats.java b/src/main/java/com/projectswg/holocore/resources/gameplay/crafting/resource/galactic/GalacticResourceStats.java index 9e0cfc1b0..166c8fa1c 100644 --- a/src/main/java/com/projectswg/holocore/resources/gameplay/crafting/resource/galactic/GalacticResourceStats.java +++ b/src/main/java/com/projectswg/holocore/resources/gameplay/crafting/resource/galactic/GalacticResourceStats.java @@ -26,12 +26,14 @@ ***********************************************************************************/ package com.projectswg.holocore.resources.gameplay.crafting.resource.galactic; +import com.projectswg.common.data.encodables.mongo.MongoData; +import com.projectswg.common.data.encodables.mongo.MongoPersistable; import com.projectswg.common.network.NetBufferStream; import com.projectswg.common.persistable.Persistable; import java.util.Random; -public class GalacticResourceStats implements Persistable { +public class GalacticResourceStats implements Persistable, MongoPersistable { private int coldResistance; private int conductivity; @@ -74,6 +76,22 @@ public class GalacticResourceStats implements Persistable { this.unitToughness = generateRandomNumber(random); } + @Override + public void read(NetBufferStream stream) { + stream.getByte(); + this.coldResistance = stream.getShort(); + this.conductivity = stream.getShort(); + this.decayResistance = stream.getShort(); + this.entangleResistance = stream.getShort(); + this.flavor = stream.getShort(); + this.heatResistance = stream.getShort(); + this.malleability = stream.getShort(); + this.overallQuality = stream.getShort(); + this.potentialEnergy = stream.getShort(); + this.shockResistance = stream.getShort(); + this.unitToughness = stream.getShort(); + } + @Override public void save(NetBufferStream stream) { stream.addByte(0); @@ -91,19 +109,33 @@ public class GalacticResourceStats implements Persistable { } @Override - public void read(NetBufferStream stream) { - stream.getByte(); - this.coldResistance = stream.getShort(); - this.conductivity = stream.getShort(); - this.decayResistance = stream.getShort(); - this.entangleResistance = stream.getShort(); - this.flavor = stream.getShort(); - this.heatResistance = stream.getShort(); - this.malleability = stream.getShort(); - this.overallQuality = stream.getShort(); - this.potentialEnergy = stream.getShort(); - this.shockResistance = stream.getShort(); - this.unitToughness = stream.getShort(); + public void readMongo(MongoData data) { + coldResistance = (short) data.getInteger("coldResistance", coldResistance); + conductivity = (short) data.getInteger("conductivity", conductivity); + decayResistance = (short) data.getInteger("decayResistance", decayResistance); + entangleResistance = (short) data.getInteger("entangleResistance", entangleResistance); + flavor = (short) data.getInteger("flavor", flavor); + heatResistance = (short) data.getInteger("heatResistance", heatResistance); + malleability = (short) data.getInteger("malleability", malleability); + overallQuality = (short) data.getInteger("overallQuality", overallQuality); + potentialEnergy = (short) data.getInteger("potentialEnergy", potentialEnergy); + shockResistance = (short) data.getInteger("shockResistance", shockResistance); + unitToughness = (short) data.getInteger("unitToughness", unitToughness); + } + + @Override + public void saveMongo(MongoData data) { + data.putInteger("coldResistance", coldResistance); + data.putInteger("conductivity", conductivity); + data.putInteger("decayResistance", decayResistance); + data.putInteger("entangleResistance", entangleResistance); + data.putInteger("flavor", flavor); + data.putInteger("heatResistance", heatResistance); + data.putInteger("malleability", malleability); + data.putInteger("overallQuality", overallQuality); + data.putInteger("potentialEnergy", potentialEnergy); + data.putInteger("shockResistance", shockResistance); + data.putInteger("unitToughness", unitToughness); } private int generateRandomNumber(Random random) {