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

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>