At the point now where existing profiles can login. Still need to fix something that's preventing new profiles from entering their names

This commit is contained in:
skogaby
2019-01-18 18:10:31 -06:00
parent 595435b649
commit 513e19fc8e
4 changed files with 84 additions and 4 deletions

View File

@@ -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 <code>eacoin</code> 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 <code>eacoin.checkin</code> 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 <code>eacoin.consume</code> 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);
}
}

View File

@@ -113,7 +113,7 @@ public class HttpConfiguration {
}
@Bean
public EacoinRequestHandler eacoinRequestHandler() {
return new EacoinRequestHandler();
public EacoinRequestHandler eacoinRequestHandler(final CardDao cardDao) {
return new EacoinRequestHandler(cardDao);
}
}

View File

@@ -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}";
}

View File

@@ -1,4 +1,3 @@
# Server properties
server.port = 80
server.maintenance = false
server.paseli.unlimited = false