diff --git a/src/main/java/com/buttongames/butterfly/hibernate/dao/impl/Ddr16ShopDao.java b/src/main/java/com/buttongames/butterfly/hibernate/dao/impl/Ddr16ShopDao.java index 0370aec..c4cc0c0 100644 --- a/src/main/java/com/buttongames/butterfly/hibernate/dao/impl/Ddr16ShopDao.java +++ b/src/main/java/com/buttongames/butterfly/hibernate/dao/impl/Ddr16ShopDao.java @@ -3,6 +3,7 @@ package com.buttongames.butterfly.hibernate.dao.impl; import com.buttongames.butterfly.hibernate.dao.AbstractHibernateDao; import com.buttongames.butterfly.model.Ddr16Shop; import org.hibernate.SessionFactory; +import org.hibernate.query.Query; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.Scope; @@ -21,4 +22,20 @@ public class Ddr16ShopDao extends AbstractHibernateDao { super(sessionFactory); setClazz(Ddr16Shop.class); } + + /** + * Finds a shop by its PCBID. + * @param pcbId The PCBID to query for. + * @return The matching Ddr16Shop, or null if none are found. + */ + public Ddr16Shop findByPcbId(final String pcbId) { + this.openCurrentSession(); + + final Query query = this.currentSession.createQuery("from Ddr16Shop where pcb_id = :pcbid"); + query.setParameter("pcbid", pcbId); + final Ddr16Shop result = query.uniqueResult(); + + this.closeCurrentSession(); + return result; + } } diff --git a/src/main/java/com/buttongames/butterfly/http/ButterflyHttpServer.java b/src/main/java/com/buttongames/butterfly/http/ButterflyHttpServer.java index c86f231..dc0dc4d 100644 --- a/src/main/java/com/buttongames/butterfly/http/ButterflyHttpServer.java +++ b/src/main/java/com/buttongames/butterfly/http/ButterflyHttpServer.java @@ -9,6 +9,7 @@ import com.buttongames.butterfly.http.exception.InvalidRequestMethodException; import com.buttongames.butterfly.http.exception.InvalidRequestModelException; import com.buttongames.butterfly.http.exception.InvalidRequestModuleException; import com.buttongames.butterfly.http.exception.MismatchedRequestUriException; +import com.buttongames.butterfly.http.exception.NoShopForMachineException; import com.buttongames.butterfly.http.handlers.impl.EventLogRequestHandler; import com.buttongames.butterfly.http.handlers.impl.FacilityRequestHandler; import com.buttongames.butterfly.http.handlers.impl.MessageRequestHandler; @@ -199,9 +200,13 @@ public class ButterflyHttpServer { response.body("Request URI does not match request body."); }))); exception(InvalidPcbIdException.class, (((exception, request, response) -> { - response.status(403); - response.body("PCBID is not valid or nonexistent."); - }))); + response.status(403); + response.body("PCBID is not valid or nonexistent."); + }))); + exception(NoShopForMachineException.class, (((exception, request, response) -> { + response.status(403); + response.body("No shop exists for the given PCBID."); + }))); } /** diff --git a/src/main/java/com/buttongames/butterfly/http/exception/NoShopForMachineException.java b/src/main/java/com/buttongames/butterfly/http/exception/NoShopForMachineException.java new file mode 100644 index 0000000..ca03c74 --- /dev/null +++ b/src/main/java/com/buttongames/butterfly/http/exception/NoShopForMachineException.java @@ -0,0 +1,8 @@ +package com.buttongames.butterfly.http.exception; + +/** + * Exception to throw when we receive a request for a facility for a PCBID that doesn't have one. + * @author skogaby (skogabyskogaby@gmail.com) + */ +public class NoShopForMachineException extends RuntimeException { +} diff --git a/src/main/java/com/buttongames/butterfly/http/handlers/impl/EventLogRequestHandler.java b/src/main/java/com/buttongames/butterfly/http/handlers/impl/EventLogRequestHandler.java index 0b3af1a..cad6568 100644 --- a/src/main/java/com/buttongames/butterfly/http/handlers/impl/EventLogRequestHandler.java +++ b/src/main/java/com/buttongames/butterfly/http/handlers/impl/EventLogRequestHandler.java @@ -31,7 +31,6 @@ public class EventLogRequestHandler extends BaseRequestHandler { */ private final Ddr16GameplayEventLogDao gameplayEventLogDao; - public EventLogRequestHandler(final Ddr16GameplayEventLogDao gameplayEventLogDao) { this.gameplayEventLogDao = gameplayEventLogDao; } diff --git a/src/main/java/com/buttongames/butterfly/http/handlers/impl/FacilityRequestHandler.java b/src/main/java/com/buttongames/butterfly/http/handlers/impl/FacilityRequestHandler.java index a0db445..ea877e0 100644 --- a/src/main/java/com/buttongames/butterfly/http/handlers/impl/FacilityRequestHandler.java +++ b/src/main/java/com/buttongames/butterfly/http/handlers/impl/FacilityRequestHandler.java @@ -1,7 +1,12 @@ package com.buttongames.butterfly.http.handlers.impl; +import com.buttongames.butterfly.hibernate.dao.impl.Ddr16ShopDao; +import com.buttongames.butterfly.hibernate.dao.impl.MachineDao; import com.buttongames.butterfly.http.exception.InvalidRequestMethodException; +import com.buttongames.butterfly.http.exception.NoShopForMachineException; import com.buttongames.butterfly.http.handlers.BaseRequestHandler; +import com.buttongames.butterfly.model.Ddr16Shop; +import com.buttongames.butterfly.model.Machine; import com.buttongames.butterfly.xml.KXmlBuilder; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -19,6 +24,22 @@ public class FacilityRequestHandler extends BaseRequestHandler { private final Logger LOG = LogManager.getLogger(FacilityRequestHandler.class); + /** + * DAO for interacting with Ddr16Shops in the database. + */ + private final Ddr16ShopDao shopDao; + + /** + * DAO for interacting with Machines in the database. + */ + private final MachineDao machineDao; + + public FacilityRequestHandler(final Ddr16ShopDao shopDao, + final MachineDao machineDao) { + this.shopDao = shopDao; + this.machineDao = machineDao; + } + /** * Handles an incoming request for the facility module. * @param requestBody The XML document of the incoming request. @@ -44,30 +65,36 @@ public class FacilityRequestHandler extends BaseRequestHandler { * @return A response object for Spark */ private Object handleGetRequest(final Request request, final Response response) { - // TODO: Remove all the hardcoded stuff + final String reqPcbId = request.attribute("pcbid"); + final Ddr16Shop shop = this.shopDao.findByPcbId(reqPcbId); + + if (shop == null) { + throw new NoShopForMachineException(); + } + + final Machine machine = this.machineDao.findByPcbId(reqPcbId); + KXmlBuilder respBuilder = KXmlBuilder.create("response") .e("facility") .e("location") - .str("id", "US-01").up() - .str("country", "US").up() - .str("region", "TX").up() - .str("name", "BUTTERFLY").up() + .str("id", shop.getLocationId()).up() + .str("country", shop.getCountry()).up() + .str("region", shop.getRegion()).up() + .str("name", shop.getName()).up() .u8("type", 0).up().up() .e("line") - .str("id", "3").up() - .u8("class", 8).up() - .u8("upclass", 8).up() - .u16("rtt", 40).up().up() + .str("id", "").up() + .u8("class", 0).up() .e("public") - .u8("flag", 1).up() - .str("name", "BUTTERFLY").up() - .str("lattitude", "0").up() - .str("longitude", "0").up().up() + .u8("flag", shop.isPublic() ? 1 : 0).up() + .str("name", shop.getName()).up() + .str("lattitude", shop.getLatitude()).up() + .str("longitude", shop.getLongitude()).up().up() .e("share") .e("eacoin") - .s32("notchamount", 0).up() - .s32("notchcount", 0).up() - .s32("supplylimit", 1000000).up().up() + .s32("notchamount", shop.getNotchAmount()).up() + .s32("notchcount", shop.getNotchCount()).up() + .s32("supplylimit", shop.getSupplyLimit()).up().up() .e("url") .str("eapass", "http://eagate.573.jp/").up() .str("arcadefan", "http://eagate.573.jp/").up() @@ -75,9 +102,10 @@ public class FacilityRequestHandler extends BaseRequestHandler { .str("konamiid", "http://eagate.573.jp/").up() .str("eagate", "http://eagate.573.jp/").up().up().up() .e("portfw") + // TODO: Use our real public IP for this element .ip("globalip", "1.0.0.127").up() - .u16("globalport", 8888).up() - .u16("privateport", 8888); + .u16("globalport", machine.getPort()).up() + .u16("privateport", machine.getPort()); return this.sendResponse(request, response, respBuilder); } 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 2213240..7256575 100644 --- a/src/main/java/com/buttongames/butterfly/spring/configuration/HttpConfiguration.java +++ b/src/main/java/com/buttongames/butterfly/spring/configuration/HttpConfiguration.java @@ -2,6 +2,7 @@ package com.buttongames.butterfly.spring.configuration; import com.buttongames.butterfly.hibernate.dao.impl.Ddr16GameplayEventLogDao; import com.buttongames.butterfly.hibernate.dao.impl.Ddr16PcbEventLogDao; +import com.buttongames.butterfly.hibernate.dao.impl.Ddr16ShopDao; import com.buttongames.butterfly.hibernate.dao.impl.MachineDao; import com.buttongames.butterfly.http.ButterflyHttpServer; import com.buttongames.butterfly.http.handlers.impl.EventLogRequestHandler; @@ -47,8 +48,8 @@ public class HttpConfiguration { } @Bean - public FacilityRequestHandler facilityRequestHandler() { - return new FacilityRequestHandler(); + public FacilityRequestHandler facilityRequestHandler(final Ddr16ShopDao shopDao, final MachineDao machineDao) { + return new FacilityRequestHandler(shopDao, machineDao); } @Bean