Unregistered users can login on the game server (read-only)

This commit is contained in:
simon 2017-11-04 14:37:33 -04:00
parent cdfdab09f8
commit 36d433fdce
9 changed files with 132 additions and 43 deletions

View File

@ -10,6 +10,11 @@ import java.net.InetSocketAddress;
public class Main {
public static void main(String[] args){
//TODO: Confirmation on code upload
//TODO: Copper/Iron/Biomass: on hover only
//TODO: Docs
//TODO:
LogManager.initialize();
ServerConfiguration config = new ServerConfiguration(new File("config.properties"));

View File

@ -86,7 +86,7 @@ public class GameUniverse implements JSONSerialisable{
return null;
}
public User getOrCreateUser(String username){
public User getOrCreateUser(String username, boolean makeControlledUnit){
User user = getUser(username);
if(user != null) {
@ -96,7 +96,12 @@ public class GameUniverse implements JSONSerialisable{
LogManager.LOGGER.info("Creating new User: " + username);
try {
user = new User();
if(makeControlledUnit) {
user = new User();
} else {
user = new User(null);
}
user.setUsername(username);
user.setCpu(new CPU(GameServer.INSTANCE.getConfig(), user));
user.setUserCode(GameServer.INSTANCE.getConfig().getString("new_user_code"));
@ -209,4 +214,20 @@ public class GameUniverse implements JSONSerialisable{
public int getNextObjectId() {
return ++nextObjectId;
}
public String getGuestUsername() {
int i = 1;
while (i < 1000) { //todo get Max guest user cap from config
if(getUser("guest" + String.valueOf(i)) != null) {
i++;
continue;
}
return "guest" + String.valueOf(i);
}
return null;
}
}

View File

@ -11,12 +11,26 @@ public class CodeRequestHandler implements MessageHandler {
LogManager.LOGGER.info("(WS) Code request from " + user.getUser().getUsername());
JSONObject response = new JSONObject();
if(user.isGuest()) {
JSONObject response = new JSONObject();
response.put("t", "code");
response.put("code", "; Create a free account to control your own Cubot with assembly language!"); //todo load from config
user.getWebSocket().send(response.toJSONString());
} else {
JSONObject response = new JSONObject();
response.put("t", "code");
response.put("code", user.getUser().getUserCode());
user.getWebSocket().send(response.toJSONString());
}
response.put("t", "code");
response.put("code", user.getUser().getUserCode());
user.getWebSocket().send(response.toJSONString());
}
}

View File

@ -14,18 +14,25 @@ public class CodeUploadHandler implements MessageHandler {
LogManager.LOGGER.info("(WS) Code upload from " + user.getUser().getUsername());
//TODO Should we wait at the end of the tick to modify the CPU ?
user.getUser().setUserCode((String)json.get("code"));
if(user.isGuest()) {
//Ignore
AssemblyResult ar = new Assembler(user.getUser().getCpu().getInstructionSet(),
user.getUser().getCpu().getRegisterSet(),
GameServer.INSTANCE.getConfig()).parse(user.getUser().getUserCode());
} else {
//TODO Should we wait at the end of the tick to modify the CPU ?
user.getUser().setUserCode((String)json.get("code"));
AssemblyResult ar = new Assembler(user.getUser().getCpu().getInstructionSet(),
user.getUser().getCpu().getRegisterSet(),
GameServer.INSTANCE.getConfig()).parse(user.getUser().getUserCode());
user.getUser().getCpu().getMemory().clear();
//Write assembled code to mem
user.getUser().getCpu().getMemory().write((char) ar.origin, ar.bytes, ar.bytes.length);
user.getUser().getCpu().setCodeSegmentOffset(ar.origin);
}
user.getUser().getCpu().getMemory().clear();
//Write assembled code to mem
user.getUser().getCpu().getMemory().write((char) ar.origin, ar.bytes, ar.bytes.length);
user.getUser().getCpu().setCodeSegmentOffset(ar.origin);
}
}

View File

@ -8,13 +8,16 @@ public class KeypressHandler implements MessageHandler {
@Override
public void handle(OnlineUser user, JSONObject json) {
if(json.get("t").equals("k")){
LogManager.LOGGER.info("(WS) Received keypress");
if(!user.isGuest()) {
if(json.get("t").equals("k")){
int key = (int)(long)json.get("k");
LogManager.LOGGER.info("(WS) Received keypress");
user.getUser().getControlledUnit().getKeyboardBuffer().add(key);
int key = (int)(long)json.get("k");
user.getUser().getControlledUnit().getKeyboardBuffer().add(key);
}
}
}
}

View File

@ -10,6 +10,8 @@ public class OnlineUser {
private WebSocket webSocket;
private boolean guest;
/**
* Associated game user (if authenticated)
*/
@ -39,4 +41,12 @@ public class OnlineUser {
public void setAuthenticated(boolean authenticated) {
this.authenticated = authenticated;
}
public void setGuest(boolean guest) {
this.guest = guest;
}
public boolean isGuest() {
return guest;
}
}

View File

@ -68,7 +68,7 @@ public class SocketServer extends WebSocketServer {
String username = database.validateAuthToken(message);
if (username != null) {
User user = GameServer.INSTANCE.getGameUniverse().getOrCreateUser(username);
User user = GameServer.INSTANCE.getGameUniverse().getOrCreateUser(username, true);
LogManager.LOGGER.info("(WS) User was successfully authenticated: " + user.getUsername());
@ -78,13 +78,19 @@ public class SocketServer extends WebSocketServer {
conn.send("{\"t\":\"auth\", \"m\":\"ok\"}");
} else {
LogManager.LOGGER.info("(WS) Unsuccessful authentication attempt " + conn.getRemoteSocketAddress());
conn.send("{\"t\":\"auth\", \"m\":\"failed\"}");
conn.close();
User user = GameServer.INSTANCE.getGameUniverse().getOrCreateUser(GameServer.INSTANCE.getGameUniverse().getGuestUsername(), false);
onlineUser.setUser(user);
onlineUser.setAuthenticated(true);
onlineUser.setGuest(true);
LogManager.LOGGER.info("(WS) Created guest user " +
onlineUser.getUser().getUsername() + conn.getRemoteSocketAddress());
conn.send("{\"t\":\"auth\", \"m\":\"ok\"}");
}
}
}
} else {
@ -125,18 +131,27 @@ public class SocketServer extends WebSocketServer {
for (OnlineUser user : userManager.getOnlineUsers()) {
if (user.getWebSocket().isOpen()) {
//Send keyboard updated buffer
try{
ArrayList<Integer> kbBuffer = user.getUser().getControlledUnit().getKeyboardBuffer();
JSONArray keys = new JSONArray();
keys.addAll(kbBuffer);
json.put("keys", keys);
//Send tick message
if(user.isGuest()) {
user.getWebSocket().send(json.toJSONString());
} catch (NullPointerException e){
//User is online but not completely initialised
} else {
//Send keyboard updated buffer
try{
ArrayList<Integer> kbBuffer = user.getUser().getControlledUnit().getKeyboardBuffer();
JSONArray keys = new JSONArray();
keys.addAll(kbBuffer);
json.put("keys", keys);
//Send tick message
user.getWebSocket().send(json.toJSONString());
} catch (NullPointerException e){
//User is online but not completely initialised
}
}
}
}

View File

@ -1,5 +1,6 @@
package net.simon987.server.webserver;
import net.simon987.server.GameServer;
import net.simon987.server.game.GameObject;
import net.simon987.server.logging.LogManager;
import org.json.simple.JSONObject;
@ -11,18 +12,33 @@ public class UserInfoRequestHandler implements MessageHandler {
public void handle(OnlineUser user, JSONObject message) {
if (message.get("t").equals("userInfo")) {
LogManager.LOGGER.info("(WS) User info request from " + user.getUser().getUsername());
GameObject object = (GameObject)user.getUser().getControlledUnit();
if(user.isGuest()) {
JSONObject json = new JSONObject();
json.put("t", "userInfo");
json.put("worldX", GameServer.INSTANCE.getConfig().getInt("new_user_worldX"));
json.put("worldY", GameServer.INSTANCE.getConfig().getInt("new_user_worldY"));
json.put("x", 1);
json.put("y", 1);
user.getWebSocket().send(json.toJSONString());
} else {
GameObject object = (GameObject)user.getUser().getControlledUnit();
JSONObject json = new JSONObject();
json.put("t", "userInfo");
json.put("worldX", object.getWorld().getX());
json.put("worldY", object.getWorld().getY());
json.put("x", object.getX());
json.put("y", object.getY());
user.getWebSocket().send(json.toJSONString());
}
JSONObject json = new JSONObject();
json.put("t", "userInfo");
json.put("worldX", object.getWorld().getX());
json.put("worldY", object.getWorld().getY());
json.put("x", object.getX());
json.put("y", object.getY());
user.getWebSocket().send(json.toJSONString());
}
}

View File

@ -22,8 +22,6 @@ memory_size=65536
new_user_interval=10
# todo set to 10^
# Initial location of new user's controlled unit
new_user_x=7
new_user_y=15
new_user_worldX = 0
new_user_worldY = 0
# Effect when the new user's controlled unit spawns