Example #1
0
 protected override void ProcessOFF(TokenMessage msg)
 {
     base.ProcessOFF(msg);
     this.parent.Invoke( new VoidDelegate( delegate
     {
         this.parent.Close();
     }));
 }
Example #2
0
 public static void CreateMapFromMDF(TokenMessage msg, Game game)
 {
     TokenMessage msgProvinces = msg.SubMessages[2];
     TokenMessage msgSupplyCenterProvinces = msgProvinces.SubMessages[0];
     foreach(TokenMessage message in msgSupplyCenterProvinces.SubMessages)
     {
         for (int index = 1; index < message.SubMessages.Count; ++index)
         {
             Province province = GetProvince(message.SubMessages[index].Tokens[0], game);
             foreach(Token powerToken in message.SubMessages[0].Tokens)
             {
                 Power power = GetPower(powerToken, game);
                 if (power != null)
                 {
                     power.HomeProvinces.Add(province);
                 }
             }
         }
     }
     //Adjacencies
     TokenMessage msgAdjacencies = msg.SubMessages[3];
     foreach(TokenMessage msgAdjacency in msgAdjacencies.SubMessages)
     {
         Province fromProvince = game.Map.Provinces[msgAdjacency.Tokens[0].ToString()];
         for (int index = 1; index < msgAdjacency.SubMessages.Count; ++index)
         {
             TokenMessage msgUnitAndAdj = msgAdjacency.SubMessages[index];
             Location fromLocation;
             UnitType unitType;
             if (msgUnitAndAdj.SubMessages.Count == 0)
             {
                 fromLocation = GetOrCreateLocation(fromProvince, msgUnitAndAdj);
                 unitType = GetUnitType(msgUnitAndAdj.Tokens[0]);
             }
             else
             {
                 fromLocation = GetOrCreateLocation(fromProvince, msgUnitAndAdj.SubMessages[0]);
                 unitType = GetUnitType(msgUnitAndAdj.SubMessages[0].Tokens[0]);
             }
             for (int ii = 1; ii < msgUnitAndAdj.SubMessages.Count; ++ii)
             {
                 Location toLocation = GetOrCreateLocation(unitType, msgUnitAndAdj.SubMessages[ii], game);
                 Trace.WriteLineIf(mySwitch.Enabled,
                     string.Format(CultureInfo.InvariantCulture,
                     ErrorMessages.Util_Trace_NewAdjacency,
                     fromLocation.ToString(), toLocation.ToString()),
                         traceCategory);
                 fromLocation.AdjacentLocations.Add(toLocation);
             }
         }
     }
     game.Map.FinalSetup();
 }
Example #3
0
 protected override void ProcessNOW(TokenMessage msg)
 {
     switch( this.Game.Turn.Phase )
     {
         case Phase.Spring:
             OrderMoves();
             break;
         case Phase.Summer:
             OrderRetreats();
             break;
         case Phase.Fall:
             OrderMoves();
             break;
         case Phase.Autumn:
             OrderRetreats();
             break;
         case Phase.Winter:
             OrderBuildsAndDisbands();
             break;
     }
     SubmitOrders();
 }
