Mineplex2018-withcommit/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/ArcadeRepository.java

108 lines
2.8 KiB
Java

package nautilus.game.arcade;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.database.RepositoryBase;
import mineplex.database.Tables;
import mineplex.database.tables.records.GamesRecord;
import org.jooq.DSLContext;
import org.jooq.Query;
import org.jooq.impl.DSL;
public class ArcadeRepository extends RepositoryBase
{
private final String serverName;
public ArcadeRepository(JavaPlugin plugin)
{
super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh");
serverName = plugin.getConfig().getString("serverstatus.name");
}
@Override
protected void initialize()
{
}
@Override
protected void update()
{
}
public void saveBasicStats(GameType type, boolean tournament, int duration, Map<UUID, Boolean> players)
{
DSLContext context;
synchronized (this)
{
context = DSL.using(getConnection());
}
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();
}
public void saveLeaderboardStats(int tournamentId, int gameId, Map<UUID, Boolean> players)
{
DSLContext context;
synchronized (this)
{
context = DSL.using(getConnection());
}
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();
}
}