mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-19 18:46:43 +00:00
Editor upload & reload buttons are now working. Floppy upload & download working.
This commit is contained in:
parent
315e33055e
commit
2c856aae80
@ -154,7 +154,6 @@ public class GameServer implements Runnable {
|
||||
GameEvent event = new TickEvent(gameUniverse.getTime());
|
||||
eventDispatcher.dispatch(event); //Ignore cancellation
|
||||
|
||||
|
||||
//Process user code
|
||||
for (User user : gameUniverse.getUsers()) {
|
||||
|
||||
@ -189,7 +188,7 @@ public class GameServer implements Runnable {
|
||||
save();
|
||||
}
|
||||
|
||||
// socketServer.tick();
|
||||
socketServer.tick();
|
||||
|
||||
// LogManager.LOGGER.info("Processed " + gameUniverse.getWorldCount() + " worlds (" + updatedWorlds +
|
||||
// " updated)");
|
||||
|
@ -2,6 +2,7 @@ package net.simon987.server;
|
||||
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import net.simon987.server.web.WebServer;
|
||||
import spark.Spark;
|
||||
|
||||
|
||||
public class Main {
|
||||
@ -10,12 +11,15 @@ public class Main {
|
||||
ServerConfiguration config = new ServerConfiguration("config.properties");
|
||||
LogManager.initialize(config);
|
||||
|
||||
//Load
|
||||
GameServer.INSTANCE.load();
|
||||
(new Thread(GameServer.INSTANCE)).start();
|
||||
|
||||
//Web server
|
||||
new WebServer(GameServer.INSTANCE.getConfig());
|
||||
WebServer webServer = new WebServer(GameServer.INSTANCE.getConfig());
|
||||
|
||||
Spark.awaitInitialization();
|
||||
GameServer.INSTANCE.setSocketServer(webServer.getSocketServer());
|
||||
|
||||
(new Thread(GameServer.INSTANCE)).start();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,36 @@
|
||||
package net.simon987.server.web;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import spark.Request;
|
||||
import spark.Response;
|
||||
import spark.Route;
|
||||
|
||||
public class FloppyDownloadRoute implements Route {
|
||||
|
||||
@Override
|
||||
public Object handle(Request request, Response response) {
|
||||
|
||||
String username = request.session().attribute("username");
|
||||
|
||||
if (username != null) {
|
||||
|
||||
response.header("Content-Type", "application/octet-stream");
|
||||
response.header("Content-Disposition", "filename=\"floppy.bin\"");
|
||||
|
||||
try {
|
||||
return GameServer.INSTANCE.getGameUniverse().getUser(username).getControlledUnit().getFloppyData().getBytes();
|
||||
} catch (Exception e) {
|
||||
String message = "Encountered exception while reading floppy data: " + e.getMessage();
|
||||
|
||||
LogManager.LOGGER.severe(message);
|
||||
return message;
|
||||
}
|
||||
|
||||
} else {
|
||||
response.status(403);
|
||||
return "Not logged in";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package net.simon987.server.web;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import spark.Request;
|
||||
import spark.Response;
|
||||
import spark.Route;
|
||||
|
||||
import javax.servlet.MultipartConfigElement;
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class FloppyUploadRoute implements Route {
|
||||
|
||||
@Override
|
||||
public Object handle(Request request, Response response) {
|
||||
|
||||
String username = request.session().attribute("username");
|
||||
|
||||
if (username != null) {
|
||||
try {
|
||||
request.attribute("org.eclipse.jetty.multipartConfig",
|
||||
new MultipartConfigElement("/tmp_floppy", 1474560L, -1L, 0));
|
||||
|
||||
try (InputStream is = request.raw().getPart("floppyData").getInputStream()) {
|
||||
|
||||
if (is.available() == 1474560) {
|
||||
|
||||
byte[] bytes = new byte[1474560];
|
||||
int bytesRead = is.read(bytes);
|
||||
|
||||
if (bytesRead == 1474560) {
|
||||
|
||||
try {
|
||||
GameServer.INSTANCE.getGameUniverse().getUser(username).getControlledUnit().getFloppyData().setBytes(bytes);
|
||||
return "ok";
|
||||
} catch (Exception e) {
|
||||
String message = "Encountered exception while writing floppy data: " + e.getMessage();
|
||||
|
||||
LogManager.LOGGER.severe(message);
|
||||
return message;
|
||||
}
|
||||
|
||||
} else {
|
||||
return "Couldn't read floppy file";
|
||||
}
|
||||
|
||||
} else {
|
||||
return "File size must be 1474560 bytes";
|
||||
}
|
||||
|
||||
} catch (IOException | ServletException e) {
|
||||
e.printStackTrace();
|
||||
return "Error reading floppy file: " + e.getMessage();
|
||||
}
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
return "File exceeds maximum size";
|
||||
}
|
||||
} else {
|
||||
response.status(403);
|
||||
return "Not logged in";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@ package net.simon987.server.web;
|
||||
|
||||
import net.simon987.server.ServerConfiguration;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import net.simon987.server.websocket.FloppyUploadRoute;
|
||||
import net.simon987.server.websocket.SocketServer;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import spark.Spark;
|
||||
@ -12,6 +11,8 @@ import java.util.Properties;
|
||||
|
||||
public class WebServer {
|
||||
|
||||
private SocketServer socketServer;
|
||||
|
||||
public WebServer(ServerConfiguration config) {
|
||||
|
||||
//Velocity config
|
||||
@ -36,7 +37,8 @@ public class WebServer {
|
||||
LogManager.LOGGER.info("(Web) Enabled ssl");
|
||||
}
|
||||
|
||||
Spark.webSocket("/socket", SocketServer.class);
|
||||
socketServer = new SocketServer();
|
||||
Spark.webSocket("/socket", socketServer);
|
||||
|
||||
Spark.get("/", new HomePage(), templateEngine);
|
||||
Spark.get("/leaderboard", new LeaderBoardPage(), templateEngine);
|
||||
@ -49,8 +51,12 @@ public class WebServer {
|
||||
Spark.post("/change_password", new ChangePasswordRoute());
|
||||
Spark.get("/server_info", new ServerInfoRoute());
|
||||
Spark.post("/floppy_upload", new FloppyUploadRoute());
|
||||
Spark.get("/floppy_download", new FloppyDownloadRoute());
|
||||
|
||||
Spark.after((request, response) -> response.header("Content-Encoding", "gzip"));
|
||||
}
|
||||
|
||||
public SocketServer getSocketServer() {
|
||||
return socketServer;
|
||||
}
|
||||
}
|
||||
|
@ -1,40 +0,0 @@
|
||||
package net.simon987.server.websocket;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class FloppyHandler implements MessageHandler {
|
||||
|
||||
SocketServerDatabase db = new SocketServerDatabase(GameServer.INSTANCE.getConfig());
|
||||
|
||||
@Override
|
||||
public void handle(OnlineUser user, JSONObject json) {
|
||||
|
||||
if (json.get("t").equals("floppyDown")) {
|
||||
|
||||
LogManager.LOGGER.fine("(WS) Floppy download request from " + user.getUser().getUsername());
|
||||
|
||||
if (user.getUser().isGuest()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (user.getUser().getControlledUnit().getFloppyData() != null) {
|
||||
byte[] bytes = user.getUser().getControlledUnit().getFloppyData().getBytes();
|
||||
LogManager.LOGGER.severe("TODO FloppyHandler.handle()");
|
||||
}
|
||||
|
||||
|
||||
} else if (json.get("t").equals("floppyUp")) {
|
||||
|
||||
LogManager.LOGGER.fine("(WS) Floppy upload request from " + user.getUser().getUsername());
|
||||
|
||||
//Check newly uploaded file on the database
|
||||
byte[] bytes = db.getFloppy(user.getUser().getUsername());
|
||||
|
||||
if (bytes != null) {
|
||||
user.getUser().getControlledUnit().getFloppyData().setBytes(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
package net.simon987.server.websocket;
|
||||
|
||||
import spark.Request;
|
||||
import spark.Response;
|
||||
import spark.Route;
|
||||
|
||||
import javax.servlet.MultipartConfigElement;
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class FloppyUploadRoute implements Route {
|
||||
|
||||
@Override
|
||||
public Object handle(Request request, Response response) {
|
||||
|
||||
try {
|
||||
request.attribute("org.eclipse.jetty.multipartConfig",
|
||||
new MultipartConfigElement("/tmp_floppy", 1474560L, -1L, 0));
|
||||
|
||||
try (InputStream is = request.raw().getPart("floppyData").getInputStream()) {
|
||||
|
||||
System.out.println(is.available());
|
||||
|
||||
// Use the input stream to create a file
|
||||
} catch (IOException | ServletException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
System.out.println(e.getMessage());
|
||||
return "File exceeds maximum size";
|
||||
}
|
||||
|
||||
return "mh";
|
||||
}
|
||||
}
|
@ -30,7 +30,6 @@ public class SocketServer {
|
||||
messageDispatcher.addHandler(new CodeUploadHandler());
|
||||
messageDispatcher.addHandler(new CodeRequestHandler());
|
||||
messageDispatcher.addHandler(new KeypressHandler());
|
||||
messageDispatcher.addHandler(new FloppyHandler());
|
||||
messageDispatcher.addHandler(new DebugCommandHandler());
|
||||
}
|
||||
|
||||
|
@ -273,3 +273,43 @@
|
||||
flex: 0 0 25%;
|
||||
max-width: 25%;
|
||||
}
|
||||
|
||||
/** https://stackoverflow.com/questions/6410730/ */
|
||||
@-webkit-keyframes rotating /* Safari and Chrome */
|
||||
{
|
||||
from {
|
||||
-webkit-transform: rotate(0deg);
|
||||
-o-transform: rotate(0deg);
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
to {
|
||||
-webkit-transform: rotate(360deg);
|
||||
-o-transform: rotate(360deg);
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes rotating {
|
||||
from {
|
||||
-ms-transform: rotate(0deg);
|
||||
-moz-transform: rotate(0deg);
|
||||
-webkit-transform: rotate(0deg);
|
||||
-o-transform: rotate(0deg);
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
to {
|
||||
-ms-transform: rotate(360deg);
|
||||
-moz-transform: rotate(360deg);
|
||||
-webkit-transform: rotate(360deg);
|
||||
-o-transform: rotate(360deg);
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
.rotating {
|
||||
-webkit-animation: rotating 2s linear infinite;
|
||||
-moz-animation: rotating 2s linear infinite;
|
||||
-ms-animation: rotating 2s linear infinite;
|
||||
-o-animation: rotating 2s linear infinite;
|
||||
animation: rotating 2s linear infinite;
|
||||
}
|
Binary file not shown.
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.1 KiB |
@ -505,7 +505,8 @@ editorOnThemeChange();
|
||||
|
||||
document.getElementById("floppyIn").onchange = function () {
|
||||
|
||||
//document.getElementById("floppyUp").innerHTML = "<i class=\"fa fa-cog fa-spin fa-fw\" aria-hidden=\"true\"></i>";
|
||||
document.getElementById("floppyUp").innerHTML = '<i class="mi rotating">cached</i> Floppy';
|
||||
|
||||
|
||||
var formData = new FormData(document.getElementById("floppyForm"));
|
||||
|
||||
@ -517,8 +518,6 @@ document.getElementById("floppyIn").onchange = function () {
|
||||
if (xhr.status === 200) {
|
||||
|
||||
if (xhr.responseText === "ok") {
|
||||
//Upload ok, notify the game server
|
||||
mar.client.notifyFloppyUp();
|
||||
alert("Uploaded floppy disk to the drive!")
|
||||
} else {
|
||||
alert(xhr.responseText)
|
||||
@ -528,7 +527,8 @@ document.getElementById("floppyIn").onchange = function () {
|
||||
alert("Couldn't upload floppy code (" + xhr.status + ")");
|
||||
}
|
||||
|
||||
document.getElementById("floppyUp").innerHTML = "<i class=\"fa fa-long-arrow-up\" aria-hidden=\"true\"></i> <i class=\"fa fa-floppy-o\" aria-hidden=\"true\"></i>";
|
||||
document.getElementById("floppyUp").innerHTML = '<i class="mi">file_upload</i> Floppy';
|
||||
|
||||
};
|
||||
xhr.onerror = function (ev) {
|
||||
ev.preventDefault();
|
||||
|
@ -667,19 +667,6 @@ var GameClient = (function () {
|
||||
this.socket.send(JSON.stringify({ t: "k", k: key }));
|
||||
}
|
||||
};
|
||||
GameClient.prototype.requestFloppy = function () {
|
||||
document.getElementById("floppyDown").innerHTML = "<i class=\"fa fa-cog fa-spin fa-fw\"></i>";
|
||||
if (DEBUG) {
|
||||
console.log("[MAR] Requesting floppy");
|
||||
}
|
||||
this.socket.send(JSON.stringify({ t: "floppyDown" }));
|
||||
};
|
||||
GameClient.prototype.notifyFloppyUp = function () {
|
||||
if (DEBUG) {
|
||||
console.log("[MAR] Notifying the game server of floppy upload");
|
||||
}
|
||||
this.socket.send(JSON.stringify({ t: "floppyUp" }));
|
||||
};
|
||||
GameClient.prototype.requestObjects = function () {
|
||||
if (DEBUG) {
|
||||
console.log("[MAR] Requesting game objects");
|
||||
|
@ -1,6 +1,6 @@
|
||||
<nav class="navbar navbar-expand-lg bg-primary navbar-light">
|
||||
|
||||
<a class="navbar-brand text-mono hvr-grow" href="/" title="Home"><img src="/images/tmp.png"></img></a>
|
||||
<a class="navbar-brand text-mono hvr-grow" href="/" title="Home"><img src="/images/icon.png"></img></a>
|
||||
|
||||
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse"
|
||||
data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
|
||||
|
@ -23,7 +23,6 @@
|
||||
<td>Test1</td>
|
||||
<td>Test2</td>
|
||||
</tr>
|
||||
Account - M.A.R
|
||||
<tr>
|
||||
<td>Test1</td>
|
||||
<td>Test2</td>
|
||||
|
@ -49,23 +49,28 @@
|
||||
|
||||
## EDITOR
|
||||
<div id="editor-tab" style="display: none">
|
||||
#if($session.attribute("username"))
|
||||
|
||||
<div class="row">
|
||||
<div class="col-sm-4">
|
||||
<select title="Select Theme" class="form-control" id="editorTheme"></select>
|
||||
</div>
|
||||
|
||||
<div class="col-sm-2">
|
||||
<button class="btn btn-shadow btn-success text-mono regular-screen" href="#"><i class="mi">file_upload</i>
|
||||
<button onclick="mar.client.uploadCode(ace.edit('editor').getValue())"
|
||||
class="btn btn-shadow btn-success text-mono regular-screen" href="#"><i class="mi">file_upload</i>
|
||||
Upload
|
||||
</button>
|
||||
<button class="btn btn-shadow btn-success text-mono small-screen" href="#"><i class="mi">file_upload</i>
|
||||
</button>
|
||||
</div>
|
||||
<div class="col-sm-2">
|
||||
<button class="btn btn-shadow btn-danger text-mono regular-screen" href="#"><i
|
||||
<button class="btn btn-shadow btn-danger text-mono regular-screen"
|
||||
onclick="mar.client.reloadCode()" href="#"><i
|
||||
class="mi">replay</i> Reload
|
||||
</button>
|
||||
<button class="btn btn-shadow btn-danger text-mono small-screen" href="#"><i
|
||||
<button class="btn btn-shadow btn-danger text-mono small-screen"
|
||||
onclick="mar.client.reloadCode()" href="#"><i
|
||||
class="mi">replay</i></button>
|
||||
</div>
|
||||
<div class="col-sm-2">
|
||||
@ -73,15 +78,18 @@
|
||||
class="btn btn-shadow btn-info text-mono regular-screen" href="#"><i class="mi">file_upload</i>
|
||||
Floppy
|
||||
</button>
|
||||
<button class="btn btn-shadow btn-info text-mono small-screen" href="#"><i class="mi">file_upload</i>
|
||||
<button onclick="document.getElementById('floppyIn').click();"
|
||||
class="btn btn-shadow btn-info text-mono small-screen" href="#"><i class="mi">file_upload</i>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="col-sm-2">
|
||||
<button class="btn btn-shadow btn-info text-mono regular-screen" href="#"><i class="mi">file_download</i>
|
||||
<button class="btn btn-shadow btn-info text-mono regular-screen"
|
||||
onclick="window.location.assign('floppy_download')"><i class="mi">file_download</i>
|
||||
Floppy
|
||||
</button>
|
||||
<button class="btn btn-shadow btn-info text-mono small-screen" href="#"><i class="mi">file_download</i>
|
||||
<button class="btn btn-shadow btn-info text-mono small-screen"
|
||||
onclick="window.location.assign('floppy_download')"><i class="mi">file_download</i>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
@ -93,7 +101,11 @@
|
||||
<div id="editor"></div>
|
||||
|
||||
<script src="js/ace/ace.js" type="text/javascript" charset="utf-8"></script>
|
||||
#else
|
||||
<p style="text-align: left"><a href="/account">Login</a> to use the editor</p>
|
||||
#end
|
||||
<script src="js/editor.js"></script>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -142,7 +154,7 @@
|
||||
</p>
|
||||
</div>
|
||||
<div class="noisy">
|
||||
<div id="consoleText" class="piece output noclick ctr-selection ctr-text">Hello World</div>
|
||||
<div id="consoleText" class="piece output noclick ctr-selection ctr-text"></div>
|
||||
<div class="piece scanlines noclick"></div>
|
||||
<div class="piece glow noclick"></div>
|
||||
</div>
|
||||
|
@ -329,26 +329,7 @@ class GameClient {
|
||||
}
|
||||
}
|
||||
|
||||
public requestFloppy(): void {
|
||||
//Start loading animation
|
||||
document.getElementById("floppyDown").innerHTML = "<i class=\"fa fa-cog fa-spin fa-fw\"></i>";
|
||||
|
||||
if (DEBUG) {
|
||||
console.log("[MAR] Requesting floppy");
|
||||
}
|
||||
|
||||
this.socket.send(JSON.stringify({t: "floppyDown"}));
|
||||
}
|
||||
|
||||
public notifyFloppyUp(): void {
|
||||
|
||||
if (DEBUG) {
|
||||
console.log("[MAR] Notifying the game server of floppy upload");
|
||||
}
|
||||
|
||||
this.socket.send(JSON.stringify({t: "floppyUp"}));
|
||||
}
|
||||
|
||||
à
|
||||
public requestObjects(): void {
|
||||
if (DEBUG) {
|
||||
console.log("[MAR] Requesting game objects");
|
||||
|
Loading…
x
Reference in New Issue
Block a user