Example #4
0
        /// <summary>Submits the <see cref="CurrentOrders"/> to the server.
        /// </summary>
        protected void SubmitOrders()
        {
            if( this.orders.Count == 0 )
            {
                return;
            }
            TokenMessage msg = new TokenMessage(Token.SUB);
            foreach( Order order in this.orders )
            {
                TokenMessage msgOrder = new TokenMessage();
                //object[] parameters = order.GetOrderParameters();
                switch (order.OrderType)
                {
                    case OrderType.Build :
                    {
                        BuildOrder o = order as BuildOrder;
                        //Debug.Assert(parameters.Length == 0);
                        msgOrder.Add( Util.ToOrderFormat(o.Unit).MessageEnclosed );
                        msgOrder.Add( Token.BLD );
                        break;
                    }

                    case OrderType.Convey :
                    {
                        ConveyOrder o = order as ConveyOrder;
                        //Debug.Assert(parameters.Length == 2);
                        //Debug.Assert(parameters[0] is Unit);
                        //Debug.Assert(parameters[1] is Location);
                        msgOrder.Add( Util.ToOrderFormat(o.Unit).MessageEnclosed );
                        msgOrder.Add( Token.CVY );
                        msgOrder.Add( Util.ToOrderFormat(o.ConveyedUnit).MessageEnclosed );
                        msgOrder.Add( Token.CTO );
                        msgOrder.Add( Util.ToOrderFormat(o.TargetProvince) );
                        break;
                    }
                    case OrderType.Disband :
                    {
                        DisbandOrder o = order as DisbandOrder;
                        //Debug.Assert(parameters.Length == 0);
                        msgOrder.Add( Util.ToOrderFormat(o.Unit).MessageEnclosed );
                        msgOrder.Add( Token.DSB );
                        break;
                    }

                    case OrderType.Hold :
                    {
                        HoldOrder o = order as HoldOrder;
                        //Debug.Assert(parameters.Length == 0);
                        msgOrder.Add( Util.ToOrderFormat(o.Unit).MessageEnclosed );
                        msgOrder.Add( Token.HLD );
                        break;
                    }

                    case OrderType.Move :
                    {
                        MoveOrder o = order as MoveOrder;
                        //Debug.Assert(parameters.Length == 1);
                        //Debug.Assert(parameters[0] is Location);
                        msgOrder.Add( Util.ToOrderFormat(o.Unit).MessageEnclosed );
                        msgOrder.Add( Token.MTO );
                        msgOrder.Add( Util.ToOrderFormat(o.TargetLocation) );
                        break;
                    }

                    case OrderType.MoveByConvoy :
                    {
                        MoveByConvoyOrder o = order as MoveByConvoyOrder;
                        //Debug.Assert(parameters.Length == 1);
                        //Debug.Assert(parameters[0] is Route);
                        //Route route = parameters[0] as Route;
                        msgOrder.Add( Util.ToOrderFormat(o.Unit).MessageEnclosed );
                        msgOrder.Add( Token.CTO );
                        msgOrder.Add( Util.ToOrderFormat(o.Route.End) );
                        msgOrder.Add( Token.VIA );
                        TokenMessage via = new TokenMessage();
                        foreach (Province province in o.Route.Via.Provinces)
                        {
                            via.Add( Util.ToOrderFormat(province) );
                        }
                        msgOrder.Add( via.MessageEnclosed );
                        break;
                    }

                    case OrderType.Remove :
                    {
                        RemoveOrder o = order as RemoveOrder;
                        //Debug.Assert(parameters.Length == 0);
                        msgOrder.Add( Util.ToOrderFormat(o.Unit).MessageEnclosed );
                        msgOrder.Add( Token.REM );
                        break;
                    }

                    case OrderType.Retreat :
                    {
                        RetreatOrder o = order as RetreatOrder;
                        //Debug.Assert(parameters.Length == 1);
                        //Debug.Assert(parameters[0] is Location);
                        msgOrder.Add( Util.ToOrderFormat(o.Unit).MessageEnclosed );
                        msgOrder.Add( Token.RTO );
                        msgOrder.Add( Util.ToOrderFormat(o.RetreatLocation) );
                        break;
                    }

                    case OrderType.SupportHold :
                    {
                        SupportHoldOrder o = order as SupportHoldOrder;
                        //Debug.Assert(parameters.Length == 1);
                        //Debug.Assert(parameters[0] is Unit);
                        msgOrder.Add( Util.ToOrderFormat(o.Unit).MessageEnclosed );
                        msgOrder.Add( Token.SUP );
                        msgOrder.Add( Util.ToOrderFormat(o.SupportedUnit).MessageEnclosed );
                        break;
                    }

                    case OrderType.SupportMove :
                    {
                        SupportMoveOrder o = order as SupportMoveOrder;
                        //Debug.Assert(parameters.Length == 2);
                        //Debug.Assert(parameters[0] is Unit);
                        //Debug.Assert(parameters[1] is Province);
                        msgOrder.Add( Util.ToOrderFormat(o.Unit).MessageEnclosed );
                        msgOrder.Add( Token.SUP );
                        msgOrder.Add( Util.ToOrderFormat(o.SupportedUnit).MessageEnclosed );
                        msgOrder.Add( Token.MTO );
                        msgOrder.Add( Util.ToOrderFormat(o.TargetProvince) );
                        break;
                    }

                    case OrderType.WaiveBuild :
                    {
                        WaiveBuildOrder o = order as WaiveBuildOrder;
                        //Debug.Assert(parameters.Length == 1);
                        //Debug.Assert(parameters[0] is Power);
                        msgOrder.Add( Util.ToOrderFormat(o.Power) );
                        msgOrder.Add( Token.WVE );
                        break;
                    }

                    default :
                        throw new InvalidOperationException(
                            string.Format(CultureInfo.InvariantCulture,
                            ErrorMessages.Basebot_Exception_UnknownOrderType,
                            order.OrderType.ToString()));
                }
                msg.Add(msgOrder.MessageEnclosed);
            }
            this.connection.Send(msg);
        }
