commit eb4dfd66130c2248858fb33435c74c21035a30d0 Author: Mysaa Date: Mon May 24 15:23:25 2021 +0200 Premier commit - Inclusion dans le système git diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..572533e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.classpath +.settings +.project +bin/ diff --git a/src/com/bernard/juliateam/JuliaGame.java b/src/com/bernard/juliateam/JuliaGame.java new file mode 100644 index 0000000..cbaed5f --- /dev/null +++ b/src/com/bernard/juliateam/JuliaGame.java @@ -0,0 +1,34 @@ +package com.bernard.juliateam; + +import java.util.List; +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 teamsNames; + public List> teams; + + + public List teamsChannels; + public TextChannel channel; + public Set unAnswered; + public boolean started = false; + public JuliaGame(String name, Guild g, List teamsNames, List> teams) { + this.name = name; + this.g = g; + this.teamsNames = teamsNames; + this.teams = teams; + for(Set team : teams) + unAnswered.addAll(team); + } + + + +} diff --git a/src/com/bernard/juliateam/TeamManager.java b/src/com/bernard/juliateam/TeamManager.java new file mode 100644 index 0000000..93dc7e4 --- /dev/null +++ b/src/com/bernard/juliateam/TeamManager.java @@ -0,0 +1,185 @@ +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> teams,List 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> teams,List teamNames) { + GuildController g = guild.getController(); + + try { + + //Creating GAME + Role role = g.createRole().setName(name + " player").complete(); + EnumSet 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(); + } + } + +}