Exemplo n.º 1
0
        public Travian(Data TravianData, Dictionary<string, string> Options)
        {
            TD = TravianData;
            //DB.Instance.Initialize(TravianData.Server);
            //DB.Instance.Initialize(TravianData.key);
            this.pageQuerier = this;

            int StdSpeed = 24;
            if (TD.Tribe != 0)
            {
                StdSpeed = Buildings.BaseSpeed[TD.Tribe][10];
            }
            //if(TD.Tribe == 1)
            //    StdSpeed = 16;
            //else if(TD.Tribe == 2)
            //    StdSpeed = 12;
            //else
            //    StdSpeed = 24;
            int MarketSpeedX = 1;

            TD.MarketSpeed = StdSpeed * MarketSpeedX;

            LoadOptions(Options);
            TD.Dirty = true;

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Thread.GetDomain().UnhandledException += new UnhandledExceptionEventHandler(UnhandledException);
        }
Exemplo n.º 2
0
 public void Snapshot(Data data)
 {
     var fn = Filename(data.key);
     string jsondata = JsonMapper.ToJson(data);
     File.WriteAllText(fn, jsondata);
     data.Dirty = false;
 }
Exemplo n.º 3
0
 public NewBuilding(Data TD, int VillageID)
 {
     InitializeComponent();
     this.dl = DisplayLang.Instance;
     for(int i = 5; i <= dl.GidLang.Count; i++)
     {
         if(i >= 31 && i <= 33 || i == 16)
             continue;
         int possible = Travian.testPossibleNewNow(TD.Tribe, TD.Villages, TD.Villages[VillageID], i, 0);
         if(possible > 0)
             comboBox1.Items.Add(i + ". " + dl.GetGidLang(i));
         else if(possible == 0)
             comboBox1.Items.Add(i + ". " + dl.GetGidLang(i) + " <--");
     }
     var Buildings = TD.Villages[VillageID].Buildings;
     for(int i = 19; i < 39; i++)
         if(!Buildings.ContainsKey(i))
             comboBox2.Items.Add(i);
     if(comboBox2.Items.Count != 0)
         comboBox2.SelectedIndex = 0;
     else
         button1.Enabled = false;
 }
Exemplo n.º 4
0
        /// <summary>
        /// Distribute a total amount based on the distribution ratio
        /// </summary>
        public TResAmount RedistributeResources(Data travianData, int villageID, int sum)
        {
            if(!travianData.Villages.ContainsKey(villageID))
            {
                return null;
            }

            TVillage village = travianData.Villages[villageID];
            if(village.isBuildingInitialized != 2)
            {
                return null;
            }

            TResAmount target = new TResAmount(this.Distribution);
            TResAmount distribution = new TResAmount();

            // Allocate by proportion
            int residual = sum - distribution.TotalAmount;
            while(target.TotalAmount > 0 && residual > 10)
            {
                double[] proportions = target.Proportions;
                for(int i = 0; i < proportions.Length; i++)
                {
                    distribution.Resources[i] += (int)(residual * proportions[i]);
                    if(distribution.Resources[i] > village.Resource[i].Capacity)
                    {
                        distribution.Resources[i] = village.Resource[i].Capacity;
                        target.Resources[i] = 0;
                    }
                }

                residual = sum - distribution.TotalAmount;
            }

            // Don't trade if residual exceeds 50% threshold
            bool tooManyResidual = true;
            for(int i = 0; i < this.Threshold.Resources.Length; i++)
            {
                if(residual < this.Threshold.Resources[i] * (100 - this.MinTradeRatio) / 100)
                {
                    tooManyResidual = false;
                    break;
                }
            }

            if(tooManyResidual)
            {
                return null;
            }

            // Allocate by capacity
            for(int i = 0; i < distribution.Resources.Length; i++)
            {
                distribution.Resources[i] = Math.Min(
                    distribution.Resources[i] + residual,
                    village.Resource[i].Capacity);
                residual = sum - distribution.TotalAmount;
            }

            return distribution;
        }
