mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-10 14:26:45 +00:00
More refactoring, updated mongodb driver + transactions on supported clusters
This commit is contained in:
parent
b361f87154
commit
5f95c17aed
@ -20,7 +20,9 @@
|
||||
<orderEntry type="module" module-name="Server" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-text:1.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.7" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongo-java-driver:3.7.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-sync:3.9.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:bson:3.9.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-core:3.9.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-core:5.0.5.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.0.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.0.6.RELEASE" level="project" />
|
||||
|
@ -19,7 +19,9 @@
|
||||
<orderEntry type="module" module-name="Server" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-text:1.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.7" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongo-java-driver:3.7.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-sync:3.9.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:bson:3.9.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-core:3.9.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-core:5.0.5.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.0.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.0.6.RELEASE" level="project" />
|
||||
|
@ -22,7 +22,9 @@
|
||||
<orderEntry type="module" module-name="Server" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-text:1.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.7" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongo-java-driver:3.7.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-sync:3.9.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:bson:3.9.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-core:3.9.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-core:5.0.5.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.0.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.0.6.RELEASE" level="project" />
|
||||
|
@ -65,6 +65,13 @@ public class Factory extends Structure implements Updatable, MessageReceiver {
|
||||
|
||||
Settlement settlement = NpcPlugin.settlementMap.get(getWorld().getId());
|
||||
|
||||
if (settlement == null) {
|
||||
//Only happens when server is killed during save function
|
||||
getWorld().decUpdatable();
|
||||
setDead(true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (cooldown == 0) {
|
||||
if (settlement.getNpcs().size() < MAX_NPC_COUNT) {
|
||||
Point p = getAdjacentTile();
|
||||
@ -72,6 +79,9 @@ public class Factory extends Structure implements Updatable, MessageReceiver {
|
||||
if (p != null) {
|
||||
NonPlayerCharacter npc = spawnNPC(p);
|
||||
settlement.addNpc(npc);
|
||||
|
||||
getWorld().addObject(npc);
|
||||
getWorld().incUpdatable();
|
||||
}
|
||||
}
|
||||
|
||||
@ -102,8 +112,6 @@ public class Factory extends Structure implements Updatable, MessageReceiver {
|
||||
npc.setObjectId(new ObjectId());
|
||||
npc.setX(p.x);
|
||||
npc.setY(p.y);
|
||||
getWorld().addObject(npc);
|
||||
getWorld().incUpdatable();
|
||||
return npc;
|
||||
}
|
||||
|
||||
@ -114,8 +122,6 @@ public class Factory extends Structure implements Updatable, MessageReceiver {
|
||||
npc.setObjectId(new ObjectId());
|
||||
npc.setX(p.x);
|
||||
npc.setY(p.y);
|
||||
getWorld().addObject(npc);
|
||||
getWorld().incUpdatable();
|
||||
|
||||
this.locked = true;
|
||||
this.programIndex = 0;
|
||||
@ -126,8 +132,6 @@ public class Factory extends Structure implements Updatable, MessageReceiver {
|
||||
@Override
|
||||
public boolean sendMessage(char[] message) {
|
||||
|
||||
String strMessage = String.valueOf(message);
|
||||
|
||||
if (locked) {
|
||||
Settlement settlement = NpcPlugin.settlementMap.get(getWorld().getId());
|
||||
|
||||
@ -136,10 +140,10 @@ public class Factory extends Structure implements Updatable, MessageReceiver {
|
||||
|
||||
return true;
|
||||
}
|
||||
} else if (programIndex <= 2048) { //todo config
|
||||
} else if (programIndex <= PROGRAM_SIZE) {
|
||||
|
||||
if (programIndex == 0) {
|
||||
program = new char[2048];
|
||||
program = new char[PROGRAM_SIZE];
|
||||
}
|
||||
|
||||
System.arraycopy(message, 0, program, programIndex, message.length);
|
||||
|
@ -2,6 +2,7 @@ package net.simon987.npcplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.*;
|
||||
import net.simon987.server.event.ObjectDeathEvent;
|
||||
import net.simon987.server.game.item.Item;
|
||||
import net.simon987.server.game.item.ItemVoid;
|
||||
import net.simon987.server.game.objects.Action;
|
||||
@ -87,6 +88,11 @@ public class HackedNPC extends NonPlayerCharacter implements ControllableUnit {
|
||||
module.update();
|
||||
}
|
||||
|
||||
//Self-destroy when age limit is reached
|
||||
if (getAge() >= NonPlayerCharacter.LIFETIME) {
|
||||
setDead(true);
|
||||
}
|
||||
|
||||
//Don't bother calling checkCompleted()
|
||||
getTask().tick(this);
|
||||
}
|
||||
@ -312,6 +318,20 @@ public class HackedNPC extends NonPlayerCharacter implements ControllableUnit {
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onDeadCallback() {
|
||||
|
||||
getWorld().decUpdatable();
|
||||
|
||||
if (getSettlement() != null && getSettlement().getNpcs() != null) {
|
||||
getSettlement().getNpcs().remove(this);
|
||||
}
|
||||
|
||||
GameServer.INSTANCE.getEventDispatcher().dispatch(new ObjectDeathEvent(this));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject debugJsonSerialise() {
|
||||
return jsonSerialise();
|
||||
|
@ -43,12 +43,6 @@ public abstract class NonPlayerCharacter extends GameObject implements Updatable
|
||||
|
||||
private Settlement settlement;
|
||||
|
||||
/**
|
||||
* If set to true, the NPC will be destroyed next tick if it is
|
||||
* not linked to a Factory
|
||||
*/
|
||||
private boolean selfDestroyNextTick = false;
|
||||
|
||||
/**
|
||||
* Age of the npc, in ticks
|
||||
*/
|
||||
@ -92,11 +86,7 @@ public abstract class NonPlayerCharacter extends GameObject implements Updatable
|
||||
|
||||
//Destroy NPCs that are not linked with a Settlement
|
||||
if (settlement == null) {
|
||||
if (selfDestroyNextTick) {
|
||||
setDead(true);
|
||||
}
|
||||
|
||||
selfDestroyNextTick = true;
|
||||
setDead(true);
|
||||
}
|
||||
|
||||
//Heal the NPC
|
||||
|
@ -37,7 +37,8 @@ public class WorldCreationListener implements GameEventListener {
|
||||
World world = (World) event.getSource();
|
||||
|
||||
try {
|
||||
NpcPlugin.settlementMap.put(world.getId(), new Settlement(world));
|
||||
Settlement settlement = new Settlement(world);
|
||||
NpcPlugin.settlementMap.put(world.getId(), settlement);
|
||||
} catch (WorldGenerationException e) {
|
||||
LogManager.LOGGER.fine(String.format("Exception during settlement generation: %s.",
|
||||
e.getMessage()));
|
||||
|
@ -22,7 +22,9 @@
|
||||
<orderEntry type="module" module-name="Server" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-text:1.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.7" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongo-java-driver:3.7.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-sync:3.9.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:bson:3.9.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-core:3.9.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-core:5.0.5.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.0.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.0.6.RELEASE" level="project" />
|
||||
|
@ -21,7 +21,9 @@
|
||||
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-text:1.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.7" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongo-java-driver:3.7.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-sync:3.9.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:bson:3.9.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-core:3.9.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-core:5.0.5.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.0.6.RELEASE" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.0.6.RELEASE" level="project" />
|
||||
|
@ -106,8 +106,8 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongo-java-driver</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<artifactId>mongodb-driver-sync</artifactId>
|
||||
<version>3.9.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
|
@ -1,9 +1,7 @@
|
||||
package net.simon987.server;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoCursor;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import com.mongodb.MongoClientException;
|
||||
import com.mongodb.client.*;
|
||||
import com.mongodb.client.model.ReplaceOptions;
|
||||
import net.simon987.server.crypto.CryptoProvider;
|
||||
import net.simon987.server.crypto.SecretKeyGenerator;
|
||||
@ -58,7 +56,9 @@ public class GameServer implements Runnable {
|
||||
public GameServer() {
|
||||
this.config = new ServerConfiguration("config.properties");
|
||||
|
||||
mongo = new MongoClient(config.getString("mongo_address"), config.getInt("mongo_port"));
|
||||
String connString = String.format("mongodb://%s:%d",
|
||||
config.getString("mongo_address"), config.getInt("mongo_port"));
|
||||
mongo = MongoClients.create(connString);
|
||||
MongoDatabase db = mongo.getDatabase(config.getString("mongo_dbname"));
|
||||
|
||||
MongoCollection<Document> userCollection = db.getCollection("user");
|
||||
@ -243,7 +243,16 @@ public class GameServer implements Runnable {
|
||||
public void save() {
|
||||
|
||||
LogManager.LOGGER.info("Saving to MongoDB | W:" + gameUniverse.getWorldCount() + " | U:" + gameUniverse.getUserCount());
|
||||
|
||||
ClientSession session = null;
|
||||
try {
|
||||
try {
|
||||
session = mongo.startSession();
|
||||
session.startTransaction();
|
||||
} catch (MongoClientException e) {
|
||||
LogManager.LOGGER.fine("Could not create mongoDB session, will not start a transaction.");
|
||||
}
|
||||
|
||||
MongoDatabase db = mongo.getDatabase(config.getString("mongo_dbname"));
|
||||
ReplaceOptions updateOptions = new ReplaceOptions();
|
||||
updateOptions.upsert(true);
|
||||
@ -284,11 +293,18 @@ public class GameServer implements Runnable {
|
||||
|
||||
//A constant id ensures only one entry is kept and updated, instead of a new entry created every save.
|
||||
server.replaceOne(new Document("_id", "serverinfo"), serverObj, updateOptions);
|
||||
if (session != null) {
|
||||
session.commitTransaction();
|
||||
}
|
||||
|
||||
LogManager.LOGGER.info("" + insertedWorlds + " worlds saved, " + unloaded_worlds + " unloaded");
|
||||
} catch (Exception e) {
|
||||
LogManager.LOGGER.severe("Problem happened during save function");
|
||||
e.printStackTrace();
|
||||
|
||||
if (session != null) {
|
||||
session.commitTransaction();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@ import spark.Spark;
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
ServerConfiguration config = new ServerConfiguration("config.properties");
|
||||
LogManager.initialize(config);
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package net.simon987.server.game;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoCursor;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
@ -20,7 +20,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class GameUniverse {
|
||||
|
||||
//private ArrayList<World> worlds;
|
||||
private ConcurrentHashMap<String, World> worlds;
|
||||
//username:user
|
||||
private ConcurrentHashMap<String, User> users;
|
||||
@ -233,6 +232,7 @@ public class GameUniverse {
|
||||
}
|
||||
|
||||
LogManager.LOGGER.severe("Couldn't find object: " + id);
|
||||
Thread.dumpStack();
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -40,7 +40,7 @@ wg_maxCopperCount=2
|
||||
wg_fluidCenterPointMin=0
|
||||
wg_fluidCenterPointMax=2
|
||||
#CPU
|
||||
tick_length=1000
|
||||
tick_length=50
|
||||
org_offset=512
|
||||
stack_bottom=65536
|
||||
memory_size=65536
|
||||
@ -67,7 +67,7 @@ npc_lifetime=1024
|
||||
npc_max_factory_distance=3
|
||||
factory_max_npc_count=16
|
||||
factory_program_size=1024
|
||||
settlement_spawn_rate=5
|
||||
settlement_spawn_rate=35
|
||||
harvester_hp_max=100
|
||||
harvester_regen=5
|
||||
harvester_biomass_drop_count=8
|
||||
|
@ -115,7 +115,6 @@
|
||||
.bottom-panel {
|
||||
min-height: 18px;
|
||||
max-height: 100%;
|
||||
height: 235px;
|
||||
width: 100%;
|
||||
position: fixed;
|
||||
bottom: 0;
|
||||
|
4
Server/src/main/resources/static/js/mar.js
vendored
4
Server/src/main/resources/static/js/mar.js
vendored
@ -1368,7 +1368,7 @@ var VaultDoor = (function (_super) {
|
||||
_this.anchor.set(0.55, 0.55);
|
||||
_this.inputEnabled = true;
|
||||
_this.events.onInputDown.add(function (self) {
|
||||
Debug.goToHex("7FFF", "7FFF", "v" + self.id + "-");
|
||||
Debug.goToHex("7FFF", "7FFF", "v" + self.id);
|
||||
document.body.style.cursor = 'default';
|
||||
document.body.setAttribute("title", "");
|
||||
}, _this);
|
||||
@ -1449,7 +1449,7 @@ var ElectricBox = (function (_super) {
|
||||
var Portal = (function (_super) {
|
||||
__extends(Portal, _super);
|
||||
function Portal(json) {
|
||||
var _this = _super.call(this, Util.getIsoX(json.x), Util.getIsoY(json.y), 15, "sheet", "objects/Portal") || this;
|
||||
var _this = _super.call(this, Util.getIsoX(json.x), Util.getIsoY(json.y), 15, "sheet", "objects/portal") || this;
|
||||
_this.anchor.set(0.5, 0.3);
|
||||
_this.tint = config.portal.tint;
|
||||
_this.setText("Portal");
|
||||
|
@ -118,11 +118,7 @@
|
||||
|
||||
#parse("footer.vm")
|
||||
|
||||
##Console
|
||||
<style>
|
||||
</style>
|
||||
|
||||
<div class="bottom-panel">
|
||||
<div class="bottom-panel" style="height: 0">
|
||||
<div class="splitter-horizontal"></div>
|
||||
<div class="console-wrapper">
|
||||
<div class="console-side-bar">
|
||||
@ -151,7 +147,6 @@
|
||||
<button class="dropdown-item" onclick="mar.client.consoleScreen.setMode(4)">64 chars</button>
|
||||
</div>
|
||||
</div>
|
||||
</p>
|
||||
<p>
|
||||
<button id="resetButton" class="btn btn-danger btn-shadow" style="margin-left: 0"
|
||||
data-container="body" data-toggle="popover" data-trigger="hover"
|
||||
@ -163,9 +158,7 @@
|
||||
<div class="piece scanlines noclick"></div>
|
||||
<div class="piece glow noclick"></div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<script>
|
||||
|
@ -771,7 +771,7 @@ class VaultDoor extends GameObject {
|
||||
|
||||
this.inputEnabled = true;
|
||||
this.events.onInputDown.add(function (self: VaultDoor) {
|
||||
Debug.goToHex("7FFF", "7FFF", "v" + self.id + "-");
|
||||
Debug.goToHex("7FFF", "7FFF", "v" + self.id);
|
||||
document.body.style.cursor = 'default';
|
||||
document.body.setAttribute("title", "")
|
||||
}, this);
|
||||
@ -873,7 +873,7 @@ class Portal extends GameObject {
|
||||
}
|
||||
|
||||
constructor(json) {
|
||||
super(Util.getIsoX(json.x), Util.getIsoY(json.y), 15, "sheet", "objects/Portal");
|
||||
super(Util.getIsoX(json.x), Util.getIsoY(json.y), 15, "sheet", "objects/portal");
|
||||
this.anchor.set(0.5, 0.3);
|
||||
this.tint = config.portal.tint;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user