Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public void PayShipyardFee(Int64 amount, ulong steamid)
        {
            bankBalance += amount;
            BankLog     log  = GetLog();
            BankLogItem item = new BankLogItem();

            item.SteamId     = steamid;
            item.Amount      = amount;
            item.Action      = "shipyard fee";
            item.TimeClaimed = DateTime.Now;
            item.BankAmount  = bankBalance;
            log.log.Add(item);
            utils.WriteToJsonFile <BankLog>(AlliancePlugin.path + "//AllianceBankLogs//" + AllianceId + "//log.json", log);
            AlliancePlugin.SaveAllianceData(this);
        }
Ejemplo n.º 3
0
        //public static void testConnection()
        //{
        //    MySqlConnection conn = new MySqlConnection(connStr);
        //    try
        //    {
        //        AlliancePlugin.Log.Info("Connecting to MySQL...");
        //        conn.Open();


        //        string sql = "CREATE DATABASE IF NOT EXISTS AllianceBank";
        //        MySqlCommand cmd = new MySqlCommand(sql, conn);
        //        cmd.ExecuteNonQuery();
        //        sql = "CREATE TABLE IF NOT EXISTS AllianceBank.BankBalances(allianceId CHAR(36) PRIMARY KEY, balance BIGINT)";
        //        cmd = new MySqlCommand(sql, conn);
        //        cmd.ExecuteNonQuery();
        //        AlliancePlugin.Log.Info("Created the tables if it needed to");
        //    }
        //    catch (Exception ex)
        //    {
        //        ReadyToSave = false;
        //        conn.Close();
        //        AlliancePlugin.Log.Error("Error connecting to database, disabling bank features.");
        //        AlliancePlugin.Log.Error(ex);
        //    }
        //    conn.Close();
        //    AlliancePlugin.Log.Info("Successfully connected to database!");
        //}
        public static void DoUpkeepForOne(Alliance alliance)
        {
            try
            {
                using (var db = new LiteDatabase(connectionString))
                {
                    var collection = db.GetCollection <BankData>("BankData");


                    FileUtils jsonStuff = new FileUtils();

                    jsonStuff.WriteToJsonFile <Alliance>(AlliancePlugin.path + "//UpkeepBackups//" + alliance.AllianceId + ".json", alliance);
                    var bank = collection.FindById(alliance.AllianceId);
                    if (bank == null)
                    {
                        bank = new BankData
                        {
                            Id      = alliance.AllianceId,
                            balance = 1
                        };
                        collection.Insert(bank);
                        alliance.failedUpkeep++;
                        if (alliance.failedUpkeep >= AlliancePlugin.config.UpkeepFailBeforeDelete)
                        {
                            AllianceChat.SendChatMessage(alliance.AllianceId, "Upkeep", "Upkeep failed, met the threshold for delete. Deleting Alliance.", true, 0);
                            AlliancePlugin.AllAlliances.Remove(alliance.name);
                            File.Delete(AlliancePlugin.path + "//AllianceData//" + alliance.AllianceId + ".json");
                            foreach (long id in alliance.AllianceMembers)
                            {
                                AlliancePlugin.FactionsInAlliances.Remove(id);
                            }
                        }
                        else
                        {
                            AllianceChat.SendChatMessage(alliance.AllianceId, "Upkeep", "Upkeep failed, Upgrades disabled.", true, 0);
                        }
                    }
                    else
                    {
                        if (bank.balance >= alliance.GetUpkeep())
                        {
                            bank.balance -= alliance.GetUpkeep();
                            collection.Update(bank);
                            AllianceChat.SendChatMessage(alliance.AllianceId, "Upkeep", "Paying upkeep of " + String.Format("{0:n0}", alliance.GetUpkeep()) + " SC.", true, 0);
                            alliance.Upkeep(alliance.GetUpkeep(), 1);
                            alliance.bankBalance -= alliance.GetUpkeep();
                            alliance.failedUpkeep = 0;
                            AlliancePlugin.SaveAllianceData(alliance);
                        }
                        else
                        {
                            alliance.failedUpkeep++;
                            if (alliance.failedUpkeep >= AlliancePlugin.config.UpkeepFailBeforeDelete)
                            {
                                AllianceChat.SendChatMessage(alliance.AllianceId, "Upkeep", "Upkeep failed, met the threshold for delete. Deleting Alliance.", true, 0);
                                AlliancePlugin.AllAlliances.Remove(alliance.name);
                                File.Delete(AlliancePlugin.path + "//AllianceData//" + alliance.AllianceId + ".json");
                                foreach (long id in alliance.AllianceMembers)
                                {
                                    AlliancePlugin.FactionsInAlliances.Remove(id);
                                }
                            }
                            else
                            {
                                AllianceChat.SendChatMessage(alliance.AllianceId, "Upkeep", "Upkeep failed, Upgrades disabled.", true, 0);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                AlliancePlugin.Log.Error("Error for all upkeep");
                AlliancePlugin.Log.Error(ex);
                return;
            }
        }
Ejemplo n.º 4
0
        public void RentGate(string name)
        {
            MyFaction fac = MySession.Static.Factions.GetPlayerFaction(Context.Player.IdentityId);

            if (fac == null)
            {
                Context.Respond("Only factions can be in alliances.");
                return;
            }
            Alliance alliance = AlliancePlugin.GetAlliance(fac);

            if (alliance == null)
            {
                Context.Respond("Only members of an alliance may rent a gate.");
                return;
            }
            if (alliance != null)
            {
                if (alliance.SupremeLeader == Context.Player.SteamUserId || alliance.HasAccess(Context.Player.SteamUserId, AccessLevel.PayFromBank))
                {
                    JumpGate gate1 = null;
                    JumpGate gate2 = null;
                    AlliancePlugin.LoadAllGates();
                    foreach (JumpGate gate in AlliancePlugin.AllGates.Values)
                    {
                        if (gate.GateName.Equals(name) && gate.CanBeRented && DateTime.Now >= gate.NextRentAvailable)
                        {
                            gate1 = gate;
                            gate2 = AlliancePlugin.AllGates[gate.TargetGateId];
                            break;
                        }
                    }
                    if (gate1 == null || gate2 == null)
                    {
                        Context.Respond("Could not find one of those gates.");
                        return;
                    }
                    if (alliance.CurrentMetaPoints >= gate1.MetaPointRentCost)
                    {
                        alliance.CurrentMetaPoints           -= gate1.MetaPointRentCost;
                        gate1.OwnerAlliance                   = alliance.AllianceId;
                        gate2.OwnerAlliance                   = alliance.AllianceId;
                        gate1.NextRentAvailable               = DateTime.Now.AddDays(gate1.DaysPerRent);
                        gate2.NextRentAvailable               = DateTime.Now.AddDays(gate1.DaysPerRent);
                        AlliancePlugin.AllGates[gate1.GateId] = gate1;
                        AlliancePlugin.AllGates[gate2.GateId] = gate2;
                        gate1.Save();
                        gate2.Save();
                        Context.Respond("Successfully rented gate for " + gate1.DaysPerRent + " Days. Fees can now be set with !jumpgate fee <gateName> <amount>");
                        Context.Respond("Gate names, " + gate1.GateName + ", " + gate2.GateName);
                        AlliancePlugin.SaveAllianceData(alliance);
                        return;
                    }
                    else
                    {
                        Context.Respond("Cannot afford the meta point cost of " + gate1.MetaPointRentCost);
                        return;
                    }
                }
                else
                {
                    Context.Respond("You dont have the rank to do this.");
                }
            }
        }
Ejemplo n.º 5
0
        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;
                }
            }
        }
Ejemplo n.º 6
0
        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;
                    }
                }
            }
        }