Exemple #1
0
        public void UpdateMarketPrices()
        {
            foreach (var item in FiefdomActions.Transactions)
            {
                using (var db = new FiefContext())
                {
                    var res = db.Market.Where(x => x.Type == item.Key).FirstOrDefault();
                    res.Price += (int)((100 - res.Price) * 0.1);

                    res.Price += item.Value / 10;
                    if (res.Price < 10)
                    {
                        res.Price = 10;
                    }
                    if (res.Price > 1000)
                    {
                        res.Price = 1000;
                    }
                    db.SaveChanges();
                }
            }

            foreach (var item in FiefdomActions.Transactions.ToArray())
            {
                FiefdomActions.Transactions[item.Key] = 0;
            }
        }
Exemple #2
0
        public void UpdateServer(object state)
        {
            if (!GameValues.clientUpdate)
            {
                GameValues.clientUpdate = true;
                using (var db = new FiefContext())
                {
                    var fiefdom = db.Fiefdom.Include("FiefdomPlot").Include("FiefdomResources").ToList();
                    foreach (Fief fief in fiefdom)
                    {
                        FiefdomUpdate.UpdateResources(fief);
                    }
                    var gameState = db.GameState.FirstOrDefault();

                    gameState.Day++;
                    UpdateMarketPrices();
                    if (gameState.Day >= 10)
                    {
                        ProcessVotes();
                        gameState.Day     = 1;
                        gameState.Season += 1;
                    }
                    if (gameState.Season > 4)
                    {
                        gameState.Season = 1;
                        gameState.Year++;
                    }
                    db.SaveChanges();
                    UpdateClients();
                    GameValues.clientUpdate = false;
                }
            }
        }
Exemple #3
0
        public static void BuyResource(string sessionId, string type, int quantity)
        {
            // Add market fluctuations in this method

            using (var db = new FiefContext())
            {
                var fiefdom = db.Fiefdom.Where(f => f.SessionId == sessionId).Include("FiefdomPlot").Include("FiefdomResources").FirstOrDefault();
                var gold    = fiefdom.FiefdomResources.Where(x => x.Type == "Gold").FirstOrDefault();
                var buyType = fiefdom.FiefdomResources.Where(x => x.Type == type).FirstOrDefault();
                var price   = db.Market.Where(w => w.Type == type).FirstOrDefault();
                int canBuy  = gold.Quantity / price.Price;
                if (canBuy < quantity)
                {
                    quantity = canBuy;
                }
                gold.Quantity    -= quantity * price.Price;
                buyType.Quantity += quantity;
                Random rnd  = new Random();
                int    diff = Math.Abs(price.Price - 10);
                if (rnd.Next(1, 1000) % (10 - diff) > 1)
                {
                    price.Price++;
                }
                db.SaveChanges();
            }
        }
Exemple #4
0
        //Data Calls

        public static void CreateNewFiefdom(string name, string sessionId)
        {
            using (var db = new FiefContext())
            {
                Fief fief = new Fief {
                    Name = name, SessionId = sessionId, Title = 0
                };
                fief.FiefdomResources.Add(new FiefdomResources {
                    Type = "Gold", Quantity = 1000
                });
                fief.FiefdomResources.Add(new FiefdomResources {
                    Type = "Wood", Quantity = 10
                });
                fief.FiefdomResources.Add(new FiefdomResources {
                    Type = "Stone", Quantity = 10
                });
                fief.FiefdomResources.Add(new FiefdomResources {
                    Type = "Food", Quantity = 10
                });
                for (int i = 0; i < 10; i++)
                {
                    fief.FiefdomPlot.Add(new FiefdomPlot {
                        Type = "Locked"
                    });
                }
                fief.FiefdomPlot[4].Type = "Empty";
                db.Fiefdom.Add(fief);
                db.SaveChanges();
            }
        }
Exemple #5
0
        public static void NewGameInstance()
        {
            using (var db = new FiefContext())
            {
                if (db.GameState.FirstOrDefault() == null)
                {
                    db.GameState.Add(new GameState {
                        Day = 1, Season = 1, Year = 1
                    });
                }

                if (db.Market.ToList().Count == 0)
                {
                    db.Market.Add(new Market {
                        Type = "Wood", Price = 100
                    });
                    db.Market.Add(new Market {
                        Type = "Stone", Price = 100
                    });
                    db.Market.Add(new Market {
                        Type = "Food", Price = 100
                    });
                }
                db.SaveChanges();
            }
            FiefdomActions.Ballots.Add(FiefdomActions.CreateVote());
            FiefdomActions.Ballots.Add(FiefdomActions.CreateVote());
            FiefdomActions.Ballots.Add(FiefdomActions.CreateVote());
        }
