Plein de code en plus, données dans la BDD
This commit is contained in:
parent
eb4dfd6613
commit
e920c67340
77
src/com/bernard/juliateam/DiscordTeamManager.java
Normal file
77
src/com/bernard/juliateam/DiscordTeamManager.java
Normal file
@ -0,0 +1,77 @@
|
||||
package com.bernard.juliateam;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.Set;
|
||||
|
||||
import com.bernard.juliateam.JuliaGame.JuliaTeam;
|
||||
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.entities.Category;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import net.dv8tion.jda.api.entities.VoiceChannel;
|
||||
|
||||
public class DiscordTeamManager {
|
||||
|
||||
public DiscordTeamManager() {}
|
||||
|
||||
public void removeGameContext(JuliaGame game,JDA jda) {
|
||||
Guild g = jda.getGuildById(game.getGuildId());
|
||||
|
||||
|
||||
Role role = g.getRoleById(game.getGeneralRoleId());
|
||||
|
||||
for(JuliaTeam team : game.getTeams()) {
|
||||
Role tr = g.getRoleById(team.roleId);
|
||||
team.membres.stream().map(g::getMemberById).forEach(m -> g.removeRoleFromMember(m,tr).queue());
|
||||
team.membres.stream().map(g::getMemberById).forEach(m -> g.removeRoleFromMember(m,role).queue());
|
||||
g.getVoiceChannelById(team.vocalChannelId).delete().queue();
|
||||
}
|
||||
g.getGuildChannelById(game.getGeneralTextChannelId()).getParent().delete().queue();
|
||||
g.getTextChannelById(game.getGeneralTextChannelId()).delete().queue();
|
||||
g.getVoiceChannelById(game.getGeneralVocChannelId()).delete().queue();
|
||||
role.delete().complete();
|
||||
|
||||
game.end();
|
||||
|
||||
}
|
||||
|
||||
public void initGameContext(JuliaGame game,JDA jda) {
|
||||
|
||||
String name = game.getName();
|
||||
Guild guild = jda.getGuildById(game.getGuildId());
|
||||
Set<JuliaGame.JuliaTeam> teams = game.getTeams();
|
||||
//Creating GAME
|
||||
Role role = guild.createRole().setName(name + " player").complete();
|
||||
EnumSet<Permission> accessorRoleSet = EnumSet.of(Permission.VOICE_CONNECT, Permission.VIEW_CHANNEL, Permission.MESSAGE_READ, Permission.MESSAGE_WRITE, Permission.VOICE_CONNECT, Permission.VOICE_SPEAK);
|
||||
Category category = (Category) guild.createCategory(name + " game")
|
||||
.addPermissionOverride(role, accessorRoleSet, null)
|
||||
.addPermissionOverride(guild.getPublicRole(), null, Permission.getPermissions(Permission.ALL_PERMISSIONS))
|
||||
.complete();
|
||||
TextChannel channel = guild.createTextChannel(name).setParent(category).complete();
|
||||
VoiceChannel voiceChannel = guild.createVoiceChannel(name).setParent(category).complete();
|
||||
|
||||
game.setChannelsMetadata(channel.getIdLong(), voiceChannel.getIdLong(), role.getIdLong());
|
||||
|
||||
//Creating TEAM
|
||||
for(JuliaTeam team : teams) {
|
||||
Role teamRole = guild.createRole().setName(name + " player - team "+team.name).complete();
|
||||
VoiceChannel chan = (VoiceChannel) guild.createVoiceChannel("Team "+team.name)
|
||||
.setParent(category)
|
||||
.addPermissionOverride(role, null, accessorRoleSet)
|
||||
.addPermissionOverride(teamRole, accessorRoleSet, null)
|
||||
.complete();
|
||||
|
||||
//Creating PLAYERS
|
||||
team.membres.stream()
|
||||
.map(guild::getMemberById)
|
||||
.forEach(m -> guild.addRoleToMember(m,teamRole));
|
||||
|
||||
team.setMetadata(chan.getIdLong(), teamRole.getIdLong());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,34 +1,237 @@
|
||||
package com.bernard.juliateam;
|
||||
|
||||
import java.util.List;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import net.dv8tion.jda.core.entities.Guild;
|
||||
import net.dv8tion.jda.core.entities.Member;
|
||||
import net.dv8tion.jda.core.entities.TextChannel;
|
||||
import net.dv8tion.jda.core.entities.VoiceChannel;
|
||||
|
||||
public class JuliaGame {
|
||||
|
||||
public String name;
|
||||
public Guild g;
|
||||
public List<String> teamsNames;
|
||||
public List<Set<Member>> teams;
|
||||
private String name;
|
||||
private Long guildId;
|
||||
private Set<JuliaTeam> teams;
|
||||
|
||||
protected Status status;
|
||||
private Long generalTextChannelId;
|
||||
private Long generalVocChannelId;
|
||||
private Long generalRoleId;
|
||||
|
||||
public List<VoiceChannel> teamsChannels;
|
||||
public TextChannel channel;
|
||||
public Set<Member> unAnswered;
|
||||
public boolean started = false;
|
||||
public JuliaGame(String name, Guild g, List<String> teamsNames, List<Set<Member>> teams) {
|
||||
private int gameBddID;
|
||||
private Connection db;
|
||||
|
||||
public JuliaGame(String name, Long guild, Connection db) {
|
||||
this.name = name;
|
||||
this.g = g;
|
||||
this.teamsNames = teamsNames;
|
||||
this.teams = teams;
|
||||
for(Set<Member> team : teams)
|
||||
unAnswered.addAll(team);
|
||||
this.guildId = guild;
|
||||
this.teams = new HashSet<JuliaGame.JuliaTeam>();
|
||||
this.db = db;
|
||||
this.status = Status.CREATED;
|
||||
|
||||
try {
|
||||
PreparedStatement gameStatement = db.prepareStatement("INSERT INTO juliateam_games (name,guildID,status,textChannelID,voiceChannelID,roleID) VALUES (?,?,CREATED,NULL,NULL,NULL);SELECT LAST_INSERT_ID();");
|
||||
gameStatement.setString(1, name);
|
||||
gameStatement.setLong(2, guild);
|
||||
gameBddID = gameStatement.executeQuery().getInt(1);
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException("Impossible de se connecter à la bdd", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void rename(String name) {
|
||||
try {
|
||||
PreparedStatement gameStatement = db.prepareStatement("UPDATE juliateam_games SET name=? WHERE ID=?");
|
||||
gameStatement.setString(1, name);
|
||||
gameStatement.setInt(2, gameBddID);
|
||||
gameStatement.executeUpdate();
|
||||
this.name = name;
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException("Impossible de se connecter à la bdd", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Long getGuildId() {
|
||||
return guildId;
|
||||
}
|
||||
|
||||
public void setTeam(String name, Set<Long> membres) {
|
||||
Optional<JuliaTeam> team = teams.stream().filter(t -> t.name.equalsIgnoreCase(name)).findAny();
|
||||
if(team.isPresent())
|
||||
team.get().setTeam(membres);
|
||||
else
|
||||
addTeam(new JuliaTeam(name, membres));
|
||||
}
|
||||
|
||||
public void addTeam(JuliaTeam team) {
|
||||
try {
|
||||
PreparedStatement teamStatement = db.prepareStatement("INSERT INTO juliateam_teams (gameID,teamName,voiceChannelID,roleID) VALUES (?,?,NULL,NULL);SELECT LAST_INSERT_ID();");
|
||||
teamStatement.setInt(1, this.gameBddID);
|
||||
teamStatement.setString(2, team.name);
|
||||
int teamBddID = teamStatement.executeQuery().getInt(1);
|
||||
team.setTeamBddID(teamBddID);
|
||||
|
||||
for(Long m : team.membres) {
|
||||
PreparedStatement playerStatement = db.prepareStatement("INSERT INTO juliateam_players (memberID,gameID,teamID) VALUES (?,?,?);");
|
||||
playerStatement.setLong(1, m);
|
||||
playerStatement.setInt(2, this.gameBddID);
|
||||
playerStatement.setInt(3, teamBddID);
|
||||
playerStatement.executeUpdate();
|
||||
}
|
||||
teams.add(team);
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException("Impossible de se connecter à la bdd", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void setStatus(Status status) {
|
||||
try {
|
||||
PreparedStatement gameStatement = db.prepareStatement("UPDATE juliateam_games SET status=? WHERE ID=?");
|
||||
gameStatement.setString(1, status.name());
|
||||
gameStatement.setInt(2, gameBddID);
|
||||
gameStatement.executeUpdate();
|
||||
this.status = status;
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException("Impossible de se connecter à la bdd", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setChannelsMetadata(long generalTextId, long generalVocalId, long generalRoleId) {
|
||||
try {
|
||||
PreparedStatement gameStatement = db.prepareStatement("UPDATE juliateam_games SET textChannelID=?,voiceChannelID=?,roleID=? WHERE ID=?");
|
||||
gameStatement.setLong(1, generalTextId);
|
||||
gameStatement.setLong(2, generalVocalId);
|
||||
gameStatement.setLong(3, generalRoleId);
|
||||
gameStatement.setInt(4, gameBddID);
|
||||
gameStatement.executeUpdate();
|
||||
this.generalTextChannelId = generalTextId;
|
||||
this.generalVocChannelId = generalVocalId;
|
||||
this.generalRoleId = generalRoleId;
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException("Impossible de se connecter à la bdd", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Set<JuliaTeam> getTeams() {
|
||||
return teams;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Long getGeneralTextChannelId() {
|
||||
return generalTextChannelId;
|
||||
}
|
||||
|
||||
public Long getGeneralVocChannelId() {
|
||||
return generalVocChannelId;
|
||||
}
|
||||
|
||||
public Long getGeneralRoleId() {
|
||||
return generalRoleId;
|
||||
}
|
||||
|
||||
|
||||
public void end() {
|
||||
try {
|
||||
PreparedStatement removeStatement = db.prepareStatement("DELETE FROM juliateam_games WHERE ID=?");
|
||||
removeStatement.setInt(1, this.gameBddID);
|
||||
removeStatement.executeUpdate();
|
||||
|
||||
teams.forEach(JuliaTeam::end);
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException("Impossible de se connecter à la bdd", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class JuliaTeam {
|
||||
|
||||
String name;
|
||||
Set<Long> membres;
|
||||
int teamBddID;
|
||||
|
||||
long vocalChannelId;
|
||||
long roleId;
|
||||
|
||||
public JuliaTeam(String name, Set<Long> membres) {
|
||||
this.name = name;
|
||||
this.membres = membres;
|
||||
}
|
||||
|
||||
protected void setTeamBddID(int teamBddID) {
|
||||
this.teamBddID = teamBddID;
|
||||
}
|
||||
|
||||
public void setTeam(Set<Long> membres) {
|
||||
try {
|
||||
PreparedStatement removeStatement = db.prepareStatement("DELETE FROM juliateam_players WHERE gameID=? AND teamID=?");
|
||||
removeStatement.setInt(1, JuliaGame.this.gameBddID);
|
||||
removeStatement.setInt(2, teamBddID);
|
||||
removeStatement.executeUpdate();
|
||||
for(Long m : membres) {
|
||||
PreparedStatement playerStatement = db.prepareStatement("INSERT INTO juliateam_players (memberID,gameID,teamID,status) VALUES (?,?,?,NOT_KNOWN);");
|
||||
playerStatement.setLong(1, m);
|
||||
playerStatement.setInt(2, JuliaGame.this.gameBddID);
|
||||
playerStatement.setInt(3, teamBddID);
|
||||
playerStatement.executeUpdate();
|
||||
}
|
||||
this.membres = membres;
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException("Impossible de se connecter à la bdd", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void setMetadata(long vocChanId, long roleId) {
|
||||
try {
|
||||
PreparedStatement teamStatement = db.prepareStatement("UPDATE juliateam_teams SET voiceChannelID=?,roleID=? WHERE ID=?");
|
||||
teamStatement.setLong(1, vocChanId);
|
||||
teamStatement.setLong(2, roleId);
|
||||
teamStatement.setInt(3, gameBddID);
|
||||
teamStatement.executeUpdate();
|
||||
|
||||
this.vocalChannelId = vocChanId;
|
||||
this.roleId = roleId;
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException("Impossible de se connecter à la bdd", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void end() {
|
||||
try {
|
||||
PreparedStatement removePlayersStatement = db.prepareStatement("DELETE FROM juliateam_players WHERE gameID=? AND teamID=?");
|
||||
removePlayersStatement.setInt(1, JuliaGame.this.gameBddID);
|
||||
removePlayersStatement.setInt(2, teamBddID);
|
||||
removePlayersStatement.executeUpdate();
|
||||
|
||||
PreparedStatement removeStatement = db.prepareStatement("DELETE FROM juliateam_teams WHERE ID=?");
|
||||
removeStatement.setInt(1, teamBddID);
|
||||
removeStatement.executeUpdate();
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException("Impossible de se connecter à la bdd", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum Status {
|
||||
CREATED,
|
||||
STARTED,
|
||||
FINISHED;
|
||||
}
|
||||
|
||||
public enum PlayerStatus {
|
||||
NOT_KNOWN,
|
||||
READY,
|
||||
REFUSED,
|
||||
GONE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
210
src/com/bernard/juliateam/JuliaTeamCommandInterface.java
Normal file
210
src/com/bernard/juliateam/JuliaTeamCommandInterface.java
Normal file
@ -0,0 +1,210 @@
|
||||
package com.bernard.juliateam;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.bernard.juliabot.api.Command;
|
||||
import com.bernard.juliabot.api.DiscordCCommande;
|
||||
import com.bernard.juliabot.api.JuLIAddon;
|
||||
import com.bernard.juliateam.JuliaGame.JuliaTeam;
|
||||
import com.bernard.juliateam.JuliaGame.Status;
|
||||
import com.thedeanda.lorem.LoremIpsum;
|
||||
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.GuildChannel;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
|
||||
@JuLIAddon(name = "juliateam",devs = "Mysaa", version = "20w33a")
|
||||
public class JuliaTeamCommandInterface {
|
||||
|
||||
|
||||
Connection juliaDB;
|
||||
DiscordTeamManager manager;
|
||||
|
||||
|
||||
Map<Long, JuliaGame> organisateurs = new HashMap<Long, JuliaGame>();
|
||||
Map<Long, JuliaGame> generalTexts = new HashMap<Long, JuliaGame>();
|
||||
Map<String, JuliaGame> gameByName = new HashMap<String, JuliaGame>();
|
||||
Set<JuliaGame> games = new HashSet<JuliaGame>();
|
||||
|
||||
|
||||
public JuliaTeamCommandInterface(Connection juliaDB) {
|
||||
this.juliaDB = juliaDB;
|
||||
this.manager = new DiscordTeamManager();
|
||||
}
|
||||
|
||||
public JuliaGame getGameFromOrga(long orgaId) {
|
||||
return organisateurs.get(orgaId);
|
||||
}
|
||||
public JuliaGame getGameFromTextChannel(long chanId) {
|
||||
return generalTexts.get(chanId);
|
||||
}
|
||||
public JuliaGame getGameFromName(String name) {
|
||||
return gameByName.get(name);
|
||||
}
|
||||
|
||||
public Optional<JuliaGame> getGameFromUser(Long uid){
|
||||
JuliaGame game = null;
|
||||
for(JuliaGame g : games) {
|
||||
if(g.getTeams().stream().anyMatch(s -> s.membres.contains(uid))) {
|
||||
// L'utilisateur est dans une des teams de ce jeu
|
||||
if(game==null)game = g;
|
||||
else return Optional.empty();
|
||||
}
|
||||
}
|
||||
return Optional.ofNullable(game);
|
||||
}
|
||||
|
||||
public JuliaGame getUserGame(DiscordCCommande commande){
|
||||
Long accepting = commande.getUser().getIdLong();
|
||||
if(commande.getArguments().getNommeCount() > 1) {
|
||||
JuliaGame game = getGameFromName(commande.getArguments().getNomme(1));
|
||||
if(game==null) {
|
||||
commande.getChannel().sendMessage("Je ne connais pas de partie "+commande.getArguments().getNomme(1)).queue();
|
||||
return null;
|
||||
}
|
||||
return game;
|
||||
}else {
|
||||
JuliaGame game = getGameFromTextChannel(commande.getChannel().getIdLong());
|
||||
if(game==null) {
|
||||
Optional<JuliaGame> isGame = getGameFromUser(accepting);
|
||||
if(!isGame.isPresent()) {
|
||||
commande.getChannel().sendMessage("Vous n'êtes en lice pour aucune ou plus d'une partie … veuillez envoyer cette commande dans un salon de jeu").queue();
|
||||
return null;
|
||||
}
|
||||
return isGame.get();
|
||||
}
|
||||
return game;
|
||||
}
|
||||
}
|
||||
|
||||
@Command(name = "newGame",
|
||||
synopsis = "newGame name",
|
||||
description = "Crée une partie",
|
||||
aliases = {"createGame", "game"},
|
||||
admin = false)
|
||||
public void newGame(DiscordCCommande commande) {
|
||||
if(!commande.getChannel().getType().isGuild()) {
|
||||
commande.getChannel().sendMessage("Euhhhhh, lancez cette commande sur un serveur, vous pourez continuer la configuration en privé si vous voulez").queue();
|
||||
return;
|
||||
}
|
||||
if(organisateurs.containsKey(commande.getUser().getIdLong())) {
|
||||
commande.getChannel().sendMessage("Euhhhhh, veuillez vous occuper d'une seule partie à la fois s'il vous plait !").queue();
|
||||
return;
|
||||
}
|
||||
String gameName = commande.getArguments().getNomme(1);
|
||||
Set<String> gameNames = games.stream().map(JuliaGame::getName).collect(Collectors.toSet());
|
||||
while(gameNames.contains(gameName))
|
||||
gameName = commande.getArguments().getNomme(1) + "_" + LoremIpsum.getInstance().getFirstName();
|
||||
JuliaGame game = new JuliaGame(gameName, ((GuildChannel)commande.getChannel()).getGuild().getIdLong(), juliaDB);
|
||||
|
||||
organisateurs.put(commande.getUser().getIdLong(), game);
|
||||
gameByName.put(gameName, game);
|
||||
}
|
||||
|
||||
@Command(name = "newTeam",
|
||||
synopsis = "newTeam name @Player1 Player2 Player3#0122 Plyr5",
|
||||
description = "Rajoute ou modifie une équipe",
|
||||
aliases = {"createTeam", "team"},
|
||||
admin = false)
|
||||
public void newTeam(DiscordCCommande commande) {
|
||||
JuliaGame game = getGameFromOrga(commande.getUser().getIdLong());
|
||||
if(game == null) {
|
||||
commande.getChannel().sendMessage("Créez déjà une partie avec !!newGame").queue();
|
||||
return;
|
||||
}
|
||||
String teamName = commande.getArguments().getNomme(1);
|
||||
Set<Long> teamMembers = new HashSet<Long>();
|
||||
Guild guild = commande.getJDA().getGuildById(game.getGuildId());
|
||||
List<Member> membres = guild.getMembers();
|
||||
for (int i = 2; i < commande.getArguments().getNommeCount(); i++) {
|
||||
String player = commande.getArguments().getNomme(i);
|
||||
Optional<Member> mentioned = membres.stream().filter(m -> m.getAsMention().equalsIgnoreCase(player)).findAny();
|
||||
if(mentioned.isPresent()) {
|
||||
teamMembers.add(mentioned.get().getIdLong());
|
||||
continue;
|
||||
}
|
||||
Optional<Member> pseudo = membres.stream().filter(m -> m.getNickname().equalsIgnoreCase(player)).findAny();
|
||||
if(pseudo.isPresent()) {
|
||||
teamMembers.add(pseudo.get().getIdLong());
|
||||
continue;
|
||||
}
|
||||
Optional<Member> username = membres.stream().filter(m -> m.getUser().getName().equalsIgnoreCase(player)).findAny();
|
||||
if(username.isPresent()) {
|
||||
teamMembers.add(username.get().getIdLong());
|
||||
continue;
|
||||
}
|
||||
Optional<Member> effective = membres.stream().filter(m -> m.getEffectiveName().equalsIgnoreCase(player)).findAny();
|
||||
if(effective.isPresent()) {
|
||||
teamMembers.add(effective.get().getIdLong());
|
||||
continue;
|
||||
}
|
||||
commande.getChannel().sendMessage("Qui est "+player+ " ?").queue();
|
||||
}
|
||||
game.setTeam(teamName, teamMembers);
|
||||
|
||||
}
|
||||
|
||||
@Command(name="launchGame",
|
||||
synopsis = "launchGame",
|
||||
description = "Lance la partie",
|
||||
aliases = {"launch"},
|
||||
admin = false)
|
||||
public void launchGame(DiscordCCommande commande) {
|
||||
JuliaGame game = getGameFromOrga(commande.getUser().getIdLong());
|
||||
if(game == null) {
|
||||
commande.getChannel().sendMessage("Créez déjà une partie avec !!newGame").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
manager.initGameContext(game,commande.getJDA());
|
||||
|
||||
game.setStatus(Status.STARTED);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Command(name = "acceptGame",
|
||||
synopsis = "acceptGame [gameName]",
|
||||
description = "Accepter de participer à une partie de la part d'un joueur.",
|
||||
aliases = {"accept","okGame","ok"},
|
||||
admin=false)
|
||||
public void acceptGame(DiscordCCommande commande) {
|
||||
JuliaGame game = getUserGame(commande);
|
||||
if(game==null)return;
|
||||
|
||||
|
||||
DiscordTeamManager.instance.acceptGame(accepting,game);
|
||||
|
||||
}
|
||||
@Command(name = "refuseGame",
|
||||
synopsis = "refuseGame [gameName]",
|
||||
description = "Rufus de participer à une partie de la part d'un joueur.",
|
||||
aliases = {"refuse","nopeGame","nope"},
|
||||
admin=false)
|
||||
public void refuseGame(DiscordCCommande commande) {
|
||||
JuliaGame game = getUserGame(commande);
|
||||
if(game==null)return;
|
||||
|
||||
}
|
||||
@Command(name = "endGame",
|
||||
synopsis = "endGame [gameName]",
|
||||
description = "Termine l'execution d'un jeu",
|
||||
aliases = {"end","finishGame"},
|
||||
admin=false)
|
||||
public void endGame(DiscordCCommande commande) {
|
||||
JuliaGame game = getUserGame(commande);
|
||||
if(game==null)return;
|
||||
|
||||
manager.removeGameContext(game, commande.getJDA());
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,185 +0,0 @@
|
||||
package com.bernard.juliateam;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import net.dv8tion.jda.core.Permission;
|
||||
import net.dv8tion.jda.core.entities.Category;
|
||||
import net.dv8tion.jda.core.entities.Guild;
|
||||
import net.dv8tion.jda.core.entities.Member;
|
||||
import net.dv8tion.jda.core.entities.Role;
|
||||
import net.dv8tion.jda.core.entities.TextChannel;
|
||||
import net.dv8tion.jda.core.entities.VoiceChannel;
|
||||
import net.dv8tion.jda.core.managers.GuildController;
|
||||
|
||||
public class TeamManager {
|
||||
|
||||
public static final String DB_CONNECTION_FILEPATH = "/var/julia/juliateam.conn";
|
||||
public static TeamManager instance = new TeamManager();
|
||||
Connection connexion;
|
||||
|
||||
|
||||
public TeamManager() {
|
||||
try (BufferedReader bis = new BufferedReader(new FileReader(DB_CONNECTION_FILEPATH))){
|
||||
String url = bis.readLine();
|
||||
String name = bis.readLine();
|
||||
String password = bis.readLine();
|
||||
connexion = DriverManager.getConnection(url, name, password);
|
||||
} catch (FileNotFoundException e) {
|
||||
System.err.println("Cannot find the file "+DB_CONNECTION_FILEPATH+" for getting connection informations");
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
System.err.println("Cannot read the file "+DB_CONNECTION_FILEPATH+" for getting connection informations");
|
||||
e.printStackTrace();
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Cannot initiate the connexion with data inside "+DB_CONNECTION_FILEPATH);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void requestGame(String name,Guild g,List<Set<Member>> teams,List<String> teamNames) {
|
||||
initGameContext(name, g, teams, teamNames);
|
||||
}
|
||||
|
||||
public void acceptGame(Member m) {
|
||||
try {
|
||||
PreparedStatement gameStatement = connexion.prepareStatement("UPDATE juliateam_players SET accepted=1 WHERE memberID=?");
|
||||
gameStatement.setLong(1, m.getUser().getIdLong());
|
||||
gameStatement.executeQuery();
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Cannot execute query to make user accept game");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void refuseGame(Member m) {
|
||||
try {
|
||||
PreparedStatement gameStatement = connexion.prepareStatement("UPDATE juliateam_players SET accepted=0 WHERE memberID=?");
|
||||
gameStatement.setLong(1, m.getUser().getIdLong());
|
||||
gameStatement.executeQuery();
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Cannot execute query to make user refuse game");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void endGame(TextChannel chan) {
|
||||
Guild g = chan.getGuild();
|
||||
try {
|
||||
|
||||
PreparedStatement gameNameStatement = connexion.prepareStatement("SELECT * FROM juliateam_games WHERE textChannelID=?");
|
||||
gameNameStatement.setLong(1, chan.getIdLong());
|
||||
ResultSet game = gameNameStatement.executeQuery();
|
||||
|
||||
PreparedStatement teamNameStatement = connexion.prepareStatement("SELECT * FROM juliateam_teams WHERE gameID=?");
|
||||
teamNameStatement.setInt(1, game.getInt("ID"));
|
||||
ResultSet team = teamNameStatement.executeQuery();
|
||||
|
||||
PreparedStatement playerNameStatement = connexion.prepareStatement("SELECT * FROM juliateam_player WHERE gameID=?");
|
||||
playerNameStatement.setInt(1, game.getInt("ID"));
|
||||
ResultSet player = playerNameStatement.executeQuery();
|
||||
|
||||
Role role = g.getRoleById(game.getLong("roleID"));
|
||||
|
||||
//Remove PLAYER
|
||||
do {
|
||||
g.getController().removeRolesFromMember(g.getMemberById(player.getLong("memberID")), role).complete();
|
||||
}while(player.next());
|
||||
|
||||
PreparedStatement playersStatement = connexion.prepareStatement("DELETE FROM juliateam_players WHERE gameID=?");
|
||||
playersStatement.setInt(1, game.getInt("ID"));
|
||||
playersStatement.executeUpdate();
|
||||
|
||||
|
||||
//Remove TEAM
|
||||
do {
|
||||
g.getVoiceChannelById(team.getLong("voiceChannelID")).delete().complete();
|
||||
}while(team.next());
|
||||
|
||||
PreparedStatement teamsStatement = connexion.prepareStatement("DELETE FROM juliateam_teams WHERE gameID=?");
|
||||
teamsStatement.setInt(1, game.getInt("ID"));
|
||||
teamsStatement.executeUpdate();
|
||||
|
||||
//Remove GAME
|
||||
TextChannel channel = g.getTextChannelById(game.getLong("textChannelID"));
|
||||
Category category = chan.getParent();
|
||||
channel.delete().complete();
|
||||
category.delete().complete();
|
||||
role.delete().complete();
|
||||
|
||||
PreparedStatement gameStatement = connexion.prepareStatement("DELETE FROM juliateam_games WHERE ID=?");
|
||||
gameStatement.setInt(1, game.getInt("ID"));
|
||||
gameStatement.executeUpdate();
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Cannot remove the game : mysql error");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void initGameContext(String name,Guild guild,List<Set<Member>> teams,List<String> teamNames) {
|
||||
GuildController g = guild.getController();
|
||||
|
||||
try {
|
||||
|
||||
//Creating GAME
|
||||
Role role = g.createRole().setName(name + " player").complete();
|
||||
EnumSet<Permission> roleAllow = EnumSet.of(Permission.VOICE_CONNECT, Permission.VIEW_CHANNEL, Permission.MESSAGE_READ, Permission.MESSAGE_WRITE, Permission.VOICE_CONNECT, Permission.VOICE_SPEAK);
|
||||
Category category = (Category) g.createCategory(name + " game")
|
||||
.addPermissionOverride(role, roleAllow, null)
|
||||
.addPermissionOverride(guild.getPublicRole(), null, Permission.getPermissions(Permission.ALL_PERMISSIONS))
|
||||
.complete();
|
||||
TextChannel channel = (TextChannel) g.createTextChannel(name).setParent(category).complete();
|
||||
|
||||
|
||||
PreparedStatement gameStatement = connexion.prepareStatement("INSERT INTO juliateam_games (name,guildID,textChannelID,roleID) VALUES (?,?,?,?);SELECT LAST_INSERT_ID();");
|
||||
gameStatement.setString(1, name);
|
||||
gameStatement.setLong(2, guild.getIdLong());
|
||||
gameStatement.setLong(3, channel.getIdLong());
|
||||
gameStatement.setLong(4, role.getIdLong());
|
||||
int gameID = gameStatement.executeQuery().getInt(1);
|
||||
|
||||
//Creating TEAMS
|
||||
|
||||
for(int i = 0;i < teamNames.size();i++) {
|
||||
VoiceChannel chan = (VoiceChannel) g.createVoiceChannel("Team "+teamNames.get(i)).setParent(category).complete();
|
||||
|
||||
PreparedStatement teamStatement = connexion.prepareStatement("INSERT INTO juliateam_teams (gameID,teamName,voiceChannelID) VALUES (?,?,?);SELECT LAST_INSERT_ID();");
|
||||
teamStatement.setInt(1, gameID);
|
||||
teamStatement.setString(2, teamNames.get(i));
|
||||
teamStatement.setLong(3, chan.getIdLong());
|
||||
int teamID = teamStatement.executeQuery().getInt(1);
|
||||
|
||||
//Creating PLAYERS
|
||||
for(Member m : teams.get(i)) {
|
||||
g.addRolesToMember(m, role);
|
||||
|
||||
PreparedStatement playerStatement = connexion.prepareStatement("INSERT INTO juliateam_players (memberID,gameID,teamID) VALUES (?,?,?);SELECT LAST_INSERT_ID();");
|
||||
playerStatement.setLong(1, m.getUser().getIdLong());
|
||||
playerStatement.setInt(2, gameID);
|
||||
playerStatement.setInt(3, teamID);
|
||||
playerStatement.executeQuery();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Cannot create game : SQL problem");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user