Example #5
0
 /// <summary>Processes an unexpected YES message.
 /// The default version sends the message to the log.
 /// </summary>
 /// <param name="msg">The YES message.</param>
 /// <param name="confirmedMessage">The message that was unexpected.</param>
 protected virtual void ProcessYES_Unexpected(TokenMessage msg, TokenMessage confirmedMessage)
 {
     Trace.WriteLineIf(baseBotSwitch.Enabled,
         string.Format(CultureInfo.InvariantCulture,
         ErrorMessages.Basebot_Trace_UnexpectedMessage,
         "YES", msg.ToString()), traceCategory);
 }
Example #6
0
 /// <summary>Processes a TME message (Indicates the number of seconds until the next deadline).
 /// The default version does nothing.
 /// </summary>
 /// <param name="msg">The TME message.</param>
 protected virtual void ProcessTME(TokenMessage msg)
 {
 }
Example #7
0
 /// <summary>Processes a SVE message (Save the game).
 /// The default version does responds with a YES message (but does not save the game).
 /// </summary>
 /// <param name="msg">The SVE message.</param>
 protected virtual void ProcessSVE(TokenMessage msg)
 {
     this.connection.Send(Token.YES & msg);
 }
Example #8
0
 /// <summary>Processes an unexpected REJ message.
 /// The default version sends the message to the log.
 /// </summary>
 /// <param name="msg">The REJ message.</param>
 /// <param name="rejectMessage">The message that was unexpected.</param>
 protected virtual void ProcessREJ_Unexpected(TokenMessage msg, TokenMessage rejectMessage)
 {
     Trace.WriteLineIf( baseBotSwitch.Enabled,
         string.Format(CultureInfo.InvariantCulture,
         ErrorMessages.Basebot_Trace_UnexpectedMessage,
         "REJ", msg.ToString()), traceCategory);
     Debug.Assert(false);
 }
Example #9
0
 /// <summary>Processes an OFF message (Turn off).
 /// The default version sets the <see cref="EndOfGameCause"/> to <see cref="FinishingCause.Off"/>.
 /// </summary>
 /// <param name="msg">The OFF message.</param>
 protected virtual void ProcessOFF(TokenMessage msg)
 {
     this.finishingCause = FinishingCause.Off;
 }
Example #10
0
 private void PreProcessSCO(TokenMessage msg)
 {
     Util.UpdateGameFromSCO(msg, this.game);
     ProcessSCO(msg);
 }
Example #11
0
 private void PreProcessREJ_NOT(TokenMessage msg, TokenMessage rejectMessage)
 {
     Token command = rejectMessage.SubMessages[1].Tokens[0];
     switch( command.ToString().ToUpper(CultureInfo.InvariantCulture) )
     {
         case "DRW":
             ProcessREJ_NOT_DRW(msg, rejectMessage);
             break;
         case "GOF":
             ProcessREJ_NOT_GOF(msg, rejectMessage);
             break;
         default:
             ProcessREJ_NOT_Unexpected(msg, rejectMessage);
             break;
     }
 }
