Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.79 KB | None | 0 0
  1. package bot.Util;
  2.  
  3. import Json.JsonObject;
  4. import bot.tenmans.TenMan;
  5. import bot.users.User;
  6. import com.mongodb.MongoClient;
  7. import com.mongodb.client.MongoCollection;
  8. import com.mongodb.client.MongoDatabase;
  9. import com.mongodb.client.model.Filters;
  10. import com.mongodb.client.result.UpdateResult;
  11. import net.dv8tion.jda.core.entities.Member;
  12. import org.bson.Document;
  13. import org.json.JSONObject;
  14.  
  15. import java.io.*;
  16. import java.util.HashMap;
  17. import java.util.Random;
  18.  
  19. /**
  20. * esports-bot
  21. * bot.Util
  22. * dzyrr
  23. * 2/19/2018
  24. * 6:07 AM
  25. * 2018
  26. * 02
  27. * 19
  28. */
  29. public class TenManManager {
  30.  
  31. public static HashMap<String, TenMan> matches = new HashMap<>();
  32. private static ShellScriptExecuter executer = new ShellScriptExecuter();
  33. private static Thread gameThread;
  34.  
  35.  
  36. public static boolean createMatch(TenMan match, Member member) {
  37. MongoClient mongoClient = new MongoClient("localhost", 27017);
  38. MongoDatabase database = mongoClient.getDatabase("glorybot");
  39. MongoCollection<Document> matchesCollection = database.getCollection("10mans");
  40.  
  41. Document fetchedMatch = matchesCollection.find(Filters.eq("name", match.getName())).first();
  42.  
  43. if (fetchedMatch != null) {
  44. return false;
  45. } else {
  46. Document matchDoc = new Document("name", match.getName()).append("players_id", member.getUser().getId());
  47. matchesCollection.insertOne(matchDoc);
  48. matches.put(match.getName(), match);
  49. mongoClient.close();
  50. return true;
  51. }
  52. }
  53.  
  54. public static boolean removeTenMan(String lobby) {
  55. MongoClient mongoClient = new MongoClient("localhost", 27017);
  56. MongoDatabase database = mongoClient.getDatabase("glorybot");
  57. MongoCollection<Document> tenMansCollection = database.getCollection("10mans");
  58.  
  59. Document fetchedMatch = tenMansCollection.find(Filters.eq("name", lobby)).first();
  60.  
  61. if (fetchedMatch != null) {
  62. System.out.println("DELETED MATCH: " + lobby);
  63. tenMansCollection.deleteOne(fetchedMatch);
  64. return true;
  65. }
  66. return false;
  67. }
  68.  
  69. public static boolean joinTenMan(String matchName, Member member) {
  70. MongoClient mongoClient = new MongoClient("localhost", 27017);
  71. MongoDatabase database = mongoClient.getDatabase("glorybot");
  72. MongoCollection<Document> matchesCollection = database.getCollection("10mans");
  73.  
  74. Document fetchedMatch = matchesCollection.find(Filters.eq("name", matchName)).first();
  75. if (fetchedMatch != null) {
  76. String matchJsonString = fetchedMatch.toJson();
  77.  
  78. JSONObject matchObject = new JSONObject(matchJsonString);
  79.  
  80. if (matchObject.get("players_id") != null) {
  81. String playerIdsFromDatabase = (String) matchObject.get("players_id");
  82. String[] playerIds = playerIdsFromDatabase.split("/");
  83.  
  84. if (playerIds != null) {
  85.  
  86. for (int i = 0; i < playerIds.length; i++) {
  87. if (playerIds[i].equals(member.getUser().getId())) {
  88. return false;
  89. }
  90. }
  91. UpdateResult updatedMatch =
  92. matchesCollection.updateOne(Filters.eq("name", matchName), new Document("$set", new Document("name", matchName).append("players_id", fetchedMatch.get("players_id") + "/" + member.getUser().getId())));
  93. mongoClient.close();
  94. if (updatedMatch.getModifiedCount() > 0) {
  95. return true;
  96. }
  97. }
  98. }
  99. }
  100. mongoClient.close();
  101. return false;
  102. }
  103.  
  104.  
  105. public static boolean startTenMan(TenMan tenMan) {
  106.  
  107. MongoClient mongoClient = new MongoClient("localhost", 27017);
  108. MongoDatabase database = mongoClient.getDatabase("glorybot");
  109. MongoCollection<Document> tenMansCollection = database.getCollection("10mans");
  110.  
  111. Document fetchedMatch = tenMansCollection.find(Filters.eq("name", tenMan.getName())).first();
  112.  
  113. if (fetchedMatch != null) {
  114. String matchJsonString = fetchedMatch.toJson();
  115. JSONObject matchObject = new JSONObject(matchJsonString);
  116.  
  117. if (matchObject.get("players_id") != null) {
  118. String playerIdsFromDatabase = (String) matchObject.get("players_id");
  119. String[] playerIds = playerIdsFromDatabase.split("/");
  120.  
  121. if (playerIds != null) {
  122. if (playerIds.length <= 10) {
  123. // Lobby is full
  124.  
  125. // TODO: Read in the start.sh file
  126. // TODO: Change the +game_mode and +map <map name>
  127. // TODO: Make start.sh exec the 10man config
  128.  
  129. Random random = new Random();
  130. String mapName = "";
  131. switch (random.nextInt(1)) {
  132. case 0:
  133. mapName = "de_cache";
  134. break;
  135. case 1:
  136. mapName = "de_mirage";
  137. break;
  138. }
  139.  
  140. String newStatScript =
  141. "#!/bin/bash\n./srcds_run -game csgo -console -usercon +game_type 0 +game_mode 1 +mapgroup mg_active -tickrate 128 +map " + mapName + " +sv_setsteamaccount STEAMTOKEN -net_port_try 1";
  142. //
  143. // FileWriter writer = new FileWriter("/home/steam/steamcmd/server/steamcmd/csgo/start.sh");
  144. // BufferedWriter bufferedWriter = new BufferedWriter(writer);
  145. // bufferedWriter.write(newStatScript);
  146.  
  147. gameThread = new Thread(new Runnable() {
  148. @Override
  149. public void run() {
  150. String[] cmdScript = new String[]{"/bin/bash", "-c", "sh /home/steam/steamcmd/server/steamcmd/csgo/srcds_run -game csgo -console -usercon +game_type 0 +game_mode 1 +mapgroup mg_active -tickrate 128 +map de_mirage +sv_setsteamaccount STEAMTOKEN -net_port_try 1"};
  151. try {
  152.  
  153. ProcessBuilder processBuilder = new ProcessBuilder(cmdScript);
  154. Process pr = processBuilder.start();
  155.  
  156. BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
  157. String line = "";
  158. while ((line = input.readLine()) != null) {
  159. System.out.println(line);
  160. }
  161. System.out.println(pr.isAlive());
  162. } catch (Exception e) {
  163. System.out.println(e.toString());
  164. e.printStackTrace();
  165. }
  166. }
  167. });
  168.  
  169. gameThread.start();
  170. return true;
  171. } else if (playerIds.length > 10) {
  172. // to many players
  173. } else if (playerIds.length < 10) {
  174. // not enough players
  175. }
  176. }
  177. }
  178. } else {
  179. return false;
  180. }
  181. return false;
  182. }
  183.  
  184. public static void stopServer() {
  185. try {
  186. gameThread.join();
  187. } catch (InterruptedException e) {
  188. e.printStackTrace();
  189. }
  190. }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement