2014-09-16 05:31:53 +02:00
|
|
|
package nautilus.game.arcade;
|
2014-09-05 10:00:51 +02:00
|
|
|
|
2014-10-29 22:54:40 +01:00
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.SQLException;
|
2014-09-05 10:00:51 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
2014-10-29 22:54:40 +01:00
|
|
|
import org.bukkit.plugin.Plugin;
|
2014-09-05 10:00:51 +02:00
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
|
2014-10-29 22:54:40 +01:00
|
|
|
import mineplex.core.database.DBPool;
|
2014-09-05 10:00:51 +02:00
|
|
|
import mineplex.core.database.RepositoryBase;
|
|
|
|
import mineplex.database.Tables;
|
2014-09-16 05:31:53 +02:00
|
|
|
import mineplex.database.tables.records.GamesRecord;
|
2014-09-05 10:00:51 +02:00
|
|
|
import org.jooq.DSLContext;
|
|
|
|
import org.jooq.Query;
|
2014-09-16 05:31:53 +02:00
|
|
|
import org.jooq.impl.DSL;
|
2014-09-05 10:00:51 +02:00
|
|
|
|
2014-09-16 05:31:53 +02:00
|
|
|
public class ArcadeRepository extends RepositoryBase
|
2014-09-05 10:00:51 +02:00
|
|
|
{
|
2014-09-18 07:56:46 +02:00
|
|
|
private final String serverName;
|
|
|
|
|
2014-10-29 22:54:40 +01:00
|
|
|
public ArcadeRepository(Plugin plugin)
|
2014-09-05 10:00:51 +02:00
|
|
|
{
|
2014-10-29 22:54:40 +01:00
|
|
|
super(plugin, DBPool.ACCOUNT);
|
2014-09-18 07:56:46 +02:00
|
|
|
|
|
|
|
serverName = plugin.getConfig().getString("serverstatus.name");
|
2014-09-05 10:00:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void initialize()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void update()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-10-29 22:54:40 +01:00
|
|
|
public void saveBasicStats(GameType type, boolean tournament, int duration, Map<UUID, Boolean> players) throws SQLException
|
2014-09-05 10:00:51 +02:00
|
|
|
{
|
2014-10-29 22:54:40 +01:00
|
|
|
try (Connection connection = getConnection())
|
2014-09-05 10:00:51 +02:00
|
|
|
{
|
2014-10-29 22:54:40 +01:00
|
|
|
DSLContext context = DSL.using(connection);
|
|
|
|
|
|
|
|
GamesRecord record = context.newRecord(Tables.games);
|
|
|
|
record.setDuration(duration);
|
|
|
|
record.setTournament(tournament);
|
|
|
|
record.setType(type.name());
|
|
|
|
record.setServer(serverName);
|
|
|
|
record.store();
|
|
|
|
|
|
|
|
List<Query> queryList = new ArrayList<>(players.size());
|
|
|
|
|
|
|
|
for (Map.Entry<UUID, Boolean> entry : players.entrySet())
|
|
|
|
{
|
|
|
|
Query query = context
|
|
|
|
.insertInto(Tables.gamePlayers)
|
|
|
|
.set(Tables.gamePlayers.gameId, record.getId())
|
|
|
|
.set(Tables.gamePlayers.accountId, DSL.select(Tables.accounts.id)
|
|
|
|
.from(Tables.accounts)
|
|
|
|
.where(Tables.accounts.uuid.eq(entry.getKey().toString())))
|
|
|
|
.set(Tables.gamePlayers.winner, entry.getValue());
|
|
|
|
|
|
|
|
queryList.add(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
context.batch(queryList).execute();
|
2014-09-05 10:00:51 +02:00
|
|
|
}
|
2014-09-16 05:31:53 +02:00
|
|
|
}
|
|
|
|
|
2014-10-29 22:54:40 +01:00
|
|
|
public void saveLeaderboardStats(int tournamentId, int gameId, Map<UUID, Boolean> players) throws SQLException
|
2014-09-16 05:31:53 +02:00
|
|
|
{
|
2014-10-29 22:54:40 +01:00
|
|
|
try (Connection connection = getConnection())
|
2014-09-05 10:00:51 +02:00
|
|
|
{
|
2014-10-29 22:54:40 +01:00
|
|
|
DSLContext context = DSL.using(connection);
|
|
|
|
|
|
|
|
List<Query> queryList = new ArrayList<>(players.size());
|
|
|
|
|
|
|
|
for (Map.Entry<UUID, Boolean> entry : players.entrySet())
|
|
|
|
{
|
|
|
|
int winIncrement = entry.getValue() ? 1 : 0;
|
|
|
|
|
|
|
|
Query query = context
|
|
|
|
.insertInto(Tables.tournamentLeaderboard)
|
|
|
|
.set(Tables.tournamentLeaderboard.tournamentId, tournamentId)
|
|
|
|
.set(Tables.tournamentLeaderboard.gameId, gameId)
|
|
|
|
.set(Tables.tournamentLeaderboard.accountId, DSL.select(Tables.accounts.id)
|
|
|
|
.from(Tables.accounts)
|
|
|
|
.where(Tables.accounts.uuid.eq(entry.getKey().toString())))
|
|
|
|
.set(Tables.tournamentLeaderboard.wins, winIncrement)
|
|
|
|
.set(Tables.tournamentLeaderboard.total, 1)
|
|
|
|
.onDuplicateKeyUpdate()
|
|
|
|
.set(Tables.tournamentLeaderboard.wins, Tables.tournamentLeaderboard.wins.plus(winIncrement))
|
|
|
|
.set(Tables.tournamentLeaderboard.total, Tables.tournamentLeaderboard.total.plus(1));
|
|
|
|
|
|
|
|
queryList.add(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
context.batch(queryList).execute();
|
2014-09-05 10:00:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|