diff --git a/src/main/java/com/buttongames/butterfly/http/handlers/impl/EacoinRequestHandler.java b/src/main/java/com/buttongames/butterfly/http/handlers/impl/EacoinRequestHandler.java index a30d6f0..226e5ce 100644 --- a/src/main/java/com/buttongames/butterfly/http/handlers/impl/EacoinRequestHandler.java +++ b/src/main/java/com/buttongames/butterfly/http/handlers/impl/EacoinRequestHandler.java @@ -1,6 +1,13 @@ package com.buttongames.butterfly.http.handlers.impl; +import com.buttongames.butterfly.hibernate.dao.impl.ButterflyUserDao; +import com.buttongames.butterfly.hibernate.dao.impl.CardDao; +import com.buttongames.butterfly.http.exception.InvalidRequestException; +import com.buttongames.butterfly.http.exception.InvalidRequestMethodException; import com.buttongames.butterfly.http.handlers.BaseRequestHandler; +import com.buttongames.butterfly.model.Card; +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.stereotype.Component; @@ -17,6 +24,20 @@ public class EacoinRequestHandler extends BaseRequestHandler { private final Logger LOG = LogManager.getLogger(EacoinRequestHandler.class); + /** + * How much Paseli to use when it's unlimited (for now, it's always unlimited). + */ + private static final int INFINITE_PASELI_AMOUNT = 50000; + + /** + * DAO for interacting with cards in the database. + */ + private final CardDao cardDao; + + public EacoinRequestHandler(final CardDao cardDao) { + this.cardDao = cardDao; + } + /** * Handles an incoming request for the eacoin module. * @param requestBody The XML document of the incoming request. @@ -27,6 +48,65 @@ public class EacoinRequestHandler extends BaseRequestHandler { @Override public Object handleRequest(final Element requestBody, final Request request, final Response response) { final String requestMethod = request.attribute("method"); - return null; + + if (requestMethod.equals("checkin")) { + return this.handleCheckinRequest(requestBody, request, response); + } else if (requestMethod.equals("consume")) { + return this.handleConsumeRequest(request, response); + } else { + throw new InvalidRequestMethodException(); + } + } + + /** + * Handles an incoming request for the eacoin.checkin method. + * @param requestBody The XML document of the incoming request. + * @param request The Spark request + * @param response The Spark response + * @return A response object for Spark + */ + private Object handleCheckinRequest(final Element requestBody, final Request request, final Response response) { + final String cardId = XmlUtils.strValueAtPath(requestBody, "/eacoin/cardid"); + final String pin = XmlUtils.strValueAtPath(requestBody, "/eacoin/passwd"); + + // verify the pin for sanity + // check the pin against the pin of the owner of the ref ID + final Card card = this.cardDao.findByNfcId(cardId); + + if (card == null || + card.getUser() == null || + !card.getUser().getPin().equals(pin)) { + throw new InvalidRequestException(); + } + + // send a response + // TODO: send the actual Paseli balance + final KXmlBuilder builder = KXmlBuilder.create("response") + .e("eacoin") + .s16("sequence", 1).up() + .u8("acstatus", 1).up() + .str("acid", "X").up() + .str("acname", "X").up() + .s32("balance", INFINITE_PASELI_AMOUNT).up() + .str("sessid", "X"); + + return this.sendResponse(request, response, builder); + } + + /** + * Handles an incoming request for the eacoin.consume method. + * @param request The Spark request + * @param response The Spark response + * @return A response object for Spark + */ + private Object handleConsumeRequest(final Request request, final Response response) { + // TODO: actually consume the Paseli and not make it unlimited + final KXmlBuilder builder = KXmlBuilder.create("response") + .e("eacoin") + .u8("acstatus", 0).up() + .u8("autocharge", 0).up() + .s32("balance", INFINITE_PASELI_AMOUNT); + + return this.sendResponse(request, response, builder); } } diff --git a/src/main/java/com/buttongames/butterfly/spring/configuration/HttpConfiguration.java b/src/main/java/com/buttongames/butterfly/spring/configuration/HttpConfiguration.java index a95822e..e801d32 100644 --- a/src/main/java/com/buttongames/butterfly/spring/configuration/HttpConfiguration.java +++ b/src/main/java/com/buttongames/butterfly/spring/configuration/HttpConfiguration.java @@ -113,7 +113,7 @@ public class HttpConfiguration { } @Bean - public EacoinRequestHandler eacoinRequestHandler() { - return new EacoinRequestHandler(); + public EacoinRequestHandler eacoinRequestHandler(final CardDao cardDao) { + return new EacoinRequestHandler(cardDao); } } diff --git a/src/main/java/com/buttongames/butterfly/util/PropertyNames.java b/src/main/java/com/buttongames/butterfly/util/PropertyNames.java index 0f29791..3388648 100644 --- a/src/main/java/com/buttongames/butterfly/util/PropertyNames.java +++ b/src/main/java/com/buttongames/butterfly/util/PropertyNames.java @@ -8,4 +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}"; } diff --git a/src/main/resources/butterfly.properties b/src/main/resources/butterfly.properties index d6cdbc1..3936d3b 100644 --- a/src/main/resources/butterfly.properties +++ b/src/main/resources/butterfly.properties @@ -1,4 +1,3 @@ # Server properties server.port = 80 server.maintenance = false -server.paseli.unlimited = false