Example #1
0
        private TimeSpan CheckDurAvail(string dur)
        {
            TimeSpan ts = UpCall.TimeSpanParse(dur);

            if (ts.TotalSeconds <= 3600 * 3)
            {
                return(ts);
            }
            else
            {
                return(TimeSpan.MinValue);
            }
        }
Example #2
0
        /// <summary>
        /// Dispatch a transportation of a given amount of resource from one village to a given destiantion
        /// </summary>
        /// <param name="VillageID">Unique ID of the departure village</param>
        /// <param name="Amount">Amounts of resources to transport</param>
        /// <param name="TargetPos">Position of the arrival village</param>
        /// <returns>Error return minus number. Succeed return single way transfer time cost.</returns>
        public int doTransfer(TResAmount Amount, TPoint TargetPos)
        {
            string result = UpCall.PageQuery(VillageID, "build.php?gid=17&t=5");

            if (result == null)
            {
                return(-1);
            }
            var CV = UpCall.TD.Villages[VillageID];
            Dictionary <string, string> PostData = new Dictionary <string, string>();
            var m = Regex.Match(result, "name=\"id\" id=\"id\" value=\"(\\d+)\"", RegexOptions.Singleline);

            if (!m.Success)
            {
                return(-1);
            }
            PostData["cmd"] = "prepareMarketplace";
            PostData["id"]  = m.Groups[1].Value;

            if (result.Contains("Popup(2,5)") && Amount.TotalAmount > CV.Market.SingleCarry * CV.Market.ActiveMerchant)
            {
                resumeTime = DateTime.Now.AddSeconds(rand.Next(200 + retrycount * 20, 300 + retrycount * 30));
                UpCall.DebugLog("0:00:0?, Will retry...", DebugLevel.I);
                return(-2);
            }
            if (Amount.TotalAmount > CV.Market.SingleCarry * CV.Market.ActiveMerchant)
            {
                retrycount++;
                if (retrycount > 5)
                {
                    UpCall.DebugLog(string.Format("Transfer cannot go on: MCarry({0}) * MCount({1}) < Amount({2})", CV.Market.SingleCarry, CV.Market.ActiveMerchant, Amount.TotalAmount), DebugLevel.W);
                    return(-2);                    // Beyond transfer ability
                }
                else
                {
                    UpCall.DebugLog("Error on 'ActiveMerchant'! Will retry...", DebugLevel.I);
                    resumeTime = DateTime.Now.AddSeconds(rand.Next(500 + retrycount * 20, 800 + retrycount * 30));
                    CV.Market.ActiveMerchant = Math.Min(Amount.TotalAmount / CV.Market.SingleCarry + 1, CV.Market.MaxMerchant);
                    return(-2);
                }
            }
            retrycount = 0;
            for (int i = 0; i < 4; i++)
            {
                PostData["r" + (i + 1).ToString()] = Amount.Resources[i].ToString();
            }

            //cmd=prepareMarketplace&r1=3000&r2=3000&r3=3000&r4=&dname=&x=50&y=105&id=27&t=5&x2=1
            PostData["dname"] = "";
            PostData["x"]     = TargetPos.X.ToString();
            PostData["y"]     = TargetPos.Y.ToString();
            PostData["t"]     = "5";
            PostData["x2"]    = "1";

            result = UpCall.PageQuery(VillageID, "ajax.php?cmd=prepareMarketplace", PostData);

            //cmd=prepareMarketplace&t=5&id=27&a=64846&sz=2788&kid=236746&c=aaa02a&x2=1&r1=3000&r2=3000&r3=3000&r4=
            if (result == null)
            {
                return(-1);
            }
            PostData.Clear();
            result          = result.Replace("\\\"", "\"");
            result          = result.Replace("\\/", "/");
            PostData["cmd"] = "prepareMarketplace";
            MatchCollection matches = Regex.Matches(result, "name=\"(\\w+)\" id=\"\\w+\" value=\"(\\w+)\"");

            for (int i = 0; i < matches.Count; i++)
            {
                PostData[matches[i].Groups[1].Value] = matches[i].Groups[2].Value;
            }
            for (int i = 0; i < 4; i++)
            {
                PostData["r" + (i + 1).ToString()] = Amount.Resources[i].ToString();
            }
            m = Regex.Match(result, "<td>([0-9:]{6,})</td>");
            if (!m.Success)
            {
                return(-1);                // Parse error!
            }
            int TimeCost = Convert.ToInt32(UpCall.TimeSpanParse(m.Groups[1].Value).TotalSeconds);

            if (UpCall.TD.MarketSpeed != 0)
            {
                // calc market speed
                var distance = CV.Coord * TargetPos;
                UpCall.TD.Dirty       = true;
                UpCall.TD.MarketSpeed = Convert.ToInt32(Math.Round(distance * 3600 / TimeCost));
            }

            UpCall.PageQuery(VillageID, "ajax.php?cmd=prepareMarketplace", PostData);
            UpCall.BuildCount();

            // write data into target village if it's my village.
            foreach (var x in UpCall.TD.Villages)
            {
                if (x.Value == CV)
                {
                    continue;
                }
                if (x.Value.Coord == TargetPos)
                {
                    if (x.Value.isBuildingInitialized == 2)
                    {
                        x.Value.Market.MarketInfo.Add(new TMInfo()
                        {
                            CarryAmount = Amount.Clone(),
                            Coord       = CV.Coord,
                            FinishTime  = DateTime.Now.AddSeconds(TimeCost),
                            MType       = TMType.OtherCome,
                            VillageName = CV.Name
                        });
                    }
                    break;
                }
            }
            UpCall.DebugLog(string.Format("Transfer {0}({1}) => {2} {3}", CV.Coord.ToString(), VillageID, TargetPos.ToString(),
                                          Amount.ToString()), DebugLevel.I);
            return(TimeCost);
        }