Example #12
0
 private void PreProcessREJ(TokenMessage msg)
 {
     TokenMessage rejectMessage = msg.SubMessages[1];
     switch( rejectMessage.Tokens[0].ToString().ToUpper(CultureInfo.InvariantCulture) )
     {
         case "ADM":
             ProcessREJ_ADM(msg, rejectMessage);
             break;
         case "DRW":
             ProcessREJ_DRW(msg, rejectMessage);
             break;
         case "GOF":
             ProcessREJ_GOF(msg, rejectMessage);
             break;
         case "HLO":
             ProcessREJ_HLO(msg, rejectMessage);
             break;
         case "HST":
             ProcessREJ_HST(msg, rejectMessage);
             break;
         case "IAM":
             ProcessREJ_IAM(msg, rejectMessage);
             break;
         case "NME":
             ProcessREJ_NME(msg, rejectMessage);
             break;
         case "NOT":
             PreProcessREJ_NOT(msg, rejectMessage);
             break;
         case "NOW":
             ProcessREJ_NOW(msg, rejectMessage);
             break;
         case "ORD":
             ProcessREJ_ORD(msg, rejectMessage);
             break;
         case "SCO":
             ProcessREJ_SCO(msg, rejectMessage);
             break;
         case "SND":
             ProcessREJ_SND(msg, rejectMessage);
             break;
         case "SUB":
             ProcessREJ_SUB(msg, rejectMessage);
             break;
         case "TME":
             ProcessREJ_TME(msg, rejectMessage);
             break;
         default:
             ProcessREJ_Unexpected(msg, rejectMessage);
             break;
     }
 }
Example #13
0
 private void PreProcessOUT(TokenMessage msg)
 {
     //TODO: Press check.
     ProcessOUT(msg);
 }
Example #14
0
 private void PreProcessORD(TokenMessage msg)
 {
     //Util.UpdateGameFromORD(msg, this.game);
     ProcessORD(msg);
 }
Example #15
0
 private void PreProcessNOW(TokenMessage msg)
 {
     BeforeNewTurn();
     Util.UpdateGameFromNOW(msg, this.game);
     this.orders.Clear();
     ProcessNOW(msg);
 }
Example #16
0
 /// <summary>Processes a NOT CCD message (the specified country is not in civil disorder).
 /// The default version does nothing.
 /// </summary>
 /// <param name="msg">The NOT message.</param>
 /// <param name="messageParameters">The CCD message.</param>
 /// <param name="newReconnection">Set to true if the indicated power is newly reconnected.</param>
 protected virtual void ProcessNOT_CCD(TokenMessage msg, TokenMessage messageParameters, bool newReconnection)
 {
 }
Example #17
0
 /// <summary>Processes a NOT TME message (Specifies that the deadline timer has stopped).
 /// The default version does nothing.
 /// </summary>
 /// <param name="msg">The NOT message.</param>
 /// <param name="messageParameters">The TME message.</param>
 protected virtual void ProcessNOT_TME(TokenMessage msg, TokenMessage messageParameters)
 {
 }
Example #18
0
 private void PreProcessSLO(TokenMessage msg)
 {
     this.finishingCause = FinishingCause.Solo;
     ProcessSLO(msg);
 }
Example #19
0
 /// <summary>Processes a REJ TME message (a rejected TME message).
 /// The default version sends the message to the log.
 /// </summary>
 /// <param name="msg">The REJ message.</param>
 /// <param name="rejectMessage">The TME message.</param>
 protected virtual void ProcessREJ_TME(TokenMessage msg, TokenMessage rejectMessage)
 {
     Trace.WriteLineIf( baseBotSwitch.Enabled,
         string.Format(CultureInfo.InvariantCulture,
         ErrorMessages.Basebot_Trace_MessageRejected,
         msg.ToString()), traceCategory);
 }
Example #20
0
 private void PreProcessYES(TokenMessage msg)
 {
     TokenMessage confirmedMessage = msg.SubMessages[1];
     switch( confirmedMessage.Tokens[0].ToString().ToUpper(CultureInfo.InvariantCulture) )
     {
         case "DRW":
             ProcessYES_DRW(msg, confirmedMessage);
             break;
         case "GOF":
             ProcessYES_GOF(msg, confirmedMessage);
             break;
         case "IAM":
             ProcessYES_IAM(msg, confirmedMessage);
             break;
         case "NME":
             ProcessYES_NME(msg, confirmedMessage);
             break;
         case "NOT":
             ProcessYES_NOT(msg, confirmedMessage);
             break;
         case "OBS":
             ProcessYES_OBS(msg, confirmedMessage);
             break;
         case "SND":
             ProcessYES_SND(msg, confirmedMessage);
             break;
         case "TME":
             ProcessYES_TME(msg, confirmedMessage);
             break;
         default:
             ProcessYES_Unexpected(msg, confirmedMessage);
             break;
     }
 }
