Browse Source

Modifications préalables a L'utilisation des Actions

Martin Passard 7 years ago
parent
commit
9b00134b37

+ 60 - 28
src/actions/Action.java

@@ -1,28 +1,60 @@
-package actions;
-
-import java.util.List;
-
-import org.jibble.pircbot.PircBot;
-
-public abstract class Action {
-
-	public List<String> keyWords;
-	public PircBot bot;
-	
-	public abstract void react(String channel, String sender,
-			String login, String hostname, String message);
-	
-	
-	public boolean hasToReact(String s) {
-		String chaineLowerCase = s.toLowerCase();
-		for(String kw : keyWords) {
-			if(chaineLowerCase.contains(kw.toLowerCase())){
-				return true;
-			}
-		}
-		return false;
-		
-	}
-	
-
-}
+package actions;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jibble.pircbot.PircBot;
+
+import main.Bot;
+
+public abstract class Action {
+
+	public List<String> keyWords;
+	public PircBot bot;
+	
+	protected Action(Bot b, List<String> keywords) {
+		this.keyWords = keywords;
+		this.bot = b;
+	}
+	
+	protected Action(Bot b) {
+		this.bot = b;
+	}
+	
+	public abstract void react(String channel, String sender,
+			String login, String hostname, String message);
+	
+	
+	public boolean hasToReact(String s) {
+		if(s.charAt(0)!=Bot.CARACTERE_COMMANDE) {
+			return false;								// On ne réagit pas si ce n'est pas une commande. Cela évite la suite du traitement.
+		}
+		s=s.substring(1); // on enleve le caractère de commande
+		
+		int premierEspace = s.indexOf(' ');
+
+		if(premierEspace>0) { // Si il y a un espace il ne faut prendre que le premier mot.
+			s=s.substring(0, premierEspace);
+			
+		}
+		String chaineLowerCase =  s.toLowerCase();
+		for(String kw : keyWords) {
+			if(chaineLowerCase.contains(kw.toLowerCase())){
+				return true;
+			}
+		}
+		return false;
+		
+	}
+	
+	public static List<Action> getAllActions(Bot b){
+		List<Action> ar= new ArrayList<>();
+		ar.add(new Contact(b));
+		ar.add(new Info(b));
+		ar.add(new Liste(b));
+		ar.add(new Source(b));
+		return ar;
+	}
+	
+
+}

+ 1 - 1
src/actions/Contact.java

@@ -13,7 +13,7 @@ import verif_saisie.EntierPositifNonVide;
 public class Contact extends Action {
 
 	public Contact(Bot b) {
-		this.bot = b;
+		super(b);
 		List<String> ar = new ArrayList<>();
 		ar.add("contact");
 		this.keyWords = ar;

+ 1 - 1
src/actions/Info.java

@@ -13,7 +13,7 @@ import verif_saisie.EntierPositifNonVide;
 public class Info extends Action {
 
 	public Info(Bot b) {
-		this.bot = b;
+		super(b);
 		List<String> ar = new ArrayList<>();
 		ar.add("info");
 		this.keyWords = ar;

+ 1 - 1
src/actions/Liste.java

@@ -13,7 +13,7 @@ import main.Cache;
 public class Liste extends Action {
 
 	public Liste(Bot b) {
-		this.bot = b;
+		super(b);
 		List<String> ar = new ArrayList<>();
 		ar.add("liste");
 		this.keyWords = ar;

+ 1 - 1
src/actions/Source.java

@@ -13,7 +13,7 @@ import verif_saisie.EntierPositifNonVide;
 public class Source extends Action {
 
 	public Source(Bot b) {
-		this.bot = b;
+		super(b);
 		List<String> ar = new ArrayList<>();
 		ar.add("source");
 		ar.add("sources");

+ 5 - 3
src/main/Bot.java

@@ -6,6 +6,7 @@ import java.util.List;
 
 import org.jibble.pircbot.PircBot;
 
+import actions.Action;
 import data.CoveredAreas;
 import data.ISP;
 import data.ISPDAO;
@@ -13,9 +14,10 @@ import verif_saisie.EntierPositifNonVide;
 
 public class Bot extends PircBot {
 
-	public static final long TIME_BETWEEN_RELOADS = 360000;
 	public static final long TIME_BETWEEN_MESSAGES = 200;
+	public static char CARACTERE_COMMANDE = '+';
 	private ISPDAO idao;
+	private List<Action> actions = Action.getAllActions(this);
 
 	public Bot() {
 		this.setName("UneFede2");
@@ -56,7 +58,7 @@ public class Bot extends PircBot {
 		if(message.equals("+reload")) {
 			Date now = new Date();
 			Date lastCU = Cache.getInstance().getLastCacheUpdate();
-			if(lastCU.getTime() < now.getTime()-TIME_BETWEEN_RELOADS ) {		// Si la dernière MAJ date de + de 5 minutes
+			if(lastCU.getTime() < now.getTime()-Cache.TIME_BETWEEN_RELOADS ) {		// Si la dernière MAJ date de + de 5 minutes
 				sendMessage(channel, "Je lance le reload!");
 				if(reload()) {
 					sendMessage(channel, sender+": Le reload s'est bien passé.");
@@ -64,7 +66,7 @@ public class Bot extends PircBot {
 					sendMessage(channel, sender+": Erreur au moment du reload.");
 				}
 			}else {
-				Date nextAllowed = new Date(lastCU.getTime()+TIME_BETWEEN_RELOADS);
+				Date nextAllowed = new Date(lastCU.getTime()+Cache.TIME_BETWEEN_RELOADS);
 				sendMessage(channel, "Trop de reload, attendez un peu. Le dernier à eu lieu le "+lastCU.toString()+" Prochain autorisé le "+nextAllowed);
 			}
 		}

+ 1 - 0
src/main/Cache.java

@@ -18,6 +18,7 @@ public class Cache implements AffichableSurIRC {
 	public static volatile Cache instance = null;
 	private Date lastCacheUpdate;
 	private List<ISP> cache;
+	public static final long TIME_BETWEEN_RELOADS = 360000;
 
 	private Cache() {
 		ISPDAO idao = ISPDAO.getInstance();