Ejemplo n.º 1
0
 public string ToYou(TradeRule st)
 {
     //your
     var m2 = string.Format("{0} ({1}+{2}) m={3}",
         lands[st.GiveLand - 1], st.YourCount, st.GiveCount, st.GiveMoney);
     return m2;
 }
Ejemplo n.º 2
0
 public string ToMe(TradeRule st)
 {
     //my
     var m1 = string.Format("{0} ({1}+{2}) m={3}",
         lands[st.GetLand - 1], st.MyCount, st.GetCount, st.GetMoney);
     return m1;
 }
Ejemplo n.º 3
0
        //example
        //from player: from have 2_russia + another 1_russia
        //to: to have 2_france + 1_france
        private static Trade CheckOnPlayersCells(Game g, TradeRule trd, int my)
        {
            var pfrom = g.GetPlayer(my);

            //from_pl get cells
            var wantedCellsGroupedByUser = g.Map.CellsByGroup(trd.GetLand)
                .Where(x => x.Owner.HasValue && x.Owner != my)
                .GroupBy(x => x.Owner.Value);

            if (!wantedCellsGroupedByUser.Any()) return null;

            //process for each player
            foreach (var wantedByUser in wantedCellsGroupedByUser)
            {
                if (wantedByUser.Count() != trd.GetCount) continue;

                var pto = g.GetPlayer(wantedByUser.First().Owner.Value);

                // i have
                var _myCells = g.Map.CellsByUserByGroup(my, trd.GetLand).Count() == trd.MyCount;

                //you have
                var _yourCells = g.Map.CellsByUserByGroup(pto.Id, trd.GiveLand).Count() == trd.YourCount;

                //i give to you
                var giveCells = g.Map.CellsByUserByGroup(my, trd.GiveLand);

                //money factor
                var money1 = g.GetPlayerAssets(my, false);
                var money2 = g.GetPlayerAssets(pto.Id, false);

                var mfac = (money1 / (double)money2) >= trd.MoneyFactor;

                if (giveCells.Count() == trd.GiveCount && _myCells && _yourCells && mfac)
                {
                    //g.trState = new TradeState
                    return new Trade
                    {
                        from = g.GetPlayer(my),
                        give_cells = giveCells.Select(x => x.Id).ToArray(),
                        giveMoney = trd.GiveMoney,
                        //fromMoney = string.IsNullOrEmpty(from_money) ? 0 : Int32.Parse(from_money),
                        to = pto,
                        get_cells = wantedByUser.Select(x => x.Id).ToArray(),
                        getMoney = trd.GetMoney,
                        ExchId = trd.Id,
                    };
                }
            }
            return null;
        }
Ejemplo n.º 4
0
        protected void ButtonLearn_Click(object sender, EventArgs e)
        {
            var st = new TradeRule();

            foreach (GridViewRow row in GridView1.Rows)
            {
                var ind = row.RowIndex;
                for (int i = 1; i <= 4; i++)
                {
                    var ch = (CheckBox)row.FindControl("ch" + i);
                    if (ch.Checked)
                    {
                        if (i == 1 || i == 2)
                            st.MyCount = i;

                        if (i == 3 || i == 4)
                        {
                            st.GetLand = ind + 1;
                            st.GetCount = i - 2;
                        }
                    }
                }
            }

            var res2 = "";
            foreach (GridViewRow row in GridView2.Rows)
            {
                var ind = row.RowIndex;
                for (int i = 1; i <= 4; i++)
                {
                    var ch = (CheckBox)row.FindControl("ch" + i);
                    if (ch.Checked)
                    {
                        if (i == 1 || i == 2)
                            st.YourCount = i;

                        if (i == 3 || i == 4)
                        {
                            st.GiveLand = ind + 1;
                            st.GiveCount = i - 2;
                        }
                    }
                }

            }
            //get money
            var m1 = TextBoxMyMoney.Text;
            if (!string.IsNullOrEmpty(m1)) st.GetMoney = Convert.ToInt32(m1);
            //give money
            var m2 = TextBoxYourMoney.Text;
            if (!string.IsNullOrEmpty(m2)) st.GiveMoney = Convert.ToInt32(m2);

            //money factor(for ex, if i have 3M, he has 2M, factor = 1.5)
            var m3 = tbMoneyFactor.Text;
            if (!string.IsNullOrEmpty(m3)) st.MoneyFactor = Convert.ToDouble(m3);

            if (!IsValid(st))
            {
                LabelRes.Text = "exchange isn't valid";
            }
            else
            {
                if (!ExistNewState(st))
                {
                    ManualLogicHelper.SaveToFile(st, ServerManageFolder);
                    LabelRes.Text = "new exch created successfully";
                }
                else
                    LabelRes.Text = "exchange already exist";
            }
        }
