Пример #1
0
        /// <summary>
        /// <para>A not optimal way of deciding whether the elevevator is faster</para>
        /// <para>Big 0 notation: 0(n)</para>
        /// </summary>
        /// <param name="from">From area</param>
        /// <param name="to">To Area</param>
        /// <returns>Returns the elevator or the To Area, depending on what is closer</returns>
        public static IArea IsElevatorCloser(IArea from, IArea to)
        {
            Elevator ev = (Elevator)Areas.Find(X => X.Position.Y == from.Position.Y && X is Elevator);

            if (ev.Position.Y == to.Position.Y)
            {
                return(to);
            }

            int dictanceWithStairs   = 0;
            int dictanceWithElevator = 0;

            // Calculating the time to walk or take the elevator
            #region
            // this part is not completely true but it works for now
            // it should count the backtrack weights
            // This is for the future to change

            // - 1 for the dequeue of the first area
            dictanceWithStairs   += GetShortestPathDijkstra(from, to).Count - 1;
            dictanceWithElevator += GetShortestPathDijkstra(from, ev).Count - 1;

            if (ev.Position.Y > to.Position.Y)
            {
                dictanceWithElevator += ev.Position.Y - to.Position.Y;
            }
            else
            {
                dictanceWithElevator += to.Position.Y - ev.Position.Y;
            }

            // Adding the distance it has to walk from elevator to room
            dictanceWithElevator += GetShortestPathDijkstra((Elevator)Areas.Find(X => X.Position.Y == to.Position.Y && X is Elevator), to).Count() - 1;
            #endregion

            // Movables will favor the elevator over the stairs if the distance is the same
            if (dictanceWithStairs >= dictanceWithElevator)
            {
                return(ev);
            }

            return(to);
        }
Пример #2
0
 public static AreaData Get(string sid)
 {
     lock (AssetReloadHelper.AreaReloadLock) {
         return(string.IsNullOrEmpty(sid) ? null : Areas.Find(area => area.GetSID() == sid));
     }
 }
