mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-19 02:36:41 +00:00
Unregistered users can login on the game server (read-only)
This commit is contained in:
parent
cdfdab09f8
commit
36d433fdce
@ -10,6 +10,11 @@ import java.net.InetSocketAddress;
|
|||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
|
|
||||||
|
//TODO: Confirmation on code upload
|
||||||
|
//TODO: Copper/Iron/Biomass: on hover only
|
||||||
|
//TODO: Docs
|
||||||
|
//TODO:
|
||||||
|
|
||||||
LogManager.initialize();
|
LogManager.initialize();
|
||||||
ServerConfiguration config = new ServerConfiguration(new File("config.properties"));
|
ServerConfiguration config = new ServerConfiguration(new File("config.properties"));
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ public class GameUniverse implements JSONSerialisable{
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User getOrCreateUser(String username){
|
public User getOrCreateUser(String username, boolean makeControlledUnit){
|
||||||
User user = getUser(username);
|
User user = getUser(username);
|
||||||
|
|
||||||
if(user != null) {
|
if(user != null) {
|
||||||
@ -96,7 +96,12 @@ public class GameUniverse implements JSONSerialisable{
|
|||||||
LogManager.LOGGER.info("Creating new User: " + username);
|
LogManager.LOGGER.info("Creating new User: " + username);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
user = new User();
|
if(makeControlledUnit) {
|
||||||
|
user = new User();
|
||||||
|
} else {
|
||||||
|
user = new User(null);
|
||||||
|
}
|
||||||
|
|
||||||
user.setUsername(username);
|
user.setUsername(username);
|
||||||
user.setCpu(new CPU(GameServer.INSTANCE.getConfig(), user));
|
user.setCpu(new CPU(GameServer.INSTANCE.getConfig(), user));
|
||||||
user.setUserCode(GameServer.INSTANCE.getConfig().getString("new_user_code"));
|
user.setUserCode(GameServer.INSTANCE.getConfig().getString("new_user_code"));
|
||||||
@ -209,4 +214,20 @@ public class GameUniverse implements JSONSerialisable{
|
|||||||
public int getNextObjectId() {
|
public int getNextObjectId() {
|
||||||
return ++nextObjectId;
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,12 +11,26 @@ public class CodeRequestHandler implements MessageHandler {
|
|||||||
|
|
||||||
LogManager.LOGGER.info("(WS) Code request from " + user.getUser().getUsername());
|
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());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,18 +14,25 @@ public class CodeUploadHandler implements MessageHandler {
|
|||||||
|
|
||||||
LogManager.LOGGER.info("(WS) Code upload from " + user.getUser().getUsername());
|
LogManager.LOGGER.info("(WS) Code upload from " + user.getUser().getUsername());
|
||||||
|
|
||||||
//TODO Should we wait at the end of the tick to modify the CPU ?
|
if(user.isGuest()) {
|
||||||
user.getUser().setUserCode((String)json.get("code"));
|
//Ignore
|
||||||
|
|
||||||
AssemblyResult ar = new Assembler(user.getUser().getCpu().getInstructionSet(),
|
} else {
|
||||||
user.getUser().getCpu().getRegisterSet(),
|
//TODO Should we wait at the end of the tick to modify the CPU ?
|
||||||
GameServer.INSTANCE.getConfig()).parse(user.getUser().getUserCode());
|
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);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,13 +8,16 @@ public class KeypressHandler implements MessageHandler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(OnlineUser user, JSONObject json) {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,8 @@ public class OnlineUser {
|
|||||||
|
|
||||||
private WebSocket webSocket;
|
private WebSocket webSocket;
|
||||||
|
|
||||||
|
private boolean guest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Associated game user (if authenticated)
|
* Associated game user (if authenticated)
|
||||||
*/
|
*/
|
||||||
@ -39,4 +41,12 @@ public class OnlineUser {
|
|||||||
public void setAuthenticated(boolean authenticated) {
|
public void setAuthenticated(boolean authenticated) {
|
||||||
this.authenticated = authenticated;
|
this.authenticated = authenticated;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setGuest(boolean guest) {
|
||||||
|
this.guest = guest;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isGuest() {
|
||||||
|
return guest;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ public class SocketServer extends WebSocketServer {
|
|||||||
String username = database.validateAuthToken(message);
|
String username = database.validateAuthToken(message);
|
||||||
|
|
||||||
if (username != null) {
|
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());
|
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\"}");
|
conn.send("{\"t\":\"auth\", \"m\":\"ok\"}");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
LogManager.LOGGER.info("(WS) Unsuccessful authentication attempt " + conn.getRemoteSocketAddress());
|
|
||||||
conn.send("{\"t\":\"auth\", \"m\":\"failed\"}");
|
User user = GameServer.INSTANCE.getGameUniverse().getOrCreateUser(GameServer.INSTANCE.getGameUniverse().getGuestUsername(), false);
|
||||||
conn.close();
|
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 {
|
} else {
|
||||||
@ -125,18 +131,27 @@ public class SocketServer extends WebSocketServer {
|
|||||||
for (OnlineUser user : userManager.getOnlineUsers()) {
|
for (OnlineUser user : userManager.getOnlineUsers()) {
|
||||||
|
|
||||||
if (user.getWebSocket().isOpen()) {
|
if (user.getWebSocket().isOpen()) {
|
||||||
//Send keyboard updated buffer
|
|
||||||
try{
|
if(user.isGuest()) {
|
||||||
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());
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package net.simon987.server.webserver;
|
package net.simon987.server.webserver;
|
||||||
|
|
||||||
|
import net.simon987.server.GameServer;
|
||||||
import net.simon987.server.game.GameObject;
|
import net.simon987.server.game.GameObject;
|
||||||
import net.simon987.server.logging.LogManager;
|
import net.simon987.server.logging.LogManager;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
@ -11,18 +12,33 @@ public class UserInfoRequestHandler implements MessageHandler {
|
|||||||
public void handle(OnlineUser user, JSONObject message) {
|
public void handle(OnlineUser user, JSONObject message) {
|
||||||
|
|
||||||
if (message.get("t").equals("userInfo")) {
|
if (message.get("t").equals("userInfo")) {
|
||||||
|
|
||||||
LogManager.LOGGER.info("(WS) User info request from " + user.getUser().getUsername());
|
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());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,8 +22,6 @@ memory_size=65536
|
|||||||
new_user_interval=10
|
new_user_interval=10
|
||||||
# todo set to 10^
|
# todo set to 10^
|
||||||
# Initial location of new user's controlled unit
|
# Initial location of new user's controlled unit
|
||||||
new_user_x=7
|
|
||||||
new_user_y=15
|
|
||||||
new_user_worldX = 0
|
new_user_worldX = 0
|
||||||
new_user_worldY = 0
|
new_user_worldY = 0
|
||||||
# Effect when the new user's controlled unit spawns
|
# Effect when the new user's controlled unit spawns
|
||||||
|
Loading…
x
Reference in New Issue
Block a user