mirror of
https://github.com/correl/mage.git
synced 2024-12-26 03:00:11 +00:00
LogService. Added saving game started event to DB. Some refactoring.
This commit is contained in:
parent
bdb2754847
commit
b0a1c07067
8 changed files with 249 additions and 7 deletions
|
@ -1,14 +1,17 @@
|
||||||
package mage.db;
|
package mage.db;
|
||||||
|
|
||||||
|
import mage.db.model.Log;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author noxx
|
* @author noxx
|
||||||
*/
|
*/
|
||||||
public enum EntityManager {
|
public enum EntityManager implements Storage {
|
||||||
|
|
||||||
instance;
|
instance;
|
||||||
|
|
||||||
|
@ -16,6 +19,9 @@ public enum EntityManager {
|
||||||
|
|
||||||
private static final String MAGE_JDBC_URL = "jdbc:sqlite:db/mage.db";
|
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 {
|
static {
|
||||||
try {
|
try {
|
||||||
init();
|
init();
|
||||||
|
@ -29,6 +35,77 @@ public enum EntityManager {
|
||||||
return instance;
|
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<Log> getAllLogs() {
|
||||||
|
List<Log> logs = new ArrayList<Log>();
|
||||||
|
|
||||||
|
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<String> args = new ArrayList<String>();
|
||||||
|
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.
|
* Inits database. Creates tables if they don't exist.
|
||||||
*
|
*
|
||||||
|
@ -41,6 +118,7 @@ public enum EntityManager {
|
||||||
try {
|
try {
|
||||||
Statement stat = conn.createStatement();
|
Statement stat = conn.createStatement();
|
||||||
stat.executeUpdate("create table if not exists users (login, password, status);");
|
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 {
|
} finally {
|
||||||
try {
|
try {
|
||||||
conn.close();
|
conn.close();
|
||||||
|
@ -63,7 +141,7 @@ public enum EntityManager {
|
||||||
try {
|
try {
|
||||||
Statement stat = conn.createStatement();
|
Statement stat = conn.createStatement();
|
||||||
stat.executeUpdate("drop table users;");
|
stat.executeUpdate("drop table users;");
|
||||||
stat.executeUpdate("create table users (login, password, status);");
|
init();
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
conn.close();
|
conn.close();
|
||||||
|
@ -117,7 +195,7 @@ public enum EntityManager {
|
||||||
rs.close();
|
rs.close();
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
conn.close();
|
if (conn != null) conn.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// swallow
|
// swallow
|
||||||
}
|
}
|
||||||
|
@ -147,6 +225,7 @@ public enum EntityManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
//EntityManager.getInstance().reinit();
|
||||||
EntityManager.getInstance().testDB();
|
EntityManager.getInstance().testDB();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
33
Mage.Common/src/mage/db/EntityManagerTest.java
Normal file
33
Mage.Common/src/mage/db/EntityManagerTest.java
Normal file
|
@ -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<Log> 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(" --------------");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
Mage.Common/src/mage/db/Storage.java
Normal file
14
Mage.Common/src/mage/db/Storage.java
Normal file
|
@ -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<Log> getAllLogs();
|
||||||
|
}
|
45
Mage.Common/src/mage/db/model/Log.java
Normal file
45
Mage.Common/src/mage/db/model/Log.java
Normal file
|
@ -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<String> 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<String> getArguments() {
|
||||||
|
return arguments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setArguments(List<String> arguments) {
|
||||||
|
this.arguments = arguments;
|
||||||
|
}
|
||||||
|
}
|
|
@ -30,8 +30,10 @@ package mage.server;
|
||||||
|
|
||||||
import mage.Constants.RangeOfInfluence;
|
import mage.Constants.RangeOfInfluence;
|
||||||
import mage.Constants.TableState;
|
import mage.Constants.TableState;
|
||||||
|
import mage.MageException;
|
||||||
import mage.cards.decks.Deck;
|
import mage.cards.decks.Deck;
|
||||||
import mage.cards.decks.DeckCardLists;
|
import mage.cards.decks.DeckCardLists;
|
||||||
|
import mage.cards.decks.InvalidDeckException;
|
||||||
import mage.game.GameException;
|
import mage.game.GameException;
|
||||||
import mage.game.GameOptions;
|
import mage.game.GameOptions;
|
||||||
import mage.game.Seat;
|
import mage.game.Seat;
|
||||||
|
@ -42,16 +44,19 @@ import mage.game.events.Listener;
|
||||||
import mage.game.events.TableEvent;
|
import mage.game.events.TableEvent;
|
||||||
import mage.game.match.Match;
|
import mage.game.match.Match;
|
||||||
import mage.game.match.MatchOptions;
|
import mage.game.match.MatchOptions;
|
||||||
|
import mage.game.match.MatchPlayer;
|
||||||
import mage.game.tournament.Tournament;
|
import mage.game.tournament.Tournament;
|
||||||
import mage.game.tournament.TournamentOptions;
|
import mage.game.tournament.TournamentOptions;
|
||||||
import mage.MageException;
|
|
||||||
import mage.players.Player;
|
import mage.players.Player;
|
||||||
import mage.server.challenge.ChallengeManager;
|
import mage.server.challenge.ChallengeManager;
|
||||||
import mage.server.draft.DraftManager;
|
import mage.server.draft.DraftManager;
|
||||||
import mage.server.game.*;
|
import mage.server.game.*;
|
||||||
|
import mage.server.services.LogService;
|
||||||
|
import mage.server.services.impl.LogServiceImpl;
|
||||||
import mage.server.tournament.TournamentFactory;
|
import mage.server.tournament.TournamentFactory;
|
||||||
import mage.server.tournament.TournamentManager;
|
import mage.server.tournament.TournamentManager;
|
||||||
import mage.server.util.ServerMessagesUtil;
|
import mage.server.util.ServerMessagesUtil;
|
||||||
|
import mage.server.util.ThreadExecutor;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
@ -60,9 +65,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
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();
|
match.startGame();
|
||||||
table.initGame();
|
table.initGame();
|
||||||
GameManager.getInstance().createGameSession(match.getGame(), userPlayerMap, table.getId(), choosingPlayerId);
|
GameManager.getInstance().createGameSession(match.getGame(), userPlayerMap, table.getId(), choosingPlayerId);
|
||||||
|
String creator = null;
|
||||||
|
String opponent = null;
|
||||||
for (Entry<UUID, UUID> entry: userPlayerMap.entrySet()) {
|
for (Entry<UUID, UUID> entry: userPlayerMap.entrySet()) {
|
||||||
User user = UserManager.getInstance().getUser(entry.getKey());
|
User user = UserManager.getInstance().getUser(entry.getKey());
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
user.gameStarted(match.getGame().getId(), entry.getValue());
|
user.gameStarted(match.getGame().getId(), entry.getValue());
|
||||||
|
if (creator == null) {
|
||||||
|
creator = user.getName();
|
||||||
|
} else {
|
||||||
|
if (opponent == null) {
|
||||||
|
opponent = user.getName();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
TableManager.getInstance().removeTable(table.getId());
|
TableManager.getInstance().removeTable(table.getId());
|
||||||
|
@ -345,6 +356,9 @@ public class TableController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ServerMessagesUtil.getInstance().incGamesStarted();
|
ServerMessagesUtil.getInstance().incGamesStarted();
|
||||||
|
|
||||||
|
// log about game started
|
||||||
|
LogServiceImpl.instance.log(LogService.KEY_GAME_STARTED, String.valueOf(userPlayerMap.size()), creator, opponent);
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
logger.fatal("Error starting game", ex);
|
logger.fatal("Error starting game", ex);
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue