Skip to content

Commit

Permalink
team check to team id (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer authored Mar 10, 2024
1 parent d04f33f commit d66a60a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ private UUID getPlayerId(String input) {

// /team delete <name>
private boolean handleDelete(CommandSender sender, String name) {
if (!this.plugin.getTeamCache().isTeamExists(name)) {
int teamId = this.plugin.getTeamDatabase().getTeamId(name);
if (teamId < 0) {
this.plugin.sendConfigMessage(sender, "messages.error.teamNotFound");
return false;
}

if (!this.plugin.getTeamDatabase().deleteTeam(name)) {
if (!this.plugin.getTeamDatabase().deleteTeam(teamId)) {
return false;
}

Expand All @@ -122,7 +123,7 @@ private boolean handleLeave(CommandSender sender, String leaver, String name) {
return false;
}

if (!this.plugin.getTeamDatabase().leaveTeam(leaverId, name)) {
if (!this.plugin.getTeamDatabase().leaveTeam(leaverId, playerTeam.getTeamId())) {
return false;
}

Expand All @@ -143,12 +144,13 @@ private boolean handleJoin(CommandSender sender, String member, String name) {
return false;
}

if (!this.plugin.getTeamCache().isTeamExists(name)) {
int teamId = this.plugin.getTeamDatabase().getTeamId(name);
if (teamId < 0) {
this.plugin.sendConfigMessage(sender, "messages.error.teamNotFound");
return false;
}

if (!this.plugin.getTeamDatabase().joinTeam(memberId, name)) {
if (!this.plugin.getTeamDatabase().joinTeam(memberId, teamId)) {
return false;
}

Expand All @@ -159,12 +161,13 @@ private boolean handleJoin(CommandSender sender, String member, String name) {
// /team modify <name> <modifyType> [value]
// modifyType: name, color
private boolean handleModify(CommandSender sender, String name, String modifyType, String value) {
if (!this.plugin.getTeamCache().isTeamExists(name)) {
int teamId = this.plugin.getTeamDatabase().getTeamId(name);
if (teamId < 0) {
this.plugin.sendConfigMessage(sender, "messages.error.teamNotFound");
return false;
}

if (!this.plugin.getTeamDatabase().modifyTeam(name, modifyType, value)) {
if (!this.plugin.getTeamDatabase().modifyTeam(teamId, modifyType, value)) {
return false;
}

Expand All @@ -174,7 +177,8 @@ private boolean handleModify(CommandSender sender, String name, String modifyTyp

// /team create <name>
private boolean handleCreate(CommandSender sender, String teamName) {
if (this.plugin.getTeamCache().isTeamExists(teamName)) {
int teamId = this.plugin.getTeamDatabase().getTeamId(teamName);
if (teamId >= 0) {
this.plugin.sendConfigMessage(sender, "messages.error.teamAlreadyExists");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ public void initCache() {
}
}

public boolean isTeamExists(String name) {
return this.teams.values().stream().anyMatch(team -> team.getName().equals(name));
}

public void addTeam(UUID key, TeamMySQL.PlayerTeam value) {
this.teams.put(key, value);
}
Expand All @@ -37,11 +33,11 @@ public void removeTeam(UUID uuid) {
this.teams.remove(uuid);
}

public void removeTeamByName(String name) {
this.teams.values().removeIf(team -> team.getName().equals(name));
public void removeTeamByTeamId(int teamId) {
this.teams.values().removeIf(team -> team.getTeamId() == teamId);
}

public void updateTeam(String name, Consumer<TeamMySQL.PlayerTeam> consumer) {
this.teams.values().stream().filter(team -> team.getName().equals(name)).forEach(consumer);
public void updateTeam(int teamId, Consumer<TeamMySQL.PlayerTeam> consumer) {
this.teams.values().stream().filter(team -> team.getTeamId() == teamId).forEach(consumer);
}
}
115 changes: 60 additions & 55 deletions src/main/java/net/simplyvanilla/simplynicks/database/TeamMySQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.logging.Level;

public class TeamMySQL {
Expand Down Expand Up @@ -81,6 +84,21 @@ public synchronized void close() {
}
}

public int getTeamId(String name) {
try (PreparedStatement preparedStatement = this.connection.prepareStatement(
String.format("SELECT `id` FROM `%s` WHERE `name` = ?", this.teamTableName)
)) {
preparedStatement.setString(1, name);
var resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
return resultSet.getInt("id");
}
} catch (Exception ex) {
this.plugin.getLogger().log(Level.SEVERE, "Unable to getTeamId...", ex);
}
return -1;
}

public boolean createTeam(String name) {
try (PreparedStatement preparedStatement = this.connection.prepareStatement(
String.format("INSERT INTO `%s` (`name`, `color`) VALUES (?, ?)", this.teamTableName)
Expand All @@ -95,25 +113,39 @@ public boolean createTeam(String name) {
}
}

public boolean modifyTeam(String name, String modifyType, String value) {
public boolean deleteTeam(int teamId) {
try (PreparedStatement preparedStatement = this.connection.prepareStatement(
String.format("DELETE FROM `%s` WHERE id = ?", this.teamTableName)
)) {
preparedStatement.setInt(1, teamId);
preparedStatement.executeUpdate();
this.plugin.getTeamCache().removeTeamByTeamId(teamId);
return true;
} catch (Exception ex) {
this.plugin.getLogger().log(Level.SEVERE, "Unable to deleteTeam...", ex);
return false;
}
}

public boolean modifyTeam(int teamId, String modifyType, String value) {
if (!modifyType.equalsIgnoreCase("name") &&
!modifyType.equalsIgnoreCase("color")) {
return false;
}

try (PreparedStatement preparedStatement = this.connection.prepareStatement(
String.format("UPDATE `%s` SET `%s` = ? WHERE `name` = ?", this.teamTableName, modifyType.toLowerCase())
String.format("UPDATE `%s` SET `%s` = ? WHERE `id` = ?", this.teamTableName, modifyType.toLowerCase())
)) {
Runnable afterRun;
if (modifyType.equalsIgnoreCase("name")) {
afterRun = () -> this.plugin.getTeamCache().updateTeam(name, team -> team.setName(value));
afterRun = () -> this.plugin.getTeamCache().updateTeam(teamId, team -> team.setName(value));
} else if (modifyType.equalsIgnoreCase("color")) {
afterRun = () -> this.plugin.getTeamCache().updateTeam(name, team -> team.setColor(value));
afterRun = () -> this.plugin.getTeamCache().updateTeam(teamId, team -> team.setColor(value));
} else {
return false;
}
preparedStatement.setString(1, value);
preparedStatement.setString(2, name);
preparedStatement.setInt(2, teamId);
preparedStatement.executeUpdate();
afterRun.run();
return true;
Expand All @@ -123,27 +155,7 @@ public boolean modifyTeam(String name, String modifyType, String value) {
}
}

private int getTeamId(String name) {
try (PreparedStatement preparedStatement = this.connection.prepareStatement(
String.format("SELECT `id` FROM `%s` WHERE `name` = ?", this.teamTableName)
)) {
preparedStatement.setString(1, name);
var resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
return resultSet.getInt("id");
}
} catch (Exception ex) {
this.plugin.getLogger().log(Level.SEVERE, "Unable to getTeamId...", ex);
}
return -1;
}

public boolean joinTeam(UUID uuid, String name) {
int teamId = getTeamId(name);
if (teamId < 0) {
return false;
}

public boolean joinTeam(UUID uuid, int teamId) {
try (PreparedStatement preparedStatement = this.connection.prepareStatement(
String.format("INSERT INTO `%s` (`id`, `team_id`) VALUES (UUID_TO_BIN(?), ?)", this.playerTeamTableName)
)) {
Expand All @@ -158,12 +170,7 @@ public boolean joinTeam(UUID uuid, String name) {
}
}

public boolean leaveTeam(UUID uuid, String name) {
int teamId = getTeamId(name);
if (teamId < 0) {
return false;
}

public boolean leaveTeam(UUID uuid, int teamId) {
try (PreparedStatement preparedStatement = this.connection.prepareStatement(
String.format("DELETE FROM `%s` WHERE `id` = UUID_TO_BIN(?) AND `team_id` = ?", this.playerTeamTableName)
)) {
Expand All @@ -178,37 +185,24 @@ public boolean leaveTeam(UUID uuid, String name) {
}
}

public boolean deleteTeam(String name) {
try (PreparedStatement preparedStatement = this.connection.prepareStatement(
String.format("DELETE FROM `%s` WHERE name = ?", this.teamTableName)
)) {
preparedStatement.setString(1, name);
preparedStatement.executeUpdate();
this.plugin.getTeamCache().removeTeamByName(name);
return true;
} catch (Exception ex) {
this.plugin.getLogger().log(Level.SEVERE, "Unable to deleteTeam...", ex);
return false;
}
}

public Map<UUID, PlayerTeam> getAllTeams() {
Map<UUID, PlayerTeam> teams = new HashMap<>();

try (PreparedStatement preparedStatement = this.connection.prepareStatement(
String.format(
"""
SELECT BIN_TO_UUID(pt.id) AS player_id, t.name AS team_name, t.color AS team_color
FROM `%s` AS pt
JOIN `%s` AS t ON pt.team_id = t.id;
""", this.playerTeamTableName, this.teamTableName)
SELECT BIN_TO_UUID(pt.id) AS player_id, t.id AS team_id, t.name AS team_name, t.color AS team_color
FROM `%s` AS pt
JOIN `%s` AS t ON pt.team_id = t.id;
""", this.playerTeamTableName, this.teamTableName)
)) {
var resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
UUID playerId = UUID.fromString(resultSet.getString("player_id"));
int teamId = resultSet.getInt("team_id");
String teamName = resultSet.getString("team_name");
String teamColor = resultSet.getString("team_color");
teams.put(playerId, new PlayerTeam(playerId, teamName, teamColor));
teams.put(playerId, new PlayerTeam(playerId, teamId, teamName, teamColor));
}
} catch (Exception ex) {
this.plugin.getLogger().log(Level.SEVERE, "Unable to getAllTeams...", ex);
Expand All @@ -220,7 +214,7 @@ SELECT BIN_TO_UUID(pt.id) AS player_id, t.name AS team_name, t.color AS team_col
public PlayerTeam getTeam(UUID uuid) {
try (PreparedStatement preparedStatement = this.connection.prepareStatement(
String.format("""
SELECT t.name AS team_name, t.color AS team_color
SELECT t.id AS team_id, t.name AS team_name, t.color AS team_color
FROM `%s` AS pt
JOIN `%s` AS t ON pt.team_id = t.id
WHERE pt.id = UUID_TO_BIN(?);
Expand All @@ -229,9 +223,10 @@ public PlayerTeam getTeam(UUID uuid) {
preparedStatement.setString(1, uuid.toString());
var resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
int teamId = resultSet.getInt("team_id");
String teamName = resultSet.getString("team_name");
String teamColor = resultSet.getString("team_color");
return new PlayerTeam(uuid, teamName, teamColor);
return new PlayerTeam(uuid, teamId, teamName, teamColor);
}
} catch (Exception ex) {
this.plugin.getLogger().log(Level.SEVERE, "Unable to getTeam...", ex);
Expand All @@ -242,11 +237,13 @@ public PlayerTeam getTeam(UUID uuid) {

public static final class PlayerTeam {
private UUID uuid;
private int teamId;
private String name;
private String color;

public PlayerTeam(UUID uuid, String name, String color) {
public PlayerTeam(UUID uuid, int teamId, String name, String color) {
this.uuid = uuid;
this.teamId = teamId;
this.name = name;
this.color = color;
}
Expand Down Expand Up @@ -274,5 +271,13 @@ public String getColor() {
public void setColor(String color) {
this.color = color;
}

public int getTeamId() {
return teamId;
}

public void setTeamId(int teamId) {
this.teamId = teamId;
}
}
}

0 comments on commit d66a60a

Please sign in to comment.