From a1ff7c3b70d7bdc1681cfc10410658b7218d61a3 Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Sun, 4 Dec 2011 05:07:46 +0000 Subject: [PATCH] Making chat smarter, trying to resume communication by itself. --- .../lotro/common/ApplicationRoot.java | 15 ++++++ .../logic/modifiers/LoggingThreadLocal.java | 3 +- .../com/gempukku/lotro/game/GameRecorder.java | 3 +- .../gempukku/lotro/server/ServerResource.java | 17 +++++-- .../gemp-lotr-web/src/main/webapp/js/chat.js | 46 ++++++++++++++----- .../src/main/webapp/js/communication.js | 5 +- 6 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 gemp-lotr/gemp-lotr-common/src/main/java/com/gempukku/lotro/common/ApplicationRoot.java diff --git a/gemp-lotr/gemp-lotr-common/src/main/java/com/gempukku/lotro/common/ApplicationRoot.java b/gemp-lotr/gemp-lotr-common/src/main/java/com/gempukku/lotro/common/ApplicationRoot.java new file mode 100644 index 000000000..ca2a45587 --- /dev/null +++ b/gemp-lotr/gemp-lotr-common/src/main/java/com/gempukku/lotro/common/ApplicationRoot.java @@ -0,0 +1,15 @@ +package com.gempukku.lotro.common; + +import java.io.File; + +public class ApplicationRoot { + private static File _root; + + public static void setRoot(File file) { + _root = file; + } + + public static File getRoot() { + return _root; + } +} diff --git a/gemp-lotr/gemp-lotr-logic/src/main/java/com/gempukku/lotro/logic/modifiers/LoggingThreadLocal.java b/gemp-lotr/gemp-lotr-logic/src/main/java/com/gempukku/lotro/logic/modifiers/LoggingThreadLocal.java index ea8a32a1f..0a6400bf2 100644 --- a/gemp-lotr/gemp-lotr-logic/src/main/java/com/gempukku/lotro/logic/modifiers/LoggingThreadLocal.java +++ b/gemp-lotr/gemp-lotr-logic/src/main/java/com/gempukku/lotro/logic/modifiers/LoggingThreadLocal.java @@ -1,5 +1,6 @@ package com.gempukku.lotro.logic.modifiers; +import com.gempukku.lotro.common.ApplicationRoot; import com.gempukku.lotro.game.PhysicalCard; import org.apache.log4j.Logger; @@ -18,7 +19,7 @@ public class LoggingThreadLocal { private static synchronized void initializeOutputStream() { if (_fos == null) { try { - _fos = new FileOutputStream(new File("/etc/gemp-lotr/longActions.txt")); + _fos = new FileOutputStream(new File(ApplicationRoot.getRoot(), "longActions.txt")); } catch (IOException exp) { throw new RuntimeException(exp); } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameRecorder.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameRecorder.java index d6db0e532..f0664b83a 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameRecorder.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameRecorder.java @@ -1,5 +1,6 @@ package com.gempukku.lotro.game; +import com.gempukku.lotro.common.ApplicationRoot; import com.gempukku.lotro.db.GameHistoryDAO; import com.gempukku.lotro.game.state.EventSerializer; import com.gempukku.lotro.game.state.GameEvent; @@ -71,7 +72,7 @@ public class GameRecorder { } private File getRecordingFile(String playerId, String gameId) { - File gameReplayFolder = new File("/etc/gemp-lotr/replay"); + File gameReplayFolder = new File(ApplicationRoot.getRoot(), "replay"); File playerReplayFolder = new File(gameReplayFolder, playerId); return new File(playerReplayFolder, gameId + ".xml"); } diff --git a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java index 774f33172..6747282c4 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java +++ b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java @@ -6,6 +6,7 @@ import com.gempukku.lotro.cards.packs.PackBox; import com.gempukku.lotro.chat.ChatMessage; import com.gempukku.lotro.chat.ChatRoomMediator; import com.gempukku.lotro.chat.ChatServer; +import com.gempukku.lotro.common.ApplicationRoot; import com.gempukku.lotro.common.Side; import com.gempukku.lotro.db.CollectionDAO; import com.gempukku.lotro.db.DbAccess; @@ -39,6 +40,7 @@ import javax.ws.rs.core.StreamingOutput; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -61,6 +63,11 @@ public class ServerResource { private PackBox _packBox; public ServerResource() { + if (!_test) + ApplicationRoot.setRoot(new File("/etc/gemp-lotr")); + else + ApplicationRoot.setRoot(new File("i:\\gemp-lotr")); + _logger.debug("starting resource"); try { @@ -693,7 +700,7 @@ public class ServerResource { public Document postMessage( @PathParam("room") String room, @FormParam("participantId") String participantId, - @FormParam("message") String message, + @FormParam("message") List messages, @Context HttpServletRequest request) throws ParserConfigurationException { if (!_test) participantId = getLoggedUser(request); @@ -702,8 +709,12 @@ public class ServerResource { if (chatRoom == null) sendError(Response.Status.NOT_FOUND); - if (message != null && message.trim().length() > 0) - chatRoom.sendMessage(participantId, StringEscapeUtils.escapeHtml(message)); + if (messages != null) { + for (String message : messages) { + if (message != null && message.trim().length() > 0) + chatRoom.sendMessage(participantId, StringEscapeUtils.escapeHtml(message)); + } + } List chatMessages = chatRoom.getPendingMessages(participantId); Set usersInRoom = chatRoom.getUsersInRoom(); diff --git a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/chat.js b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/chat.js index 706dd8fc9..7e779fa2c 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/chat.js +++ b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/chat.js @@ -8,25 +8,38 @@ var ChatBoxUI = Class.extend({ showTimestamps: false, talkBoxHeight: 25, chatUpdateInterval: 1000, + unsentMessages: null, + processingMessages: null, init: function(name, div, url, showList) { var that = this; this.name = name; this.div = div; + this.unsentMessages = new Array(); this.communication = new GempLotrCommunication(url, function(xhr, ajaxOptions, thrownError) { if (thrownError != "abort") { if (xhr != null) { if (xhr.status == 401) { - that.appendMessage("Chat problem - You're not logged in, go to the main page to log in", "warningMessage"); + that.appendMessage("You're not logged in, go to the main page to log in", "warningMessage"); + return; + } else if (xhr.status == 404) { + that.appendMessage("Chat room was closed, please go to the Game Hall.", "warningMessage"); + return; + } else if (xhr.status == 503) { + that.appendMessage("Server is being restarted, please wait for the restart to finish and try again later.", "warningMessage"); return; } else { - that.appendMessage("Chat had a problem communicating with the server (" + xhr.status + "), no new messages will be displayed, but your messages still might get sent.", "warningMessage"); - that.appendMessage("Reload the browser page (press F5) to resume the chat.", "warningMessage"); + that.appendMessage("Chat had a problem communicating with the server (error " + xhr.statis + "). Retrying...", "systemMessage"); + setTimeout(function() { + that.updateChatMessages(); + }, that.chatUpdateInterval); return; } } - that.appendMessage("Chat had a problem communicating with the server, no new messages will be displayed, but your messages still might get sent.", "warningMessage"); - that.appendMessage("Reload the browser page (press F5) to resume the chat.", "warningMessage"); + that.appendMessage("Chat had an unknown problem communicating with the server. Retrying...", "systemMessage"); + setTimeout(function() { + that.updateChatMessages(); + }, that.chatUpdateInterval); } }); @@ -103,6 +116,7 @@ var ChatBoxUI = Class.extend({ processMessages: function(xml, processAgain) { var root = xml.documentElement; if (root.tagName == 'chat') { + this.processingMessages = null; var messages = root.getElementsByTagName("message"); for (var i = 0; i < messages.length; i++) { var message = messages[i]; @@ -143,16 +157,26 @@ var ChatBoxUI = Class.extend({ updateChatMessages: function(xml) { var that = this; - this.communication.updateChat(this.name, function(xml) { - that.processMessages(xml, true); - }); + if (this.processingMessages != null) { + this.communication.sendChatMessage(this.name, this.processingMessages, function(xml) { + that.processMessages(xml, true); + }); + } else if (this.unsentMessages.length > 0) { + this.communication.sendChatMessage(this.name, this.unsentMessages, function(xml) { + that.processMessages(xml, true); + }); + this.processingMessages = this.unsentMessages; + this.unsentMessages = new Array(); + } else { + this.communication.updateChat(this.name, function(xml) { + that.processMessages(xml, true); + }); + } }, sendMessage: function(message) { var that = this; - this.communication.sendChatMessage(this.name, message, function(xml) { - that.processMessages(xml, false); - }); + this.unsentMessages.push(message); } }); \ No newline at end of file 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 501146043..6eb7a0745 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 @@ -220,15 +220,16 @@ var GempLotrCommunication = Class.extend({ dataType: "xml" }); }, - sendChatMessage: function(room, message, callback) { + sendChatMessage: function(room, messages, callback) { $.ajax({ type: "POST", url: this.url + "/chat/" + room, cache: false, data: { participantId: getUrlParam("participantId"), - message: message}, + message: messages}, success: callback, + traditional: true, error: this.failure, dataType: "xml" });