mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-19 18:46:43 +00:00
Newly registered users can now login on the game server,
This commit is contained in:
parent
56c0bee586
commit
cdfdab09f8
@ -1,6 +1,7 @@
|
|||||||
package net.simon987.cubotplugin;
|
package net.simon987.cubotplugin;
|
||||||
|
|
||||||
import net.simon987.server.game.*;
|
import net.simon987.server.game.*;
|
||||||
|
import net.simon987.server.user.User;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -23,6 +24,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
|
|||||||
|
|
||||||
private ArrayList<Integer> keyboardBuffer = new ArrayList<>();
|
private ArrayList<Integer> keyboardBuffer = new ArrayList<>();
|
||||||
|
|
||||||
|
private User parent;
|
||||||
|
|
||||||
public Cubot() {
|
public Cubot() {
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -67,6 +70,9 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
|
|||||||
json.put("heldItem", heldItem);
|
json.put("heldItem", heldItem);
|
||||||
json.put("hp", hp);
|
json.put("hp", hp);
|
||||||
json.put("action", lastAction.ordinal());
|
json.put("action", lastAction.ordinal());
|
||||||
|
if(parent != null){
|
||||||
|
json.put("parent", parent.getUsername()); //Only used client-side for now
|
||||||
|
}
|
||||||
|
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
@ -80,6 +86,7 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
|
|||||||
cubot.hp = (int)(long)json.get("hp");
|
cubot.hp = (int)(long)json.get("hp");
|
||||||
cubot.setDirection(Direction.getDirection((int)(long)json.get("direction")));
|
cubot.setDirection(Direction.getDirection((int)(long)json.get("direction")));
|
||||||
cubot.heldItem = (int)(long)json.get("heldItem");
|
cubot.heldItem = (int)(long)json.get("heldItem");
|
||||||
|
cubot.heldItem = (int)(long)json.get("heldItem");
|
||||||
|
|
||||||
return cubot;
|
return cubot;
|
||||||
|
|
||||||
@ -110,4 +117,12 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
|
|||||||
public void setCurrentAction(CubotAction currentAction) {
|
public void setCurrentAction(CubotAction currentAction) {
|
||||||
this.currentAction = currentAction;
|
this.currentAction = currentAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public User getParent() {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParent(User parent) {
|
||||||
|
this.parent = parent;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,8 @@ import net.simon987.server.event.GameEventListener;
|
|||||||
import net.simon987.server.event.UserCreationEvent;
|
import net.simon987.server.event.UserCreationEvent;
|
||||||
import net.simon987.server.user.User;
|
import net.simon987.server.user.User;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
public class UserCreationListener implements GameEventListener {
|
public class UserCreationListener implements GameEventListener {
|
||||||
@Override
|
@Override
|
||||||
public Class getListenedEventType() {
|
public Class getListenedEventType() {
|
||||||
@ -20,12 +22,18 @@ public class UserCreationListener implements GameEventListener {
|
|||||||
|
|
||||||
|
|
||||||
Cubot cubot = new Cubot();
|
Cubot cubot = new Cubot();
|
||||||
|
|
||||||
cubot.setWorld(GameServer.INSTANCE.getGameUniverse().getWorld(0,0));
|
cubot.setWorld(GameServer.INSTANCE.getGameUniverse().getWorld(0,0));
|
||||||
|
cubot.getWorld().getGameObjects().add(cubot);
|
||||||
|
|
||||||
cubot.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
|
cubot.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
|
||||||
|
|
||||||
cubot.setX(6);
|
cubot.setParent(user);
|
||||||
cubot.setY(6);
|
|
||||||
|
Point point = cubot.getWorld().getRandomPassableTile();
|
||||||
|
|
||||||
|
cubot.setX(point.x);
|
||||||
|
cubot.setY(point.y);
|
||||||
|
|
||||||
user.setControlledUnit(cubot);
|
user.setControlledUnit(cubot);
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ public class WorldCreationListener implements GameEventListener {
|
|||||||
outerLoop:
|
outerLoop:
|
||||||
for (int i = 0; i < treeCount; i++) {
|
for (int i = 0; i < treeCount; i++) {
|
||||||
|
|
||||||
Point p = WorldGenerator.getRandomPlainTile(world.getTileMap().getTiles());
|
Point p = world.getTileMap().getRandomPlainTile();
|
||||||
|
|
||||||
if (p != null) {
|
if (p != null) {
|
||||||
|
|
||||||
|
@ -10,9 +10,6 @@ import java.net.InetSocketAddress;
|
|||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
|
|
||||||
//todo: User registration
|
|
||||||
//todo: Display usernames under cubot
|
|
||||||
|
|
||||||
LogManager.initialize();
|
LogManager.initialize();
|
||||||
ServerConfiguration config = new ServerConfiguration(new File("config.properties"));
|
ServerConfiguration config = new ServerConfiguration(new File("config.properties"));
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ public class MulInstruction extends Instruction {
|
|||||||
@Override
|
@Override
|
||||||
public Status execute(Target src, int srcIndex, Status status) {
|
public Status execute(Target src, int srcIndex, Status status) {
|
||||||
|
|
||||||
int result = (char)cpu.getRegisterSet().getRegister("A").getValue() * (char)src.get(srcIndex);
|
int result = cpu.getRegisterSet().getRegister("A").getValue() * (char)src.get(srcIndex);
|
||||||
|
|
||||||
int hWord = Util.getHigherWord(result);
|
int hWord = Util.getHigherWord(result);
|
||||||
if (hWord != 0) {
|
if (hWord != 0) {
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package net.simon987.server.game;
|
package net.simon987.server.game;
|
||||||
|
|
||||||
|
import net.simon987.server.user.User;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public interface ControllableUnit {
|
public interface ControllableUnit {
|
||||||
@ -8,6 +10,8 @@ public interface ControllableUnit {
|
|||||||
|
|
||||||
void setKeyboardBuffer(ArrayList<Integer> kbBuffer);
|
void setKeyboardBuffer(ArrayList<Integer> kbBuffer);
|
||||||
|
|
||||||
|
void setParent(User user);
|
||||||
|
|
||||||
ArrayList<Integer> getKeyboardBuffer();
|
ArrayList<Integer> getKeyboardBuffer();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package net.simon987.server.game;
|
package net.simon987.server.game;
|
||||||
|
|
||||||
|
import net.simon987.server.GameServer;
|
||||||
import net.simon987.server.ServerConfiguration;
|
import net.simon987.server.ServerConfiguration;
|
||||||
|
import net.simon987.server.assembly.Assembler;
|
||||||
|
import net.simon987.server.assembly.AssemblyResult;
|
||||||
|
import net.simon987.server.assembly.CPU;
|
||||||
import net.simon987.server.assembly.exception.CancelledException;
|
import net.simon987.server.assembly.exception.CancelledException;
|
||||||
import net.simon987.server.io.JSONSerialisable;
|
import net.simon987.server.io.JSONSerialisable;
|
||||||
import net.simon987.server.logging.LogManager;
|
import net.simon987.server.logging.LogManager;
|
||||||
@ -82,6 +86,43 @@ public class GameUniverse implements JSONSerialisable{
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public User getOrCreateUser(String username){
|
||||||
|
User user = getUser(username);
|
||||||
|
|
||||||
|
if(user != null) {
|
||||||
|
return user;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
LogManager.LOGGER.info("Creating new User: " + username);
|
||||||
|
|
||||||
|
try {
|
||||||
|
user = new User();
|
||||||
|
user.setUsername(username);
|
||||||
|
user.setCpu(new CPU(GameServer.INSTANCE.getConfig(), user));
|
||||||
|
user.setUserCode(GameServer.INSTANCE.getConfig().getString("new_user_code"));
|
||||||
|
|
||||||
|
//Compile user code
|
||||||
|
AssemblyResult ar = new Assembler(user.getCpu().getInstructionSet(), user.getCpu().getRegisterSet(),
|
||||||
|
GameServer.INSTANCE.getConfig()).parse(user.getUserCode());
|
||||||
|
|
||||||
|
user.getCpu().getMemory().clear();
|
||||||
|
|
||||||
|
//Write assembled code to mem
|
||||||
|
user.getCpu().getMemory().write((short) ar.origin, ar.bytes, ar.bytes.length);
|
||||||
|
user.getCpu().setCodeSegmentOffset(ar.origin);
|
||||||
|
|
||||||
|
users.add(user);
|
||||||
|
|
||||||
|
return user;
|
||||||
|
|
||||||
|
} catch (CancelledException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public GameObject getObject(int id) {
|
public GameObject getObject(int id) {
|
||||||
|
|
||||||
for (World world : worlds) {
|
for (World world : worlds) {
|
||||||
|
@ -4,9 +4,11 @@ package net.simon987.server.game;
|
|||||||
import net.simon987.server.io.JSONSerialisable;
|
import net.simon987.server.io.JSONSerialisable;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
import java.util.Random;
|
||||||
import java.util.zip.Deflater;
|
import java.util.zip.Deflater;
|
||||||
import java.util.zip.DeflaterOutputStream;
|
import java.util.zip.DeflaterOutputStream;
|
||||||
import java.util.zip.Inflater;
|
import java.util.zip.Inflater;
|
||||||
@ -157,4 +159,27 @@ public class TileMap implements JSONSerialisable {
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Point getRandomPlainTile() {
|
||||||
|
|
||||||
|
Random random = new Random();
|
||||||
|
|
||||||
|
int counter = 0;
|
||||||
|
while (true) {
|
||||||
|
counter++;
|
||||||
|
|
||||||
|
//Prevent infinite loop
|
||||||
|
if (counter >= 2500) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
int rx = random.nextInt(World.WORLD_SIZE);
|
||||||
|
int ry = random.nextInt(World.WORLD_SIZE);
|
||||||
|
|
||||||
|
if (tiles[rx][ry] == TileMap.PLAIN_TILE) {
|
||||||
|
return new Point(rx, ry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
package net.simon987.server.game;
|
package net.simon987.server.game;
|
||||||
|
|
||||||
|
import net.simon987.server.game.pathfinding.Pathfinder;
|
||||||
import net.simon987.server.io.JSONSerialisable;
|
import net.simon987.server.io.JSONSerialisable;
|
||||||
import org.json.simple.JSONArray;
|
import org.json.simple.JSONArray;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class World implements JSONSerialisable{
|
public class World implements JSONSerialisable{
|
||||||
|
|
||||||
@ -168,6 +171,42 @@ public class World implements JSONSerialisable{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a random tile that is empty and passable
|
||||||
|
* The function ensures that a object spawned there will not be trapped
|
||||||
|
* and will be able to leave the World
|
||||||
|
* <br>
|
||||||
|
* Note: This function is quite expensive and shouldn't be used
|
||||||
|
* by some CpuHardware in its current state
|
||||||
|
*
|
||||||
|
* @return random non-blocked tile
|
||||||
|
*/
|
||||||
|
public Point getRandomPassableTile(){
|
||||||
|
Random random = new Random();
|
||||||
|
|
||||||
|
int counter = 0;
|
||||||
|
while (true) {
|
||||||
|
counter++;
|
||||||
|
|
||||||
|
//Prevent infinite loop
|
||||||
|
if (counter >= 2500) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
int rx = random.nextInt(World.WORLD_SIZE);
|
||||||
|
int ry = random.nextInt(World.WORLD_SIZE);
|
||||||
|
|
||||||
|
if(!isTileBlocked(rx, ry)){
|
||||||
|
|
||||||
|
Object path = Pathfinder.findPath(this, rx, ry, 1,1,0);
|
||||||
|
|
||||||
|
if(path != null) {
|
||||||
|
return new Point(rx, ry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the list of game objects at a location
|
* Get the list of game objects at a location
|
||||||
*
|
*
|
||||||
|
@ -81,29 +81,6 @@ public class WorldGenerator {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Point getRandomPlainTile(int[][] tiles) {
|
|
||||||
|
|
||||||
Random random = new Random();
|
|
||||||
|
|
||||||
int counter = 0;
|
|
||||||
while (true) {
|
|
||||||
counter++;
|
|
||||||
|
|
||||||
//Prevent infinite loop
|
|
||||||
if (counter >= 1000) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
int rx = random.nextInt(World.WORLD_SIZE);
|
|
||||||
int ry = random.nextInt(World.WORLD_SIZE);
|
|
||||||
|
|
||||||
if (tiles[rx][ry] == TileMap.PLAIN_TILE) {
|
|
||||||
return new Point(rx, ry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates an empty World
|
* Generates an empty World
|
||||||
*/
|
*/
|
||||||
@ -192,7 +169,7 @@ public class WorldGenerator {
|
|||||||
|
|
||||||
for (int i = 0; i < ironCount; i++) {
|
for (int i = 0; i < ironCount; i++) {
|
||||||
|
|
||||||
Point p = getRandomPlainTile(world.getTileMap().getTiles());
|
Point p = world.getTileMap().getRandomPlainTile();
|
||||||
|
|
||||||
if (p != null) {
|
if (p != null) {
|
||||||
world.getTileMap().getTiles()[p.x][p.y] = TileMap.IRON_TILE;
|
world.getTileMap().getTiles()[p.x][p.y] = TileMap.IRON_TILE;
|
||||||
@ -200,7 +177,7 @@ public class WorldGenerator {
|
|||||||
}
|
}
|
||||||
for (int i = 0; i < copperCount; i++) {
|
for (int i = 0; i < copperCount; i++) {
|
||||||
|
|
||||||
Point p = getRandomPlainTile(world.getTileMap().getTiles());
|
Point p = world.getTileMap().getRandomPlainTile();
|
||||||
|
|
||||||
if (p != null) {
|
if (p != null) {
|
||||||
world.getTileMap().getTiles()[p.x][p.y] = TileMap.COPPER_TILE;
|
world.getTileMap().getTiles()[p.x][p.y] = TileMap.COPPER_TILE;
|
||||||
|
@ -55,6 +55,8 @@ public class User implements JSONSerialisable{
|
|||||||
user.username = (String)userJson.get("username");
|
user.username = (String)userJson.get("username");
|
||||||
user.userCode = (String)userJson.get("code");
|
user.userCode = (String)userJson.get("code");
|
||||||
|
|
||||||
|
user.getControlledUnit().setParent(user);
|
||||||
|
|
||||||
user.cpu = CPU.deserialize((JSONObject)userJson.get("cpu"), user);
|
user.cpu = CPU.deserialize((JSONObject)userJson.get("cpu"), user);
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
|
@ -62,10 +62,14 @@ public class SocketServer extends WebSocketServer {
|
|||||||
} else {
|
} else {
|
||||||
LogManager.LOGGER.info("(WS) Received message from unauthenticated user " + conn.getRemoteSocketAddress());
|
LogManager.LOGGER.info("(WS) Received message from unauthenticated user " + conn.getRemoteSocketAddress());
|
||||||
|
|
||||||
String username = database.validateAuthToken(message);
|
//We expect a 128 characters long token
|
||||||
User user = GameServer.INSTANCE.getGameUniverse().getUser(username);
|
if(message.length() == 128) {
|
||||||
|
|
||||||
|
String username = database.validateAuthToken(message);
|
||||||
|
|
||||||
|
if (username != null) {
|
||||||
|
User user = GameServer.INSTANCE.getGameUniverse().getOrCreateUser(username);
|
||||||
|
|
||||||
if (user != null) {
|
|
||||||
LogManager.LOGGER.info("(WS) User was successfully authenticated: " + user.getUsername());
|
LogManager.LOGGER.info("(WS) User was successfully authenticated: " + user.getUsername());
|
||||||
|
|
||||||
onlineUser.setUser(user);
|
onlineUser.setUser(user);
|
||||||
@ -76,6 +80,8 @@ public class SocketServer extends WebSocketServer {
|
|||||||
} else {
|
} else {
|
||||||
LogManager.LOGGER.info("(WS) Unsuccessful authentication attempt " + conn.getRemoteSocketAddress());
|
LogManager.LOGGER.info("(WS) Unsuccessful authentication attempt " + conn.getRemoteSocketAddress());
|
||||||
conn.send("{\"t\":\"auth\", \"m\":\"failed\"}");
|
conn.send("{\"t\":\"auth\", \"m\":\"failed\"}");
|
||||||
|
conn.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
# MySQL username
|
# MySQL username
|
||||||
mysql_user=mar
|
mysql_user=mar
|
||||||
# MySQL password
|
# MySQL password/
|
||||||
mysql_pass=mar
|
mysql_pass=mar
|
||||||
# MySQL address
|
# MySQL address
|
||||||
mysql_url=jdbc:mysql://localhost:3306/mar?useSSL=false
|
mysql_url=jdbc:mysql://localhost:3306/mar?useSSL=false
|
||||||
save_interval=10
|
save_interval=10
|
||||||
# Web server port
|
# Web server port
|
||||||
webSocket_port=8887
|
webSocket_port=8887
|
||||||
webSocket_host=localhost
|
webSocket_host=0.0.0.0
|
||||||
# ----------------------------------------------
|
# ----------------------------------------------
|
||||||
|
|
||||||
# Length of a tick in ms
|
# Length of a tick in ms
|
||||||
@ -29,13 +29,11 @@ new_user_worldY = 0
|
|||||||
# Effect when the new user's controlled unit spawns
|
# Effect when the new user's controlled unit spawns
|
||||||
new_user_effect=WARNING
|
new_user_effect=WARNING
|
||||||
# Default user code
|
# Default user code
|
||||||
new_user_code=; TODO: Add link to wiki or something here\n\
|
new_user_code=; Welcome to Much Assembly required!\n\
|
||||||
.data\n\
|
; You will find useful information on the game here: https://github.com/simon987/Much-Assembly-Required/wiki\n\
|
||||||
; Declare data here\n\
|
|
||||||
.text\n\
|
.text\n\
|
||||||
main:\n\
|
\t; Write code here\n\
|
||||||
; Write code here\n\
|
\tbrk
|
||||||
brk
|
|
||||||
# Default held item
|
# Default held item
|
||||||
new_user_item=0
|
new_user_item=0
|
||||||
# ----------------------------------------------
|
# ----------------------------------------------
|
||||||
|
Loading…
x
Reference in New Issue
Block a user