예제 #1
0
 /// <summary>
 /// Записывает дефолтные данные в БД
 /// </summary>
 /// <param name="db"></param>
 protected override void Seed(Entities db)
 {
     db.Accounts.Add(new Account {
         UserID = 1, Username = "******", Password = AccountManager.Base64Encode("123456"), Role = 1, Token = "Tokenfgio"
     });
     db.Accounts.Add(new Account {
         UserID = 2, Username = "******", Password = AccountManager.Base64Encode("123"), Role = 0, Token = "Tokenfl4o"
     });
     db.Players.Add(new Player {
         UserID = 1, Playername = "admin"
     });
     db.Players.Add(new Player {
         UserID = 2, Playername = "user"
     });
     db.Bases.Add(new Base {
         Basename = "adminBase", OwnerID = 1, CoordX = 1, CoordY = 1, Level = 1, IsActive = true
     });
     db.Bases.Add(new Base {
         Basename = "userBase", OwnerID = 2, CoordX = 4, CoordY = 2, Level = 1, IsActive = true
     });
     db.Resources.Add(new Resource {
         Instance = "bas1", Credits = 200, Energy = 200, Neutrino = 0.0
     });
     db.Resources.Add(new Resource {
         Instance = "bas2", Credits = 200, Energy = 200, Neutrino = 0.0
     });
     db.Units.Add(new Unit {
         Instance = "bas1", Type = "droneUnit", Count = 1
     });
     db.Units.Add(new Unit {
         Instance = "bas2", Type = "droneUnit", Count = 2
     });
     db.SaveChanges();
     ProceedActions.Log("Event", "Database default data successfully filled. EntitiesInitializer.Seed");
 }
예제 #2
0
        /// <summary>
        /// Управляет авторизацией пользователя
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ReturnAuthData AuthClient(AuthData data)
        {
            data.username = data.username.ToLower();
            var user = GetUserByName(data.username);

            if (user != null)
            {
                var result = new ReturnAuthData {
                    success = false
                };
                if (Base64Decode(user.Password) == data.password)
                {
                    result.success = true;
                    result.token   = user.Token;
                    result.role    = user.Role;
                    return(result);
                }
                else
                {
                    ProceedActions.Log("Event", $"Неудачная попытка авторизоваться под аккаунтом {user.Username}");
                    result.message = "wrongpassword";
                    return(result);
                }
            }
            else
            {
                return(CreateUser(data.username, data.password));
            }
        }
예제 #3
0
        /// <summary>
        /// Управляет созданием построек
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string BuildStructure(BaseAction obj)
        {
            if (CheckInput(obj) != "success")
            {
                return(CheckInput(obj));
            }

            try
            {
                if (IsOwner(obj.baseid, obj.token) && obj.result != null)
                {
                    obj.result = obj.result ?? "lifeComplex";

                    var Struct = HasBaseStructure(obj.baseid, obj.result);
                    if (Struct != null)
                    {
                        return("alreadyexists");
                    }
                    if (!CanAfford(obj.baseid, obj.result))
                    {
                        return("notenoughresources");
                    }

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

                    using (var db = new Entities())
                    {
                        db.Structures.Add(new Structure {
                            BaseID = obj.baseid, Type = obj.result, Level = 1
                        });
                        db.SaveChanges();
                    }
                    const string result = "success";
                    return(result);
                }
                else
                {
                    const string result = "notanowner";
                    return(result);
                }
            }
            catch (Exception ex)
            {
                ProceedActions.Log("Exception", $"Исключение: {ex.Message}, функция BaseManager.BuildStructure");
                return("Error#Exception: " + ex.Message);
            }
        }
예제 #4
0
        /// <summary>
        /// Получает приказ на атаку отряда
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SendAttackOrder(SquadAction obj)
        {
            var attacker   = AccountManager.GetAccountByToken(obj.token);
            var victimBase = BaseManager.GetBaseByID(obj.to);

            if (attacker.UserID == victimBase.OwnerID)
            {
                const string result = "cannotuseatyourself";
                return(result);
            }
            if (!victimBase.IsActive)
            {
                const string result = "baseisinactive";
                return(result);
            }

            return(ProceedActions.Battle(attacker, victimBase));
        }