Exemplo n.º 5
0
        private TResAmount NeedCrop(Data travianData)
        {
            return null;
            if(NoCrop)
                return null;
            if(Distribution == ResourceDistributionType.Uniform ||
                Distribution == ResourceDistributionType.BalanceSource ||
                Distribution == ResourceDistributionType.BalanceSourceTime)
                return null;
            if(Distribution == ResourceDistributionType.None &&
                ResourceAmount.Resources[3] <= 0)
                return null;
            if(travianData == null ||
            !travianData.Villages.ContainsKey(TargetVillageID) ||
            !travianData.Villages.ContainsKey(VillageID))
                return null;

            TVillage source = travianData.Villages[VillageID];
            TVillage destination = travianData.Villages[TargetVillageID];
            if(destination.isBuildingInitialized != 2)
                return null;

            if(destination.Resource[3].Produce >= 0)
            {
                if(UpCall != null)
                    UpCall.DebugLog("Target Produce >= 0, no need crop rule.", DebugLevel.I);
                return null;
            }

            int speed = travianData.MarketSpeed == 0 ? 24 : travianData.MarketSpeed;
            int timecost = Convert.ToInt32(source.Coord * destination.Coord * 3600 / speed) + 30;

            int cropcap = destination.Resource[3].CurrAmount + timecost * destination.Resource[3].Produce / 3600;

            foreach(TMInfo transfer in destination.Market.MarketInfo)
            {
                if(transfer.MType == TMType.OtherCome && transfer.FinishTime < DateTime.Now.AddSeconds(timecost))
                {
                    cropcap += transfer.CarryAmount.Resources[3];
                }
            }

            if(cropcap <= 0)
            {
                return new TResAmount(0, 0, 0, ResourceAmount.TotalAmount);
            }
            if(UpCall != null)
                UpCall.DebugLog("Target village don't need crop, no need crop rule.", DebugLevel.I);

            return null;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Estimate the target village capacity when transportantion arrives, based on 
        /// its current resource amount, production rate, distance, and merchant speed.
        /// </summary>
        /// <param name="travianData">Contains game info</param>
        /// <param name="VillageID">Where the merchant starts, for computing distance</param>
        /// <returns>Estimated capacity</returns>
        private TResAmount GetTargetCapacity(Data travianData, int VillageID)
        {
            if(travianData != null &&
                travianData.Villages.ContainsKey(this.TargetVillageID) &&
                travianData.Villages.ContainsKey(VillageID))
            {
                TVillage source = travianData.Villages[VillageID];
                TVillage destination = travianData.Villages[this.TargetVillageID];
                if(destination.isBuildingInitialized == 2)
                {
                    TResource[] VR = destination.Resource;
                    int[] resources = new int[VR.Length];
                    int speed = travianData.MarketSpeed == 0 ? 24 : travianData.MarketSpeed;
                    double timecost = source.Coord * destination.Coord / speed;
                    for(int i = 0; i < resources.Length; i++)
                    {
                        resources[i] = VR[i].Capacity * this.LimitRate / 100;
                        if(destination.Market.UpperLimit != null)
                        {
                            resources[i] = destination.Market.UpperLimit.Resources[i];
                        }

                        resources[i] -= VR[i].CurrAmount + (int)(VR[i].Produce * timecost);
                    }

                    TResAmount capacity = new TResAmount(resources);
                    foreach(TMInfo transfer in destination.Market.MarketInfo)
                    {
                        if(transfer.MType == TMType.OtherCome)
                        {
                            capacity -= transfer.CarryAmount;
                        }
                    }

                    capacity.NoNegative();
                    return capacity;
                }
            }

            return null;
        }
Exemplo n.º 7
0
        private void BalanceSourceTime(Data travianData, int VillageID)
        {
            // TODO: FIXME
            int total = ResourceAmount.TotalAmount;
            int slots = NoCrop ? 3 : 4;
            TResAmount targetAmount = new TResAmount(0, 0, 0, 0);
            if(travianData != null &&
                travianData.Villages.ContainsKey(VillageID) &&
                travianData.Villages[VillageID].isBuildingInitialized == 2)
            {
                TVillage TV = travianData.Villages[VillageID];
                int[] fulltime = new int[slots];
                int maxtime = 0, total2 = 0, totalproduce = 0, total3 = 0;

                for(int i = 0; i < fulltime.Length; i++)
                {
                    fulltime[i] = Convert.ToInt32(TV.Resource[i].LeftTime.TotalSeconds);

                    maxtime = Math.Max(maxtime, fulltime[i]);
                }

                for(int i = 0; i < fulltime.Length; i++)
                {
                    targetAmount.Resources[i] = Convert.ToInt32(Convert.ToInt64(TV.Resource[i].Produce) * (maxtime - fulltime[i]) / 3600);

                    total2 += targetAmount.Resources[i];
                    totalproduce += TV.Resource[i].Produce;
                }
                if(total2 > total)
                {
                    for(int i = 0; i < fulltime.Length; i++)
                    {
                        targetAmount.Resources[i] = Convert.ToInt32(targetAmount.Resources[i] * Convert.ToDouble(total) / total2);
                        total3 += targetAmount.Resources[i];
                    }
                }
                else
                {

                    double seconds = Convert.ToDouble(total - total2) / totalproduce;
                    for(int i = 0; i < fulltime.Length; i++)
                    {
                        targetAmount.Resources[i] += Convert.ToInt32(TV.Resource[i].Produce * seconds);
                        total3 += targetAmount.Resources[i];
                    }
                }
                targetAmount.Resources[slots - 1] += total - total3;
                targetAmount.NoNegative();

            }
            ResourceAmount = targetAmount;
        }
Exemplo n.º 8
0
        private void BalanceSourceResource(Data travianData, int VillageID)
        {
            TResAmount targetAmount = new TResAmount(0, 0, 0, 0);
            if(travianData != null &&
                travianData.Villages.ContainsKey(VillageID) &&
                travianData.Villages[VillageID].isBuildingInitialized == 2)
            {
                TVillage village = travianData.Villages[VillageID];
                for(int i = 0; i < targetAmount.Resources.Length; i++)
                {
                    targetAmount.Resources[i] = village.Resource[i].CurrAmount;
                    if(village.Market.LowerLimit != null)
                    {
                        targetAmount.Resources[i] -= village.Market.LowerLimit.Resources[i];
                    }
                }

                targetAmount.NoNegative();
            }

            this.DoBalance(targetAmount);
        }
Exemplo n.º 9
0
        private void BalanceDestinationResource(Data travianData, int VillageID)
        {
            TResAmount targetAmount = this.GetTargetCapacity(travianData, VillageID);
            if(targetAmount == null)
            {
                targetAmount = new TResAmount(0, 0, 0, 0);
            }

            this.DoBalance(targetAmount);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Test if the target village have enought storage capacity for the to-be-transfered resources
        /// </summary>
        /// <param name="travianData">User game info, including target village distance and storage capacity</param>
        /// <returns>True if the transportation will overflow the target village</returns>
        public bool ExceedTargetCapacity(Data travianData)
        {
            if(ForceGo)
                return false;
            TResAmount targetCapacity = this.GetTargetCapacity(travianData, VillageID);
            if(targetCapacity == null)
            {
                if(this.Distribution == ResourceDistributionType.BalanceTarget)
                {
                    return true;
                }
            }
            else
            {
                for(int i = 0; i < targetCapacity.Resources.Length; i++)
                {
                    if(this.ResourceAmount.Resources[i] > targetCapacity.Resources[i])
                    {
                        return true;
                    }
                }
            }

            return false;
        }
Exemplo n.º 11
0
        /// <summary>
        /// Distribute transported resource amount 
        /// </summary>
        /// <param name="travianData">Game info of the current user</param>
        public void CalculateResourceAmount(Data travianData, int VillageID)
        {
            switch(this.Distribution)
            {
                case ResourceDistributionType.None:
                    break;
                case ResourceDistributionType.Uniform:
                    this.EvenlyDistibuteResource();
                    break;
                case ResourceDistributionType.BalanceSource:
                    this.BalanceSourceResource(travianData, VillageID);
                    break;
                case ResourceDistributionType.BalanceTarget:
                    this.BalanceDestinationResource(travianData, VillageID);
                    break;
                case ResourceDistributionType.BalanceSourceTime:
                    this.BalanceSourceTime(travianData, VillageID);
                    break;

            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Distribute a total amount based on the distribution ratio
        /// </summary>
        public TResAmount RedistributeResources(Data travianData, int villageID, int sum)
        {
            if (!travianData.Villages.ContainsKey(villageID))
            {
                return(null);
            }

            TVillage village = travianData.Villages[villageID];

            if (village.isBuildingInitialized != 2)
            {
                return(null);
            }

            TResAmount target       = new TResAmount(this.Distribution);
            TResAmount distribution = new TResAmount();

            // Allocate by proportion
            int residual = sum - distribution.TotalAmount;

            while (target.TotalAmount > 0 && residual > 10)
            {
                double[] proportions = target.Proportions;
                for (int i = 0; i < proportions.Length; i++)
                {
                    distribution.Resources[i] += (int)(residual * proportions[i]);
                    if (distribution.Resources[i] > village.Resource[i].Capacity)
                    {
                        distribution.Resources[i] = village.Resource[i].Capacity;
                        target.Resources[i]       = 0;
                    }
                }

                residual = sum - distribution.TotalAmount;
            }

            // Don't trade if residual exceeds 50% threshold
            bool tooManyResidual = true;

            for (int i = 0; i < this.Threshold.Resources.Length; i++)
            {
                if (residual < this.Threshold.Resources[i] * (100 - this.MinTradeRatio) / 100)
                {
                    tooManyResidual = false;
                    break;
                }
            }

            if (tooManyResidual)
            {
                return(null);
            }

            // Allocate by capacity
            for (int i = 0; i < distribution.Resources.Length; i++)
            {
                distribution.Resources[i] = Math.Min(
                    distribution.Resources[i] + residual,
                    village.Resource[i].Capacity);
                residual = sum - distribution.TotalAmount;
            }

            return(distribution);
        }
Exemplo n.º 13
0
        public void TestInitialize()
        {
            this.travianData = new Data()
            {
                Server = "none",
                Username = "******"
            };

            Dictionary<string, string> Options = new Dictionary<string, string>();
            this.travian = new Travian(travianData, Options, "cn");

            this.villageID = 1;
            this.village = new TVillage();
            this.travianData.Villages[villageID] = village;

            this.village.Name = "Home";
            this.village.isTroopInitialized = 2;
            this.village.Troop = this.troop = new TTroop();
            int[] troopsAtHome = new int[11];
            troopsAtHome[0] = 8;
            TTInfo troopAtHome = new TTInfo
            {
                Troops = troopsAtHome,
                Owner = this.village.Name,
                TroopType = TTroopType.InVillage,
                VillageName = "Own troops"
            };
            this.troop.Troops.Add(troopAtHome);

            int[] troopsOnTheRood = new int[11];
            troopsOnTheRood[0] = 5;
            TTInfo troopOnTheRoad = new TTInfo
            {
                Troops = troopsAtHome,
                TroopType = TTroopType.MyReturnWay,
                FinishTime = DateTime.Now.AddSeconds(100),
                VillageName = "Raid on abc Village"
            };
            this.troop.Troops.Add(troopOnTheRoad);

            this.troops = new int[11];
            this.troops[0] = 5;

            this.targets = new List<TPoint>();
            this.targets.Add(new TPoint(1, 2));
            this.targets.Add(new TPoint(0, 0));

            this.target = new RaidQueue()
            {
                UpCall = this.travian,
                VillageID = this.villageID,
                Troops = this.troops,
                Targets = this.targets,
                RaidType = RaidType.AttackRaid,
            };

            this.pageQuerier = new MockPageQuerier(this.villageID);
            this.travian.pageQuerier = pageQuerier;
        }
Exemplo n.º 14
0
        public void Login()
        {
            if (tr != null)
                tr = null;
            TravianData = DB.Instance.RestoreData(LoginInfo.GetKey());
            if (TravianData == null)
                TravianData = new Data();
            TravianData.Username = LoginInfo.Username;
            TravianData.Password = LoginInfo.Password;
            TravianData.Tribe = LoginInfo.Tribe;
            TravianData.Server = LoginInfo.Server;
            if (!string.IsNullOrEmpty(LoginInfo.Proxy))
            {
                TravianData.Proxy = new WebProxy(LoginInfo.Proxy);
            }
            //if (MainForm.Options.ContainsKey("proxy"))
            //{
            //    string proxy = MainForm.Options["proxy"];
            //    TravianData.Proxy = new WebProxy(proxy);
            //}
            tr = DB.Instance.RestoreTravian(LoginInfo.Server);
            if (tr == null)
                tr = new Travian(TravianData, MainForm.Options, LoginInfo.ServerLang);
            else
            {
                tr.LoadRegexLang(LoginInfo.ServerLang);
                tr.TD = TravianData;
                tr.LoadOptions(MainForm.Options);
            }
            foreach (var v in TravianData.Villages)
            {
                v.Value.UpCall = tr;
                foreach (var q in v.Value.Queue)
                    q.UpCall = tr;
            }
            dl = new DisplayLang(LoginInfo.Language);
            DisplayLang.Instance = dl;
            tr.StatusUpdate += new EventHandler<Travian.StatusChanged>(tr_StatusUpdate);
            tr.OnError += new EventHandler<LogArgs>(tr_OnError);

            m_villagelist.listViewVillage.Items.Clear();
            m_buildinglist.listViewBuilding.Items.Clear();
            tr.CachedFetchVillages();
            UpTP.Text = string.Format("{0} @ {1}", LoginInfo.Username, LoginInfo.Server.Replace("travian.", ""));
        }
Exemplo n.º 15
0
        public void Login()
        {
            if (tr != null)
                tr = null;
            TravianData = DB.Instance.RestoreData(LoginInfo.GetKey());
            if (TravianData == null)
                TravianData = new Data();
            TravianData.Username = LoginInfo.Username;
            TravianData.Password = LoginInfo.Password;
            if (TravianData.Tribe == 0)
                TravianData.Tribe = LoginInfo.Tribe;
            TravianData.Server = LoginInfo.Server;
            TravianData.GetOrPostTimesThreshold = LoginInfo.GetOrPostTimesThreshold;
            TravianData.GetOrPostDelaySeconds = LoginInfo.GetOrPostDelaySeconds;
            if (MainForm.Options.ContainsKey("proxy"))
            {
                System.Net.ServicePointManager.Expect100Continue = false;
                string proxy = MainForm.Options["proxy"];
                if (proxy != "IE")
                {
                    TravianData.Proxy = new WebProxy(proxy);
                }
                else
                {
                    TravianData.Proxy = WebProxy.GetDefaultProxy();
                    TravianData.Proxy.UseDefaultCredentials = true;
                }
            }
            tr = DB.Instance.RestoreTravian(LoginInfo.Server);
            if (tr == null)
                tr = new Travian(TravianData, MainForm.Options);
            else
            {
                tr.TD = TravianData;
                tr.LoadOptions(MainForm.Options);
            }
            foreach (var v in TravianData.Villages)
            {
                v.Value.UpCall = tr;
                foreach (var q in v.Value.Queue)
                    q.UpCall = tr;
            }
            dl = new DisplayLang(LoginInfo.Language);
            DisplayLang.Instance = dl;
            tr.StatusUpdate += new EventHandler<Travian.StatusChanged>(tr_StatusUpdate);
            tr.OnError += new EventHandler<LogArgs>(tr_OnError);
            tr.OnOasisFoundLog += new EventHandler<OasisFoundLogArgs>(tr_OnOasisFoundLog);
            tr.OnAnimalsFoundLog += new EventHandler<AnimalsFoundLogArgs>(tr_OnAnimalsFoundLog);
            tr.OnAnimalsInfoUpdate += new EventHandler<AnimalsInfoArgs>(tr_OnAnimalsInfoUpdate);

            m_villagelist.listViewVillage.Items.Clear();
            m_buildinglist.listViewBuilding.Items.Clear();
            tr.CachedFetchVillages();
            UpTP.Text = string.Format("{0} @ {1}", LoginInfo.Username, LoginInfo.Server.Replace("travian.", ""));
        }