/**
* This source file is part of WZ Hybrid Bots
* For the latest information, see http://sourceforge.net/projects/wzhybridbots
*
* Copyright (c) 2006 - WzCtf
* For more information about WzCtf, see http://www.wzctf.net
* Also see the license in license.txt
*/
package bots.WzBot;
/** The imported classes/interfaces */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import hybrid.core.BotSpawn;
import hybrid.core.consts.Events;
import hybrid.core.consts.SSEvents;
import hybrid.core.events.HybridEvent;
import hybrid.core.tools.INIReader;
import frontend.alphaspawn.AlphaSpawn;
import frontend.alphaspawn.ASConnection;
import frontend.alphaspawn.LoginInfo;
import frontend.alphaspawn.SpawnConfiguration;
import frontend.alphaspawn.tools.command.Command;
import frontend.alphaspawn.tools.command.CommandManager;
import wzhybridbots.consts.AccessLevel;
import wzhybridbots.security.BotOperatorLineNumberReader;
import wzhybridbots.security.BotOperatorReader;
import wzhybridbots.security.BotOperatorSet;
import wzhybridbots.security.BotOperatorSetRetriever;
import wzhybridbots.security.HashBotOperatorSet;
import wzhybridbots.tools.actions.ServerCommands;
import wzhybridbots.tools.actions.ServerCommandsAbstractFactory;
import wzhybridbots.tools.actions.ServerCommandsRetriever;
import wzhybridbots.tools.command.Help;
/**
* Representation of the bot used in Warzone public area.
*
* @author Witlospock
* @version 1.0
*/
public class WzBot extends AlphaSpawn
implements ServerCommandsRetriever,
BotOperatorSetRetriever {
/** A connection object used to track and handle commands. */
protected ASConnection objConnection = null;
/** A set of the bot operators and their access level. */
protected BotOperatorSet objBotOperators = null;
/** A server command object that deals with server commands. */
protected ServerCommands objServerCommands = null;
/** The help command */
private Command objHelpCmd = null;
/**
* The default constructor of the class. It sets the contact
* information and it attempts to connect to the default zone
* found in the spawn.ini file.
*/
public WzBot () {
// Set the bot author and contact information
// TODO Change the author and contact information to a devteam@wzctf.net of some sort
super( "Witlospock", "" );
// Initialize the fields
this.objConnection = null;
this.objBotOperators = new HashBotOperatorSet();
this.objServerCommands = null;
// Attempt to connect the bot
connect();
// Create the appropriate ServerCommands object
createServerCommands();
// Attempt to read the bot operators and their access level
readBotOperators();
// Register a few basics commands
registerBotCommands();
}
/**
* Attempt to connect to the default zone found in the spawn.ini file
* of the bot. The method returns false if an error happens while connecting
* to the zone, otherwise it returns true.
* <br><br>
* Note: The method may return true and still the connection may fail.
* A connection may be considered connected to a zone once the bot receive
* the Connection event.
*
* @return see above.
*/
protected boolean connect () {
boolean failed = true;
// Attempt to open a connection to the server
ASConnection conn = (ASConnection)ASConnection.open();
if ( conn != null ) {
// A connection has been successfully open
// Find the username/password for the bot in it's spawn.ini file
LoginInfo login = SpawnConfiguration.allocateLoginInfo();
if ( login != null ) {
// The username/password for the bot has been found
// Memorize the info for further usage
conn.setLoginInfo( login );
try {
// Connecting to the zone
if ( conn.connect() ) {
// The connection to the zone is in process
// The connection will be establish successfully once
// the Connected event is receive in processEvent method.
// Memorize the connection object for further usage
this.objConnection = conn;
failed = false;
}
else {
// A locally error was detected
// Shutting down the bot
BotSpawn.shutdown( "A locally error was detected while connecting. Please refer to Hybrid's documentation for further precision." );
}
}
catch ( RuntimeException re ) {
// The default server wasn't found in the spawn.ini file
// Shutting down the bot
BotSpawn.shutdown( "Cannot connect to the default server. Please verify the spawn.ini file." );
}
}
else {
// Unable to find the username/password
// Shutting down the bot
BotSpawn.shutdown( "Unable to allocate a username." );
}
}
else {
// Unable to open a connection
// Shutting down the bot
BotSpawn.shutdown( "Unable to allocate a connection." );
}
return failed;
}
/**
* Creates the {@link wzhybridbots.tools.actions.ServerCommands ServerCommands}
* has specified in the configuration file of the bot. If the configuration don't
* mention which ServerCommands to use, the {@link wzhybridbots.tools.actions.VIEServerCommands VIEServerCommands}
* is use by default.
*/
protected void createServerCommands () {
// TODO Load the info about the ServerCommands from the config file
this.objServerCommands = ServerCommandsAbstractFactory.createServerCommands( ServerCommandsAbstractFactory.VIE );
}
/**
* Reads all the necessery files containing information about
* the bot operators and their access level.
*/
protected void readBotOperators () {
// Path to the source files
String pathSysOp = "";
String pathSMod = "";
String pathMod = "";
// Section & Keys
String sect = "BotOperator";
String sectSysOp = "SysOp";
String sectSMod = "SMod";
String sectMod = "Mod";
// Get the container reader
this.objBotOperators.clear();
// Get a reader to the spawn.ini file
INIReader reader = SpawnConfiguration.getSpawnConfig().asReadOnly();
// Get the path to the source files
String tmp = "";
tmp = reader.get( sect, sectSysOp, pathSysOp );
if ( tmp.trim().length() > 0 ) { pathSysOp = tmp; }
tmp = reader.get( sect, sectSMod, pathSMod );
if ( tmp.trim().length() > 0 ) { pathSMod = tmp; }
tmp = reader.get( sect, sectMod, pathMod );
if ( tmp.trim().length() > 0 ) { pathMod = tmp; }
// Read the file one by one and add the operators
// SysOp
if ( pathSysOp.trim().length() > 0 ) {
try {
BotOperatorReader opReader =
new BotOperatorLineNumberReader( new InputStreamReader(
new FileInputStream (
new File ( pathSysOp ) ) ),
AccessLevel.SYSOP );
// Read the file content and add the result to the container
BotOperatorSet toAdd = opReader.readBotOperators();
if ( toAdd != null ) {
this.objBotOperators.addAll( toAdd );
}
}
catch ( FileNotFoundException fnfe ) {
// The file was not found
// Print a message for debugging
System.out.println( "Cannot find file: " + pathSysOp );
}
}
// SMod
if ( pathSMod.trim().length() > 0 ) {
try {
BotOperatorReader opReader =
new BotOperatorLineNumberReader( new InputStreamReader(
new FileInputStream (
new File ( pathSMod ) ) ),
AccessLevel.SMOD );
// Read the file content and add the result to the container
BotOperatorSet toAdd = opReader.readBotOperators();
if ( toAdd != null ) {
this.objBotOperators.addAll( toAdd );
}
}
catch ( FileNotFoundException fnfe ) {
// The file was not found
// Print a message for debugging
System.out.println( "Cannot find file: " + pathSMod );
}
}
// Mod
if ( pathMod.trim().length() > 0 ) {
try {
BotOperatorReader opReader =
new BotOperatorLineNumberReader( new InputStreamReader(
new FileInputStream (
new File ( pathMod ) ) ),
AccessLevel.MOD );
// Read the file content and add the result to the container
BotOperatorSet toAdd = opReader.readBotOperators();
if ( toAdd != null ) {
this.objBotOperators.addAll( toAdd );
}
}
catch ( FileNotFoundException fnfe ) {
// The file was not found
// Print a message for debugging
System.out.println( "Cannot find file: " + pathMod );
}
}
}
/**
* Registers basics command that every bot should have like
* !help, etc.
*/
protected void registerBotCommands () {
// Get the command manager
CommandManager cmdMgr = CommandManager.getInstance();
// Register the !help command
this.objHelpCmd = new Help( this.objConnection );
cmdMgr.registerCommand( "!help", this.objHelpCmd );
}
/**
* Returns the {@link wzhybridbots.tools.actions.ServerCommands ServerCommands} of
* the class.
*
* @return see above.
*/
public ServerCommands getServerCommands () {
return this.objServerCommands;
}
/**
* Returns the {@link wzhybridbots.security.BotOperatorSet BotOperatorSet} of
* the class.
*
* @return see above.
*/
public BotOperatorSet getBotOperatorSet () {
return this.objBotOperators;
}
/**
* <b>Description copied from class {@link frontend.alphaspawn.AlphaSpawn#processEvent(hybrid.core.events.HybridEvent) AlphaSpawn}</b>
* <br>
* Called when the next event in the bots event queue is to be processed.
*/
protected void processEvent( HybridEvent event ) {
// Drop null or dead events
if ( event == null || !event.isAlive() ) { return; }
// Prints the event (for debugging purpose)
//System.out.println( "Received event: " + event.getEventName() );
// Check the group ID of the event
switch ( event.getGroupID() ) {
// Core Events
///////////////////////////////////////
case Events.Group:
// Check the event ID
switch ( event.getEventID() ) {
// The bot has successfully connected to the zone
case Events.Connected:
// From here, we may consider the bot has connected to the zone
break;
}
break;
// Subspace Events
///////////////////////////////////////
case SSEvents.Group:
break;
// Unhandled Group
///////////////////////////////////////
default:
// This event is part of a unhandled group
// Prints the event (for debugging purpose)
//System.out.println( "Unknown or unhandled group id: " + event.getGroupID() );
break;
}
}
}