예제 #5
0
        /// <summary>
        /// Управляет улучшением построек
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string UpgradeStructure(BaseAction obj)
        {
            if (CheckInput(obj) != "success")
            {
                return(CheckInput(obj));
            }
            try
            {
                if (IsOwner(obj.baseid, obj.token) && obj.result != null)
                {
                    var Struct = HasBaseStructure(obj.baseid, obj.result);
                    if (Struct == null)
                    {
                        return("notexists");
                    }
                    if (!CanAfford(obj.baseid, obj.result, Struct.Level))
                    {
                        return("notenoughresources");
                    }

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

                    using (var db = new Entities())
                    {
                        var str = db.Structures.FirstOrDefault(o => o.Type == obj.result && o.BaseID == obj.baseid);
                        str.Level++;
                        db.SaveChanges();
                    }

                    const string result = "success";
                    return(result);
                }
                else
                {
                    const string result = "notanowner";
                    return(result);
                }
            }
            catch (Exception ex)
            {
                ProceedActions.Log("Exception", $"Исключение: {ex.Message}, функция BaseManager.UpgradeStructure");
                return("Error#Exception: " + ex.Message);
            }
        }
예제 #6
0
        /// <summary>
        /// Управляет починкой базы
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string RepairBase(BaseAction obj)
        {
            if (CheckInput(obj) != "success")
            {
                return(CheckInput(obj));
            }

            try
            {
                if (IsOwner(obj.baseid, obj.token))
                {
                    var BaseData = GetBaseByID(obj.baseid);
                    if (!CanAfford(obj.baseid, "repairBase", BaseData.Level - 2))
                    {
                        return("notenoughresources");
                    }
                    ProceedActions.DoBuyItem(obj.baseid, "repairBase", BaseData.Level - 2);

                    using (var db = new Entities())
                    {
                        var BaseEntry = db.Bases.FirstOrDefault(o => o.BaseID == obj.baseid);
                        BaseEntry.IsActive = !BaseEntry.IsActive;
                        db.SaveChanges();
                    }
                    const string result = "success";
                    return(result);
                }
                else
                {
                    const string result = "notanowner";
                    return(result);
                }
            }
            catch (Exception ex)
            {
                ProceedActions.Log("Exception", $"Исключение: {ex.Message}, функция BaseManager.RepairBase");
                return("Error#Exception: " + ex.Message);
            }
        }
예제 #7
0
 /// <summary>
 /// Управляет улучшением базы
 /// </summary>
 /// <param name="obj"></param>
 /// <returns></returns>
 public static string UpgradeBase(BaseAction obj)
 {
     if (CheckInput(obj) != "success")
     {
         return(CheckInput(obj));
     }
     try
     {
         if (IsOwner(obj.baseid, obj.token))
         {
             var curbase = GetBaseByID(obj.baseid);
             if (!CanAfford(obj.baseid, "upgradeBase", curbase.Level))
             {
                 return("notenoughresources");
             }
             ProceedActions.DoBuyItem(obj.baseid, "upgradeBase", curbase.Level);
             using (Entities db = new Entities())
             {
                 var BaseEntry = db.Bases.FirstOrDefault(o => o.BaseID == obj.baseid);
                 BaseEntry.Level++;
                 db.SaveChanges();
             }
             ProceedActions.Log("Event", $"Base ID {obj.baseid} has been upgraded at 1 level up.");
             const string result = "success";
             return(result);
         }
         else
         {
             const string result = "notanowner";
             return(result);
         }
     }
     catch (Exception ex)
     {
         ProceedActions.Log("Exception", $"Исключение: {ex.Message}, функция BaseManager.UpgradeBase");
         return("Error#Exception: " + ex.Message);
     }
 }
예제 #8
0
        /// <summary>
        /// Управляет созданием аккаунта
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static ReturnAuthData CreateUser(string username, string password)
        {
            var result = new ReturnAuthData {
                success = false
            };

            using (var db = new Entities())
                try
                {
                    var user = new Account {
                        Username = username, Password = Base64Encode(password), Role = 0, Token = "Token" + Base64Encode(username + "salt")
                    };
                    db.Accounts.Add(user);
                    db.SaveChanges();

                    int newIdentityValue = user.UserID;
                    db.Players.Add(new Player {
                        UserID = newIdentityValue, Playername = username
                    });
                    db.Bases.Add(new Base {
                        Basename = username + "Base", OwnerID = newIdentityValue, CoordX = 1, CoordY = 1, Level = 1, IsActive = true
                    });
                    db.Resources.Add(new Resource {
                        Instance = "bas" + newIdentityValue, Credits = 200, Energy = 200, Neutrino = 0.0
                    });
                    db.SaveChanges();
                    result.success = true;
                    result.token   = user.Token;
                    result.role    = user.Role;
                    return(result);
                }
                catch (Exception ex)
                {
                    ProceedActions.Log("Exception", $"Исключение: {ex.Message}, функция AccountManager.CreateUser");
                    result.message = ex.Message;
                    return(result);
                }
        }
예제 #9
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);
            }
        }
예제 #10
0
 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
     ProceedActions.Log("Event", "Database model successfully created. Entities.OnModelCreating");
 }