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(); } }
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(); } }
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()); }
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(); } }
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; } }
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; } } }
//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(); } }
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(); } }
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(); } }
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(); } }
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(); } }
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(); } }
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(); } }
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(); } }
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); }