Пример #3
0
        /// <summary>
        /// Criando regras iniciais e de testes
        /// </summary>
        private void Seed_Security()
        {
            if (!Groups.Any())
            {
                var groups = new Group[]
                {
                    new Group {
                        Name = "Security"
                    },
                    new Group {
                        Name = "Management"
                    },
                    new Group {
                        Name = "Configuration"
                    },
                    new Group {
                        Name = "Sales"
                    },
                    new Group {
                        Name = "Operation"
                    }
                };

                Groups.AddRange(groups);

                foreach (var enterprise in Enterprises)
                {
                    var name = enterprise.Name.EndsWith('s')
                        ? enterprise.Name + "\'s"
                        : enterprise.Name;

                    var group = new Group
                    {
                        Name       = $"{name} User",
                        Enterprise = enterprise
                    };

                    Groups.Add(group);
                }

                SaveChanges();
            }

            if (IsTest)
            {
                if (!Users.Any())
                {
                    {
                        var enterprise = Enterprises
                                         .Single(e => e.Name == "Enterprise Test");

                        var email = $"user@{enterprise.Name}.com".Replace(" ", "").ToLower();

                        var user = new User
                        {
                            Name       = "User",
                            PublicId   = CryptoHelper.Guid("U$1"),
                            PrivateId  = CryptoHelper.Guid("U$1"),
                            Email      = email,
                            Password   = CryptoHelper.Guid("user2019"),
                            Enterprise = enterprise
                        };

                        Users.Add(user);
                    }

                    foreach (var group in Groups
                             .Where(e => e.EnterpriseId == null))
                    {
                        foreach (var enterprise in Enterprises)
                        {
                            var email = $"{group.Name}@{enterprise.Name}.com".Replace(" ", "").ToLower();

                            var user = new User
                            {
                                Name       = group.Name,
                                PublicId   = CryptoHelper.Guid(group.Name + "#" + enterprise.Name),
                                PrivateId  = CryptoHelper.Guid(group.Name + "$" + enterprise.Name),
                                Email      = email,
                                Password   = CryptoHelper.Guid(group.Name.ToLower()),
                                Enterprise = enterprise
                            };

                            Users.Add(user);

                            var userGroup = new UserGroup
                            {
                                User  = user,
                                Group = group
                            };

                            UserGroups.AddRange(userGroup);
                        }
                    }

                    SaveChanges();
                }
            }

            if (!Areas.Any())
            {
                var areas = new Area[]
                {
                    new Area {
                        Id   = (int)AreaEnum.User,
                        Name = "User"
                    },
                    new Area {
                        Id   = (int)AreaEnum.Group,
                        Name = "Group"
                    },
                    new Area {
                        Id   = (int)AreaEnum.Customer,
                        Name = "Customer"
                    }
                };

                Areas.AddRange(areas);

                SaveChanges();
            }

            if (IsTest)
            {
                if (!AreaAccess.Any())
                {
                    // Sales tem acesso de leitura e escrita no CRM

                    {
                        var area = Areas
                                   .Find((int)AreaEnum.Customer);

                        var group = Groups
                                    .First(e => e.Name == "Sales");

                        var areaAccess = new AreaAccess
                        {
                            Area      = area,
                            Group     = group,
                            CanCreate = true,
                            CanDelete = false,
                            CanModify = true,
                            CanRead   = true
                        };

                        AreaAccess.Add(areaAccess);
                    }

                    // Management tem acesso completo ao CRM

                    {
                        var area = Areas
                                   .Find((int)AreaEnum.Customer);

                        var group = Groups
                                    .First(e => e.Name == "Management");

                        var areaAccess = new AreaAccess
                        {
                            Area      = area,
                            Group     = group,
                            CanCreate = true,
                            CanDelete = true,
                            CanModify = true,
                            CanRead   = true
                        };

                        AreaAccess.Add(areaAccess);
                    }

                    // Operation só pode consultar o CRM

                    {
                        var area = Areas
                                   .Find((int)AreaEnum.Customer);

                        var group = Groups
                                    .First(e => e.Name == "Operation");

                        var areaAccess = new AreaAccess
                        {
                            Area      = area,
                            Group     = group,
                            CanCreate = false,
                            CanDelete = false,
                            CanModify = false,
                            CanRead   = true
                        };

                        AreaAccess.Add(areaAccess);
                    }

                    // Segurança pode criar novos grupos

                    {
                        var area = Areas
                                   .Find((int)AreaEnum.Group);

                        var group = Groups
                                    .First(e => e.Name == "Security");

                        var areaAccess = new AreaAccess
                        {
                            Area      = area,
                            Group     = group,
                            CanCreate = true,
                            CanDelete = true,
                            CanModify = true,
                            CanRead   = true
                        };

                        AreaAccess.Add(areaAccess);
                    }

                    // Segurança pode criar novos usuários

                    {
                        var area = Areas
                                   .Find((int)AreaEnum.User);

                        var group = Groups
                                    .First(e => e.Name == "Security");

                        var areaAccess = new AreaAccess
                        {
                            Area      = area,
                            Group     = group,
                            CanCreate = true,
                            CanDelete = true,
                            CanModify = true,
                            CanRead   = true
                        };

                        AreaAccess.Add(areaAccess);
                    }

                    SaveChanges();
                }
            }

            if (!Policies.Any())
            {
                var area = Areas
                           .Find((int)AreaEnum.User);

                var policies = new Policy[]
                {
                    new Policy {
                        Id   = (int)PolicyEnum.User_ChangePassword,
                        Area = area,
                        Name = "Change Password"
                    },
                    new Policy {
                        Id   = (int)PolicyEnum.User_ChangeEmail,
                        Area = area,
                        Name = "Change Email"
                    }
                };

                Policies.AddRange(policies);

                SaveChanges();
            }

            if (IsTest)
            {
                if (!PolicyAccess.Any())
                {
                    // Segurança tem acesso às políticas de segurança

                    {
                        var area = Areas
                                   .Find((int)AreaEnum.User);

                        var group = Groups
                                    .First(e => e.Name == "Security");

                        foreach (var policy in Policies
                                 .Where(e => e.AreaId == area.Id))
                        {
                            var policyAccess = new PolicyAccess
                            {
                                Group  = group,
                                Policy = policy
                            };

                            PolicyAccess.AddRange(policyAccess);
                        }
                    }

                    SaveChanges();
                }
            }
        }
Пример #4
0
 public static AreaData Get(string sid)
 {
     return(Areas.Find(area => area.GetSID() == sid));
 }
Пример #5
0
 public Area FindArea(string name)
 => Areas.Find(x => x.Name == name) ?? throw new ArgumentException("invalid area");
Пример #6
0
 public static AreaData Get(string sid)
 {
     return(string.IsNullOrEmpty(sid) ? null : Areas.Find(area => area.GetSID() == sid));
 }
Пример #7
0
 public static AreaData Get(string sid)
 {
     lock (OuiHelper_ChapterSelect_Reload.AreaReloadLock) {
         return(string.IsNullOrEmpty(sid) ? null : Areas.Find(area => area.GetSID() == sid));
     }
 }