Ejemplo n.º 5
0
 private bool IsValid(TradeRule st)
 {
     if (st.GetLand == st.GiveLand) return false;
     return true;
 }
Ejemplo n.º 6
0
 private bool ExistNewState(TradeRule newSt)
 {
     if (newSt.GetLand == 0 || newSt.GiveLand == 0) return true;
     foreach (var st in ManualLogicHelper.LoadExchanges(ServerManageFolder))
     {
         var q = (st.GetLand == newSt.GetLand
             && st.GetCount == newSt.GetCount
             && st.MyCount == newSt.MyCount
             && st.GetMoney >= newSt.GetMoney
             && st.GiveLand == newSt.GiveLand
              && st.GiveCount == newSt.GiveCount
               && st.YourCount == newSt.YourCount
                && st.GiveMoney <= newSt.GiveMoney
                && st.MoneyFactor >= newSt.MoneyFactor
             );
         if (q) return true;
     }
     return false;
 }
Ejemplo n.º 7
0
        private static TradeRule ReverseRule(TradeRule ex)
        {
            var reversed = new TradeRule();

            reversed.Id = ex.Id;

            reversed.MyCount = ex.YourCount;
            reversed.YourCount = ex.MyCount;

            reversed.GetCount = ex.GiveCount;
            reversed.GetLand = ex.GiveLand;
            reversed.GetMoney = ex.GiveMoney;

            reversed.GiveCount = ex.GetCount;
            reversed.GiveLand = ex.GetLand;
            reversed.GiveMoney = ex.GetMoney;

            return reversed;
        }
Ejemplo n.º 8
0
        public static void SaveToFile(TradeRule st, string path, string fileName = "exchange.txt")
        {
            //var p = Server.MapPath("~/manage");
            var fpath = Path.Combine(path, fileName);
            string res = "";
            res = string.Format("{0}-{1}-{2};{3}-{4}-{5};{6}-{7};{8};d={9}",
                st.GetLand, st.GetCount, st.MyCount,
                st.GiveLand, st.GiveCount, st.YourCount,
                st.GetMoney, st.GiveMoney,
                (st.MoneyFactor == 0 ? 1 : st.MoneyFactor),
                (st.Disabled ? 1 : 0)
                );

            using (var sw = File.AppendText(fpath))
            {
                sw.WriteLine(res);
            }
        }
Ejemplo n.º 9
0
        public static IEnumerable<TradeRule> LoadExchanges(string p, string fileName = "exchange.txt")
        {
            //var p = Server.MapPath("~/manage");
            var fpath = Path.Combine(p, fileName);
            int i = 0;
            foreach (var str in File.ReadAllLines(fpath))
            {

                if (str.StartsWith("///") || string.IsNullOrWhiteSpace(str)) continue;

                var arr = str.Split(';');
                var count = arr.Count();

                var st = new TradeRule();
                st.Id = i++;
                if (count > 1)
                {

                    //to me
                    var mm1 = arr[0].Split('-');
                    st.GetLand = Int32.Parse(mm1[0]);
                    st.GetCount = Int32.Parse(mm1[1]);
                    st.MyCount = Int32.Parse(mm1[2]);

                    //to you
                    var mm2 = arr[1].Split('-');
                    st.GiveLand = Int32.Parse(mm2[0]);
                    st.GiveCount = Int32.Parse(mm2[1]);
                    st.YourCount = Int32.Parse(mm2[2]);
                }
                //parse money
                if (count > 2)
                {
                    var mm3 = arr[2].Split('-');
                    st.GetMoney = Int32.Parse(mm3[0]);
                    st.GiveMoney = Int32.Parse(mm3[1]);
                }
                //parse money factor
                st.MoneyFactor = (count > 3) ? Double.Parse(arr[3]) : 1;

                if (count > 4)
                    st.Disabled = arr[4] == "d=1";

                if (count > 4 && !st.Disabled)
                    yield return st;

            }
        }