Newly registered users can now login on the game server,

This commit is contained in:
simon
2017-11-04 10:41:57 -04:00
parent 56c0bee586
commit cdfdab09f8
13 changed files with 162 additions and 50 deletions

View File

@@ -10,9 +10,6 @@ import java.net.InetSocketAddress;
public class Main {
public static void main(String[] args){
//todo: User registration
//todo: Display usernames under cubot
LogManager.initialize();
ServerConfiguration config = new ServerConfiguration(new File("config.properties"));

View File

@@ -18,7 +18,7 @@ public class MulInstruction extends Instruction {
@Override
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);
if (hWord != 0) {

View File

@@ -1,5 +1,7 @@
package net.simon987.server.game;
import net.simon987.server.user.User;
import java.util.ArrayList;
public interface ControllableUnit {
@@ -8,6 +10,8 @@ public interface ControllableUnit {
void setKeyboardBuffer(ArrayList<Integer> kbBuffer);
void setParent(User user);
ArrayList<Integer> getKeyboardBuffer();
}

View File

@@ -1,6 +1,10 @@
package net.simon987.server.game;
import net.simon987.server.GameServer;
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.io.JSONSerialisable;
import net.simon987.server.logging.LogManager;
@@ -82,6 +86,43 @@ public class GameUniverse implements JSONSerialisable{
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) {
for (World world : worlds) {

View File

@@ -4,9 +4,11 @@ package net.simon987.server.game;
import net.simon987.server.io.JSONSerialisable;
import org.json.simple.JSONObject;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Random;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
@@ -157,4 +159,27 @@ public class TileMap implements JSONSerialisable {
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);
}
}
}
}

View File

@@ -1,10 +1,13 @@
package net.simon987.server.game;
import net.simon987.server.game.pathfinding.Pathfinder;
import net.simon987.server.io.JSONSerialisable;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
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
*

View File

@@ -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
*/
@@ -192,7 +169,7 @@ public class WorldGenerator {
for (int i = 0; i < ironCount; i++) {
Point p = getRandomPlainTile(world.getTileMap().getTiles());
Point p = world.getTileMap().getRandomPlainTile();
if (p != null) {
world.getTileMap().getTiles()[p.x][p.y] = TileMap.IRON_TILE;
@@ -200,7 +177,7 @@ public class WorldGenerator {
}
for (int i = 0; i < copperCount; i++) {
Point p = getRandomPlainTile(world.getTileMap().getTiles());
Point p = world.getTileMap().getRandomPlainTile();
if (p != null) {
world.getTileMap().getTiles()[p.x][p.y] = TileMap.COPPER_TILE;

View File

@@ -55,6 +55,8 @@ public class User implements JSONSerialisable{
user.username = (String)userJson.get("username");
user.userCode = (String)userJson.get("code");
user.getControlledUnit().setParent(user);
user.cpu = CPU.deserialize((JSONObject)userJson.get("cpu"), user);
return user;

View File

@@ -62,20 +62,26 @@ public class SocketServer extends WebSocketServer {
} else {
LogManager.LOGGER.info("(WS) Received message from unauthenticated user " + conn.getRemoteSocketAddress());
String username = database.validateAuthToken(message);
User user = GameServer.INSTANCE.getGameUniverse().getUser(username);
//We expect a 128 characters long token
if(message.length() == 128) {
if (user != null) {
LogManager.LOGGER.info("(WS) User was successfully authenticated: " + user.getUsername());
String username = database.validateAuthToken(message);
onlineUser.setUser(user);
onlineUser.setAuthenticated(true);
if (username != null) {
User user = GameServer.INSTANCE.getGameUniverse().getOrCreateUser(username);
conn.send("{\"t\":\"auth\", \"m\":\"ok\"}");
LogManager.LOGGER.info("(WS) User was successfully authenticated: " + user.getUsername());
} else {
LogManager.LOGGER.info("(WS) Unsuccessful authentication attempt " + conn.getRemoteSocketAddress());
conn.send("{\"t\":\"auth\", \"m\":\"failed\"}");
onlineUser.setUser(user);
onlineUser.setAuthenticated(true);
conn.send("{\"t\":\"auth\", \"m\":\"ok\"}");
} else {
LogManager.LOGGER.info("(WS) Unsuccessful authentication attempt " + conn.getRemoteSocketAddress());
conn.send("{\"t\":\"auth\", \"m\":\"failed\"}");
conn.close();
}
}