Exemple #6
0
 public static List <Market> GetMarketPrice()
 {
     using (var db = new FiefContext())
     {
         return(db.Market.ToList());
     }
 }
Exemple #7
0
        public static void SellResource(string sessionId, string type, int quantity)
        {
            // Add market fluctuations in this method
            using (var db = new FiefContext())
            {
                var fiefdom  = db.Fiefdom.Where(f => f.SessionId == sessionId).Include("FiefdomPlot").Include("FiefdomResources").FirstOrDefault();
                var gold     = fiefdom.FiefdomResources.Where(x => x.Type == "Gold").FirstOrDefault();
                var sellItem = fiefdom.FiefdomResources.Where(x => x.Type == type).FirstOrDefault();
                var price    = db.Market.Where(w => w.Type == type).FirstOrDefault();

                if (quantity > sellItem.Quantity)
                {
                    quantity = sellItem.Quantity;
                }

                gold.Quantity += quantity * GetMarketSellPrice(sellItem.Type, price.Price);
                //gold.Quantity += quantity * GetMarketSellPrice(sellItem.Type, 100);
                sellItem.Quantity -= quantity;

                Transactions[sellItem.Type] -= quantity;
                //Random rnd = new Random();
                //int diff = Math.Abs(price.Price - 10);
                //if (rnd.Next(1, 1000) % (10 - diff) > 1)
                //{
                //	price.Price--;
                //}
                db.SaveChanges();
            }
        }
Exemple #8
0
        public static void BuyResource(string sessionId, string type, int quantity)
        {
            using (var db = new FiefContext())
            {
                var fiefdom     = db.Fiefdom.Where(f => f.SessionId == sessionId).Include("FiefdomPlot").Include("FiefdomResources").FirstOrDefault();
                var gold        = fiefdom.FiefdomResources.Where(x => x.Type == "Gold").FirstOrDefault();
                var buyItem     = fiefdom.FiefdomResources.Where(x => x.Type == type).FirstOrDefault();
                var marketPrice = db.Market.Where(w => w.Type == type).FirstOrDefault();

                int price = GetMarketBuyPrice(buyItem.Type, marketPrice.Price);
                //int price = GetMarketBuyPrice(buyItem.Type, 100);

                int canBuy = gold.Quantity / price;
                if (canBuy < quantity)
                {
                    quantity = canBuy;
                }
                gold.Quantity    -= quantity * price;
                buyItem.Quantity += quantity;

                Transactions[buyItem.Type] += quantity;
                //Random rnd = new Random();
                //int diff = Math.Abs(price.Price - 10);
                //if (rnd.Next(1, 1000) % (10 - diff) > 1)
                //{
                //	price.Price++;
                //}
                db.SaveChanges();
            }
        }
Exemple #9
0
 public static int GetPrice(string name)
 {
     using (var db = new FiefContext())
     {
         var item = db.Market.Where(t => t.Type == name).FirstOrDefault();
         return(item.Price);
     }
 }
Exemple #10
0
 public static Fief GetFiefdomById(int Id)
 {
     using (var db = new FiefContext())
     {
         var fiefdom = db.Fiefdom.Where(f => f.Id == Id).Include("FiefdomPlot").Include("FiefdomResources").FirstOrDefault();
         return(fiefdom);
     }
 }
Exemple #11
0
 public static Fief GetFiefdomByUserName(string userName)
 {
     using (var db = new FiefContext())
     {
         var fiefdom = db.Fiefdom.Where(f => f.Name == userName).Include("FiefdomPlot").Include("FiefdomResources").FirstOrDefault();
         return(fiefdom);
     }
 }
Exemple #12
0
 public static bool UserExist(string name)
 {
     using (var db = new FiefContext())
     {
         var user = db.Fiefdom.Where(f => f.Name == name).FirstOrDefault();
         return(user != null);
     }
 }
Exemple #13
0
 public static void UserUpdateSessionId(string name, string sessionId)
 {
     using (var db = new FiefContext())
     {
         var user = db.Fiefdom.Where(f => f.Name == name).FirstOrDefault();
         user.SessionId = sessionId;
         db.SaveChanges();
     }
 }
Exemple #14
0
        public static List <Market> GetMarketList()
        {
            using (var db = new FiefContext())
            {
                var items = db.Market.ToList();

                return(items);
            }
        }
