From a47bbb01e9bcbda087c2a639c9e37d2a5f26549a Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Fri, 16 Dec 2011 21:43:34 +0000 Subject: [PATCH] Delivery is working on deck builder now. --- .../gempukku/lotro/packs/FixedPackBox.java | 4 +- .../lotro/server/DeliveryResource.java | 65 +++++++++++++++ .../src/main/webapp/deckBuild.html | 1 + .../src/main/webapp/js/communication.js | 80 +++++++++++++------ .../src/main/webapp/js/deliveryService.js | 70 ++++++++++++++++ 5 files changed, 192 insertions(+), 28 deletions(-) create mode 100644 gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/DeliveryResource.java create mode 100644 gemp-lotr/gemp-lotr-web/src/main/webapp/js/deliveryService.js diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/packs/FixedPackBox.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/packs/FixedPackBox.java index 887edbd15..2e562577e 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/packs/FixedPackBox.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/packs/FixedPackBox.java @@ -5,13 +5,13 @@ import com.gempukku.lotro.game.CardCollection; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; public class FixedPackBox implements PackBox { - private Map _contents = new HashMap(); + private Map _contents = new LinkedHashMap(); public FixedPackBox(String packName) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(FixedPackBox.class.getResourceAsStream("/" + packName + ".pack"))); diff --git a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/DeliveryResource.java b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/DeliveryResource.java new file mode 100644 index 000000000..18b30980a --- /dev/null +++ b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/DeliveryResource.java @@ -0,0 +1,65 @@ +package com.gempukku.lotro.server; + +import com.gempukku.lotro.game.CardCollection; +import com.gempukku.lotro.game.Player; +import com.sun.jersey.spi.resource.Singleton; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.util.List; +import java.util.Map; + +@Singleton +@Path("/delivery") +public class DeliveryResource extends AbstractResource { + @GET + public Document getDelivery( + @Context HttpServletRequest request) throws ParserConfigurationException { + Player resourceOwner = getResourceOwnerSafely(request, null); + Map> delivery = _deliveryService.consumePackages(resourceOwner); + if (delivery == null) + throw new WebApplicationException(Response.Status.NOT_FOUND); + + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + + Document doc = documentBuilder.newDocument(); + + Element deliveryElem = doc.createElement("delivery"); + for (Map.Entry> collectionTypeItems : delivery.entrySet()) { + String collectionType = collectionTypeItems.getKey(); + List items = collectionTypeItems.getValue(); + + Element collectionTypeElem = doc.createElement("collectionType"); + collectionTypeElem.setAttribute("name", collectionType); + for (CardCollection.Item item : items) { + String blueprintId = item.getBlueprintId(); + if (item.getType() == CardCollection.Item.Type.CARD) { + Element card = doc.createElement("card"); + card.setAttribute("count", String.valueOf(item.getCount())); + card.setAttribute("blueprintId", blueprintId); + collectionTypeElem.appendChild(card); + } else { + Element pack = doc.createElement("pack"); + pack.setAttribute("count", String.valueOf(item.getCount())); + pack.setAttribute("blueprintId", blueprintId); + collectionTypeElem.appendChild(pack); + } + } + deliveryElem.appendChild(collectionTypeElem); + } + + doc.appendChild(deliveryElem); + + return doc; + } +} diff --git a/gemp-lotr/gemp-lotr-web/src/main/webapp/deckBuild.html b/gemp-lotr/gemp-lotr-web/src/main/webapp/deckBuild.html index 073aa7d4c..37dc65ae0 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/webapp/deckBuild.html +++ b/gemp-lotr/gemp-lotr-web/src/main/webapp/deckBuild.html @@ -95,6 +95,7 @@ + diff --git a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/communication.js b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/communication.js index f850134cb..cb6bff904 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/communication.js +++ b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/communication.js @@ -7,6 +7,29 @@ var GempLotrCommunication = Class.extend({ this.failure = failure; }, + getDelivery: function(callback) { + $.ajax({ + type: "GET", + url: this.url + "/delivery", + cache: false, + data: { + participantId: getUrlParam("participantId") }, + success: callback, + error: null, + dataType: "xml" + }); + }, + + deliveryCheck: function(callback) { + var that = this; + return function(xml, status, request) { + var delivery = request.getResponseHeader("Delivery-Service-Package"); + if (delivery == "true" && window.deliveryService != null) + that.getDelivery(window.deliveryService); + callback(xml); + }; + }, + getGameHistory: function(start, count, callback) { $.ajax({ type: "GET", @@ -16,7 +39,7 @@ var GempLotrCommunication = Class.extend({ start: start, count: count, participantId: getUrlParam("participantId") }, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -29,7 +52,7 @@ var GempLotrCommunication = Class.extend({ cache: false, data: { participanId: getUrlParam("participantId") }, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -40,7 +63,7 @@ var GempLotrCommunication = Class.extend({ type: "GET", url: this.url + "/replay/" + replayId, cache: false, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -51,7 +74,7 @@ var GempLotrCommunication = Class.extend({ url: this.url + "/game/" + getUrlParam("gameId"), cache: false, data: { participantId: getUrlParam("participantId") }, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -62,7 +85,7 @@ var GempLotrCommunication = Class.extend({ url: this.url + "/game/" + getUrlParam("gameId"), cache: false, data: { participantId: getUrlParam("participantId") }, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -74,7 +97,7 @@ var GempLotrCommunication = Class.extend({ cache: false, data: { cardId: cardId, participantId: getUrlParam("participantId") }, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "html" }); @@ -88,7 +111,7 @@ var GempLotrCommunication = Class.extend({ participantId: getUrlParam("participantId"), decisionId: decisionId, decisionValue: response}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -112,7 +135,7 @@ var GempLotrCommunication = Class.extend({ data: { participantId: getUrlParam("participantId"), deckName: deckName}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -124,7 +147,7 @@ var GempLotrCommunication = Class.extend({ cache: false, data: { participantId: getUrlParam("participantId")}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -136,7 +159,7 @@ var GempLotrCommunication = Class.extend({ cache: false, data: { participantId: getUrlParam("participantId")}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -151,7 +174,7 @@ var GempLotrCommunication = Class.extend({ filter: filter, start: start, count: count}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -164,7 +187,12 @@ var GempLotrCommunication = Class.extend({ data: { participantId: getUrlParam("participantId"), pack: pack}, - success: callback, + success: function(xml, status, request) { + var delivery = request.getResponseHeader("Delivery-Service-Package"); + if (delivery == "true") + + callback(xml); + }, error: this.failure, dataType: "xml" }); @@ -178,7 +206,7 @@ var GempLotrCommunication = Class.extend({ participantId: getUrlParam("participantId"), deckName: deckName, deckContents: contents}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -192,7 +220,7 @@ var GempLotrCommunication = Class.extend({ participantId: getUrlParam("participantId"), oldDeckName: oldDeckName, deckName: deckName}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -205,7 +233,7 @@ var GempLotrCommunication = Class.extend({ data: { participantId: getUrlParam("participantId"), deckName: deckName}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -218,7 +246,7 @@ var GempLotrCommunication = Class.extend({ data: { participantId: getUrlParam("participantId"), deckContents: contents}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "html" }); @@ -230,7 +258,7 @@ var GempLotrCommunication = Class.extend({ cache: false, data: { participantId: getUrlParam("participantId")}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -242,7 +270,7 @@ var GempLotrCommunication = Class.extend({ cache: false, data: { participantId: getUrlParam("participantId")}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -255,7 +283,7 @@ var GempLotrCommunication = Class.extend({ data: { participantId: getUrlParam("participantId"), message: messages}, - success: callback, + success: this.deliveryCheck(callback), traditional: true, error: this.failure, dataType: "xml" @@ -268,7 +296,7 @@ var GempLotrCommunication = Class.extend({ cache: false, data: { participantId: getUrlParam("participantId")}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -281,7 +309,7 @@ var GempLotrCommunication = Class.extend({ data: { deckName: deckName, participantId: getUrlParam("participantId")}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -295,7 +323,7 @@ var GempLotrCommunication = Class.extend({ format: format, deckName: deckName, participantId: getUrlParam("participantId")}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "xml" }); @@ -318,7 +346,7 @@ var GempLotrCommunication = Class.extend({ cache: false, data: { participantId: getUrlParam("participantId")}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "html" }); @@ -332,7 +360,7 @@ var GempLotrCommunication = Class.extend({ login: login, password: password, participantId: getUrlParam("participantId")}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "html" }); @@ -346,7 +374,7 @@ var GempLotrCommunication = Class.extend({ login: login, password: password, participantId: getUrlParam("participantId")}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "html" }); @@ -358,7 +386,7 @@ var GempLotrCommunication = Class.extend({ cache: false, data: { participantId: getUrlParam("participantId")}, - success: callback, + success: this.deliveryCheck(callback), error: this.failure, dataType: "html" }); diff --git a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/deliveryService.js b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/deliveryService.js new file mode 100644 index 000000000..ad8aca70f --- /dev/null +++ b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/deliveryService.js @@ -0,0 +1,70 @@ +var deliveryDialog = null; +var deliveryGroup = null; + +function deliveryService(xml) { + log("Delivered a package:"); + log(xml); + + var root = xml.documentElement; + if (root.tagName == "delivery") { + + var deliveryDialogResize = function() { + var width = deliveryDialog.width() + 10; + var height = deliveryDialog.height() + 10; + deliveryGroup.setBounds(2, 2, width - 2 * 2, height - 2 * 2); + }; + + if (deliveryDialog == null) { + deliveryDialog = $("
").dialog({ + title: "You've received the following items", + autoOpen: false, + closeOnEscape: false, + resizable: true, + width: 400, + height: 200 + }); + + deliveryGroup = new NormalCardGroup(deliveryDialog, function(card) { + return true; + }, false); + + deliveryDialog.bind("dialogresize", deliveryDialogResize); + deliveryDialog.bind("dialogclose", + function() { + deliveryDialog.html(""); + }); + } + + var collections = root.getElementsByTagName("collectionType"); + for (var i = 0; i < collections.length; i++) { + var collection = collections[i]; + + var packs = collection.getElementsByTagName("pack"); + for (var j = 0; j < packs.length; j++) { + var packElem = packs[j]; + var blueprintId = packElem.getAttribute("blueprintId"); + var count = packElem.getAttribute("count"); + var card = new Card(blueprintId, "delivery", "deliveryPack" + i, "player"); + card.tokens = {"count":count}; + var cardDiv = createCardDiv(card.imageUrl, null, card.isFoil(), true); + cardDiv.data("card", card); + deliveryDialog.append(cardDiv); + } + + var cards = collection.getElementsByTagName("card"); + for (var j = 0; j < cards.length; j++) { + var cardElem = cards[j]; + var blueprintId = cardElem.getAttribute("blueprintId"); + var count = cardElem.getAttribute("count"); + var card = new Card(blueprintId, "delivery", "deliveryCard" + i, "player"); + card.tokens = {"count":count}; + var cardDiv = createCardDiv(card.imageUrl, null, card.isFoil()); + cardDiv.data("card", card); + deliveryDialog.append(cardDiv); + } + } + + deliveryDialog.dialog("open"); + deliveryDialogResize(); + } +} \ No newline at end of file