Пример #8
0
        /// <summary>
        /// Searches the asset databases and attempts to find and set references
        /// to run-time objects that correspond to ID's loaded from JSON.
        /// </summary>
        private void LinkObjects()
        {
            // First validate each id is unique.
            void ValidateUniqueIDs <T>(List <T> assets) where T : IAsset
            {
                Dictionary <int, string> ids = new Dictionary <int, string>();

                foreach (var asset in assets)
                {
                    if (ids.ContainsKey(asset.ID))
                    {
                        throw new Exception(
                                  $"Duplicate {typeof(T).Name} id found for {asset.Name} and {ids[asset.ID]}."
                                  );
                    }
                    else
                    {
                        ids.Add(asset.ID, asset.Name);
                    }
                }
            }

            ValidateUniqueIDs(Factions);
            ValidateUniqueIDs(Areas);
            ValidateUniqueIDs(Units);
            ValidateUniqueIDs(Items);

            // Link objects.
            foreach (var unit in Units)
            {
                Faction faction = Factions.Find(x => unit.FactionID == x.ID);
                if (faction != null)
                {
                    faction.Units.Add(unit);
                    unit.Faction = faction;
                }
                else
                {
                    throw new Exception(
                              $"No faction with id {unit.FactionID} could be found for unit {unit.Name}."
                              );
                }

                Area area = Areas.Find(x => unit.AreaID == x.ID);
                if (area != null)
                {
                    area.Units.Add(unit);
                    unit.Area = area;
                }
                else
                {
                    throw new Exception(
                              $"No area with id {unit.AreaID} could be found for unit {unit.Name}."
                              );
                }

                Weapon weaponToEquip = null;

                foreach (var itemID in unit.ItemIDs)
                {
                    Weapon weapon = Weapons.Find(x => itemID == x.ID);
                    if (weapon != null)
                    {
                        // We have to clone the weapon since it will maintain a state.
                        Weapon instancedWeapon = (Weapon)weapon.CreateInstance();

                        unit.Inventory.Add(instancedWeapon);
                        if (weaponToEquip == null)
                        {
                            weaponToEquip = instancedWeapon;
                        }
                    }
                    else
                    {
                        throw new Exception(
                                  $"No weapon with id {itemID} could be found for unit {unit.Name}."
                                  );
                    }
                }

                unit.Weapon  = weaponToEquip;
                unit.HasTurn = true;
            }

            foreach (var area in Areas)
            {
                foreach (var itemID in area.ItemIds)
                {
                    Item item = Items.Find(x => x.ID == itemID);
                    if (item != null)
                    {
                        area.Inventory.Add(item);
                    }
                    else
                    {
                        throw new Exception(
                                  $"Failed to add item with id {itemID} to area {area.Name} because no such item exists."
                                  );
                    }
                }
            }

            foreach (var connection in Connections)
            {
                connection.AreaOne = Areas.Find(x => connection.AreaOneID == x.ID);
                connection.AreaTwo = Areas.Find(x => connection.AreaTwoID == x.ID);

                if (connection.AreaOne != null && connection.AreaTwo != null)
                {
                    if (connection.AreaOne.Connections.Contains(connection.AreaTwo) == false)
                    {
                        connection.AreaOne.Connections.Add(connection.AreaTwo);
                    }

                    if (connection.AreaTwo.Connections.Contains(connection.AreaOne) == false)
                    {
                        connection.AreaTwo.Connections.Add(connection.AreaOne);
                    }
                }
                else
                {
                    throw new Exception(
                              $"Area connection error: One or more unknown areas with ids {connection.AreaOneID} and {connection.AreaTwoID} for 'area-one' and 'area-two' respectively."
                              );
                }
            }
        }
Пример #9
0
        public void Load(string filePath)
        {
            string jsonString;

            using (FileStream fs = new FileStream(filePath, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    jsonString = sr.ReadToEnd();
                }
            }

            JsonSerializerOptions jso = new JsonSerializerOptions
            {
                WriteIndented = true
            };

            jso.Converters.Add(new TNodeJsonConverter());
            jso.Converters.Add(new AreaJsonConverter());
            jso.Converters.Add(new ChoiceJsonConverter());
            jso.Converters.Add(new StatementJsonConverter());
            jso.Converters.Add(new UnitConverter());
            jso.Converters.Add(new OtherJsonConverter());
            ValidateLogic vl = JsonSerializer.Deserialize <ValidateLogic>(jsonString, jso);

            TNodes    = vl.TNodes;
            Areas     = vl.Areas;
            Name      = vl.Name;
            StartNode = TNodes[vl.StartNode.ID];
            for (int i = 0; i < Areas.Count; i++)
            {
                if (Areas[i].StartNode != null)
                {
                    Areas[i].StartNode = TNodes[(Areas[i].StartNode as IDNode).ID];
                }
                if (Areas[i].Parent != null)
                {
                    if (Name == (Areas[i].Parent as IDArea).Name)
                    {
                        Areas[i].Parent = this;
                    }
                    else
                    {
                        Areas[i].Parent = Areas.Find(m => m.Name == (Areas[i].Parent as IDArea).Name);
                    }
                }
            }

            foreach (KeyValuePair <string, TNode> kv in TNodes)
            {
                if (kv.Value.NextNode != null)
                {
                    kv.Value.NextNode = TNodes[(kv.Value.NextNode as IDNode).ID];
                }
                if (kv.Value.Parent != null)
                {
                    if (Name == (kv.Value.Parent as IDArea).Name)
                    {
                        kv.Value.Parent = this;
                    }
                    else
                    {
                        kv.Value.Parent = Areas.Find(m => m.Name == (kv.Value.Parent as IDArea).Name);
                    }
                }
                if (kv.Value is Status st)
                {
                    for (int i = 0; i < st.Choices.Count; i++)
                    {
                        st.Choices[i].Node = st.Choices[i].Node != null ?
                                             TNodes[(st.Choices[i].Node as IDNode).ID] : null;
                    }
                }
            }
        }