Martin Passard il y a 7 ans
Parent
commit
aa8b2614a7
3 fichiers modifiés avec 139 ajouts et 1 suppressions
  1. 76 0
      src/comportement/Cafe.java
  2. 46 0
      src/comportement/Comportement.java
  3. 17 1
      src/main/Bot.java

+ 76 - 0
src/comportement/Cafe.java

@@ -0,0 +1,76 @@
+package comportement;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import main.Bot;
+
+public class Cafe extends Comportement {
+	public static volatile Cafe instance = null;
+	public Map<String, Boolean> rep = new HashMap<>();
+	public Map<String, Long> lastcafe = new HashMap<>();
+	public Map<String, Long> lastthe= new HashMap<>();
+	public static final long MAX_THE = 30;
+	public static final long MAX_CAFE = 360;
+	
+	public Cafe(Bot b) {
+		super(b);
+	}
+	
+	@Override
+	public boolean hastoreact(String mesg) {
+		String m=mesg.toLowerCase();
+		return (m.contains(getBotNick()+" fait moi un café")||m.contains(getBotNick()+" fait moi un thé"));
+	}
+
+	@Override
+	public void react(String channel, String sender, String login, String hostname, String message) {
+		Long lastone;
+		Date d = new Date();
+		Bot b = this.getBot();
+		if(istheorcafe(message.toLowerCase())) {
+			lastone = lastthe.get(sender);
+			if(lastone == null) {
+			b.sendMessage(channel, "Ok pour le thé");
+			}else {
+				if(d.getTime()-lastone < MAX_THE*1000) {
+					b.sendMessage(channel, "Ok, mais la dernière fois c'était il y a moins de "+MAX_THE+" secondes, tu devrais y aller plus doucement...");
+				}else {
+					b.sendMessage(channel, "Ok, pas de problème. La dernière fois c'était le "+new Date(lastone).toString());
+				}
+			}
+			lastthe.put(sender, d.getTime());
+		}else {
+			lastone = lastcafe.get(sender);
+			if(sender.equals("quota_atypique")) {
+				b.sendMessage(channel, "Ok Quota!");
+			}else if(lastone == null) {
+				b.sendMessage(channel, "Ok pour le café!");
+			}else {
+				if(d.getTime()-lastone < MAX_CAFE*1000) {
+					b.sendMessage(channel, "Eu... ok, mais, tu devrai plutôt prendre un thé, ça fait vraiment trop peu de temps la... ");
+				}else {
+					b.sendMessage(channel, "Ok, pas de problème, la dernière fois c'était le "+new Date(lastone).toString());
+				}
+			}
+			lastcafe.put(sender, d.getTime());
+		}
+			
+	}
+	//true = thé, false = café
+	private boolean istheorcafe(String msg) {
+		return msg.contains(getBotNick()+" fait moi un thé");	
+	}
+	
+	public final static Cafe getInstance(Bot b) {
+		if (Cafe.instance == null) {
+			synchronized (Cafe.class) {
+				if(Cafe.instance == null) {
+					Cafe.instance = new Cafe(b);
+				}
+			}
+		}
+		return Cafe.instance;
+	}
+}

+ 46 - 0
src/comportement/Comportement.java

@@ -0,0 +1,46 @@
+package comportement;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import main.Bot;
+
+public abstract class Comportement {
+
+	private Bot bot;
+	public Comportement(Bot b) {
+		this.bot = b;
+	}
+
+
+	public static List<Comportement> getAllComportements(Bot b){
+		List<Comportement> liste = new ArrayList<>();
+		liste.add(Cafe.getInstance(b));
+		return liste;
+	}
+	
+	public abstract boolean hastoreact(String message);
+	
+	public abstract void react(String channel, String sender,
+			String login, String hostname, String message);
+
+
+	/**
+	 * @return the bot
+	 */
+	public Bot getBot() {
+		return bot;
+	}
+
+
+	/**
+	 * @param bot the bot to set
+	 */
+	public void setBot(Bot bot) {
+		this.bot = bot;
+	}
+	
+	public String getBotNick() {
+		return bot.getNick().toLowerCase();
+	}
+}

+ 17 - 1
src/main/Bot.java

@@ -5,11 +5,13 @@ import java.util.List;
 import org.jibble.pircbot.PircBot;
 import org.jibble.pircbot.PircBot;
 
 
 import actions.Action;
 import actions.Action;
+import comportement.Comportement;
 
 
 public class Bot extends PircBot {
 public class Bot extends PircBot {
 
 
 	private volatile static long TIME_BETWEEN_MESSAGES = 200;
 	private volatile static long TIME_BETWEEN_MESSAGES = 200;
 	private List<Action> actions = Action.getAllActions(this);
 	private List<Action> actions = Action.getAllActions(this);
+	private List<Comportement> comportements = Comportement.getAllComportements(this);
 	private String[] admins;
 	private String[] admins;
 	private boolean responseOnPrivateChannel = true;
 	private boolean responseOnPrivateChannel = true;
 	private boolean responseOnPrivateMessages = true;
 	private boolean responseOnPrivateMessages = true;
@@ -43,11 +45,25 @@ public class Bot extends PircBot {
 				a.react(channel, sender, login, hostname, message);
 				a.react(channel, sender, login, hostname, message);
 			}
 			}
 		}
 		}
-		//easter Egg
+		//easter Eggs
 		String ea="Ehlo "+this.getNick();
 		String ea="Ehlo "+this.getNick();
 		if (message.toLowerCase().contains(ea.toLowerCase())) {
 		if (message.toLowerCase().contains(ea.toLowerCase())) {
 			sendMessage(channel, "Ehlo "+sender+"!!");
 			sendMessage(channel, "Ehlo "+sender+"!!");
 		}
 		}
+		
+		if(message.toLowerCase().contains("mousse au chocolat")) {
+			sendMessage(channel, "J'adore la mousse au chocolat de Benjamin B.");
+		}
+		System.out.println("cprt");
+		for(Comportement c : comportements) {
+			System.out.println("Comportements");
+			if(c.hastoreact(message)) {
+				c.react(channel, sender, login, hostname, message);
+				System.out.println("a reagi");
+			}else {
+				System.out.println("ne reagit pas");
+			}
+		}
 
 
 	}
 	}