1
0
Fork 0
mirror of https://github.com/correl/mage.git synced 2025-03-27 17:00:20 -09:00

Done Issue 16: Games played counter plugin.

This commit is contained in:
magenoxx 2010-11-14 17:01:12 +00:00
parent a2561515a3
commit b143a5a44d
14 changed files with 342 additions and 32 deletions
Mage.Client
Mage.Common/src/mage/interfaces
Mage.Plugins
Mage.Card.Plugin
Mage.Counter.Plugin
pom.xml
src/main/java/org/mage/plugins/counter
pom.xml

Binary file not shown.

View file

@ -34,6 +34,7 @@
package mage.client;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
@ -47,6 +48,7 @@ import java.util.prefs.Preferences;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JToolBar.Separator;
@ -130,9 +132,9 @@ public class MageFrame extends javax.swing.JFrame {
else
disableButtons();
//TODO:
//TODO: move to plugin impl
if (Plugins.getInstance().isCardPluginLoaded()) {
Separator separator = new javax.swing.JToolBar.Separator();
Separator separator = new javax.swing.JToolBar.Separator();
mageToolbar.add(separator);
JButton btnDownload = new JButton("Images");
@ -147,6 +149,15 @@ public class MageFrame extends javax.swing.JFrame {
});
mageToolbar.add(btnDownload);
}
if (Plugins.getInstance().isCounterPluginLoaded()) {
int i = Plugins.getInstance().getGamesPlayed();
JLabel label = new JLabel(" Games played: " + String.valueOf(i));
desktopPane.add(label);
label.setVisible(true);
label.setForeground(Color.white);
label.setBounds(0, 0, 100, 30);
}
}
private void btnImagesActionPerformed(java.awt.event.ActionEvent evt) {

View file

@ -35,6 +35,7 @@
package mage.client.game;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
@ -44,6 +45,7 @@ import java.util.UUID;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
@ -84,15 +86,11 @@ public class GamePanel extends javax.swing.JPanel {
//FIXME: remove from here
try {
/*BufferedImage background = ImageIO.read(this.getClass().getResourceAsStream("/green.jpg"));
ImagePanel bgPanel = new ImagePanel(background, ImagePanel.TILED);*/
// Override layout (I can't edit generated code)
this.setLayout(new BorderLayout());
final JLayeredPane j = new JLayeredPane();
j.add(ArrowBuilder.getArrowsPanel(), JLayeredPane.MODAL_LAYER);
j.setSize(1024,768);
//j.setBorder(BorderFactory.createLineBorder(Color.green));
this.add(j);
j.add(jSplitPane1, JLayeredPane.DEFAULT_LAYER);

View file

@ -19,6 +19,9 @@ public interface MagePlugins {
void updateGamePanel(Map<String, JComponent> ui);
MagePermanent getMagePermanent(PermanentView card, BigCard bigCard, CardDimensions dimension, UUID gameId);
boolean isCardPluginLoaded();
boolean isCounterPluginLoaded();
void sortPermanents(Map<String, JComponent> ui, Collection<MagePermanent> permanents);
void downloadImage(Set<Card> allCards);
int getGamesPlayed();
int addGamesPlayed();
}

View file

@ -2,7 +2,6 @@ package mage.client.plugins.impl;
import java.io.File;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@ -13,7 +12,6 @@ import javax.swing.JComponent;
import mage.cards.Card;
import mage.cards.CardDimensions;
import mage.cards.ExpansionSet;
import mage.cards.MagePermanent;
import mage.cards.action.impl.EmptyCallback;
import mage.client.cards.BigCard;
@ -22,9 +20,10 @@ import mage.client.plugins.MagePlugins;
import mage.client.util.Config;
import mage.client.util.DefaultActionCallback;
import mage.constants.Constants;
import mage.interfaces.PluginException;
import mage.interfaces.plugin.CardPlugin;
import mage.interfaces.plugin.CounterPlugin;
import mage.interfaces.plugin.ThemePlugin;
import mage.sets.Sets;
import mage.util.Logging;
import mage.view.PermanentView;
import net.xeoh.plugins.base.PluginManager;
@ -38,6 +37,7 @@ public class Plugins implements MagePlugins {
private static PluginManager pm;
private final static Logger logger = Logging.getLogger(Plugins.class.getName());
private CardPlugin cardPlugin = null;
private CounterPlugin counterPlugin = null;
protected static DefaultActionCallback defaultCallback = DefaultActionCallback.getInstance();
private static final EmptyCallback emptyCallback = new EmptyCallback();
@ -51,6 +51,7 @@ public class Plugins implements MagePlugins {
pm = PluginManagerFactory.createPluginManager();
pm.addPluginsFrom(new File(Constants.PLUGINS_DIRECTORY).toURI());
this.cardPlugin = pm.getPlugin(CardPlugin.class);
this.counterPlugin = pm.getPlugin(CounterPlugin.class);
logger.log(Level.INFO, "Done.");
}
@ -91,4 +92,39 @@ public class Plugins implements MagePlugins {
public void downloadImage(Set<Card> allCards) {
if (this.cardPlugin != null) this.cardPlugin.downloadImages(allCards);
}
@Override
public int getGamesPlayed() {
if (this.counterPlugin != null) {
synchronized(Plugins.class) {
try {
return this.counterPlugin.getGamePlayed();
} catch (PluginException e) {
logger.log(Level.SEVERE, e.getMessage());
throw new RuntimeException(e);
}
}
}
return -1;
}
@Override
public int addGamesPlayed() {
if (this.counterPlugin != null) {
synchronized(Plugins.class) {
try {
this.counterPlugin.addGamePlayed();
} catch (PluginException e) {
logger.log(Level.SEVERE, e.getMessage());
throw new RuntimeException(e);
}
}
}
return 0;
}
@Override
public boolean isCounterPluginLoaded() {
return this.counterPlugin != null;
}
}

View file

@ -35,6 +35,7 @@ import java.util.logging.Logger;
import javax.swing.JOptionPane;
import mage.client.MageFrame;
import mage.client.chat.ChatPanel;
import mage.client.plugins.impl.Plugins;
import mage.interfaces.callback.CallbackClient;
import mage.interfaces.callback.ClientCallback;
import mage.util.Logging;
@ -169,6 +170,10 @@ public class Client implements CallbackClient {
catch (Exception ex) {
handleException(ex);
}
if (Plugins.getInstance().isCounterPluginLoaded()) {
Plugins.getInstance().addGamesPlayed();
}
}
protected void watchGame(UUID gameId) {

View file

@ -29,13 +29,19 @@
package mage.interfaces;
/**
*
* Root application exception.
*
* @author BetaSteward_at_googlemail.com
*/
public class MageException extends Exception {
private static final long serialVersionUID = 8340806803178193696L;
public MageException(String message) {
super(message);
}
public MageException(Throwable t) {
super(t);
}
}

View file

@ -0,0 +1,20 @@
package mage.interfaces;
/**
* Exception thrown by plugin on errors.
*
* @author nantuko
*
*/
public class PluginException extends MageException {
private static final long serialVersionUID = 5528005696138392272L;
public PluginException(String message) {
super(message);
}
public PluginException(Throwable t) {
super(t);
}
}

View file

@ -0,0 +1,15 @@
package mage.interfaces.plugin;
import mage.interfaces.PluginException;
import net.xeoh.plugins.base.Plugin;
/**
* Interface for counter plugins
*
* @version 0.1 14.112010
* @author nantuko
*/
public interface CounterPlugin extends Plugin {
void addGamePlayed() throws PluginException;
int getGamePlayed() throws PluginException;
}

View file

@ -58,28 +58,6 @@
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<artifactSet>
<includes>
<include>log4j:log4j:jar:</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>mage-card-plugin</finalName>

View file

@ -0,0 +1,58 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.mage</groupId>
<artifactId>Mage-Plugins</artifactId>
<version>0.3</version>
</parent>
<groupId>org.mage</groupId>
<artifactId>Mage-Counter-Plugin</artifactId>
<packaging>jar</packaging>
<version>0.3</version>
<name>Mage Counter Plugin</name>
<description>Implements game counter to display amount of games played</description>
<dependencies>
<dependency>
<groupId>org.mage</groupId>
<artifactId>Mage-Common</artifactId>
<version>${mage-version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.jspf</groupId>
<artifactId>jspf-core</artifactId>
<version>${jspf-version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<finalName>mage-counter-plugin</finalName>
</build>
<properties>
<plugin-version>0.3</plugin-version>
<jspf-version>0.9.1</jspf-version>
</properties>
</project>

View file

@ -0,0 +1,30 @@
package org.mage.plugins.counter;
import java.io.Serializable;
/**
* Class for storing plugin data.
*
* @version 1.0 int version & int gamesPlayed fields
* @author nantuko
*/
public class CounterBean implements Serializable {
private static final long serialVersionUID = 6382055182568871761L;
private int gamesPlayed;
private int version = 1;
public int getGamesPlayed() {
return gamesPlayed;
}
public void setGamesPlayed(int gamesPlayed) {
this.gamesPlayed = gamesPlayed;
}
public final int getVersion() {
return version;
}
}

View file

@ -0,0 +1,149 @@
package org.mage.plugins.counter;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import mage.interfaces.PluginException;
import mage.interfaces.plugin.CounterPlugin;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import net.xeoh.plugins.base.annotations.events.Init;
import net.xeoh.plugins.base.annotations.events.PluginLoaded;
import net.xeoh.plugins.base.annotations.meta.Author;
import org.apache.log4j.Logger;
/**
* Implementation of {@link CounterPlugin}.<br/>
* Stores data in data folder.
*
* @version 0.1 14.11.2010 Initial Version
* @author nantuko
*/
@PluginImplementation
@Author(name = "nantuko")
public class CounterPluginImpl implements CounterPlugin {
private static final String PLUGIN_DATA_FOLDER_PATH = "plugins" + File.separator + "plugin.data" + File.separator + "counters";
private static final String DATA_STORAGE_FILE = "counters";
private static final Logger log = Logger.getLogger(CounterPluginImpl.class);
private boolean isLoaded = false;
@Init
public void init() {
File dataFolder = new File(PLUGIN_DATA_FOLDER_PATH);
if (!dataFolder.exists()) {
dataFolder.mkdirs();
if (!dataFolder.exists()) {
throw new RuntimeException("CounterPluginImpl: Couldn't create folders: " + PLUGIN_DATA_FOLDER_PATH);
}
}
File data = new File(PLUGIN_DATA_FOLDER_PATH + File.separator + DATA_STORAGE_FILE);
if (!data.exists()) {
try {
data.createNewFile();
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new RuntimeException("Couldn't create data file for counter plugin: " + e.getMessage());
}
}
this.isLoaded = true;
}
@PluginLoaded
public void newPlugin(CounterPlugin plugin) {
log.info(plugin.toString() + " has been loaded.");
}
public String toString() {
return "[Game counter plugin, version 0.1]";
}
@Override
public void addGamePlayed() throws PluginException {
if (!isLoaded) return;
File data = new File(PLUGIN_DATA_FOLDER_PATH + File.separator + DATA_STORAGE_FILE);
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
if (data.exists()) {
int prev = 0;
try {
ois = new ObjectInputStream(new FileInputStream(data));
Object o = ois.readObject();
CounterBean c = null;
if (o instanceof CounterBean) {
c = (CounterBean)o;
prev = c.getGamesPlayed();
}
} catch (EOFException e) {
// do nothing
} catch (IOException e) {
throw new PluginException(e);
} catch (ClassNotFoundException e) {
throw new PluginException(e);
} finally {
if (ois != null) try { ois.close(); } catch (Exception e) {}
}
try {
synchronized (this) {
oos = new ObjectOutputStream(new FileOutputStream(data));
CounterBean c = new CounterBean();
c.setGamesPlayed(prev+1);
oos.writeObject(c);
oos.close();
}
} catch (IOException e) {
throw new PluginException(e);
} finally {
if (oos != null) try { oos.close(); } catch (Exception e) {}
}
} else {
log.error("Counter plugin: data file doesn't exist, please restart plugin.");
}
}
@Override
public int getGamePlayed() throws PluginException {
if (!isLoaded) return -1;
File data = new File(PLUGIN_DATA_FOLDER_PATH + File.separator + DATA_STORAGE_FILE);
if (!data.exists()) {
return 0;
}
if (data.exists()) {
ObjectInputStream ois = null;
try {
synchronized (this) {
ois = new ObjectInputStream(new FileInputStream(data));
Object o = ois.readObject();
CounterBean c = null;
if (o instanceof CounterBean) {
c = (CounterBean)o;
}
ois.close();
return c.getGamesPlayed();
}
} catch (EOFException e) {
return 0;
} catch (IOException e) {
throw new PluginException(e);
} catch (ClassNotFoundException e) {
throw new PluginException(e);
} finally {
if (ois != null) try { ois.close(); } catch (Exception e) {}
}
} else {
log.error("Counter plugin: data file doesn't exist, please restart plugin.");
return 0;
}
}
}

View file

@ -20,6 +20,7 @@
<modules>
<module>Mage.Theme.Plugin</module>
<module>Mage.Card.Plugin</module>
<module>Mage.Counter.Plugin</module>
</modules>
</project>