Distance.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package actions;
  2. import java.text.NumberFormat;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import data.AddresseToGPS;
  6. import data.AddresseToGPS.Lieu;
  7. import data.Coordinates;
  8. import data.ISP;
  9. import data.MultiplePossibleAddressException;
  10. import main.Bot;
  11. import main.Cache;
  12. public class Distance extends Action {
  13. public static final int NOMBRE_AFFICHABLE = 3;
  14. public Distance(Bot b) {
  15. super(b);
  16. List<String> kw = new ArrayList<>();
  17. kw.add("dist");
  18. kw.add("distance");
  19. this.keyWords=kw;
  20. }
  21. @Override
  22. public void react(String channel, String sender, String login, String hostname, String message) {
  23. String s = message.substring(1);
  24. s=s.replace(',', '.');
  25. s=s.substring(s.indexOf(' ')+1); // Je me met après la commande
  26. double latitude = Double.POSITIVE_INFINITY, longitude = latitude;
  27. try {
  28. latitude = Double.parseDouble(s.substring(0, s.indexOf(' ')));
  29. s=s.substring(s.indexOf(' ')+1); // Je me et au second paramètre
  30. longitude = Double.parseDouble(s);
  31. }catch(Exception e) { //Cela doit alors être une adresse!
  32. try {
  33. Coordinates ca = getCoordinatesFromMessage(message, channel);
  34. latitude = ca.getLatitude();
  35. longitude = ca.getLongitude();
  36. affichePlusProches(latitude, longitude, channel);
  37. } catch (MultiplePossibleAddressException e1) {
  38. bot.sendMessage(channel, "Plusieurs possibilités pour cet endroit, nous choisirons le premier:");
  39. for(int i = 0; i<e1.lieux.length; ++i) {
  40. bot.sendMessage(channel, (i+1)+":"+e1.lieux[i].toString());
  41. }
  42. latitude = e1.lieux[0].coordonees.getLatitude();
  43. longitude = e1.lieux[0].coordonees.getLongitude();
  44. }
  45. finally {
  46. affichePlusProches(latitude, longitude, channel);
  47. }
  48. }
  49. }
  50. private void affichePlusProches(double latitude, double longitude, String channel) {
  51. ISP[] plusProches = getISPPlusProche(latitude, longitude);
  52. for(int i=0;i<plusProches.length;++i) {
  53. if(plusProches[i]!=null) {
  54. double distance = plusProches[i].getData().getCoordonnees().distanceAvec(latitude, longitude);
  55. NumberFormat nf = NumberFormat.getInstance();
  56. nf.setMaximumFractionDigits(2);
  57. nf.setMinimumFractionDigits(0);
  58. distance = distance / 1000.0; //On met en KM
  59. bot.sendMessage(channel, (i+1)+": "+plusProches[i].getBetterName()+" à "+nf.format(distance)+" Km");
  60. }
  61. }
  62. }
  63. private Coordinates getCoordinatesFromMessage(String message, String channel) throws MultiplePossibleAddressException {
  64. final double MAX_DIFF = 0.1; //Differences there MUST between 2 coordinates so they are seen as differents
  65. AddresseToGPS a2gps = new AddresseToGPS(message.substring(message.indexOf(' ')+1));
  66. Lieu[] lieux = a2gps.getAllLieu();
  67. if(lieux == null || lieux.length == 0) {
  68. bot.sendMessage(channel, "Aucun lieu ne correspond. Requete effectuée: "+a2gps.getAddressToQuerry());
  69. return null;
  70. }else if(lieux.length == 1) {
  71. return lieux[0].coordonees;
  72. }else {
  73. for(int i=0;i<lieux.length; ++i) {
  74. for(int j=0;j<lieux.length; ++j) {
  75. if(!lieux[i].coordonees.equals(lieux[j].coordonees, MAX_DIFF)) {
  76. throw new MultiplePossibleAddressException(lieux, a2gps.getAdresse());
  77. }
  78. }
  79. }
  80. return lieux[0].coordonees;
  81. }
  82. }
  83. private ISP[] getISPPlusProche(double latitude, double longitude) {
  84. return getISPPlusProche(new Coordinates(latitude, longitude));
  85. }
  86. private ISP[] getISPPlusProche(Coordinates coord) {
  87. ISP[] res = new ISP[NOMBRE_AFFICHABLE];
  88. Cache c = Cache.getInstance();
  89. List<ISP> allFAI = c.getListeInFede();
  90. for(ISP fai:allFAI) {
  91. Coordinates faicoord = fai.getData().getCoordonnees();
  92. double distance = faicoord.distanceAvec(coord);
  93. if(distance>=0) {
  94. for(int i=0;i<NOMBRE_AFFICHABLE;++i) {
  95. if(res[i] == null || distance<res[i].getData().getCoordonnees().distanceAvec(coord)) { //TODO creer un accesseur plus rapide
  96. if(res[i]!=null) {
  97. decale(res,i);
  98. res[i] = fai; //J'insere
  99. }else {
  100. res[i] = fai;
  101. }
  102. break;
  103. }
  104. }
  105. }else {
  106. }
  107. }
  108. return res;
  109. }
  110. private void decale(ISP [] array , int id) {
  111. int length = array.length;
  112. for(int i = length-1; i>id; --i) {
  113. array[i]=array[i-1];
  114. }
  115. }
  116. @Override
  117. public String help() {
  118. return " suivi de la latitude, puis la longitude au format décimal. Exemple: +"+keyWords.get(0)+" 50,410658 61.574548 Renvoie les "+NOMBRE_AFFICHABLE+" FAI de la fédération les plus proches à vol d'oiseau";
  119. }
  120. }