diff --git a/Mage.Client/src/main/java/mage/client/table/TablesPanel.form b/Mage.Client/src/main/java/mage/client/table/TablesPanel.form index 18a55840610..e6f2cb29906 100644 --- a/Mage.Client/src/main/java/mage/client/table/TablesPanel.form +++ b/Mage.Client/src/main/java/mage/client/table/TablesPanel.form @@ -21,7 +21,7 @@ - + @@ -32,14 +32,12 @@ - + + + - - - - - + @@ -121,7 +119,7 @@ - + @@ -131,11 +129,10 @@ - + - @@ -154,12 +151,21 @@ - + - + + + + + + + + + + diff --git a/Mage.Client/src/main/java/mage/client/table/TablesPanel.java b/Mage.Client/src/main/java/mage/client/table/TablesPanel.java index ab1009ed8f5..c6b61b38a39 100644 --- a/Mage.Client/src/main/java/mage/client/table/TablesPanel.java +++ b/Mage.Client/src/main/java/mage/client/table/TablesPanel.java @@ -48,6 +48,7 @@ import mage.remote.Session; import mage.client.util.ButtonColumn; import mage.game.match.MatchOptions; import mage.sets.Sets; +import mage.utils.CompressUtil; import mage.view.TableView; import org.apache.log4j.Logger; @@ -80,6 +81,8 @@ public class TablesPanel extends javax.swing.JPanel { private NewTableDialog newTableDialog; private NewTournamentDialog newTournamentDialog; private Session session; + private List messages; + private int currentMessage; /** Creates new form TablesPanel */ public TablesPanel() { @@ -227,7 +230,21 @@ public class TablesPanel extends javax.swing.JPanel { else { hideTables(); } - + + // reload server messages + List messages = session.getServerMessages(); + synchronized (this) { + this.messages = messages; + this.currentMessage = 0; + } + if (messages == null || messages.isEmpty()) { + this.jPanel2.setVisible(false); + } else { + this.jPanel2.setVisible(true); + this.jLabel2.setText(messages.get(0)); + this.jButton1.setVisible(messages.size() > 1); + } + MageFrame.getUI().addButton(MageComponents.NEW_GAME_BUTTON, btnNewTable); } @@ -278,10 +295,10 @@ public class TablesPanel extends javax.swing.JPanel { btnNewTable.setText("New Match"); btnNewTable.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - btnNewTableActionPerformed(evt); - } - }); + public void actionPerformed(java.awt.event.ActionEvent evt) { + btnNewTableActionPerformed(evt); + } + }); btnQuickStart.setText("Quick Start"); btnQuickStart.addActionListener(new java.awt.event.ActionListener() { @@ -292,74 +309,76 @@ public class TablesPanel extends javax.swing.JPanel { btnNewTournament.setText("New Tournament"); btnNewTournament.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - btnNewTournamentActionPerformed(evt); - } - }); + public void actionPerformed(java.awt.event.ActionEvent evt) { + btnNewTournamentActionPerformed(evt); + } + }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( - jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(jPanel1Layout.createSequentialGroup() - .addContainerGap() - .addComponent(btnNewTable) - .addGap(6, 6, 6) - .addComponent(btnNewTournament) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(btnQuickStart) - .addContainerGap(414, Short.MAX_VALUE)) - ); + jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(jPanel1Layout.createSequentialGroup() + .addContainerGap() + .addComponent(btnNewTable) + .addGap(6, 6, 6) + .addComponent(btnNewTournament) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(btnQuickStart) + .addContainerGap(414, Short.MAX_VALUE)) + ); jPanel1Layout.setVerticalGroup( - jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(jPanel1Layout.createSequentialGroup() - .addContainerGap() - .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(btnNewTable) - .addComponent(btnQuickStart) - .addComponent(btnNewTournament)) - .addContainerGap(16, Short.MAX_VALUE)) - ); + jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(jPanel1Layout.createSequentialGroup() + .addContainerGap() + .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(btnNewTable) + .addComponent(btnQuickStart) + .addComponent(btnNewTournament)) + .addContainerGap(16, Short.MAX_VALUE)) + ); jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)); jPanel2.setPreferredSize(new java.awt.Dimension(664, 39)); - jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); + jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel1.setText("Message of the Day:"); jLabel1.setAlignmentY(0.3F); - jLabel2.setText("You are playing Mage version 0.7.5. Welcome! == Mage dev team =="); + jLabel2.setText("You are playing Mage version 0.7.5. Welcome! -- Mage dev team --"); - jButton1.setText(">>"); + jButton1.setText("Next"); + jButton1.setMaximumSize(new java.awt.Dimension(55, 25)); + jButton1.setMinimumSize(new java.awt.Dimension(55, 25)); + jButton1.setPreferredSize(new java.awt.Dimension(55, 25)); jButton1.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton1ActionPerformed(evt); - } - }); + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1ActionPerformed(evt); + } + }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( - jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(jPanel2Layout.createSequentialGroup() - .addContainerGap() - .addComponent(jLabel1) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jButton1) - .addGap(22, 22, 22)) - ); + jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(jPanel2Layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(22, 22, 22)) + ); jPanel2Layout.setVerticalGroup( - jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(jPanel2Layout.createSequentialGroup() - .addContainerGap() - .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jLabel2) - .addComponent(jLabel1) - .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - ); + jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(jPanel2Layout.createSequentialGroup() + .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jLabel2) + .addComponent(jLabel1)) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); jSplitPane1.setDividerSize(3); jSplitPane1.setResizeWeight(1.0); @@ -379,22 +398,21 @@ public class TablesPanel extends javax.swing.JPanel { .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 721, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addContainerGap(47, Short.MAX_VALUE))) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addContainerGap(47, Short.MAX_VALUE))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() - .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(0, 0, 0) - .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE)) + .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(0, 0, 0) + .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 532, Short.MAX_VALUE) + .addGap(0, 0, 0) + .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() - .addContainerGap(518, Short.MAX_VALUE) - .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE) - .addContainerGap())) + .addGap(0, 607, Short.MAX_VALUE)) ); }// //GEN-END:initComponents @@ -424,15 +442,24 @@ public class TablesPanel extends javax.swing.JPanel { } }//GEN-LAST:event_btnQuickStartActionPerformed - private void btnNewTableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewTableActionPerformed - newTableDialog.showDialog(roomId); - if (newTableDialog.getTable() != null) - showTableWaitingDialog(roomId, newTableDialog.getTable().getTableId(), false); -}//GEN-LAST:event_btnNewTableActionPerformed + private void btnNewTableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewTableActionPerformed + newTableDialog.showDialog(roomId); + if (newTableDialog.getTable() != null) { + showTableWaitingDialog(roomId, newTableDialog.getTable().getTableId(), false); + } + }//GEN-LAST:event_btnNewTableActionPerformed - private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed - // TODO add your handling code here: - }//GEN-LAST:event_jButton1ActionPerformed + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + synchronized (this) { + if (messages != null && !messages.isEmpty()) { + currentMessage++; + if (currentMessage >= messages.size()) { + currentMessage = 0; + } + this.jLabel2.setText(messages.get(currentMessage)); + } + } + }//GEN-LAST:event_jButton1ActionPerformed private void handleError(Exception ex) { logger.fatal("Error loading deck: ", ex); diff --git a/Mage.Common/src/mage/interfaces/MageServer.java b/Mage.Common/src/mage/interfaces/MageServer.java index 2982bf681ba..d71127c5996 100644 --- a/Mage.Common/src/mage/interfaces/MageServer.java +++ b/Mage.Common/src/mage/interfaces/MageServer.java @@ -120,5 +120,7 @@ public interface MageServer { public List getUsers(String sessionId) throws MageException; public void disconnectUser(String sessionId, String userSessionId) throws MageException; public void removeTable(String sessionId, UUID tableId) throws MageException; - + + // messages of the day + public Object getServerMessagesCompressed(String sessionId) throws MageException; } diff --git a/Mage.Common/src/mage/remote/Session.java b/Mage.Common/src/mage/remote/Session.java index 55079d9de7b..0fab2b56303 100644 --- a/Mage.Common/src/mage/remote/Session.java +++ b/Mage.Common/src/mage/remote/Session.java @@ -46,6 +46,7 @@ import mage.interfaces.MageClient; import mage.interfaces.MageServer; import mage.interfaces.ServerState; import mage.interfaces.callback.ClientCallback; +import mage.utils.CompressUtil; import mage.view.DraftPickView; import mage.view.GameTypeView; import mage.view.TableView; @@ -838,6 +839,18 @@ public class Session { } return null; } + + public List getServerMessages() { + try { + if (isConnected()) + return (List) CompressUtil.decompress(server.getServerMessagesCompressed(sessionId)); + } catch (MageException ex) { + handleMageException(ex); + } catch (Throwable t) { + handleThrowable(t); + } + return null; + } public boolean disconnectUser(String userSessionId) { try { diff --git a/Mage.Server/src/main/java/mage/server/MageServerImpl.java b/Mage.Server/src/main/java/mage/server/MageServerImpl.java index bb7910a1f97..48c29278ba4 100644 --- a/Mage.Server/src/main/java/mage/server/MageServerImpl.java +++ b/Mage.Server/src/main/java/mage/server/MageServerImpl.java @@ -49,7 +49,9 @@ import mage.server.game.PlayerFactory; import mage.server.game.ReplayManager; import mage.server.tournament.TournamentFactory; import mage.server.tournament.TournamentManager; +import mage.server.util.ServerMessagesUtil; import mage.server.util.ThreadExecutor; +import mage.utils.CompressUtil; import mage.utils.MageVersion; import mage.view.ChatMessage.MessageColor; import mage.view.DraftPickView; @@ -74,6 +76,7 @@ public class MageServerImpl implements MageServer { public MageServerImpl(String password, boolean testMode) { this.password = password; this.testMode = testMode; + ServerMessagesUtil.getInstance().getMessages(); } @Override @@ -937,4 +940,17 @@ public class MageServerImpl implements MageServer { } } } + + @Override + public Object getServerMessagesCompressed(String sessionId) throws MageException { + if (SessionManager.getInstance().isValidSession(sessionId)) { + try { + return CompressUtil.compress(ServerMessagesUtil.getInstance().getMessages()); + } + catch (Exception ex) { + handleException(ex); + } + } + return null; + } } diff --git a/Mage.Server/src/main/java/mage/server/util/ServerMessagesUtil.java b/Mage.Server/src/main/java/mage/server/util/ServerMessagesUtil.java new file mode 100644 index 00000000000..90bc0b2eaa2 --- /dev/null +++ b/Mage.Server/src/main/java/mage/server/util/ServerMessagesUtil.java @@ -0,0 +1,112 @@ +/* +* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, are +* permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, this list of +* conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, this list +* of conditions and the following disclaimer in the documentation and/or other materials +* provided with the distribution. +* +* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR +* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +* The views and conclusions contained in the software and documentation are those of the +* authors and should not be interpreted as representing official policies, either expressed +* or implied, of BetaSteward_at_googlemail.com. +*/ +package mage.server.util; + +import org.apache.log4j.Logger; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * Handles server messages (Messages of the Day). + * Reloads messages every 5 minutes. + * + * @author nantuko + */ +public class ServerMessagesUtil { + + private static final ServerMessagesUtil instance = new ServerMessagesUtil(); + + private static final Logger log = Logger.getLogger(ServerMessagesUtil.class); + static final String SERVER_MSG_TXT_FILE = "/server.msg.txt"; + + private List messages = new ArrayList(); + private ReadWriteLock lock = new ReentrantReadWriteLock(); + + public ServerMessagesUtil() { + timer.setInitialDelay(5000); + timer.start(); + } + + public static ServerMessagesUtil getInstance() { + return instance; + } + + public List getMessages() { + lock.readLock().lock(); + try { + return messages; + } finally { + lock.readLock().unlock(); + } + } + + private void reloadMessages() { + log.info("Reading server messages..."); + List newMessages = readFromFile(); + if (newMessages != null && !newMessages.isEmpty()) { + lock.writeLock().lock(); + try { + messages.clear(); + messages.addAll(newMessages); + } finally { + lock.writeLock().unlock(); + } + } + } + + private List readFromFile() { + InputStream is = ServerMessagesUtil.class.getResourceAsStream(SERVER_MSG_TXT_FILE); + if (is == null) { + log.info("Couldn't find server.msg"); + return null; + } + Scanner scanner = new Scanner(is); + List messages = new ArrayList(); + while (scanner.hasNextLine()) { + String message = scanner.nextLine(); + if (!message.trim().isEmpty()) { + messages.add(message.trim()); + } + } + return messages; + } + + private Timer timer = new Timer(1000 * 60 * 5, new ActionListener() { + public void actionPerformed(ActionEvent e) { + reloadMessages(); + } + }); +} diff --git a/Mage.Server/src/main/resources/server.msg.txt b/Mage.Server/src/main/resources/server.msg.txt new file mode 100644 index 00000000000..263e1791518 --- /dev/null +++ b/Mage.Server/src/main/resources/server.msg.txt @@ -0,0 +1,3 @@ +Welcome! You are playing Mage version 0.7.4. +Mage 0.7.5 will be released at the end of this week. +Find us on www.slightlymagic.net.