context: using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Text;
  using Server.ContextMenus;
  using Server.Gumps;
  using Server.Items;
  using Server.Mobiles;
  using Server.Network;
  using Server.Targeting;
  
  namespace Server.Engines.ConPVP
  {
      public enum TournamentStage
      {
          Inactive,
          Signup,
          Fighting
      }
  
      public enum GroupingType
      {
          HighVsLow,
          Nearest,
          Random
      }
  
      public enum TieType
      {
          Random,
          Highest,
          Lowest,
          FullElimination,
          FullAdvancement
      }
  
      public class TournamentRegistrar : Banker
      {
          private TournamentController m_Tournament;
  
          [CommandProperty(AccessLevel.GameMaster)]
          public TournamentController Tournament
          {
              get
              {
                  return this.m_Tournament;
              }
              set
              {
                  this.m_Tournament = value;
              }
          }
  
          [Constructable]
          public TournamentRegistrar()
          {
              Timer.DelayCall(TimeSpan.FromSeconds(30.0), TimeSpan.FromSeconds(30.0), new TimerCallback(Announce_Callback));
          }
  
          private void Announce_Callback()
          {
              Tournament tourny = null;
  
              if (this.m_Tournament != null)
                  tourny = this.m_Tournament.Tournament;
  
              if (tourny != null && tourny.Stage == TournamentStage.Signup)
                  this.PublicOverheadMessage(MessageType.Regular, 0x35, false, "Come one, come all! Do you aspire to be a fighter of great renown? Join this tournament and show the world your abilities.");
          }
  
          public override void OnMovement(Mobile m, Point3D oldLocation)
          {
              base.OnMovement(m, oldLocation);
  
              Tournament tourny = null;
  
              if (this.m_Tournament != null)
                  tourny = this.m_Tournament.Tournament;
  
              if (this.InRange(m, 4) && !this.InRange(oldLocation, 4) && tourny != null && tourny.Stage == TournamentStage.Signup && m.CanBeginAction(this))
              {
                  Ladder ladder = Ladder.Instance;
  
                  if (ladder != null)
                  {
                      LadderEntry entry = ladder.Find(m);
  
                      if (entry != null && Ladder.GetLevel(entry.Experience) < tourny.LevelRequirement)
                          return;
                  }
  
                  if (tourny.HasParticipant(m))
                      return;
  
                  this.PrivateOverheadMessage(MessageType.Regular, 0x35, false, String.Format("Hello m'{0}. Dost thou wish to enter this tournament? You need only to write your name in this book.", m.Female ? "Lady" : "Lord"), m.NetState);
                  m.BeginAction(this);
                  Timer.DelayCall(TimeSpan.FromSeconds(10.0), new TimerStateCallback(ReleaseLock_Callback), m);
              }
          }
  
          private void ReleaseLock_Callback(object obj)
          {
              ((Mobile)obj).EndAction(this);
          }
  
          public TournamentRegistrar(Serial serial)
              : base(serial)
          {
          }
  
          public override void Serialize(GenericWriter writer)
          {
              base.Serialize(writer);
  
              writer.Write((int)0);
  
              writer.Write((Item)this.m_Tournament);
          }
  
          public override void Deserialize(GenericReader reader)
          {
              base.Deserialize(reader);
  
              int version = reader.ReadInt();
  
              switch ( version )
              {
                  case 0:
                      {
                          this.m_Tournament = reader.ReadItem() as TournamentController;
                          break;
                      }
              }
  
              Timer.DelayCall(TimeSpan.FromSeconds(30.0), TimeSpan.FromSeconds(30.0), new TimerCallback(Announce_Callback));
          }
      }
  
      public class TournamentSignupItem : Item
      {
          private TournamentController m_Tournament;
          private Mobile m_Registrar;
  
          [CommandProperty(AccessLevel.GameMaster)]
          public TournamentController Tournament
          {
              get
              {
                  return this.m_Tournament;
              }
              set
              {
                  this.m_Tournament = value;
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public Mobile Registrar
          {
              get
              {
                  return this.m_Registrar;
              }
              set
              {
                  this.m_Registrar = value;
              }
          }
  
          public override string DefaultName
          {
              get
              {
                  return "tournament signup book";
              }
          }
  
          [Constructable]
          public TournamentSignupItem()
              : base(4029)
          {
              this.Movable = false;
          }
  
          public override void OnDoubleClick(Mobile from)
          {
              if (!from.InRange(this.GetWorldLocation(), 2))
              {
                  from.LocalOverheadMessage(MessageType.Regular, 0x3B2, 1019045); // I can't reach that
              }
              else if (this.m_Tournament != null)
              {
                  Tournament tourny = this.m_Tournament.Tournament;
  
                  if (tourny != null)
                  {
                      if (this.m_Registrar != null)
                          this.m_Registrar.Direction = this.m_Registrar.GetDirectionTo(this);
  
                      switch ( tourny.Stage )
                      {
                          case TournamentStage.Fighting:
                              {
                                  if (this.m_Registrar != null)
                                  {
                                      if (tourny.HasParticipant(from))
                                      {
                                          this.m_Registrar.PrivateOverheadMessage(MessageType.Regular,
                                              0x35, false, "Excuse me? You are already signed up.", from.NetState);
                                      }
                                      else
                                      {
                                          this.m_Registrar.PrivateOverheadMessage(MessageType.Regular,
                                              0x22, false, "The tournament has already begun. You are too late to signup now.", from.NetState);
                                      }
                                  }
  
                                  break;
                              }
                          case TournamentStage.Inactive:
                              {
                                  if (this.m_Registrar != null)
                                      this.m_Registrar.PrivateOverheadMessage(MessageType.Regular,
                                          0x35, false, "The tournament is closed.", from.NetState);
  
                                  break;
                              }
                          case TournamentStage.Signup:
                              {
                                  Ladder ladder = Ladder.Instance;
  
                                  if (ladder != null)
                                  {
                                      LadderEntry entry = ladder.Find(from);
  
                                      if (entry != null && Ladder.GetLevel(entry.Experience) < tourny.LevelRequirement)
                                      {
                                          if (this.m_Registrar != null)
                                          {
                                              this.m_Registrar.PrivateOverheadMessage(MessageType.Regular,
                                                  0x35, false, "You have not yet proven yourself a worthy dueler.", from.NetState);
                                          }
  
                                          break;
                                      }
                                  }
  
                                  if (from.HasGump(typeof(AcceptTeamGump)))
                                  {
                                      if (this.m_Registrar != null)
                                          this.m_Registrar.PrivateOverheadMessage(MessageType.Regular,
                                              0x22, false, "You must first respond to the offer I've given you.", from.NetState);
                                  }
                                  else if (from.HasGump(typeof(AcceptDuelGump)))
                                  {
                                      if (this.m_Registrar != null)
                                          this.m_Registrar.PrivateOverheadMessage(MessageType.Regular,
                                              0x22, false, "You must first cancel your duel offer.", from.NetState);
                                  }
                                  else if (from is PlayerMobile && ((PlayerMobile)from).DuelContext != null)
                                  {
                                      if (this.m_Registrar != null)
                                          this.m_Registrar.PrivateOverheadMessage(MessageType.Regular,
                                              0x22, false, "You are already participating in a duel.", from.NetState);
                                  }
                                  else if (!tourny.HasParticipant(from))
                                  {
                                      ArrayList players = new ArrayList();
                                      players.Add(from);
                                      from.CloseGump(typeof(ConfirmSignupGump));
                                      from.SendGump(new ConfirmSignupGump(from, this.m_Registrar, tourny, players));
                                  }
                                  else if (this.m_Registrar != null)
                                  {
                                      this.m_Registrar.PrivateOverheadMessage(MessageType.Regular,
                                          0x35, false, "You have already entered this tournament.", from.NetState);
                                  }
  
                                  break;
                              }
                      }
                  }
              }
          }
  
          public TournamentSignupItem(Serial serial)
              : base(serial)
          {
          }
  
          public override void Serialize(GenericWriter writer)
          {
              base.Serialize(writer);
  
              writer.Write((int)0);
  
              writer.Write((Item)this.m_Tournament);
              writer.Write((Mobile)this.m_Registrar);
          }
  
          public override void Deserialize(GenericReader reader)
          {
              base.Deserialize(reader);
  
  
gt:             int version = reader.ReadInt();