Exemple #15
0
 public void UpdateServer(Object source, ElapsedEventArgs e)
 {
     using (var db = new FiefContext())
     {
         var fiefdom = db.Fiefdom.Include("FiefdomPlot").Include("FiefdomResources").ToList();
         foreach (Fief fief in fiefdom)
         {
             FiefdomUpdate.UpdateResources(fief);
         }
         db.SaveChanges();
     }
 }
Exemple #16
0
        public static void BuyTitle(string sessionId)
        {
            using (var db = new FiefContext())
            {
                Fief fiefdom = db.Fiefdom.Where(f => f.SessionId == sessionId).Include("FiefdomPlot").Include("FiefdomResources").FirstOrDefault();
                var  gold    = fiefdom.FiefdomResources.Where(x => x.Type == "Gold").FirstOrDefault();
                switch (fiefdom.Title)
                {
                case 0:
                    if (gold.Quantity >= 1000)
                    {
                        fiefdom.Title  = 1;
                        gold.Quantity -= 1000;
                        UnlockPlot(fiefdom);
                        UnlockPlot(fiefdom);
                    }
                    break;

                case 1:
                    if (gold.Quantity >= 1000)
                    {
                        fiefdom.Title  = 2;
                        gold.Quantity -= 1000;
                        UnlockPlot(fiefdom);
                        UnlockPlot(fiefdom);
                    }
                    break;

                case 2:
                    if (gold.Quantity >= 1000)
                    {
                        fiefdom.Title  = 3;
                        gold.Quantity -= 1000;
                        UnlockPlot(fiefdom);
                        UnlockPlot(fiefdom);
                    }
                    break;

                case 3:
                    if (gold.Quantity >= 1000)
                    {
                        fiefdom.Title  = 4;
                        gold.Quantity -= 1000;
                        UnlockPlot(fiefdom);
                        UnlockPlot(fiefdom);
                        UnlockPlot(fiefdom);
                    }
                    break;
                }
                db.SaveChanges();
            }
        }
Exemple #17
0
        public static void ClearVote()
        {
            using (var db = new FiefContext())
            {
                foreach (Fief fief in db.Fiefdom.ToList())
                {
                    fief.Ballot1 = "vote";
                    fief.Ballot2 = "vote";
                    fief.Ballot3 = "vote";
                }

                db.SaveChanges();
            }
        }
Exemple #18
0
        public static void BuyQuanity(int Id, string name, int quantity)
        {
            using (var db = new FiefContext())
            {
                var fiefdom = db.Fiefdom.Where(f => f.Id == Id).Include("FiefdomPlot").Include("FiefdomResources").FirstOrDefault();
                var gold    = fiefdom.FiefdomResources.Where(t => t.Type == "Gold").FirstOrDefault();
                var item    = fiefdom.FiefdomResources.Where(t => t.Type == name).FirstOrDefault();

                var marketItem = db.Market.Where(t => t.Type == name).FirstOrDefault();
                int cost       = marketItem.Price * quantity;
                //Add market flux here
                item.Quantity += quantity;
                gold.Quantity -= cost;
                db.SaveChanges();
            }
        }
Exemple #19
0
        public async Task RequestFiefdomData()
        {
            GameState     gameState;
            List <Market> baseMarket;
            List <Market> buyPrice  = new List <Market>();
            List <Market> sellPrice = new List <Market>();
            Fief          fief;

            using (var db = new FiefContext())
            {
                gameState  = db.GameState.FirstOrDefault();
                baseMarket = db.Market.ToList();
                fief       = db.Fiefdom.Where(f => f.SessionId == Context.ConnectionId).Include("FiefdomPlot").Include("FiefdomResources").FirstOrDefault();
            }

            for (int i = 0; i < baseMarket.Count; i++)
            {
                buyPrice.Add(new Market
                {
                    Type  = baseMarket[i].Type,
                    Price = FiefdomActions.GetMarketBuyPrice(baseMarket[i].Type, baseMarket[i].Price)
                });
                sellPrice.Add(new Market
                {
                    Type  = baseMarket[i].Type,
                    Price = FiefdomActions.GetMarketSellPrice(baseMarket[i].Type, baseMarket[i].Price)
                });
            }

            if (fief != null)
            {
                await Clients.Caller.SendAsync("RecieveFiefdomData", fief, gameState,
                                               new GameValues
                {
                    Ballots    = FiefdomActions.Ballots,
                    Edicts     = FiefdomActions.Edicts,
                    MarketTax  = FiefdomActions.MarketTax,
                    baseMarket = baseMarket,
                    buyMarket  = buyPrice,
                    sellMarket = sellPrice
                });
            }
            else
            {
                await Clients.Caller.SendAsync("RecieveFiefdomData", null);
            }
        }
