Browse Source

Refactoring: Creation du de la classe Message pour améliorer le traitement du message reçu sur IRC

Martin Passard 6 years ago
parent
commit
cd9e2e4fce
3 changed files with 125 additions and 1 deletions
  1. 29 0
      src/actions/Action.java
  2. 93 0
      src/data/Message.java
  3. 3 1
      src/main/Bot.java

+ 29 - 0
src/actions/Action.java

@@ -3,6 +3,7 @@ package actions;
 import java.util.ArrayList;
 import java.util.List;
 
+import data.Message;
 import main.Bot;
 
 public abstract class Action {
@@ -46,6 +47,7 @@ public abstract class Action {
 	 * Indique si oui ou non cette action doit être executée
 	 * @param s message envoyé
 	 * @return true si l'action contenue doit être executée, false sinon.
+	 * @deprecated
 	 */
 	public boolean hasToReact(String s) {
 		if(s.charAt(0)!=CARACTERE_COMMANDE) {
@@ -70,6 +72,27 @@ public abstract class Action {
 	}
 	
 	/**
+	 * 
+	 * Methode réagissant au message @see PircBot;
+	 * @param channel channer sur l'IRC
+	 * @param sender personne ayant envoyé le message
+	 * @param login	login du bot
+	 * @param hostname hostname actuell
+	 * @param message message en question
+	 */
+	public boolean hasToReact(Message m) {
+		if(m.getC()!=CARACTERE_COMMANDE) {
+			return false;								// On ne réagit pas si ce n'est pas une commande. Cela évite la suite du traitement.
+		}
+		for(String kw : keyWords) {
+			if(m.getCommande().equalsIgnoreCase(kw)){
+				return true;
+			}
+		}
+		return false;		
+	}
+	
+	/**
 	 * Renvoie toutes les actions possibles prêtes à utiliser le Bot pour s'executer.
 	 * @param b Bot que nous utiliserons pour nos actions
 	 * @return Liste d'actions prete à être utilisée dans un forEach verifiant si elle doivent être executées.
@@ -95,6 +118,12 @@ public abstract class Action {
 	 */
 	public abstract String help();
 	
+	/**
+	 * modifie la chaine pour supprimer les espaces
+	 * @deprecated
+	 * @param s chaine de caracteres
+	 * @return renvoie la chaine mise en lowercase et sans espaces
+	 */
 	public static String messageSansEspace(String s) {
 		return s.toLowerCase().replaceAll("\\s", "");
 	}

+ 93 - 0
src/data/Message.java

@@ -0,0 +1,93 @@
+package data;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Message {
+	private final char c;
+	private final String commande;
+	private List<String> parameters = new ArrayList<String>();
+	
+	public Message(String input) {
+		c=input.charAt(0); // caractère de commande
+		if(input.indexOf(' ')==-1) {
+			this.commande = input.substring(1); // Remove the first character which is the command caracter
+		}else {
+			String[] tab = input.split("( )+");
+			this.commande = tab[0].substring(1);
+			String[] params = new String[tab.length-1];
+			for(int i=1;i<tab.length;++i) {
+				params[i-1]=tab[i];
+			}
+			addAllParameters(params);
+		}
+		
+	}
+	
+	private void addAllParameters(String[] param) {
+		for(int i=0;i<param.length;++i) {
+			parameters.add(param[i]);
+		}
+	}
+	
+	private String get(int id) {
+		return parameters.get(id);
+	}
+	
+	public int getElementAsInt(int id) throws NumberFormatException  {
+		return Integer.parseInt(get(id));
+	}
+	public double getElementAsDouble(int id) throws NumberFormatException  {
+		return Double.parseDouble(get(id));
+	}
+	public float getElementAsFloat(int id) throws NumberFormatException  {
+		return Float.parseFloat(get(id));
+	}
+	public String getElementAsString(int id) throws NumberFormatException  {
+		return get(id);
+	}
+	public boolean getElementAsBoolean(int id) throws NumberFormatException  {
+		return Boolean.parseBoolean(get(id));
+	}
+	
+	public int parameterSize() {
+		return parameters.size();
+	}
+	public List<String> getParameters() {
+		return parameters;
+	}
+	public void setParameters(List<String> parameters) {
+		this.parameters = parameters;
+	}
+	public char getC() {
+		return c;
+	}
+	public String getCommande() {
+		return commande;
+	}
+	
+	public String getAllParametersAsOneString() {
+		int size = parameterSize();
+		String res = "";
+		for(int i=0; i < size;++i) {
+			res+=parameters.get(i);
+			if(i != size-1) {
+				res+=" ";
+			}
+		}
+		return res;
+	}
+
+	@Override
+	public String toString() {
+		String res = "Message [c=" + c + ", commande=" + commande + ", parameters=";
+		for(int i=0;i<parameterSize();++i) {
+			res+= parameters.get(i);
+			if(i!=parameterSize()-1) {
+				res+=", ";
+			}
+		}
+		res+= " ]";
+		return res;
+	}
+}

+ 3 - 1
src/main/Bot.java

@@ -11,6 +11,7 @@ import org.jibble.pircbot.PircBot;
 
 import actions.Action;
 import comportement.Comportement;
+import data.Message;
 import rss.RssDataRemainder;
 
 public class Bot extends PircBot {
@@ -88,7 +89,8 @@ public class Bot extends PircBot {
 	
 	public void onMessage(String channel, String sender,
 			String login, String hostname, String message) {
-		
+		Message m = new Message(message);
+		System.out.println(m.toString());
 		for(Action a:actions){
 			if(a.hasToReact(message)) {
 				a.react(channel, sender, login, hostname, message);