public static Boolean Taxes(Dictionary <Guid, Dictionary <long, float> > taxes) { try { using (var db = new LiteDatabase(connectionString)) { var collection = db.GetCollection <BankData>("BankData"); foreach (KeyValuePair <Guid, Dictionary <long, float> > key in taxes) { Alliance alliance = AlliancePlugin.GetAlliance(key.Key); long amount = 0; foreach (float f in key.Value.Values) { amount += (long)f; } var bank = collection.FindById(key.Key); if (bank == null) { bank = new BankData { Id = key.Key, balance = amount }; collection.Insert(bank); } else { bank.balance += amount; collection.Update(bank); } foreach (KeyValuePair <long, float> tax in key.Value) { if (EconUtils.getBalance(tax.Key) >= tax.Value) { alliance.DepositTax((long)tax.Value, MySession.Static.Players.TryGetSteamId(tax.Key)); EconUtils.takeMoney(tax.Key, (long)tax.Value); } } AlliancePlugin.SaveAllianceData(alliance); } } } catch (Exception ex) { AlliancePlugin.Log.Error("Error with taxes"); AlliancePlugin.Log.Error(ex); return(false); } return(true); }
public void BuyShip(int key, long BuyerId) { if (items.ContainsKey(key)) { MarketItem item = items[key]; MyIdentity SellerId = AlliancePlugin.TryGetIdentity(item.SellerSteamId.ToString()); if (SellerId != null) { EconUtils.addMoney(SellerId.IdentityId, item.Price); EconUtils.takeMoney(BuyerId, item.Price); } } }
public float upgrade(Int64 Balance, long playerID) { int nextUpgrade = upgradeSlots + 1; if (nextUpgrade > AlliancePlugin.shipyardConfig.MaxShipyardSlots) { return(0); } if (Balance >= upgradePrice()) { EconUtils.takeMoney(playerID, Convert.ToInt64(upgradePrice())); upgradeSlots += 1; return(upgradePrice()); } return(0); }
public void BuyGrid(int number) { if (!list.items.ContainsKey(number)) { Context.Respond("There is no item in the market for that slot number."); confirmations.Remove(Context.Player.IdentityId); return; } MarketItem item = list.items[number]; if (!File.Exists(AlliancePlugin.path + "//ShipMarket//ForSale//" + item.ItemId + ".json")) { Context.Respond("That grid is no longer available for sale."); confirmations.Remove(Context.Player.IdentityId); return; } if (!File.Exists(AlliancePlugin.path + "//ShipMarket//Grids//" + item.ItemId + ".xml")) { Context.Respond("This grid should be available, but its file for the grid doesnt exist."); confirmations.Remove(Context.Player.IdentityId); return; } if (MyGravityProviderSystem.IsPositionInNaturalGravity(Context.Player.GetPosition())) { Context.Respond("You cannot use this command in natural gravity!"); confirmations.Remove(Context.Player.IdentityId); return; } foreach (DeniedLocation denied in AlliancePlugin.HangarDeniedLocations) { if (Vector3.Distance(Context.Player.GetPosition(), new Vector3(denied.x, denied.y, denied.z)) <= denied.radius) { Context.Respond("Cannot buy here, too close to a denied location."); confirmations.Remove(Context.Player.IdentityId); return; } } if (EconUtils.getBalance(Context.Player.IdentityId) >= item.Price) { if (confirmations.ContainsKey(Context.Player.IdentityId)) { if (confirmations[Context.Player.IdentityId] >= DateTime.Now) { if (GridManager.LoadGrid(AlliancePlugin.path + "//ShipMarket//Grids//" + item.ItemId + ".xml", Context.Player.GetPosition(), false, Context.Player.SteamUserId, item.Name)) { EconUtils.takeMoney(Context.Player.IdentityId, item.Price); long sellerId = MySession.Static.Players.TryGetIdentityId(item.SellerSteamId); EconUtils.addMoney(sellerId, item.Price); if (AlliancePlugin.GridBackupInstalled) { AlliancePlugin.BackupGridMethod(GridManager.GetObjectBuilders(AlliancePlugin.path + "//ShipMarket//Grids//" + item.ItemId + ".xml"), Context.Player.IdentityId); } item.Buyer = Context.Player.SteamUserId; item.soldAt = DateTime.Now; item.Status = ItemStatus.Sold; Context.Respond("The grid should appear near you."); confirmations.Remove(Context.Player.IdentityId); if (!Directory.Exists(AlliancePlugin.path + "//ShipMarket//Sold//" + item.SellerSteamId)) { Directory.CreateDirectory(AlliancePlugin.path + "//ShipMarket//Sold//" + item.SellerSteamId); } list.items.Remove(number); File.Delete(AlliancePlugin.path + "//ShipMarket//ForSale//" + item.ItemId + ".json"); utils.WriteToJsonFile <MarketItem>(AlliancePlugin.path + "//ShipMarket//Sold//" + item.SellerSteamId + "//" + item.ItemId, item); } else { Context.Respond("Failed to load the grid! transaction cancelled."); return; } } else { Context.Respond("Time ran out, start again"); confirmations[Context.Player.IdentityId] = DateTime.Now.AddSeconds(20); } } else { Context.Respond("Run command again within 20 seconds to confirm. Target grid name is " + item.Name + " Sold by " + AlliancePlugin.GetPlayerName(item.SellerSteamId)); confirmations.Add(Context.Player.IdentityId, DateTime.Now.AddSeconds(20)); Context.Respond("It costs " + String.Format("{0:n0}", item.Price) + " SC."); } } else { Context.Respond("You cannot afford that. It costs " + String.Format("{0:n0}", item.Price) + " SC."); } }
public void EndListing(int number) { if (!list.items.ContainsKey(number)) { Context.Respond("There is no item in the market for that slot number."); return; } MarketItem item = list.items[number]; if (!item.SellerSteamId.Equals(Context.Player.SteamUserId)) { Context.Respond("This listing doesnt belong to you."); return; } if (!File.Exists(AlliancePlugin.path + "//ShipMarket//ForSale//" + item.ItemId + ".json")) { Context.Respond("That grid is no longer available for sale."); return; } if (!File.Exists(AlliancePlugin.path + "//ShipMarket//Grids//" + item.ItemId + ".xml")) { Context.Respond("This grid should be available, but its file for the grid doesnt exist."); return; } if (MyGravityProviderSystem.IsPositionInNaturalGravity(Context.Player.GetPosition())) { Context.Respond("You cannot use this command in natural gravity!"); return; } foreach (DeniedLocation denied in AlliancePlugin.HangarDeniedLocations) { if (Vector3.Distance(Context.Player.GetPosition(), new Vector3(denied.x, denied.y, denied.z)) <= denied.radius) { Context.Respond("Cannot buy here, too close to a denied location."); return; } } if (GridManager.LoadGrid(AlliancePlugin.path + "//ShipMarket//Grids//" + item.ItemId + ".xml", Context.Player.GetPosition(), true, Context.Player.SteamUserId, item.Name)) { EconUtils.takeMoney(Context.Player.IdentityId, item.Price); long sellerId = MySession.Static.Players.TryGetIdentityId(item.SellerSteamId); EconUtils.addMoney(sellerId, item.Price); if (AlliancePlugin.GridBackupInstalled) { AlliancePlugin.BackupGridMethod(GridManager.GetObjectBuilders(AlliancePlugin.path + "//ShipMarket//Grids//" + item.ItemId + ".xml"), Context.Player.IdentityId); } item.Buyer = Context.Player.SteamUserId; item.soldAt = DateTime.Now; if (!Directory.Exists(AlliancePlugin.path + "//ShipMarket//Sold//" + item.SellerSteamId)) { Directory.CreateDirectory(AlliancePlugin.path + "//ShipMarket//Sold//" + item.SellerSteamId); } list.items.Remove(number); File.Delete(AlliancePlugin.path + "//ShipMarket//ForSale//" + item.ItemId + ".json"); File.Delete(AlliancePlugin.path + "//ShipMarket//Grids//" + item.ItemId + ".xml"); Context.Respond("Ended the listing, the grid should appear near you."); } else { Context.Respond("Failed to load the grid! transaction cancelled."); return; } }
public void UnlockHangar() { if (!AlliancePlugin.config.HangarEnabled) { Context.Respond("Alliance hangar is not enabled."); return; } Boolean console = false; if (Context.Player == null) { console = true; } MyFaction fac = MySession.Static.Factions.GetPlayerFaction(Context.Player.IdentityId); if (fac == null) { Context.Respond("You must be in a faction to use alliance features."); return; } Alliance alliance = AlliancePlugin.GetAlliance(fac); if (alliance == null) { Context.Respond("You are not a member of an alliance."); return; } if (AlliancePlugin.HasFailedUpkeep(alliance)) { Context.Respond("Alliance failed to pay upkeep. Upgrades disabled."); return; } if (!alliance.hasUnlockedHangar) { ConcurrentBag <MyGroups <MyCubeGrid, MyGridPhysicalGroupData> .Group> gridWithSubGrids = GridFinder.FindLookAtGridGroup(Context.Player.Character); List <MyCubeGrid> grids = new List <MyCubeGrid>(); foreach (var item in gridWithSubGrids) { foreach (MyGroups <MyCubeGrid, MyGridPhysicalGroupData> .Node groupNodes in item.Nodes) { MyCubeGrid grid = groupNodes.NodeData; if (grid.Projector != null) { continue; } if (FacUtils.GetPlayersFaction(FacUtils.GetOwner(grid)) != null) { if (FacUtils.InSameFaction(FacUtils.GetOwner(grid), Context.Player.IdentityId)) { if (!grids.Contains(grid)) { grids.Add(grid); } } } else { if (FacUtils.GetOwner(grid).Equals(Context.Player.Identity.IdentityId)) { if (!grids.Contains(grid)) { grids.Add(grid); } } } } } //Do stuff with taking components from grid storage //GridCosts localGridCosts = GetComponentsAndCost(projectedGrid); //gridCosts.setComponents(localGridCosts.getComponents()); UpgradeCost cost = new UpgradeCost(); List <VRage.Game.ModAPI.IMyInventory> invents = new List <VRage.Game.ModAPI.IMyInventory>(); foreach (MyCubeGrid grid in grids) { invents.AddList(ShipyardCommands.GetInventories(grid)); } cost = ShipyardCommands.LoadUnlockCost(AlliancePlugin.path + "//HangarUnlockCost.txt"); if (cost != null) { if (cost.MetaPointCost > 0) { if (alliance.CurrentMetaPoints < cost.MetaPointCost) { Context.Respond("Cannot afford the meta point cost of " + cost.MetaPointCost); return; } } if (cost.MoneyRequired > 0) { if (EconUtils.getBalance(Context.Player.IdentityId) >= cost.MoneyRequired) { if (ShipyardCommands.ConsumeComponents(invents, cost.itemsRequired, Context.Player.SteamUserId)) { alliance.CurrentMetaPoints -= cost.MetaPointCost; EconUtils.takeMoney(Context.Player.IdentityId, cost.MoneyRequired); alliance.hasUnlockedHangar = true; HangarData hangar = alliance.LoadHangar(); AlliancePlugin.SaveAllianceData(alliance); ShipyardCommands.SendMessage("[Alliance Hangar]", "Unlocking the hangar. You were charged: " + String.Format("{0:n0}", cost.MoneyRequired) + " and components taken", Color.Green, (long)Context.Player.SteamUserId); } } else { ShipyardCommands.SendMessage("[Alliance Hangar]", "You cant afford the upgrade price of: " + String.Format("{0:n0}", cost.MoneyRequired), Color.Red, (long)Context.Player.SteamUserId); } } else { if (ShipyardCommands.ConsumeComponents(invents, cost.itemsRequired, Context.Player.SteamUserId)) { alliance.CurrentMetaPoints -= cost.MetaPointCost; alliance.hasUnlockedHangar = true; HangarData hangar = alliance.LoadHangar(); AlliancePlugin.SaveAllianceData(alliance); } } } else { Context.Respond("Error loading upgrade details."); return; } } }
public void Upgrade(Boolean upgrade = false) { if (!AlliancePlugin.config.HangarEnabled) { Context.Respond("Hangar not enabled."); return; } if (Context.Player != null) { IMyFaction faction = FacUtils.GetPlayersFaction(Context.Player.IdentityId); if (faction == null) { ShipyardCommands.SendMessage("[Hangar]", " You arent in a faction.", Color.Red, (long)Context.Player.SteamUserId); return; } Alliance alliance = AlliancePlugin.GetAlliance(faction as MyFaction); if (alliance == null) { Context.Respond("You are not a member of an alliance with an unlocked shipyard."); return; } if (AlliancePlugin.HasFailedUpkeep(alliance)) { Context.Respond("Alliance failed to pay upkeep. Upgrades disabled."); return; } if (!alliance.hasUnlockedHangar) { ShipyardCommands.SendMessage("[Alliance Hangar]", "To unlock use !ah unlock", Color.Cyan, (long)Context.Player.SteamUserId); return; } HangarData hangar = alliance.LoadHangar(); UpgradeCost cost = new UpgradeCost(); if (!upgrade) { ShipyardCommands.SendMessage("[Alliance Hangar]", "To upgrade use !ah upgrade true ,while looking at an owned grid.", Color.Cyan, (long)Context.Player.SteamUserId); StringBuilder sb = new StringBuilder(); try { cost = slotUpgrades[hangar.SlotUpgradeNum += 1]; } catch (Exception ex) { Context.Respond("Cannot upgrade any further as there are no more defined upgrade files."); return; } if (cost != null) { if (cost.MoneyRequired > 0) { ShipyardCommands.SendMessage("[Alliance Hangar]", "SC Cost for next slot upgrade " + String.Format("{0:n0}", cost.MoneyRequired), Color.Cyan, (long)Context.Player.SteamUserId); } if (cost.MetaPointCost > 0) { ShipyardCommands.SendMessage("[Alliance Hangar]", "Metapoint cost for next slot upgrade " + cost.MetaPointCost, Color.Cyan, (long)Context.Player.SteamUserId); } sb.AppendLine("Items required."); foreach (KeyValuePair <MyDefinitionId, int> id in cost.itemsRequired) { sb.AppendLine(id.Key.ToString() + " - " + id.Value); } Context.Respond(sb.ToString()); } } else { ConcurrentBag <MyGroups <MyCubeGrid, MyGridMechanicalGroupData> .Group> gridWithSubGrids = GridFinder.FindLookAtGridGroupMechanical(Context.Player.Character); List <MyCubeGrid> grids = new List <MyCubeGrid>(); foreach (var item in gridWithSubGrids) { foreach (MyGroups <MyCubeGrid, MyGridMechanicalGroupData> .Node groupNodes in item.Nodes) { MyCubeGrid grid = groupNodes.NodeData; if (grid.Projector != null) { continue; } if (FacUtils.GetPlayersFaction(FacUtils.GetOwner(grid)) != null) { if (FacUtils.InSameFaction(FacUtils.GetOwner(grid), Context.Player.IdentityId)) { if (!grids.Contains(grid)) { grids.Add(grid); } } } else { if (FacUtils.GetOwner(grid).Equals(Context.Player.Identity.IdentityId)) { if (!grids.Contains(grid)) { grids.Add(grid); } } } } } List <VRage.Game.ModAPI.IMyInventory> invents = new List <VRage.Game.ModAPI.IMyInventory>(); foreach (MyCubeGrid grid in grids) { invents.AddList(ShipyardCommands.GetInventories(grid)); } if (hangar.SlotsAmount >= AlliancePlugin.config.MaxHangarSlots) { Context.Respond("Cannot upgrade any further"); return; } try { cost = slotUpgrades[hangar.SlotUpgradeNum += 1]; } catch (Exception) { Context.Respond("Cannot upgrade any further as there are no more defined upgrade files."); return; } if (cost != null) { if (cost.MetaPointCost > 0) { if (alliance.CurrentMetaPoints < cost.MetaPointCost) { Context.Respond("Cannot afford the meta point cost of " + cost.MetaPointCost); return; } } if (cost.MoneyRequired > 0) { if (EconUtils.getBalance(Context.Player.IdentityId) >= cost.MoneyRequired) { if (ShipyardCommands.ConsumeComponents(invents, cost.itemsRequired, Context.Player.SteamUserId)) { alliance.CurrentMetaPoints -= cost.MetaPointCost; EconUtils.takeMoney(Context.Player.IdentityId, cost.MoneyRequired); hangar.SlotsAmount = (int)cost.NewLevel; hangar.SlotUpgradeNum++; hangar.SaveHangar(alliance); AlliancePlugin.SaveAllianceData(alliance); ShipyardCommands.SendMessage("[Alliance Hangar]", "Upgrading slots. You were charged: " + String.Format("{0:n0}", cost.MoneyRequired), Color.LightBlue, (long)Context.Player.SteamUserId); } } else { ShipyardCommands.SendMessage("[Alliance Hangar]", "You cant afford the upgrade price of: " + String.Format("{0:n0}", cost.MoneyRequired), Color.Red, (long)Context.Player.SteamUserId); } } else { if (ShipyardCommands.ConsumeComponents(invents, cost.itemsRequired, Context.Player.SteamUserId)) { alliance.CurrentMetaPoints -= cost.MetaPointCost; hangar.SlotsAmount = (int)cost.NewLevel; hangar.SlotUpgradeNum++; hangar.SaveHangar(alliance); AlliancePlugin.SaveAllianceData(alliance); } } } else { Context.Respond("Error loading upgrade details."); return; } } } }