package mirc;
import org.jibble.pircbot.*;
import pickup.pickup;
import configuration.*;
public class mircBot extends PircBot {
// Configuration
private config configuration;
private pickup myPickup;
public mircBot()
{
this.configuration = new config("Configuration/l4dpickup.conf");
this.setName(this.configuration.getTnJavaBotMircNickName());
this.myPickup = new pickup();
this.myPickup.start();
}
protected void onMessage(String channel,String sender,String login,String hostname,String message)
{
if(channel.equals(this.configuration.getTnJavaBotChannel()))
{
if(message.indexOf("!")==0)
{
if(message.equals("!add teama"))
{
if(this.myPickup.isNotAlreadyRegistered(sender,hostname))
{
if(this.myPickup.getTeamA().getFreeSlot()!=999)
{
this.myPickup.getTeamA().addPlayer(sender, hostname);
checkifPickupisFull();
}
}
}
else if(message.equals("!add teamb"))
{
if(this.myPickup.isNotAlreadyRegistered(sender,hostname))
{
if(this.myPickup.getTeamB().getFreeSlot()!=999)
{
this.myPickup.getTeamB().addPlayer(sender, hostname);
checkifPickupisFull();
}
}
}
else if(message.equals("!add"))
{
if(this.myPickup.isNotAlreadyRegistered(sender,hostname))
{
if(this.myPickup.getTeamA().getFreeSlot()!=999)
{
this.myPickup.getTeamA().addPlayer(sender, hostname);
checkifPickupisFull();
}
else if (this.myPickup.getTeamB().getFreeSlot()!=999)
{
this.myPickup.getTeamB().addPlayer(sender, hostname);
checkifPickupisFull();
}
}
}
else if(message.equals("!help"))
{
sendMessage(sender, " ");
sendMessage(sender, " <<In Channel>> command list :");
sendMessage(sender, "!add");
sendMessage(sender, "!add teama");
sendMessage(sender, "!add teamb");
sendMessage(sender, "!status");
sendMessage(sender, "!remove");
if(isAdmin(sender))
{
sendMessage(sender, " <<Query/Private Message>> command list :");
sendMessage(sender, "!resetteams");
sendMessage(sender, "!resetpickup");
sendMessage(sender, "!addip ip");
sendMessage(sender, "!addport port");
sendMessage(sender, "!resetserver");
}
sendMessage(sender, " ");
}
else if(message.equals("!remove"))
{
if(this.myPickup.isRegistered(sender,hostname))
{
this.myPickup.removePlayer(sender);
this.myPickup.removePlayer(hostname);
this.updateTopic();
}
}
else if(message.equals("!status"))
{
if(this.myPickup.getServer().isDefined())
{
sendMessage(this.configuration.getTnJavaBotChannel()," Serveur :"+this.myPickup.getServer().getIP()+":"+this.myPickup.getServer().getPort());
}
else
{
sendMessage(this.configuration.getTnJavaBotChannel()," Serveur non définit" );
}
}
else if(message.equals("!op"))
{
this.autoOp(sender);
}
}
}
}
protected void onPrivateMessage(String sender, String login, String hostname, String message)
{
if(isAdmin(sender))
{
if(message.indexOf("!addip")>=0)
{
String[] command= message.split(" ");
String ip=command[1];
this.myPickup.getServer().setIP(ip);
sendMessage(sender, " ip adresse : "+ip+" ajoutée");
checkifPickupisFull();
}
else if(message.indexOf("!addport")>=0)
{
String[] command= message.split(" ");
String port=command[1];
this.myPickup.getServer().setPort(port);
sendMessage(sender, " port : "+port+" ajouté");
checkifPickupisFull();
}
else if(message.equals("!resetserver"))
{
this.myPickup.getServer().setIP("");
this.myPickup.getServer().setPort("");
}
else if(message.equals("!resetteams"))
{
this.myPickup.getTeamA().resetTeam();
this.myPickup.getTeamB().resetTeam();
this.updateTopic();
}
else if(message.equals("!resetpickup"))
{
this.myPickup.getTeamA().resetTeam();
this.myPickup.getTeamB().resetTeam();
this.myPickup.getServer().setIP("");
this.myPickup.getServer().setPort("");
this.updateTopic();
}
}
else
{
sendMessage(sender, " Vous n'êtes pas admin ");
}
}
protected void onPart(String channel, String sender, String login, String hostname)
{
if(this.myPickup.isRegistered(sender,hostname))
{
this.myPickup.removePlayer(sender);
this.myPickup.removePlayer(hostname);
this.updateTopic();
}
}
protected void onJoin(String channel, String sender, String login, String hostname)
{
if( (channel.equals(this.configuration.getTnJavaBotChannel())) || (channel.equals(this.configuration.getTnJavaBotChannelOps())))
{
this.autoVoice(sender,channel);
}
}
protected void onQuit(String channel, String sender, String login, String hostname)
{
if(this.myPickup.isRegistered(sender,hostname))
{
this.myPickup.removePlayer(sender);
this.myPickup.removePlayer(hostname);
this.updateTopic();
}
}
protected void checkifPickupisFull()
{
if((this.myPickup.getTeamA().getFreeSlot()==999) && (this.myPickup.getTeamB().getFreeSlot()==999))
{
if(this.myPickup.getServer().isDefined())
{
sendMessage(this.configuration.getTnJavaBotChannel(), " The Pickup is full, and start in 10 seconds");
sendMessage(this.configuration.getTnJavaBotChannel(), " Server IP/Port is sent to players by /PM");;
String list=this.myPickup.getTeamA().getPlayerlist();
String[] names=list.split(" / ");
for (int i =0;i<(list.split(" / ").length);i++)
{
sendMessage(names[i], " You are in Human Team");
sendMessage(names[i], "connect to this server "+this.myPickup.getServer().getIP()+":"+this.myPickup.getServer().getPort()+"");
this.myPickup.removePlayer(names[i]);
}
list=this.myPickup.getTeamB().getPlayerlist();
names=list.split(" / ");
for (int i =0;i<(list.split(" / ").length);i++)
{
sendMessage(names[i], " You are in Infected Team");
sendMessage(names[i], "connect to this server "+this.myPickup.getServer().getIP()+":"+this.myPickup.getServer().getPort()+"");
this.myPickup.removePlayer(names[i]);
}
sendMessage(this.configuration.getTnJavaBotChannel(), " Send : OK");
this.myPickup.getServer().resetServer();
sendMessage(this.configuration.getTnJavaBotChannel(), " New Pickup Ready for inscriptions");
this.updateTopic();
}
else
{
sendMessage(this.configuration.getTnJavaBotChannel(), " No server defined : !help to add a server");
}
}
else
{
this.updateTopic();
}
}
public void updateTopic()
{
this.setTopic(this.configuration.getTnJavaBotChannel(), Colors.BOLD + "L4D Pickup Channel - " + Colors.BLACK + "Human Team: " + Colors.BOLD +Colors.DARK_BLUE + this.myPickup.getTeamA().getPlayerlist() + Colors.BOLD+ Colors.BLACK +" Infected Team : "+ Colors.BOLD+Colors.DARK_BLUE +this.myPickup.getTeamB().getPlayerlist());
}
protected boolean isAdmin(String name)
{
boolean result=false;
User[] userList=getUsers(this.configuration.getTnJavaBotChannel());
for (int i=0;i<userList.length;i++)
{
if(userList[i].getNick().equals(name))
{
if(userList[i].isOp())
{
sendMessage(name, "Vous êtes Admin : OK");
result=true;
break;
}
}
}
return result;
}
protected void autoOp(String name)
{
boolean result=false;
User[] userList=getUsers(this.configuration.getTnJavaBotChannelOps());
System.out.println("Nombre de users du chan : "+userList.length);
for (int i=0;i<userList.length;i++)
{
System.out.println( "nickame : "+userList[i].getNick()+ "" + name);
if(userList[i].getNick().equals(name))
{
if(userList[i].isOp())
{
sendMessage(name, "Vous êtes Admin : OK");
result=true;
break;
}
}
}
if(result)
{
op(this.configuration.getTnJavaBotChannel(), name);
}
else
{
sendMessage(name, "Stop Flood me");
}
}
protected void autoVoice(String name,String channel)
{
voice(channel, name);
}
}