using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Globalization;
using System.Timers;
using AoLib.Utils;
namespace VhaBot.Plugins
{
public class AICityPlugin : PluginBase
{
private Config AIConfig;
private CultureInfo MyCultureInfo = new CultureInfo("en-GB");
private DateTimeFormatInfo dtfi = new CultureInfo("en-US", false).DateTimeFormat;
private System.Timers.Timer CityTimer = new System.Timers.Timer();
private int CityTimerMinutesGone = 60;
private string CityCloakMode = "";
private string CityCloakWho = "";
private int LastAnnounce = 0;
public AICityPlugin()
{
this.Settings.Name = "Alien Invasion City Plugin";
this.Settings.InternalName = "AICityPlugin";
this.Settings.Version = 1;
this.Settings.Author = "Daniel Eriksson";
this.Settings.RegisterCommand("city", new BotCommandRights(UserLevel.Guest, UserLevel.Member, UserLevel.Guest));
this.Settings.RegisterCommandAlias("cloak", "city");
this.Settings.RegisterCommand("city log", new BotCommandRights(UserLevel.Guest, UserLevel.Member, UserLevel.Guest));
this.Settings.RegisterCommand("city house", new BotCommandRights(UserLevel.Guest, UserLevel.Member, UserLevel.Guest));
this.Settings.RegisterCommand("city set", new BotCommandRights(UserLevel.Disabled, UserLevel.Admin, UserLevel.Disabled));
this.Settings.RegisterCommand("city del", new BotCommandRights(UserLevel.Disabled, UserLevel.Admin, UserLevel.Disabled));
}
private void CityTimerElapsed(object sender, ElapsedEventArgs e)
{
CityTimerMinutesGone += 1;
// If the cloak have been on this is run, this means that the cloak can be turned down again
if (CityCloakMode == "on")
{
// If it is configured to send a pre warning before it can be turned off do so.
if (((60 - Bot.Config.GetInt("aicity", "prealertofftime", 0)) == CityTimerMinutesGone) && Bot.Config.GetInt("aicity", "prealertofftime", 0) != 0)
{
// if it is in the config to send a tell to when before cloak can be put down, do so
if (Bot.Config.GetInt("aicity", "prealertofftotell", 0) == 1)
{
Bot.SendPrivateMessage(CityCloakWho, "Alien Invasion »» It is " + Bot.Config.GetInt("aicity", "prealertofftime", 0) + " minutes til cloak can be lowered again.");
}
// Throw away a generic message to SendAIMessage that handles all the processing.
SendAIMessage("It is " + Bot.Config.GetInt("aicity", "prealertofftime", 0) + " minutes til cloak can be lowered again.", 4);
} // 60 minutes have gone and the cloak can be turned off.
else if (CityTimerMinutesGone == 60)
{
// Throws it to SendAIMessage that process the message and says where it should be sent.
SendAIMessage("One hour has passed since cloak was put on, Cloak can now be lowered again.", 3);
CityTimer.Enabled = false; // No more reapetion needed since the cloak is ready to go.
}
}// if the cloak havent been on, that means it have been off, and it can be turned on
else
{
// Is it configured for a pre warning before the cloak can be turned on?
if (((60 - Bot.Config.GetInt("aicity", "prealertontime", 0)) == CityTimerMinutesGone) && Bot.Config.GetInt("aicity", "prealertontime", 0) != 0)
{
// if it is in the config to send a tell to when before cloak can be put down, do so
if (Bot.Config.GetInt("aicity", "prealertontotell", 0) == 1)
{
Bot.SendPrivateMessage(CityCloakWho, "Alien Invasion »» It is " + Bot.Config.GetInt("aicity", "prealertontime", 0) + " minutes til cloak can be put up again.");
}
SendAIMessage("It is " + Bot.Config.GetInt("aicity", "prealertontime", 0) + " minutes til cloak can be put up again.", 5);
}// 60 minutes have gone and the cloak can be turned on.
else if (CityTimerMinutesGone == 60)
{
if (Bot.Config.GetInt("aicity", "offalerttotell", 1) == 1)
{
Bot.SendPrivateMessage(CityCloakWho, "Alien Invasion »» One hour have passed since you turned down cloak, remember to activate it again.");
}
SendAIMessage("One hour have passed since cloak was put down by " + CityCloakWho + ". It can now be put up again.", 3);
LastAnnounce = CityTimerMinutesGone; // Sets when last announce was sent so we know when 60 minutes have past.
}// If it havent gone 60 minutes, It maybe have gone more, check if it have gone one hour.
else if ((LastAnnounce + 60) == CityTimerMinutesGone)
{
SendAIMessage("More then one hour have passed since cloak was put down by " + CityCloakWho + ". It can now be put up again.", 6);
LastAnnounce = CityTimerMinutesGone; // Sets when last announce was sent so we know when 60 minutes have past.
}
}
}
/// <summary>
/// This function Inserts a logentrance to the database.
/// </summary>
/// <param name="eventID">The eventID thats been recieved.</param>
/// <param name="args">The arguments you have recieved.</param>
public bool InsertCityLog(Int32 entryID, params string[] args)
{
string sqlLine = "INSERT INTO [ai] ([ai_entryid], [ai_date], [ai_arg0], [ai_arg1], [ai_arg2], [ai_arg3]) ";
sqlLine += "VALUES (" + entryID + ", " + TimeStamp.Now;
int i = 0;
foreach (string arg in args)
{
if (i == 4)
break;
i++;
sqlLine += ", '" + Config.EscapeString(arg) + "'";
}
for (int c = i; c < 4; c++)
{
sqlLine += ", ''";
}
sqlLine += ")";
try
{
using (DbCommand command = this.AIConfig.Connection.CreateCommand())
{
command.CommandText = sqlLine;
DbDataReader reader = command.ExecuteReader();
reader.Close();
}
return true;
}
catch
{
return false;
}
}
public string ParseAIMessage(Int64 EntryId, params string[] Arguments)
{
string AIMessage;
switch (EntryId)
{
case 1:
AIMessage = "Cloak was turned " + Arguments[1] + " by " + Arguments[0];
break;
case 2:
AIMessage = "Your radar station is picking up alien activity in the area surrounding your city.";
break;
case 3:
AIMessage = "Aliens invading our city in " + Arguments[0] + ", take cover or arm yourself.";
break;
case 4:
AIMessage = Arguments[0] + " removed the organization headquarters in " + Arguments[1] + "! All personal belongings or houses in the city were instantly destroyed!";
break;
case 5:
AIMessage = Arguments[0] + " has initiated removal of a " + Arguments[1] + " in " + Arguments[2] + "! The house and all belongings in the house will be destroyed in 2 minutes.";
break;
case 6:
AIMessage = Arguments[0] + " removed a " + Arguments[1] + " in " + Arguments[2] + "! All benefits from this house and all belongings in the house were instantly destroyed!";
break;
case 7:
AIMessage = Arguments[0] + " has initiated removal of the HQ in " + Arguments[1] + "! The house and all belongings in the house will be destroyed in 2 minutes. This will also cause the other houses in the city to be deleted!";
break;
default:
AIMessage = "Could not parse.";
break;
}
return AIMessage;
}
/// <summary>
/// Used to send messages to group/tell/other.
/// </summary>
/// <param name="Message"></param>
/// <param name="Type">1 = cloak changes, 2 = House changes, 3 = Timer events, 4 = Pre Alert off, 5 = Pre Alert on, 6 = Remind timer</param>
public void SendAIMessage(string Message, Int32 Type)
{
if (Type == 1)
{
if (Bot.Config.GetInt("aicity", "cloaktoguild", 0) == 1)
{
Bot.SendGuildMessage(Bot.ColorHighlight + "Alien Invasion »» City » " + Message);
}
if (Bot.Config.GetInt("aicity", "cloaktopgroup", 0) == 1)
{
Bot.SendPrivateChannelMessage(Bot.ColorHighlight + "Alien Invasion »» City » " + Message);
}
}
else if (Type == 2)
{
if (Bot.Config.GetInt("aicity", "housetoguild", 0) == 1)
{
Bot.SendGuildMessage(Bot.ColorHighlight + "Alien Invasion »» City » " + Message);
}
if (Bot.Config.GetInt("aicity", "housetopgroup", 0) == 1)
{
Bot.SendPrivateChannelMessage(Bot.ColorHighlight + "Alien Invasion »» City » " + Message);
}
}
else if (Type == 3)
{
if (Bot.Config.GetInt("aicity", "alerttoguild", 1) == 1)
{
Bot.SendGuildMessage(Bot.ColorHighlight + "Alien Invasion »» City » " + Message);
}
if (Bot.Config.GetInt("aicity", "alerttopgroup", 1) == 1)
{
Bot.SendPrivateChannelMessage(Bot.ColorHighlight + "Alien Invasion »» City » " + Message);
}
}
else if (Type == 4)
{
if (Bot.Config.GetInt("aicity", "prealertofftoguild", 1) == 1)
{
Bot.SendGuildMessage(Bot.ColorHighlight + "Alien Invasion »» City » " + Message);
}
if (Bot.Config.GetInt("aicity", "prealertofftopgroup", 0) == 1)
{
Bot.SendPrivateChannelMessage(Bot.ColorHighlight + "Alien Invasion »» City » " + Message);
}
}
else if (Type == 5)
{
if (Bot.Config.GetInt("aicity", "prealertontoguild", 1) == 1)
{
Bot.SendGuildMessage(Bot.ColorHighlight + "Alien Invasion »» City » " + Message);
}
if (Bot.Config.GetInt("aicity", "prealertontopgroup", 0) == 1)
{
Bot.SendPrivateChannelMessage(Bot.ColorHighlight + "Alien Invasion »» City » " + Message);
}
}
else if (Type == 6)
{
if (Bot.Config.GetInt("aicity", "remindertoguild", 1) == 1)
{
Bot.SendGuildMessage(Bot.ColorHighlight + "Alien Invasion »» City » " + Message);
}
if (Bot.Config.GetInt("aicity", "remindertopgroup", 0) == 1)
{
Bot.SendPrivateChannelMessage(Bot.ColorHighlight + "Alien Invasion »» City » " + Message);
}
}
}
public void Events_ChannelMessageEvent(BotShell sender, ChannelMessageArgs e)
{
if (e.Type == ChannelType.Guild && e.SenderID == 0 && e.IsDescrambled && e.Descrambled != null)
{
if (e.Descrambled.CategoryID == 1001)
{
switch (e.Descrambled.EntryID)
{
case 1:
SendAIMessage(ParseAIMessage(e.Descrambled.EntryID, e.Descrambled.Arguments[0].Text, e.Descrambled.Arguments[1].Text), 1);
InsertCityLog(1, e.Descrambled.Arguments[0].Text, e.Descrambled.Arguments[1].Text);
CityTimerMinutesGone = 0;
LastAnnounce = 0;
CityTimer.Enabled = true;
CityCloakWho = e.Descrambled.Arguments[0].Text;
CityCloakMode = e.Descrambled.Arguments[1].Text;
break;
case 2:
SendAIMessage(ParseAIMessage(e.Descrambled.EntryID), 1);
InsertCityLog(2);
break;
case 3:
SendAIMessage(ParseAIMessage(e.Descrambled.EntryID, e.Descrambled.Arguments[0].Text), 1);
InsertCityLog(3, e.Descrambled.Arguments[0].Text);
break;
case 4:
SendAIMessage(ParseAIMessage(e.Descrambled.EntryID, e.Descrambled.Arguments[0].Text, e.Descrambled.Arguments[1].Text), 2);
InsertCityLog(4, e.Descrambled.Arguments[0].Text, e.Descrambled.Arguments[1].Text);
break;
case 5:
SendAIMessage(ParseAIMessage(e.Descrambled.EntryID, e.Descrambled.Arguments[0].Text, e.Descrambled.Arguments[1].Message, e.Descrambled.Arguments[2].Text), 2);
InsertCityLog(5, e.Descrambled.Arguments[0].Text, e.Descrambled.Arguments[1].Message, e.Descrambled.Arguments[2].Text);
break;
case 6:
SendAIMessage(ParseAIMessage(e.Descrambled.EntryID, e.Descrambled.Arguments[0].Text, e.Descrambled.Arguments[1].Message, e.Descrambled.Arguments[2].Text), 2);
InsertCityLog(6, e.Descrambled.Arguments[0].Text, e.Descrambled.Arguments[1].Message, e.Descrambled.Arguments[2].Text);
break;
case 7:
SendAIMessage(ParseAIMessage(e.Descrambled.EntryID, e.Descrambled.Arguments[0].Text, e.Descrambled.Arguments[1].Text), 3);
InsertCityLog(7, e.Descrambled.Arguments[0].Text, e.Descrambled.Arguments[1].Text);
break;
default:
Console.WriteLine("Alien Invasion »» City » Error! Something strange happend, I dont know what it is. " + e.Message);
break;
}
}
}
}
public override void OnInstall()
{
}
public override void OnLoad()
{
if (this.AIConfig == null)
{
this.AIConfig = new Config("ai." + this.Bot.ID);
//creating table
this.AIConfig.ExecuteNonQuery("CREATE TABLE [ai] ( [ai_id] integer PRIMARY KEY AUTOINCREMENT , [ai_entryid] integer, [ai_date] integer, [ai_arg0] varchar (50), [ai_arg1] varchar (50), [ai_arg2] varchar (50), [ai_arg3] varchar (50) )");
}
// Hook up the Elapsed event for the timer.
CityTimer.Elapsed += new ElapsedEventHandler(CityTimerElapsed);
// Set the Interval to 1 minute.
CityTimer.Interval = 60000;
this.Bot.Events.ChannelMessageEvent += new ChannelMessageHandler(Events_ChannelMessageEvent);
}
public override void OnUnload()
{
CityTimer.Enabled = false;
this.Bot.Events.ChannelMessageEvent -= new ChannelMessageHandler(Events_ChannelMessageEvent);
}
public override void OnCommand(BotShell sender, CommandArgs e)
{
double d;
switch (e.Command)
{
case "city set":
if (e.Args.Length == 0)
{
Bot.SendReply(e, "Alien Invasion »» City » To set the cloak you need to append on/off of current setting.");
}
else
{
if (e.Args[0].ToLower() == "on")
{
CityTimer.Enabled = false;
Bot.SendReply(e, Bot.ColorHighlight + "Alien Invasion »» City » Cloak set to On manually by " + e.Sender + ". Timer stopped!");
InsertCityLog(1, e.Sender, "on");
CityCloakWho = e.Sender;
CityCloakMode = "on";
CityTimerMinutesGone = 60;
}
else if (e.Args[0].ToLower() == "off")
{
CityTimer.Enabled = false;
Bot.SendReply(e, Bot.ColorHighlight + "Alien Invasion »» City » Cloak set to Off manually by " + e.Sender + ". Timer stopped!");
InsertCityLog(1, e.Sender, "off");
CityCloakWho = e.Sender;
CityCloakMode = "off";
CityTimerMinutesGone = 60;
}
else if (e.Args[0].ToLower() == "timer")
{
if (double.TryParse(e.Args[1], System.Globalization.NumberStyles.Integer, MyCultureInfo, out d) == true)
{
if (Int32.Parse(e.Args[1]) >= 0 && Int32.Parse(e.Args[1]) < 61)
{
CityTimer.Enabled = true;
Bot.SendReply(e, Bot.ColorHighlight + "Alien Invasion »» City » Cloaks timer set manually by " + e.Sender + " to " + e.Args[1] + ".");
CityTimerMinutesGone = Int32.Parse(e.Args[1]);
}
else
{
Bot.SendReply(e, "Alien Invasion »» City » To change timer use city set timer XX, where XX is the number to set it to. (0-60).");
}
}
else
{
Bot.SendReply(e, "Alien Invasion »» City » To change timer use city set timer XX, where XX is the number to set it to (0-60).");
}
}
else
{
Bot.SendReply(e, "Alien Invasion »» City » To set the cloak you need to append on/off of current setting nothing else.");
}
}
break;
case "city del":
if (e.Type == BotCommandType.Tell)
{
if (e.Args.Length == 0)
{
Bot.SendReply(e, "Alien Invasion »» City » No ID!");
}
else if (double.TryParse(e.Args[0], System.Globalization.NumberStyles.Integer, MyCultureInfo, out d) == true)
{
if (e.Args.Length == 2)
{
if (e.Args[1] == "yes")
{
try
{
using (DbCommand command = this.AIConfig.Connection.CreateCommand())
{
if (Bot.Users.Authorized(e.Sender, UserLevel.Admin))
{
command.CommandText = "DELETE FROM [ai] WHERE [ai_id] = " + e.Args[0];
command.ExecuteNonQuery();
}
Bot.SendReply(e, "Alien Invasion »» City » Deleting... Done!");
}
}
catch
{
Bot.SendReply(e, "Alien Invasion »» City » Deleting... Error! Can not connect to database.");
}
}
else
{
Bot.SendReply(e, "Alien Invasion »» City » Deleting... Error! You have not confirmed correct.");
}
}
else
{
RichTextWindow replyWindow = new RichTextWindow(this.Bot);
replyWindow.AppendTitle("Alien Invasion - City");
try
{
using (DbCommand command = this.AIConfig.Connection.CreateCommand())
{
string sqlLine = "SELECT [ai_id], [ai_entryid], [ai_date], [ai_arg0], [ai_arg1], [ai_arg2], [ai_arg3] FROM [ai] WHERE [ai_id] = "+e.Args[0];
command.CommandText = sqlLine;
DbDataReader reader = command.ExecuteReader();
bool empty = true;
while (reader.Read())
{
empty = false;
replyWindow.AppendHighlight("Do you want to delete this post?");
replyWindow.AppendLineBreak(2);
replyWindow.AppendNormal(" ");
replyWindow.AppendHighlight(TimeStamp.ToDateTime(reader.GetInt64(2)).ToUniversalTime().ToString("dddd, d MMMM yyyy, HH:mm", dtfi) + " GMT");
replyWindow.AppendLineBreak(true);
replyWindow.AppendNormal(ParseAIMessage(reader.GetInt64(1), reader.GetString(3), reader.GetString(4), reader.GetString(5), reader.GetString(6)));
replyWindow.AppendLineBreak(2);
replyWindow.AppendBotCommandStart("city del " + reader.GetInt64(0) + " yes");
replyWindow.AppendImage("GFX_GUI_DIALOG_YES");
replyWindow.AppendLinkEnd();
replyWindow.AppendNormal(" ");
replyWindow.AppendCommandStart("/close InfoView");
replyWindow.AppendImage("GFX_GUI_DIALOG_NO");
replyWindow.AppendLinkEnd();
}
reader.Close();
if (empty == true)
{
replyWindow.AppendHighlight("No post with this ID");
replyWindow.AppendLineBreak(2);
replyWindow.AppendNormal(" Somebody just deleted this or you just did wrong.");
}
}
}
catch
{
replyWindow.AppendHighlight("Error!");
replyWindow.AppendLineBreak();
replyWindow.AppendNormal("Something went wrong in the connection to the database.");
}
Bot.SendReply(e, "Alien Invasion »» City » " + replyWindow.ToString("Delete post?"));
}
}
else
{
Bot.SendReply(e, "Alien Invasion »» City » Deleting... Error! No ID.");
}
}
break;
default:
try
{
RichTextWindow replyWindow = new RichTextWindow(this.Bot);
replyWindow.AppendTitle("Alien Invasion - City");
string StatusBarGreen = "";
string StatusBarGray = "";
string StatusBar = "";
if (e.Command == "city house" || e.Command == "city log")
{
using (DbCommand command = this.AIConfig.Connection.CreateCommand())
{
string sqlLine;
switch (e.Command)
{
case "city house":
sqlLine = "SELECT [ai_id], [ai_entryid] , [ai_date], [ai_arg0], [ai_arg1], [ai_arg2], [ai_arg3] FROM [ai] WHERE [ai_entryid] = 4 OR [ai_entryid] = 5 OR [ai_entryid] = 6 OR [ai_entryid] = 7 ORDER BY [ai_date] DESC LIMIT 15";
break;
default:
sqlLine = "SELECT [ai_id], [ai_entryid] , [ai_date], [ai_arg0], [ai_arg1], [ai_arg2], [ai_arg3] FROM [ai] WHERE [ai_entryid] = 1 OR [ai_entryid] = 2 OR [ai_entryid] = 3 ORDER BY [ai_date] DESC LIMIT 15";
break;
}
command.CommandText = sqlLine;
DbDataReader reader = command.ExecuteReader();
bool empty = true;
while (reader.Read())
{
empty = false;
if (Bot.Users.Authorized(e.Sender, UserLevel.Admin) && e.Type == BotCommandType.Tell)
{
replyWindow.AppendBotCommandStart("city del " + reader.GetInt64(0));
replyWindow.AppendImage("GFX_GUI_WINDOW_CLOSE_X");
replyWindow.AppendLinkEnd();
replyWindow.AppendNormal(" ");
}
replyWindow.AppendHighlight(TimeStamp.ToDateTime(reader.GetInt64(2)).ToUniversalTime().ToString("dddd, d MMMM yyyy, HH:mm", dtfi) + " GMT");
replyWindow.AppendLineBreak(true);
replyWindow.AppendNormal(ParseAIMessage(reader.GetInt64(1), reader.GetString(3), reader.GetString(4), reader.GetString(5), reader.GetString(6)));
replyWindow.AppendLineBreak(2);
}
reader.Close();
if (empty == true)
{
replyWindow.AppendHighlight("No city changes registred.");
}
}
}
else
{
if (CityCloakMode == "")
{
replyWindow.AppendHighlight("No city changes registred assuming cloak is up and ready to be taken down.");
}
else
{
if (CityTimerMinutesGone < 60)
{
for (int c = 0; c < CityTimerMinutesGone; c++)
{
StatusBarGreen += "l";
}
for (int i = CityTimerMinutesGone; i < 60; i++)
{
StatusBarGray += "l";
}
replyWindow.AppendLineBreak(true);
replyWindow.AppendColorString(ColorsHex.Green, StatusBarGreen);
replyWindow.AppendColorString(ColorsHex.Gray, StatusBarGray);
replyWindow.AppendLineBreak(2, true);
replyWindow.AppendNormal(CityTimerMinutesGone + " minutes have gone since last change.");
replyWindow.AppendLineBreak(true);
replyWindow.AppendNormal(CityCloakWho + " changed the cloak to " + CityCloakMode + " then.");
StatusBar = " [ <font color=#" + ColorsHex.Green + ">" + StatusBarGreen + "</font><font color=#" + ColorsHex.Gray + ">" + StatusBarGray + "</font> ]";
}
else
{
for (int x = 0; x < 60; x++)
{
StatusBarGreen += "l";
}
replyWindow.AppendLineBreak(true);
replyWindow.AppendColorString(ColorsHex.Green, StatusBarGreen);
replyWindow.AppendLineBreak(2, true);
replyWindow.AppendNormal("The cloak is ready to be taken down.");
StatusBar = " [ <font color=#" + ColorsHex.Green + ">" + StatusBarGreen + "</font> ]";
}
}
replyWindow.AppendLineBreak(2);
replyWindow.AppendHeader("Menu");
replyWindow.AppendBotCommand("City Cloak Log", "city log");
replyWindow.AppendLineBreak();
replyWindow.AppendBotCommand("City House Log", "city house");
replyWindow.AppendLineBreak();
}
Bot.SendReply(e, "Alien Invasion »» City » " + replyWindow.ToString("Click here") + StatusBar);
}
catch {
Bot.SendReply(e, "Alien Invasion »» City » Fetching data... Cant gather data!");
}
break;
}
}
}
}