private void OnIncoming(IrcClient irc, IRCQEvent ev)
        {
            if (ev is ServerStatusEvent) {
                if (((ServerStatusEvent)ev).Status == ServerStatus.Connected) {
                    _channel = GameChannel.Config["Channel"];
                    _irc.JoinChannel(_channel);

                    PostConnect();
                } else {
                    // Disconnected for some reason.
                    _irc.Connect();
                }
            }

            // At this point checking if the bot somehow left its channel
            else if (ev is PartEvent) {
                var pe = (PartEvent)ev;
                if (pe.Sender.IsClient) {
                    _irc.JoinChannel(_channel);
                }
            } else if (ev is KickEvent) {
                var ke = (KickEvent)ev;
                if (ke.Target.IsClient) {
                    _irc.JoinChannel(_channel);
                }
            }
        }
 private void PrintIncoming(IrcClient irc, IRCQEvent ev)
 {
     if (ev is TickEvent) {
         return;
     } else if (ev is ChannelEvent) {
         if (((ChannelEvent)ev).Channel == _channel.ToLower()) {
             GameChannel.Out(_channel, ev.ToString());
         }
     } else if (ev is IrcEvent) {
         GameChannel.Out("IRCQueue", ev.ToString());
     } else if (ev is UserMessageEvent) {
         GameChannel.Out(GameChannel.Config["Server"], ev.ToString());
     } else {
         GameChannel.Out("GameChannel", ev.ToString());
     }
 }
Exemple #3
0
 public void Enqueue(IRCQEvent ev)
 {
     _queue.Add(ev);
 }
        private void DispatchToGame(IRCQEvent ev)
        {
            try {
                if (ev is TickEvent) {
                    _game.OnTick(_tickCount);

                    // Reset timer
                    _ticker.Change(_tickInterval, Timeout.Infinite);
                    _tickCount++;
                } else if (ev is ChannelEvent) {
                    if (((ChannelEvent)ev).Channel != Channel.ToLower()) return;
                    if (((ChannelEvent)ev).Sender.IsClient) return;

                    if (ev is JoinEvent) _game.OnJoin(((ChannelEvent)ev).Sender);
                    else if (ev is PartEvent) _game.OnPart(((ChannelEvent)ev).Sender);
                    else if (ev is ChannelMessageEvent) {
                        var cme = (ChannelMessageEvent)ev;
                        if (cme.Type == MessageType.Notice) return;
                        _game.OnInput(cme.Sender, cme.Message, cme.Type == MessageType.Action);
                    }
                } else if (ev is UserMessageEvent) {
                    var ue = (UserMessageEvent)ev;
                    if (ue.Type == MessageType.Notice) return;
                    if (ue.Sender.Channels.Contains(Channel.ToLower()))
                        _game.OnInputPriv(ue.Sender, ue.Message, ue.Type == MessageType.Action);
                    else
                        GameChannel.Out("GameManager", "Ignoring private message from " + ue.Sender
                            + " (not in channel)");
                } else if (ev is NickChangeEvent) {
                    var ne = (NickChangeEvent)ev;
                    if (ne.Sender.Channels.Any(c => c == Channel.ToLower())) {
                        _game.OnRename(ne.Sender, ne.NewNick);
                    }
                } else if (ev is QuitEvent) {
                    var qe = (QuitEvent)ev;
                    if (qe.Sender.Channels.Any(c => c == Channel.ToLower())) {
                        _game.OnPart(qe.Sender);
                    }
                }
            } catch (Exception ex) {
                // When an exception occurrs in a game, let the channel know about it.
                _irc.SendMessage(Channel, "\u000300,04An error occurred within the game. "
                    + ex.GetType().FullName + ": " + ex.Message);
                _irc.SendMessage(Channel, "\u000300,04The game may be unstable at this point.");
                GameChannel.Out(GameDisplayName, ex.ToString());
                _canKill = true; // kill command may now be used
            }

            // Game instruction flags:
            // Certain requests by the game are handled after the event handlers return,
            // preventing the game from accidentally interfering with itself.
            // Events could be sent to IRCQueue, but these things must be done immediately.
            if (reqTickReset) {
                _tickCount = 0;
                reqTickReset = false;
            }
            if (reqEndGame) {
                string name = GameDisplayName;
                TimeSpan uptime = DateTime.Now - GameStartTime;
                KillGame();
                _irc.SendMessage(Channel, name + " has ended. Duration: " +
                    Math.Floor(uptime.TotalMinutes) + ":" + uptime.Seconds.ToString("00") + ".");
                reqEndGame = false;
            }
        }
        // Common processing for events done here before branching
        private void OnIncoming(IrcClient irc, IRCQEvent ev)
        {
            if (ev is ServerStatusEvent) {
                if (((ServerStatusEvent)ev).Status == ServerStatus.Connected) {
                    // Get channel name from config
                    Channel = GameChannel.Config["Channel"];
                } else {
                    // Connection lost
                    KillGame();
                    return;
                }
            } else if (ev is KickEvent) {
                // Kicked out
                if (((KickEvent)ev).Target.IsClient) {
                    KillGame();
                    return;
                }
            } else if (ev is ChannelMessageEvent) {
                var cme = (ChannelMessageEvent)ev;
                if (cme.Channel == Channel.ToLower()) {
                    string[] line = cme.Message.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (line.Length != 0 && line[0].Length > 0 && line[0][0] == '%') {
                        line[0] = line[0].Substring(1).ToLower();
                        CommandHandler command;
                        if (_commands.TryGetValue(line[0], out command)) {
                            // GameChannel command
                            command(cme, line);
                            return;
                        } else if (_game == null && _loader.CreateInstance(line[0], out _game, out _gameDispName)) {
                            // Game start command
                            GameChannel.Out(GameDisplayName, "Instance created.");
                            _game.Insert(this, _irc);
                            try {
                                _game.OnInit(cme.Sender, cme.Channel.Users, line);
                            } catch (Exception ex) {
                                _irc.SendMessage(Channel, "\u000300,04An error occurred during game startup "
                                    + "(" + ex.GetType().FullName + " - Details in log).");
                                _irc.SendMessage(Channel, "\u000300,04The game will now be killed.");
                                GameChannel.Out(line[0], ex.ToString());
                                KillGame();
                                return;
                            }
                            _gameStartTime = DateTime.Now;
                            reqTickReset = false;
                            reqEndGame = false;
                            _tickCount = 0;
                            _ticker.Change(_tickInterval, Timeout.Infinite);
                            _canKill = false;
                            return;
                        }
                    }
                }
            }

            if (_game != null) DispatchToGame(ev);
        }
 /// <summary>
 /// Adds an event to the queue, to be sent to all handlers later.
 /// </summary>
 /// <param name="ev">The event to add to the queue.</param>
 public void Enqueue(IRCQEvent ev)
 {
     if (ev == null) return;
     _queue.Enqueue(ev);
 }
 // Used by IRCQueue class
 internal void SendEvent(IRCQEvent ev)
 {
     if (OnIRCQEvent != null)
         OnIRCQEvent(this, ev);
 }