1
0
Atdalīts 0
Pārlūkot izejas kodu

Added controller to handle multiple bot instances.

Changed interface to deal with the controller directly.
Added listing of connected servers.
master
Teknikode pirms 7 gadiem
vecāks
revīzija
1912f1f45e
  1. 1
      Combot/Combot.csproj
  2. 51
      Combot/Controller.cs
  3. 37
      Interface/ViewModels/MainViewModel.cs
  4. 15
      Modules/Owner Control/Owner_Control.cs

1
Combot/Combot.csproj

@ -66,6 +66,7 @@ @@ -66,6 +66,7 @@
<Compile Include="AccessType.cs" />
<Compile Include="Bot.cs" />
<Compile Include="Configurations\DatabaseConfig.cs" />
<Compile Include="Controller.cs" />
<Compile Include="Databases\Database.cs" />
<Compile Include="MessageType.cs" />
<Compile Include="Modules\Command.cs" />

51
Combot/Controller.cs

@ -0,0 +1,51 @@ @@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using Combot.Configurations;
namespace Combot
{
public class Controller
{
public static Controller Instance { get { return GetInstance(); } }
public static Controller GetInstance() { return _controller ?? (_controller = new Controller()); }
private static Controller _controller;
private List<Bot> _bots;
public List<Bot> Bots { get; private set; }
public readonly Config Config = new Config();
private Controller()
{
Load();
}
public void Load()
{
Config.LoadServers();
Bots = new List<Bot>();
foreach (ServerConfig server in Config.Servers)
{
Bot Combot = new Bot(server);
Bots.Add(Combot);
}
}
public void AutoConnect()
{
Bots.ForEach(bot =>
{
if (bot.ServerConfig.AutoConnect) { bot.Connect(); }
});
}
public Bot GetBot(string server)
{
return Bots.Find(bot => bot.ServerConfig.Name == server);
}
}
}

37
Interface/ViewModels/MainViewModel.cs