Example #21
0
 /// <summary>Processes a SLO message (Someone has won solo).
 /// The default version set the <see cref="finishingCause"/> to <see cref="FinishingCause.Solo"/>.
 /// </summary>
 /// <param name="msg">The SLO message.</param>
 protected virtual void ProcessSLO(TokenMessage msg)
 {
     this.finishingCause = FinishingCause.Solo;
 }
Example #22
0
 /// <summary>Connects to a DAIDE server running at the specified location.
 /// </summary>
 /// <param name="hostName">The name of the server. This can either be an IP number or a server name.</param>
 /// <param name="port">The port to connect to.</param>
 /// <param name="name">The name of the bot.</param>
 /// <param name="version">The version of the bot.</param>
 protected void Connect(string hostName, int port, string name, string version)
 {
     this.connection.Connect(hostName, port);
     TokenMessage msg = new TokenMessage(Token.NME);
     msg += new TokenMessage(TokenFactory.FromString(name)).MessageEnclosed;
     msg += new TokenMessage(TokenFactory.FromString(version)).MessageEnclosed;
     Send(msg);
 }
Example #23
0
        /// <summary>Processes a THX message (Thanks for an order).
        /// The default version sends the message to the log if the message indicated an error.
        /// If possible, it also sends a replacement order.
        /// </summary>
        /// <param name="msg">The THX message.</param>
        /// <remarks>
        /// The action that is taken depends on the note that is returned from the server, as follows:
        /// MBV - no action taken, the order is correct as it stands.
        /// NYU - message is logged.
        /// NRS - message is logged.
        /// HLD - message is logged.
        /// NRN - message is logged.
        /// NMB - message is logged.
        /// NMR - message is logged.
        /// FAR - message is logged, and a replacement Hold order is sent.
        /// NSP - message is logged, and a replacement Hold order is sent.
        /// NSU - message is logged, and a replacement Hold order is sent.
        /// NAS - message is logged, and a replacement Hold order is sent.
        /// NSF - message is logged, and a replacement Hold order is sent.
        /// NSA - message is logged, and a replacement Hold order is sent.
        /// NVR - message is logged, and a replacement Disband order is sent.
        /// YSC - message is logged, and a replacement Waive order is sent.
        /// ESC - message is logged, and a replacement Waive order is sent.
        /// HSC - message is logged, and a replacement Waive order is sent.
        /// NSC - message is logged, and a replacement Waive order is sent.
        /// CST - message is logged, and a replacement Waive order is sent.
        /// </remarks>
        protected virtual void ProcessTHX(TokenMessage msg)
        {
            TokenMessage order = msg.SubMessages[1];
            Token note = msg.SubMessages[2].Tokens[0];

            TokenMessage replacementOrder = null;

            if (note == Token.MBV)
            {
                //Valid order
                return;
            }

            //All invalid orders are here!
            if (   note == Token.NYU
                || note == Token.NRS )
            {
                // Not the right season / not your unit
                // There's not much to do.
            }

            TokenMessage unit = order.SubMessages[0].MessageEnclosed;;
            if (order.SubMessages[1].Tokens[0] == Token.HLD)
            {
                // A unit was ordered to hold, but the order failed.
                // There's not much we can do!
            }

            if (   note == Token.NRN
                || note == Token.NMB
                || note == Token.NMR )
            {
                // Order wasn't needed in the first place!
            }

            if (   note == Token.FAR
                || note == Token.NSP
                || note == Token.NSU
                || note == Token.NAS
                || note == Token.NSF
                || note == Token.NSA )
            {
                // Illegal movement order. Replace with a hold order.
                replacementOrder = unit + Token.HLD;
            }

            if (note == Token.NVR)
            {
                // Illegal retreat order. Replace with a disband order
                replacementOrder = unit + Token.DSB;
            }

            if (   note == Token.YSC
                || note == Token.ESC
                || note == Token.HSC
                || note == Token.NSC
                || note == Token.CST )
            {
                // Illegal build order. Replace with a waive order
                replacementOrder = unit.SubMessages[0] + Token.WVE;
            }

            if (replacementOrder != null)
            {
                Trace.WriteLineIf(baseBotSwitch.Enabled,
                    string.Format(CultureInfo.InvariantCulture,
                    ErrorMessages.Basebot_Trace_InvalidOrderReplaced,
                    order.ToString(), replacementOrder.ToString()), traceCategory);

                replacementOrder = Token.SUB & replacementOrder;
                this.connection.Send(replacementOrder);
            }
            else
            {
                Trace.WriteLineIf( baseBotSwitch.Enabled,
                    string.Format(CultureInfo.InvariantCulture,
                    ErrorMessages.Basebot_Trace_InvalidOrderNotReplaced,
                    order.ToString()), traceCategory);
            }
        }
