diff --git a/Mage.Common/src/mage/db/EntityManager.java b/Mage.Common/src/mage/db/EntityManager.java index fea5b8bf2e..131daa217e 100644 --- a/Mage.Common/src/mage/db/EntityManager.java +++ b/Mage.Common/src/mage/db/EntityManager.java @@ -1,14 +1,17 @@ package mage.db; +import mage.db.model.Log; import org.apache.log4j.Logger; import java.io.File; import java.sql.*; +import java.util.ArrayList; +import java.util.List; /** * @author noxx */ -public enum EntityManager { +public enum EntityManager implements Storage { instance; @@ -16,6 +19,9 @@ public enum EntityManager { private static final String MAGE_JDBC_URL = "jdbc:sqlite:db/mage.db"; + private static String QUERY_SAVE_LOG = "insert into logs values (?, ?, ?, ?, ?, ?, ?, ?)"; + private static String QUERY_GET_ALL_LOGS = "select * from logs"; + static { try { init(); @@ -29,6 +35,77 @@ public enum EntityManager { return instance; } + /** + * Inserts log entry to DB. + * + * @param key + * @param date + * @param args + * @throws Exception + */ + public void insertLog(String key, java.util.Date date, String... args) throws SQLException { + Connection conn = DriverManager.getConnection(MAGE_JDBC_URL); + + try { + PreparedStatement prep = conn.prepareStatement(QUERY_SAVE_LOG); + + prep.setString(1, key); + prep.setDate(2, new java.sql.Date(date.getTime())); + + int index = 3; + for (String arg : args) { + if (index > 8) break; + prep.setString(index++, arg); + } + + prep.execute(); + + } finally { + try { + if (conn != null) conn.close(); + } catch (Exception e) { + // swallow + } + } + } + + @Override + public List getAllLogs() { + List logs = new ArrayList(); + + try { + Connection conn = DriverManager.getConnection(MAGE_JDBC_URL); + try { + Statement stat = conn.createStatement(); + ResultSet rs = stat.executeQuery(QUERY_GET_ALL_LOGS); + while (rs.next()) { + Log log = new Log(rs.getString(1), rs.getDate(2)); + List args = new ArrayList(); + for (int index = 0; index < 6; index++) { + String arg = rs.getString(3 + index); + if (arg == null) { + break; + } + args.add(arg); + } + log.setArguments(args); + logs.add(log); + } + rs.close(); + } finally { + try { + if (conn != null) conn.close(); + } catch (Exception e) { + // swallow + } + } + } catch (SQLException e) { + log.fatal("SQL Exception: ", e); + } + + return logs; + } + /** * Inits database. Creates tables if they don't exist. * @@ -41,6 +118,7 @@ public enum EntityManager { try { Statement stat = conn.createStatement(); stat.executeUpdate("create table if not exists users (login, password, status);"); + stat.executeUpdate("create table if not exists logs (key, created_dt, arg0, arg1, arg2, arg3, arg4, arg5);"); } finally { try { conn.close(); @@ -63,7 +141,7 @@ public enum EntityManager { try { Statement stat = conn.createStatement(); stat.executeUpdate("drop table users;"); - stat.executeUpdate("create table users (login, password, status);"); + init(); } finally { try { conn.close(); @@ -117,7 +195,7 @@ public enum EntityManager { rs.close(); } finally { try { - conn.close(); + if (conn != null) conn.close(); } catch (Exception e) { // swallow } @@ -147,6 +225,7 @@ public enum EntityManager { } public static void main(String[] args) throws Exception { + //EntityManager.getInstance().reinit(); EntityManager.getInstance().testDB(); } } diff --git a/Mage.Common/src/mage/db/EntityManagerTest.java b/Mage.Common/src/mage/db/EntityManagerTest.java new file mode 100644 index 0000000000..613ac98a09 --- /dev/null +++ b/Mage.Common/src/mage/db/EntityManagerTest.java @@ -0,0 +1,33 @@ +package mage.db; + +import mage.db.model.Log; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.List; + +/** + * @author noxx + */ +public class EntityManagerTest { + + private static DateFormat timeFormatter = SimpleDateFormat.getTimeInstance(SimpleDateFormat.FULL); + + public static void main(String[] args) throws Exception { + EntityManager.instance.testDB(); + List logs = EntityManager.instance.getAllLogs(); + System.out.println("logs found: " + logs.size()); + for (Log log : logs) { + System.out.println(" key=" + log.getKey()); + System.out.println(" date=" + timeFormatter.format(log.getCreatedDate())); + System.out.print(" arguments=[ "); + if (log.getArguments() != null) { + for (String argument : log.getArguments()) { + System.out.print("arg=" + argument + " "); + } + } + System.out.println("]"); + System.out.println(" --------------"); + } + } +} diff --git a/Mage.Common/src/mage/db/Storage.java b/Mage.Common/src/mage/db/Storage.java new file mode 100644 index 0000000000..a309587046 --- /dev/null +++ b/Mage.Common/src/mage/db/Storage.java @@ -0,0 +1,14 @@ +package mage.db; + +import mage.db.model.Log; + +import java.util.Date; +import java.util.List; + +/** + * + */ +public interface Storage { + void insertLog(String key, Date date, String... args) throws Exception; + List getAllLogs(); +} diff --git a/Mage.Common/src/mage/db/model/Log.java b/Mage.Common/src/mage/db/model/Log.java new file mode 100644 index 0000000000..c69648296b --- /dev/null +++ b/Mage.Common/src/mage/db/model/Log.java @@ -0,0 +1,45 @@ +package mage.db.model; + +import java.util.Date; +import java.util.List; + +/** + * @author noxx + */ +public class Log { + + private String key; + + private Date createdDate; + + private List arguments; + + public Log(String key, Date createdDate) { + this.key = key; + this.createdDate = createdDate; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public Date getCreatedDate() { + return createdDate; + } + + public void setCreatedDate(Date createdDate) { + this.createdDate = createdDate; + } + + public List getArguments() { + return arguments; + } + + public void setArguments(List arguments) { + this.arguments = arguments; + } +} diff --git a/Mage.Server/src/main/java/mage/server/TableController.java b/Mage.Server/src/main/java/mage/server/TableController.java index 4351355151..29b2d300e7 100644 --- a/Mage.Server/src/main/java/mage/server/TableController.java +++ b/Mage.Server/src/main/java/mage/server/TableController.java @@ -30,8 +30,10 @@ package mage.server; import mage.Constants.RangeOfInfluence; import mage.Constants.TableState; +import mage.MageException; import mage.cards.decks.Deck; import mage.cards.decks.DeckCardLists; +import mage.cards.decks.InvalidDeckException; import mage.game.GameException; import mage.game.GameOptions; import mage.game.Seat; @@ -42,16 +44,19 @@ import mage.game.events.Listener; import mage.game.events.TableEvent; import mage.game.match.Match; import mage.game.match.MatchOptions; +import mage.game.match.MatchPlayer; import mage.game.tournament.Tournament; import mage.game.tournament.TournamentOptions; -import mage.MageException; import mage.players.Player; import mage.server.challenge.ChallengeManager; import mage.server.draft.DraftManager; import mage.server.game.*; +import mage.server.services.LogService; +import mage.server.services.impl.LogServiceImpl; import mage.server.tournament.TournamentFactory; import mage.server.tournament.TournamentManager; import mage.server.util.ServerMessagesUtil; +import mage.server.util.ThreadExecutor; import org.apache.log4j.Logger; import java.util.Map.Entry; @@ -60,9 +65,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; -import mage.cards.decks.InvalidDeckException; -import mage.game.match.MatchPlayer; -import mage.server.util.ThreadExecutor; /** * @@ -332,10 +334,19 @@ public class TableController { match.startGame(); table.initGame(); GameManager.getInstance().createGameSession(match.getGame(), userPlayerMap, table.getId(), choosingPlayerId); + String creator = null; + String opponent = null; for (Entry entry: userPlayerMap.entrySet()) { User user = UserManager.getInstance().getUser(entry.getKey()); if (user != null) { user.gameStarted(match.getGame().getId(), entry.getValue()); + if (creator == null) { + creator = user.getName(); + } else { + if (opponent == null) { + opponent = user.getName(); + } + } } else { TableManager.getInstance().removeTable(table.getId()); @@ -345,6 +356,9 @@ public class TableController { } } ServerMessagesUtil.getInstance().incGamesStarted(); + + // log about game started + LogServiceImpl.instance.log(LogService.KEY_GAME_STARTED, String.valueOf(userPlayerMap.size()), creator, opponent); } catch (Exception ex) { logger.fatal("Error starting game", ex); diff --git a/Mage.Server/src/main/java/mage/server/services/LogService.java b/Mage.Server/src/main/java/mage/server/services/LogService.java new file mode 100644 index 0000000000..6fcd5a8b85 --- /dev/null +++ b/Mage.Server/src/main/java/mage/server/services/LogService.java @@ -0,0 +1,12 @@ +package mage.server.services; + +/** + * Responsible for gathering logs and storing them in DB. + * + * @author noxx + */ +public interface LogService { + public static final String KEY_GAME_STARTED = "gameStarted"; + + void log(String key, String... args); +} diff --git a/Mage.Server/src/main/java/mage/server/services/MageService.java b/Mage.Server/src/main/java/mage/server/services/MageService.java new file mode 100644 index 0000000000..d80d7568d7 --- /dev/null +++ b/Mage.Server/src/main/java/mage/server/services/MageService.java @@ -0,0 +1,18 @@ +package mage.server.services; + +/** + * Common interface for all services. + * + * @author noxx + */ +public interface MageService { + /** + * Restores data on startup. + */ + void initService(); + + /** + * Dumps data to DB. + */ + void saveData(); +} diff --git a/Mage.Server/src/main/java/mage/server/services/impl/LogServiceImpl.java b/Mage.Server/src/main/java/mage/server/services/impl/LogServiceImpl.java new file mode 100644 index 0000000000..8ab91fd2b6 --- /dev/null +++ b/Mage.Server/src/main/java/mage/server/services/impl/LogServiceImpl.java @@ -0,0 +1,27 @@ +package mage.server.services.impl; + +import mage.db.EntityManager; +import mage.server.services.LogService; +import org.apache.log4j.Logger; + +import java.util.Calendar; + +/** + * @author noxx + */ +public enum LogServiceImpl implements LogService { + instance; + + private static Logger log = Logger.getLogger(LogServiceImpl.class); + + @Override + public void log(String key, String... args) { + Calendar cal = Calendar.getInstance(); + try { + EntityManager.instance.insertLog(key, cal.getTime(), args); + } catch (Exception e) { + log.fatal(e); + } + } + +}