@ -12,17 +12,12 @@ using System.Windows; @@ -12,17 +12,12 @@ using System.Windows;
using System.Windows.Documents;
using Combot;
using Combot.IRCServices.Messaging;
using Combot.Configurations;
using Combot.IRCServices;
using Combot.IRCServices.Commanding;
namespace Interface.ViewModels
{
public class MainViewModel : ViewModelBase
{
public List<Bot> CombotSessions = new List<Bot>();
public Config Config = new Config();
public string ApplicationTitle { get; set; }
private string _CurrentBuffer = string.Empty;
@ -148,15 +143,15 @@ namespace Interface.ViewModels @@ -148,15 +143,15 @@ namespace Interface.ViewModels
public MainViewModel()
{
ApplicationTitle = "Combot";
Config.LoadServers();
ServerList = new ObservableCollection<string>();
LocationList = new ObservableCollection<string>();
BufferLock = new ReaderWriterLockSlim();
foreach (ServerConfig server in Config.Servers)
Controller.Instance.Load();
foreach (Bot Combot in Controller.Instance.Bots)
{
ServerList.Add(server.Name);
Bot Combot = new Bot(server);
ServerList.Add(Combot.ServerConfig.Name);
Combot.ErrorEvent += e => BotErrorHandler(e, Combot.ServerConfig.Name);
@ -179,15 +174,11 @@ namespace Interface.ViewModels @@ -179,15 +174,11 @@ namespace Interface.ViewModels
Combot.IRC.DisconnectEvent += () => DisconnectHandler(Combot.ServerConfig.Name);
Combot.IRC.TCPErrorEvent += e => TCPErrorHandler(e, Combot.ServerConfig.Name);
CombotSessions.Add(Combot);
SelectedServer = server.Name;
if (server.AutoConnect)
{
Combot.Connect();
}
SelectedServer = Combot.ServerConfig.Name;
}
Controller.Instance.AutoConnect();
ToggleConnection = new DelegateCommand(ExecuteToggleConnection, CanToggleConnection);
SubmitText = new DelegateCommand(ExecuteSubmitText, CanSubmitText);
RemoveLocation = new DelegateCommand(ExecuteRemoveLocation, CanRemoveLocation);
@ -264,7 +255,7 @@ namespace Interface.ViewModels @@ -264,7 +255,7 @@ namespace Interface.ViewModels
private void PrivateMessageCommandHandler(object sender, PrivateMessageCommand message, string server)
{
string curNick = string.Empty;
Bot session = CombotSessions.Find(bot => bot.ServerConfig.Name == SelectedServer);
Bot session = Controller.Instance.GetBot(SelectedServer);
if (session != null)
{
curNick = session.IRC.Nickname;
@ -275,7 +266,7 @@ namespace Interface.ViewModels @@ -275,7 +266,7 @@ namespace Interface.ViewModels
private void PrivateNoticeCommandHandler(object sender, PrivateNoticeCommand message, string server)
{
string curNick = string.Empty;
Bot session = CombotSessions.Find(bot => bot.ServerConfig.Name == SelectedServer);
Bot session = Controller.Instance.GetBot(SelectedServer);
if (session != null)
{
curNick = session.IRC.Nickname;
@ -321,7 +312,7 @@ namespace Interface.ViewModels @@ -321,7 +312,7 @@ namespace Interface.ViewModels
private void ExecuteSubmitText()
{
Bot botInstance = CombotSessions.Find(bot => bot.ServerConfig.Name == SelectedServer);
Bot botInstance = Controller.Instance.GetBot(SelectedServer);
if (botInstance != null && botInstance.Connected)
{
string message = InputBoxText;
@ -378,7 +369,7 @@ namespace Interface.ViewModels @@ -378,7 +369,7 @@ namespace Interface.ViewModels
if (location.StartsWith("#") || location.StartsWith("&"))
{
Bot botInstance = CombotSessions.Find(bot => bot.ServerConfig.Name == SelectedServer);
Bot botInstance = Controller.Instance.GetBot(SelectedServer);
if (botInstance.IRC.Channels.Exists(chan => chan.Name == location))
{
botInstance.IRC.Command.SendPart(location);
@ -421,12 +412,12 @@ namespace Interface.ViewModels @@ -421,12 +412,12 @@ namespace Interface.ViewModels
private void Connect(string server)
{
CombotSessions.Find(bot => bot.ServerConfig.Name == server).Connect();
Controller.Instance.GetBot(server).Connect();
}
private void Disconnect(string server)
{
CombotSessions.Find(bot => bot.ServerConfig.Name == server).Disconnect();
Controller.Instance.GetBot(server).Disconnect();
}
private void AddToBuffer(string server, string location, string message)
@ -490,7 +481,7 @@ namespace Interface.ViewModels @@ -490,7 +481,7 @@ namespace Interface.ViewModels
}
CurrentBuffer = string.Join(Environment.NewLine, BufferList.Find(buf => buf.Server == SelectedServer && buf.Location == SelectedLocation).Buffer);
BufferLock.ExitWriteLock();
Bot session = CombotSessions.Find(bot => bot.ServerConfig.Name == SelectedServer);
Bot session = Controller.Instance.GetBot(SelectedServer);
if (session != null)
{
Connected = session.Connected;

15
Modules/Owner Control/Owner_Control.cs

@ -143,7 +143,20 @@ namespace Combot.Modules.Plugins @@ -143,7 +143,20 @@ namespace Combot.Modules.Plugins
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, channelMessage);
break;
case "servers":
// TODO Add server list
string serverList = string.Empty;
foreach (Bot bot in Controller.Instance.Bots)
{
if (bot.Connected)
{
serverList = string.Join(", ", serverList, bot.ServerConfig.Name);
}
}
if (string.IsNullOrEmpty(serverList))
{
serverList = "None Connected";
}
string serverMessage = string.Format("I am connected to the following servers: \u0002{0}\u000F", serverList.TrimStart(',').Trim());
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, serverMessage);
break;
case "modules":
string moduleList = string.Join(", ", Bot.Modules.Select(module => module.Name));

Notiek ielāde…
Atcelt
Saglabāt