Example #24
0
 /// <summary>Processes a CCD message (County in Civil Disorder).
 /// The default version does nothing.
 /// </summary>
 /// <param name="msg">The CCD message</param>
 /// <param name="isNewDisconnection">Set to true if the notification is a new country in civil disorder.
 /// <see cref="powersInCivilDisorder"/></param>
 protected virtual void ProcessCCD(TokenMessage msg, bool isNewDisconnection)
 {
 }
Example #25
0
 /// <summary>Processes a YES TME message (a confirmation of a TME message).
 /// The default version does nothing.
 /// </summary>
 /// <param name="msg">The YES message.</param>
 /// <param name="confirmedMessage">The TME message.</param>
 protected virtual void ProcessYES_TME(TokenMessage msg, TokenMessage confirmedMessage)
 {
 }
Example #26
0
 /// <summary>Processes a DRW message (a Draw has been declared by the server).
 /// The default version sets <see cref="finishingCause"/> to <see cref="FinishingCause.Draw"/>.
 /// </summary>
 /// <param name="msg">The DRW message.</param>
 protected virtual void ProcessDRW(TokenMessage msg)
 {
     this.finishingCause = FinishingCause.Draw;
 }
Example #27
0
 /// <summary>Sends the <see cref="TokenMessage"/> to the server.
 /// </summary>
 /// <param name="msg">The message to send.</param>
 protected void Send(TokenMessage msg)
 {
     //TODO: Preprocess some orders, so that we will not react
     // to some client orders, such as HST, etc.
     this.connection.Send(msg);
 }
Example #28
0
        /// <summary>Processes a FRM message (press from another <see cref="Power"/>).
        /// The default version responds with a HUH message, followed with a TRY message that is empty.
        /// </summary>
        /// <param name="msg">The FRM message.</param>
        protected virtual void ProcessFRM(TokenMessage msg)
        {
            // The BaseBot does not understand press.
            // Reply with a "HUH" and and "TRY" that is
            // empty.
            if (msg.SubMessages[3].Tokens[0] != Token.HUH
                && msg.SubMessages[3].Tokens[0] != Token.TRY)
            {
                TokenMessage huh = Token.SND
                    & msg.SubMessages[1].Tokens[0]
                    & ( Token.HUH
                    & ( Token.ERR + msg.SubMessages[3] ) );
                TokenMessage tryMsg = Token.SND
                    & msg.SubMessages[1].Tokens[0]
                    & (Token.TRY
                    & new TokenMessage());

                this.connection.Send(huh);
                this.connection.Send(tryMsg);
            }
        }
Example #29
0
 protected override void ProcessNOW(TokenMessage msg)
 {
     switch (this.Game.Turn.Phase)
     {
         case Phase.Spring:
             randomOrders();
             break;
         case Phase.Summer:
             randomRetreat();
             break;
         case Phase.Fall:
             randomOrders();
             break;
         case Phase.Autumn:
             randomRetreat();
             break;
         case Phase.Winter:
             randomBuild();
             break;
         default:
             break;
     }
     SubmitOrders();
 }
Example #30
0
 /// <summary>Processes a LOD message (Load a previously saved game).
 /// The default version responds by rejecting the message.
 /// </summary>
 /// <param name="msg">The LOD message.</param>
 protected virtual void ProcessLOD(TokenMessage msg)
 {
     this.connection.Send(Token.REJ & msg);
 }