Let all versions of Ace run on the server, not just 042300. Add the concept of a homedir so this can be run from a JAR on something like an EC2 instance with the cardconv.py sitting in the homedir

This commit is contained in:
skogaby
2019-01-30 22:32:31 -06:00
parent 3edf9e45d8
commit 3e75e38ca1
11 changed files with 60 additions and 9515 deletions

View File

@@ -75,7 +75,7 @@ public class ButterflyHttpServer {
// Do a static setup of our supported models, modules, etc.
static {
SUPPORTED_MODELS = ImmutableSet.of("mdx_2018042300");
SUPPORTED_MODELS = ImmutableSet.of("mdx");
SUPPORTED_MODULES = ImmutableSet.of("services", "pcbtracker", "message", "facility", "pcbevent",
"package", "eventlog", "tax", "playerdata", "cardmng", "system", "eacoin");
}
@@ -272,7 +272,7 @@ public class ButterflyHttpServer {
LOG.info("Request received: " + requestUriModel + " (" + requestUriModule + "." + requestUriMethod + ")");
// 1) validate the model is supported
if (!SUPPORTED_MODELS.contains(com.buttongames.butterfly.util.StringUtils.getSanitizedModel(requestUriModel))) {
if (!SUPPORTED_MODELS.contains(requestUriModel.split(":")[0].toLowerCase())) {
LOG.warn("Invalid model requested: " + requestUriModel);
throw new InvalidRequestModelException();
}

View File

@@ -41,9 +41,15 @@ public class CardManageRequestHandler extends BaseRequestHandler {
*/
private final ButterflyUserDao userDao;
public CardManageRequestHandler(final CardDao cardDao, final ButterflyUserDao userDao) {
/**
* Helper class for converting card IDs.
*/
private final CardIdUtils cardIdUtils;
public CardManageRequestHandler(final CardDao cardDao, final ButterflyUserDao userDao, final CardIdUtils cardIdUtils) {
this.cardDao = cardDao;
this.userDao = userDao;
this.cardIdUtils = cardIdUtils;
}
/**
@@ -125,7 +131,7 @@ public class CardManageRequestHandler extends BaseRequestHandler {
userDao.create(newUser);
// create the card and save it
final Card card = new Card(newUser, cardType, cardId, CardIdUtils.encodeCardId(cardId),
final Card card = new Card(newUser, cardType, cardId, this.cardIdUtils.encodeCardId(cardId),
StringUtils.getRandomHexString(16), LocalDateTime.now(), LocalDateTime.now());
this.cardDao.create(card);

View File

@@ -134,16 +134,10 @@ public class PlayerDataRequestHandler extends BaseRequestHandler {
/**
* Static list of events for the server.
*/
private static NodeList EVENTS_2018042300;
/**
* Static list of songs for the server.
*/
private static List<Song> SONGS_2018042300;
private static NodeList EVENTS;
static {
loadEvents();
loadMusicDb();
}
public PlayerDataRequestHandler(final ButterflyUserDao userDao, final CardDao cardDao, final ProfileDao profileDao,
@@ -161,35 +155,9 @@ public class PlayerDataRequestHandler extends BaseRequestHandler {
private static void loadEvents() {
try {
final byte[] respBody = ByteStreams.toByteArray(
Main.class.getResourceAsStream("/static_responses/mdx_2018042300/events.xml"));
Main.class.getResourceAsStream("/static_responses/mdx/events.xml"));
final Element doc = XmlUtils.byteArrayToXmlFile(respBody);
EVENTS_2018042300 = XmlUtils.nodesAtPath(doc, "/events/eventdata");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
/**
* Load the music database into memory.
*/
private static void loadMusicDb() {
try {
final byte[] body = ByteStreams.toByteArray(
Main.class.getResourceAsStream("/static_responses/mdx_2018042300/musicdb.xml"));
final Element doc = XmlUtils.byteArrayToXmlFile(body);
final NodeList nodes = XmlUtils.nodesAtPath(doc, "/mdb/music");
SONGS_2018042300 = new ArrayList<>();
for (int i = 0; i < nodes.getLength(); i++) {
Element node = (Element) nodes.item(i);
SONGS_2018042300.add(new Song(XmlUtils.intAtChild(node, "mcode"),
XmlUtils.strAtChild(node, "basename"),
XmlUtils.strAtChild(node, "title"),
XmlUtils.strAtChild(node, "artist")));
}
EVENTS = XmlUtils.nodesAtPath(doc, "/events/eventdata");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
@@ -342,10 +310,6 @@ public class PlayerDataRequestHandler extends BaseRequestHandler {
* @return A response object for Spark
*/
private Object handleUserLoadRequest(final String refId, final Request request, final Response response) {
if (!StringUtils.getSanitizedModel(request.attribute("model")).equals("mdx_2018042300")) {
throw new UnsupportedRequestException();
}
KXmlBuilder respBuilder = KXmlBuilder.create("response")
.e("playerdata")
.s32("result", 0).up()
@@ -408,12 +372,12 @@ public class PlayerDataRequestHandler extends BaseRequestHandler {
}
}
// TODO: Events probably isn't a static response
// TODO: Events isn't supposed to be a static response
final Document document = respBuilder.getDocument();
final Element elem = respBuilder.getElement();
for (int i = 0; i < EVENTS_2018042300.getLength(); i++) {
Node tmp = document.importNode(EVENTS_2018042300.item(i), true);
for (int i = 0; i < EVENTS.getLength(); i++) {
Node tmp = document.importNode(EVENTS.item(i), true);
elem.appendChild(tmp);
}

View File

@@ -7,6 +7,7 @@ import com.buttongames.butterfly.xml.XmlUtils;
import com.buttongames.butterfly.xml.kbinxml.KXmlBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.w3c.dom.Element;
import spark.Request;
@@ -21,6 +22,16 @@ public class SystemRequestHandler extends BaseRequestHandler {
private final Logger LOG = LogManager.getLogger(SystemRequestHandler.class);
/**
* Helper class for converting card IDs.
*/
private final CardIdUtils cardIdUtils;
@Autowired
public SystemRequestHandler(final CardIdUtils cardIdUtils) {
this.cardIdUtils = cardIdUtils;
}
/**
* Handles an incoming request for the <code>system</code> module.
* @param requestBody The XML document of the incoming request.
@@ -48,7 +59,7 @@ public class SystemRequestHandler extends BaseRequestHandler {
*/
private Object handleConvCardNumberRequest(final Element requestBody, final Request request, final Response response) {
final String cardId = XmlUtils.strAtPath(requestBody, "/system/data/card_id");
final String convertedId = CardIdUtils.encodeCardId(cardId);
final String convertedId = this.cardIdUtils.encodeCardId(cardId);
// send a response
final KXmlBuilder builder = KXmlBuilder.create("response")

View File

@@ -23,6 +23,7 @@ import com.buttongames.butterfly.http.handlers.impl.PlayerDataRequestHandler;
import com.buttongames.butterfly.http.handlers.impl.ServicesRequestHandler;
import com.buttongames.butterfly.http.handlers.impl.SystemRequestHandler;
import com.buttongames.butterfly.http.handlers.impl.TaxRequestHandler;
import com.buttongames.butterfly.util.CardIdUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@@ -58,6 +59,11 @@ public class HttpConfiguration {
eacoinRequestHandler, machineDao, userDao);
}
@Bean
public CardIdUtils cardIdUtils() {
return new CardIdUtils();
}
@Bean
public EventLogRequestHandler eventLogRequestHandler(final GameplayEventLogDao gameplayEventLogDao) {
return new EventLogRequestHandler(gameplayEventLogDao);
@@ -106,13 +112,14 @@ public class HttpConfiguration {
}
@Bean
public CardManageRequestHandler cardManageRequestHandler(final CardDao cardDao, final ButterflyUserDao userDao) {
return new CardManageRequestHandler(cardDao, userDao);
public CardManageRequestHandler cardManageRequestHandler(final CardDao cardDao, final ButterflyUserDao userDao,
final CardIdUtils cardIdUtils) {
return new CardManageRequestHandler(cardDao, userDao, cardIdUtils);
}
@Bean
public SystemRequestHandler systemRequestHandler() {
return new SystemRequestHandler();
public SystemRequestHandler systemRequestHandler(final CardIdUtils cardIdUtils) {
return new SystemRequestHandler(cardIdUtils);
}
@Bean

View File

@@ -2,9 +2,12 @@ package com.buttongames.butterfly.util;
import com.buttongames.butterfly.Main;
import com.google.common.io.ByteStreams;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -14,17 +17,30 @@ import java.nio.file.Paths;
* to a borrowed Python script, need to port this to a native implementation.
* @author anonymous
*/
@Component
public class CardIdUtils {
/**
* Says whether or not this server is running in maintenance mode.
*/
@Value(PropertyNames.HOME_DIR)
private String homeDirectory;
/**
* Converts an internal NFC ID to a display ID for e-amusement passes.
* @param id The NFC ID for the pass
* @return The display ID for the pass
*/
public static String encodeCardId(final String id) {
public String encodeCardId(final String id) {
try {
// TODO: Port the python script
final Path path = Paths.get(Main.class.getResource("/cardconv.py").toURI());
// let the script be loadable either from the home directory or from the resources dir (when running in IDE)
Path path = Paths.get(this.homeDirectory, "cardconv.py");
if (!Files.exists(path)) {
path = Paths.get(Main.class.getResource("/cardconv.py").toURI());
}
final ProcessBuilder builder = new ProcessBuilder("python",
"-c",
"import cardconv; print(cardconv.CardCipher.encode('" + id + "'));");

View File

@@ -8,5 +8,5 @@ public class PropertyNames {
public static final String PORT = "${server.port}";
public static final String MAINT_MODE = "${server.maintenance}";
public static final String UNLIMITED_PASELI = "${server.paseli.unlimited}";
public static final String HOME_DIR = "${server.home_dir}";
}

View File

@@ -1,3 +1,4 @@
# Server properties
server.port = 80
server.maintenance = false
server.home_dir = /home/ec2-user/

View File

@@ -5,4 +5,4 @@ jdbc.password =
hibernate.hbm2ddl.auto = update
hibernate.dialect = com.buttongames.butterfly.hibernate.SQLiteDialect
hibernate.show_sql = false
hibernate.db_path = C:\\butterfly.sqlite
hibernate.db_path = /home/ec2-user/butterfly.sqlite

File diff suppressed because it is too large Load Diff