예제 #1
0
        /// <summary>
        /// Проводит атаку на базу
        /// </summary>
        /// <param name="attackerID"></param>
        /// <param name="victimID"></param>
        /// <returns></returns>
        public static string Battle(Account attackerAcc, Base victimBase)
        {
            if (BaseManager.GetBaseUnitsCount(attackerAcc.UserID) == 0)
            {
                return("nounits");
            }
            var attacker      = PlayerManager.GetPlayerByID(attackerAcc.UserID);
            var victim        = PlayerManager.GetPlayerByID(victimBase.OwnerID);
            var attackerUnits = BaseManager.GetBaseUnits(attackerAcc.UserID);
            var victimUnits   = BaseManager.GetBaseUnits(victimBase.OwnerID);

            Log("Event", $"Player {attacker.Playername} initiated a battle.");

            // Расчёт базовой силы на основе числа юнитов
            var attackerPower = 0.0;
            var victimPower   = 0.0;

            foreach (var unit in attackerUnits)
            {
                attackerPower += (double)(unit.Count * ItemsVars.GetPower(unit.Type));
                Log("Battle", $"Player {attacker.Playername} has {unit.Count} units of {unit.Type} class.");
            }
            foreach (var unit in victimUnits)
            {
                victimPower += (double)(unit.Count * ItemsVars.GetPower(unit.Type));
                Log("Battle", $"Player {victim.Playername} has {unit.Count} units of {unit.Type} class.");
            }

            // Множители силы
            var attackerBaseMultiplier = BaseManager.HasBaseStructure(
                BaseManager.GetBaseInfo(attackerAcc).BaseID,
                "aircraftsComplex"
                );

            attackerPower *= attackerBaseMultiplier != null
                ? ItemsVars.GetBaseAttackMultiplier(attackerBaseMultiplier.Level)
                : 1;

            victimPower *= ItemsVars.GetBaseDefenceMultiplier(victimBase.Level);

            var result = "";

            if (attackerPower > victimPower)
            {
                var delta = (attackerPower > 0 && victimPower > 0 ? 1 - (victimPower / attackerPower) : 1.0);
                DoBattle(attacker, victim, delta);
                result = "youwin";
            }
            else
            {
                var delta = (attackerPower > 0 && victimPower > 0 ? 1 - (attackerPower / victimPower) : 1.0);
                DoBattle(victim, attacker, delta);
                result = "youlose";
            }
            Log("Event", $"Player {attacker.Playername} (units power is {attackerPower}) attacked {victim.Playername} " +
                $"(units power is {victimPower}) and {(attackerPower > victimPower ? "wins" : "loses")} that battle.");

            return(result);
        }
예제 #2
0
        /// <summary>
        /// Проверяет на возможность приобрести предмет
        /// </summary>
        /// <param name="baseid"></param>
        /// <param name="itemName"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public static bool CanAfford(int baseid, string itemName, int level = 0)
        {
            var resources = GetBaseResources(baseid);
            var cost      = ItemsVars.GetCost(itemName);

            level++;
            if (resources.Credits < cost.Credits * level || resources.Energy < cost.Energy * level)
            {
                return(false);
            }
            return(true);
        }
예제 #3
0
        /// <summary>
        ///  Реализует механизм покупки
        /// </summary>
        /// <param name="baseid"></param>
        /// <param name="itemName"></param>
        /// <param name="level"></param>
        public static void DoBuyItem(int baseid, string itemName, int level = 0)
        {
            level++;
            var cost = ItemsVars.GetCost(itemName);

            using (var db = new Entities())
            {
                var resources = db.Resources.FirstOrDefault(o => o.Instance == "bas" + baseid.ToString());
                resources.Credits  -= cost.Credits * level;
                resources.Energy   -= cost.Energy * level;
                resources.Neutrino -= cost.Neutrino * level;
                db.SaveChanges();
            }
        }
예제 #4
0
        /// <summary>
        /// Выдаёт всем базам ресурсы исходя из наличия необходимых строений
        /// </summary>
        /// <returns></returns>
        public static bool BaseGatherResources()
        {
            var Bases = GetBaseList();

            foreach (var CurrentBase in Bases)
            {
                if (CurrentBase.IsActive)
                {
                    using (var DB = new Entities())
                    {
                        var Resources = DB.Resources.FirstOrDefault(o => o.Instance == "bas" + CurrentBase.BaseID.ToString());

                        var creditsStruct = HasBaseStructure(CurrentBase.BaseID, "resourceComplex");
                        if (creditsStruct != null)
                        {
                            Resources.Credits += (int)(ItemsVars.GetCreditProducingAmount(creditsStruct.Level) * 0.1);
                        }

                        var energyStruct = HasBaseStructure(CurrentBase.BaseID, "energyComplex");
                        if (energyStruct != null)
                        {
                            Resources.Energy += (int)(ItemsVars.GetEnergyProducingAmount(energyStruct.Level) * 0.1);
                        }

                        var neutrinoStruct = HasBaseStructure(CurrentBase.BaseID, "researchStation");
                        if (neutrinoStruct != null)
                        {
                            Resources.Neutrino += ItemsVars.GetNeutrinoProducingAmount(neutrinoStruct.Level) * 0.1;
                        }

                        // базовое значение * уровень здания / 10 частей в минуте
                        DB.SaveChanges();
                    }
                }
            }
            return(true);
        }
예제 #5
0
        /// <summary>
        /// Управляет созданием юнитов
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string MakeUnit(BaseAction obj)
        {
            if (CheckInput(obj) != "success")
            {
                return(CheckInput(obj));
            }
            try
            {
                if (IsOwner(obj.baseid, obj.token) && obj.result != null)
                {
                    if (HasBaseStructure(obj.baseid, "aircraftsComplex") == null)
                    {
                        return("noAircrafts");
                    }

                    if (!CanAfford(obj.baseid, obj.result))
                    {
                        return("notenoughresources");
                    }

                    var population = HasBaseStructure(obj.baseid, "lifeComplex");
                    if (population == null)
                    {
                        return("noLifeComplex");
                    }
                    ProceedActions.Log("Event", $"Игрок покупает юнит. На базе максимум популяции" +
                                       $" {ItemsVars.GetPopulation(population.Level)} и уже {GetBaseUnitsCount(obj.baseid)} юнитов создано");
                    if (ItemsVars.GetPopulation(population.Level) <= GetBaseUnitsCount(obj.baseid))
                    {
                        return("populationLimit");
                    }

                    ProceedActions.DoBuyItem(obj.baseid, obj.result);

                    var u = SquadManager.GetUnit("bas" + obj.baseid.ToString(), obj.result);

                    using (var db = new Entities())
                    {
                        if (u == null)
                        {
                            db.Units.Add(new Unit {
                                Instance = "bas" + obj.baseid.ToString(), Type = obj.result, Count = 1
                            });
                        }
                        else
                        {
                            ProceedActions.Log("Event", $"В инстансе {obj.baseid.ToString()} {u.Count} юнитов типа {obj.result}");
                            var unit = db.Units.FirstOrDefault(o => o.Instance == u.Instance && o.Type == u.Type);
                            unit.Count++;
                        }
                        db.SaveChanges();
                    }

                    const string result = "success";
                    return(result);
                }
                else
                {
                    const string result = "notanowner";
                    return(result);
                }
            }
            catch (Exception ex)
            {
                ProceedActions.Log("Exception", $"Исключение: {ex.Message}, функция BaseManager.MakeUnit");
                return("Error#Exception: " + ex.Message);
            }
        }