Exemple #20
0
        public static void BuildPlot(string sessionId, int id, string type)
        {
            using (var db = new FiefContext())
            {
                List <FiefdomResources> cost = new List <FiefdomResources> {
                    new FiefdomResources {
                        Type = "Gold", Quantity = 500
                    },
                    new FiefdomResources {
                        Type = "Food", Quantity = 5
                    },
                    new FiefdomResources {
                        Type = "Stone", Quantity = 5
                    },
                    new FiefdomResources {
                        Type = "Wood", Quantity = 5
                    }
                };
                Fief fief      = db.Fiefdom.Where(f => f.SessionId == sessionId).Include("FiefdomPlot").Include("FiefdomResources").FirstOrDefault();
                bool canAfford = true;
                foreach (var res in cost)
                {
                    if (fief.FiefdomResources.Where(t => t.Type == res.Type).FirstOrDefault().Quantity < res.Quantity)
                    {
                        canAfford = false;
                    }
                }
                //Subtract resources
                if (canAfford == true)
                {
                    if (fief.FiefdomPlot[id].Type == "Empty")
                    {
                        fief.FiefdomPlot[id].Type = type;
                        foreach (var res in cost)
                        {
                            fief.FiefdomResources.Where(t => t.Type == res.Type).FirstOrDefault().Quantity -= res.Quantity;
                        }
                    }
                }

                db.SaveChanges();
            }
        }
Exemple #21
0
 public static void SubmitVote(string sessionId, int ballot, string vote)
 {
     using (var db = new FiefContext())
     {
         var fiefdom = db.Fiefdom.Where(f => f.SessionId == sessionId).FirstOrDefault();
         if (ballot == 0)
         {
             fiefdom.Ballot1 = vote;
         }
         if (ballot == 1)
         {
             fiefdom.Ballot2 = vote;
         }
         if (ballot == 2)
         {
             fiefdom.Ballot3 = vote;
         }
         db.SaveChanges();
     }
 }
Exemple #22
0
 public static string CreateVote()
 {
     using (var db = new FiefContext())
     {
         Random        rnd         = new Random();
         List <string> keywordList = new List <string> {
             "Tax", "Market"
         };
         string        keyword      = keywordList[rnd.Next(0, keywordList.Count)];
         List <string> resourceList = new List <string> {
             "Wood", "Stone", "Food"
         };
         if (keyword == "Market" || keyword == "Levy")
         {
             keyword += " " + resourceList[rnd.Next(0, 2)] + " " + rnd.Next(5, 15);
         }
         if (keyword == "Tax")
         {
             keyword += " " + rnd.Next(5, 15);
         }
         return(keyword);
     }
 }
Exemple #23
0
        public static List <bool> CountVotes()
        {
            List <bool> votes = new List <bool> {
            };

            using (var db = new FiefContext())
            {
                Random rnd     = new Random();
                var    fiefdom = db.Fiefdom.Include("FiefdomPlot").Include("FiefdomResources").ToList();
                int    ballot1 = 0;
                int    ballot2 = 0;
                int    ballot3 = 0;
                foreach (var fief in fiefdom)
                {
                    int influence = FiefdomActions.GetInfluence(fief);

                    if (fief.Ballot1 == "Fore")
                    {
                        ballot1 += influence;
                    }
                    else if (fief.Ballot1 == "Nay")
                    {
                        ballot1 -= influence;
                    }
                    if (fief.Ballot2 == "Fore")
                    {
                        ballot2 += influence;
                    }
                    else if (fief.Ballot2 == "Nay")
                    {
                        ballot2 -= influence;
                    }
                    if (fief.Ballot3 == "Fore")
                    {
                        ballot3 += influence;
                    }
                    else if (fief.Ballot3 == "Nay")
                    {
                        ballot3 -= influence;
                    }
                }
                if (ballot1 == 0)
                {
                    if (rnd.Next(1, 1000) % 2 == 1)
                    {
                        ballot1++;
                    }
                    else
                    {
                        ballot1--;
                    }
                }
                if (ballot2 == 0)
                {
                    if (rnd.Next(1, 1000) % 2 == 1)
                    {
                        ballot2++;
                    }
                    else
                    {
                        ballot2--;
                    }
                }
                if (ballot3 == 0)
                {
                    if (rnd.Next(1, 1000) % 2 == 1)
                    {
                        ballot3++;
                    }
                    else
                    {
                        ballot3--;
                    }
                }
                votes.Add(ballot1 > 0);
                votes.Add(ballot2 > 0);
                votes.Add(ballot3 > 0);

                db.SaveChanges();
            }
            return(votes);
        }