Ejemplo n.º 1
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);
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 3
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="Target">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 Target)
        {
            string result = UpCall.PageQuery(VillageID, "build.php?gid=17");
            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\" value=\"(\\d+)\"");
            if(!m.Success)
                return -1; // Parse error!
            PostData["id"] = m.Groups[1].Value;
            /*
            m = Regex.Match(result, "var haendler = (\\d+);");
            if (!m.Success)
                return -1;
            var MCount = Convert.ToInt32(m.Groups[1].Value);
            m = Regex.Match(result, "var carry = (\\d+);");
            if (!m.Success)
                return -1;
            var MCarry = Convert.ToInt32(m.Groups[1].Value);
            */

            //if (Amount.TotalAmount > MCarry * MCount)
            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.W);
                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 -5; // Beyond transfer ability
                }
                else
                {
                    UpCall.DebugLog("Error on 'ActiveMerchant'! Will retry...", DebugLevel.W);
                    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();
            }

            PostData["dname"] = "";
            PostData["x"] = Target.X.ToString();
            PostData["y"] = Target.Y.ToString();
            PostData["s1"] = "ok";

            result = UpCall.PageQuery(VillageID, "build.php", PostData);
            if(result == null)
                return -1;

            m = Regex.Match(result, "<p class=\"error\">([^<]*?)</p>");
            if (m.Success)
            {
                refreshcount++;
                if (refreshcount > 2)
                {
                    UpCall.DebugLog("Error:" + m.Groups[1].Value, DebugLevel.E);
                    return -5;
                }
                else
                {
                    UpCall.DebugLog("Data isn't refreshed?! Will retry...", DebugLevel.W);
                    return -2;
                }
            }
            refreshcount = 0;

            m = Regex.Match(result, "name=\"sz\" value=\"(\\d+)\"");
            if(!m.Success)
                return -1; // Parse error!
            PostData["sz"] = m.Groups[1].Value;
            PostData["kid"] = Target.Z.ToString();
            PostData["a"] = VillageID.ToString();
            m = Regex.Match(result, "name=\"c\" value=\"(.*?)\"");
            if (m.Success)
                PostData["c"] = m.Groups[1].Value;

            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.MarketSpeed = Convert.ToInt32(Math.Round(distance * 3600 / TimeCost));
                UpCall.TD.Dirty = true;
                if (UpCall.TD.MarketSpeed == 0)
                {
                    int StdSpeed = 24;
                    if (UpCall.TD.Tribe != 0)
                    {
                        StdSpeed = Buildings.BaseSpeed[UpCall.TD.Tribe][10];
                    }
                    //判断如果是speed服则商人速度×2
                    if (UpCall.TD.Server.Substring(0, 5) == "speed")
                        StdSpeed *= 2;
                    UpCall.TD.MarketSpeed = StdSpeed;
                    UpCall.TD.Dirty = true;
                }
            }

            UpCall.JustTransferredData = Amount;
            result = UpCall.PageQuery(VillageID, "build.php", PostData);
            if (result == null)
                return -1;
            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 == Target)
                {
                    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, Target.ToString(),
                Amount.ToString()), DebugLevel.I);
            return TimeCost;
        }