Philo.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package comportement;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Random;
  6. import org.apache.commons.io.FileUtils;
  7. import org.json.JSONArray;
  8. import org.json.JSONException;
  9. import org.json.JSONObject;
  10. import actions.Action;
  11. import main.Bot;
  12. import main.Main;
  13. public class Philo extends Comportement {
  14. public static volatile Philo instance = null;
  15. public static final ArrayList<JSONObject> JSONS = new ArrayList<>();
  16. private final static String here = (new File(".")).getAbsolutePath();
  17. private final File folder=new File(here+File.separator+"ressources"+File.separator+"philo"+File.separator);
  18. private final String[] files = folder.list();
  19. private final Random random = new Random();
  20. private Philo(Bot b) {
  21. super(b);
  22. load();
  23. }
  24. public final static Philo getInstance(Bot b) {
  25. if (Philo.instance == null) {
  26. synchronized (Philo.class) {
  27. if(Philo.instance == null) {
  28. Philo.instance = new Philo(b);
  29. }
  30. }
  31. }
  32. return Philo.instance;
  33. }
  34. private void load() {
  35. if(Main.isDebug()) {
  36. System.out.println("Creation de la liste des citations de philo");
  37. }
  38. for(int i=0; i<files.length;++i) {
  39. File f = new File(folder.getAbsolutePath()+File.separator+files[i]);
  40. if(f.isFile() && f.canRead()) {
  41. try {
  42. String s = FileUtils.readFileToString(f, "UTF-8");
  43. JSONS.add(new JSONObject(s));
  44. }catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. }else {
  48. System.err.println("Attention, dans les ressources de philo, la ressource "+folder.getAbsolutePath()+File.separator+files[i]+" n'est pas un fichier lisible.");
  49. }
  50. }
  51. }
  52. @Override
  53. public boolean hastoreact(String message) {
  54. if(message.toLowerCase().contains(this.getBotNick())) {
  55. return getJSONObjectForMessage(message)!=null;
  56. }else if(message.toLowerCase().replaceAll(" ", "").equals(Action.CARACTERE_COMMANDE+"philo")) { //TODO eviter cette exception pour déclancher l'évenement comme une action
  57. return true;
  58. }else {
  59. return false;
  60. }
  61. }
  62. private JSONObject getJSONObjectForMessage(String message) {
  63. for(JSONObject json : JSONS) {
  64. String topic = json.getString("topic");
  65. if(message.toLowerCase().contains(topic)) return json;
  66. }
  67. return null;
  68. }
  69. private String giveMeAQuoteFrom(JSONObject jo) {
  70. JSONArray array = jo.getJSONArray("quotes");
  71. int nbrCit = array.length();
  72. int choix = random.nextInt(nbrCit);
  73. JSONObject quote = array.getJSONObject(choix);
  74. final String UNKNOWN = "Inconnu";
  75. String author="";
  76. try {
  77. author = quote.getString("author");
  78. }catch(JSONException e) {
  79. author = UNKNOWN;
  80. }
  81. if(author == null) {
  82. author = UNKNOWN;
  83. }
  84. return "\""+quote.getString("quote")+"\" par : "+author;
  85. }
  86. @Override
  87. public void react(String channel, String sender, String login, String hostname, String message) {
  88. Bot b = this.getBot();
  89. String res = ""+sender;
  90. JSONObject jo;
  91. if(message.toLowerCase().replaceAll(" ", "").equals(Action.CARACTERE_COMMANDE+"philo")) {
  92. int size = JSONS.size();
  93. int choix = random.nextInt(size);
  94. jo = JSONS.get(choix);
  95. res += ": Voici une citation à propos de "+jo.getString("topic");
  96. }else {
  97. res += ", je connais de la philo a propos de ";
  98. jo = getJSONObjectForMessage(message);
  99. res += jo.getString("topic")+":";
  100. }
  101. b.sendMessage(channel, res);
  102. b.sendMessage(channel, giveMeAQuoteFrom(jo));
  103. }
  104. }