2014-05-28 20:18:30 +02:00
package mineplex.hub.queue ;
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.sql.Statement ;
public class QueueRepository
{
private static Object _connectionLock = new Object ( ) ;
private String _connectionString ;
private String _userName = " root " ;
private String _password = " tAbechAk3wR7tuTh " ;
private boolean _us = true ;
2014-05-30 04:47:57 +02:00
private static String CREATE_ELO_QUEUE_TABLE = " CREATE TABLE IF NOT EXISTS playerQueue (id INT NOT NULL AUTO_INCREMENT, playerList VARCHAR(256), gameType VARCHAR(256), playerCount INT, elo INT, state VARCHAR(256), time LONG, assignedMatch INT, US BOOLEAN NOT NULL DEFAULT '1', PRIMARY KEY (id), UNIQUE INDEX name_gametype (playerList, gameType)); " ;
2014-05-28 20:18:30 +02:00
private static String SAVE_STATE_VALUE = " UPDATE playerQueue SET state = ? WHERE id = ?; " ;
private static String DELETE_QUEUE_RECORD = " DELETE FROM playerQueue WHERE id = ?; " ;
2014-05-30 04:47:57 +02:00
private static String INSERT_ACCOUNT = " INSERT INTO playerQueue (playerList, gameType, elo, state, time, playerCount, assignedMatch) VALUES (?, ?, ?, 'Awaiting Match', now(), ?, -1) ON DUPLICATE KEY UPDATE time=VALUES(time); " ;
2014-05-28 20:18:30 +02:00
private static String RETRIEVE_MATCH_STATUS = " SELECT state, assignedMatch FROM playerQueue WHERE id = ?; " ;
private static String RETRIEVE_OTHER_MATCH_STATUS = " SELECT state, playerCount FROM playerQueue WHERE assignedMatch = ? AND id != ? ORDER BY id DESC; " ;
private Connection _connection = null ;
public QueueRepository ( String connectionUrl , boolean us )
{
_connectionString = connectionUrl ;
_us = us ;
initialize ( ) ;
}
public void initialize ( )
{
PreparedStatement preparedStatement = null ;
try
{
_connection = DriverManager . getConnection ( _connectionString , _userName , _password ) ;
// Create table
preparedStatement = _connection . prepareStatement ( CREATE_ELO_QUEUE_TABLE ) ;
preparedStatement . execute ( ) ;
}
catch ( Exception exception )
{
exception . printStackTrace ( ) ;
}
finally
{
if ( preparedStatement ! = null )
{
try
{
preparedStatement . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
}
}
public void deleteQueueRecord ( PlayerMatchStatus matchStatus )
{
PreparedStatement preparedStatement = null ;
try
{
synchronized ( _connectionLock )
{
if ( _connection . isClosed ( ) )
{
_connection = DriverManager . getConnection ( _connectionString , _userName , _password ) ;
}
preparedStatement = _connection . prepareStatement ( DELETE_QUEUE_RECORD ) ;
preparedStatement . setInt ( 1 , matchStatus . Id ) ;
if ( preparedStatement . executeUpdate ( ) = = 0 )
{
System . out . println ( " Error deleting queue record. " ) ;
}
}
}
catch ( Exception exception )
{
exception . printStackTrace ( ) ;
}
finally
{
if ( preparedStatement ! = null )
{
try
{
preparedStatement . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
}
}
public void updateState ( PlayerMatchStatus matchStatus )
{
PreparedStatement preparedStatement = null ;
try
{
synchronized ( _connectionLock )
{
if ( _connection . isClosed ( ) )
{
_connection = DriverManager . getConnection ( _connectionString , _userName , _password ) ;
}
preparedStatement = _connection . prepareStatement ( SAVE_STATE_VALUE ) ;
preparedStatement . setString ( 1 , matchStatus . State ) ;
preparedStatement . setInt ( 2 , matchStatus . Id ) ;
if ( preparedStatement . executeUpdate ( ) = = 0 )
{
System . out . println ( " Error updating state. " ) ;
}
}
}
catch ( Exception exception )
{
exception . printStackTrace ( ) ;
}
finally
{
if ( preparedStatement ! = null )
{
try
{
preparedStatement . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
}
}
public PlayerMatchStatus addQueueRecord ( String playerList , int playerCount , String gameType , int elo )
{
ResultSet resultSet = null ;
PreparedStatement preparedStatement = null ;
PlayerMatchStatus matchStatus = new PlayerMatchStatus ( ) ;
try
{
synchronized ( _connectionLock )
{
if ( _connection . isClosed ( ) )
{
_connection = DriverManager . getConnection ( _connectionString , _userName , _password ) ;
}
preparedStatement = _connection . prepareStatement ( INSERT_ACCOUNT , Statement . RETURN_GENERATED_KEYS ) ;
preparedStatement . setString ( 1 , playerList ) ;
preparedStatement . setString ( 2 , gameType ) ;
preparedStatement . setInt ( 3 , elo ) ;
//preparedStatement.setBoolean(4, _us);
preparedStatement . setInt ( 4 , playerCount ) ;
preparedStatement . executeUpdate ( ) ;
resultSet = preparedStatement . getGeneratedKeys ( ) ;
while ( resultSet . next ( ) )
{
matchStatus . Id = resultSet . getInt ( 1 ) ;
}
}
}
catch ( Exception exception )
{
exception . printStackTrace ( ) ;
}
finally
{
if ( preparedStatement ! = null )
{
try
{
preparedStatement . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
if ( resultSet ! = null )
{
try
{
resultSet . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
}
return matchStatus ;
}
2014-06-01 07:46:43 +02:00
public PlayerMatchStatus updateQueueStatus ( int id )
2014-05-28 20:18:30 +02:00
{
ResultSet resultSet = null ;
PreparedStatement preparedStatement = null ;
2014-06-01 07:46:43 +02:00
PlayerMatchStatus matchStatus = null ;
2014-05-28 20:18:30 +02:00
try
{
synchronized ( _connectionLock )
{
if ( _connection . isClosed ( ) )
{
_connection = DriverManager . getConnection ( _connectionString , _userName , _password ) ;
}
preparedStatement = _connection . prepareStatement ( RETRIEVE_MATCH_STATUS ) ;
preparedStatement . setInt ( 1 , id ) ;
resultSet = preparedStatement . executeQuery ( ) ;
while ( resultSet . next ( ) )
{
2014-06-01 07:46:43 +02:00
matchStatus = new PlayerMatchStatus ( ) ;
2014-05-28 20:18:30 +02:00
matchStatus . Id = id ;
matchStatus . State = resultSet . getString ( 1 ) ;
matchStatus . AssignedMatch = resultSet . getInt ( 2 ) ;
}
}
}
catch ( Exception exception )
{
exception . printStackTrace ( ) ;
}
finally
{
if ( preparedStatement ! = null )
{
try
{
preparedStatement . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
if ( resultSet ! = null )
{
try
{
resultSet . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
}
return matchStatus ;
}
public PlayerMatchStatus updateOtherPlayersMatchStatus ( PlayerMatchStatus matchStatus )
{
ResultSet resultSet = null ;
PreparedStatement preparedStatement = null ;
try
{
synchronized ( _connectionLock )
{
if ( _connection . isClosed ( ) )
{
_connection = DriverManager . getConnection ( _connectionString , _userName , _password ) ;
}
preparedStatement = _connection . prepareStatement ( RETRIEVE_OTHER_MATCH_STATUS ) ;
preparedStatement . setInt ( 1 , matchStatus . AssignedMatch ) ;
preparedStatement . setInt ( 2 , matchStatus . Id ) ;
resultSet = preparedStatement . executeQuery ( ) ;
matchStatus . OtherStatuses . clear ( ) ;
while ( resultSet . next ( ) )
{
int playerCount = resultSet . getInt ( 2 ) ;
for ( int i = 0 ; i < playerCount ; i + + )
matchStatus . OtherStatuses . add ( resultSet . getString ( 1 ) ) ;
}
}
}
catch ( Exception exception )
{
exception . printStackTrace ( ) ;
}
finally
{
if ( preparedStatement ! = null )
{
try
{
preparedStatement . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
if ( resultSet ! = null )
{
try
{
resultSet . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
}
return matchStatus ;
}
}