Exemple #1
0
        public void OnEntityComponentAdded(ComponentsChangedEvent args)
        {
            if (CachedEntities.ContainsKey(args.Entity.Id))
            {
                if (false == Token.LookupGroup.ContainsAnyExcludedComponents(args.Entity))
                {
                    return;
                }

                entityRemoving.OnNext(args.Entity);

                CachedEntities.Remove(args.Entity.Id);

                entityRemoved.OnNext(args.Entity);

                return;
            }

            if (false == Token.LookupGroup.Matches(args.Entity))
            {
                return;
            }

            CachedEntities.Add(args.Entity.Id, args.Entity);

            entityAdded.OnNext(args.Entity);
        }
Exemple #2
0
        public void OnEntityRemovedFromCollection(CollectionEntityEvent args)
        {
            if (false == CachedEntities.ContainsKey(args.Entity.Id))
            {
                return;
            }

            CachedEntities.Remove(args.Entity.Id);

            entityRemoved.OnNext(args.Entity);
        }
Exemple #3
0
        public void OnEntityComponentRemoving(ComponentsChangedEvent args)
        {
            if (false == CachedEntities.ContainsKey(args.Entity.Id))
            {
                return;
            }

            if (Token.LookupGroup.ContainsAnyRequiredComponents(args.ComponentTypeIds))
            {
                entityRemoving.OnNext(args.Entity);
            }
        }
Exemple #4
0
        public GuildEntity GetOrCreate(SnowflakeObject sf)
        {
            if (!(sf is DiscordGuild gld))
            {
                throw new InvalidOperationException("Given entity was not a DiscordGuild.");
            }

            if (CachedEntities.TryGetValue(gld.Id, out var entity))
            {
                _db.Logger.Print(LogLevel.Debug, "Database", $"GET_FROM_CACHE://guild/{gld.Id}");
                return(entity);
            }

            entity = Get(gld.Id);

            if (!(entity is null))
            {
                if (!CachedEntities.TryAdd(gld.Id, entity))
                {
                    throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen.");
                }

                return(entity);
            }

            Create(new GuildEntity
            {
                Id       = gld.Id,
                Prefixes = new List <string>
                {
                    _db.Configuration.Prefix
                },
                Levenshtein = true,
                Music       = new MusicConfiguration
                {
                    Volume = 50,
                    DjOnly = false,
                    Loop   = false
                },
                CustomCommands = new List <CustomCommand>()
            });

            entity = Get(gld.Id);

            if (!CachedEntities.TryAdd(gld.Id, entity))
            {
                throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen.");
            }

            return(entity);
        }
Exemple #5
0
        public void OnEntityAddedToCollection(CollectionEntityEvent args)
        {
            // This is because you may have fired a blueprint before it is created
            if (CachedEntities.ContainsKey(args.Entity.Id))
            {
                return;
            }

            if (false == Token.LookupGroup.Matches(args.Entity))
            {
                return;
            }

            CachedEntities.Add(args.Entity.Id, args.Entity);

            entityAdded.OnNext(args.Entity);
        }
Exemple #6
0
        public bool UnReify(EqGoal goal)
        {
            if (CachedEntities.Count == 0)
            {
                return(false);
            }

            var goalVar = goal.Lhs as Var;

            Debug.Assert(goalVar != null);

            if (ContainsVar(goalVar))
            {
                CachedEntities.Clear();
                return(true);
            }
            return(false);
        }
Exemple #7
0
        public UserEntity GetOrCreate(SnowflakeObject sf)
        {
            if (!(sf is DiscordUser usr))
            {
                throw new InvalidOperationException("Given entity was not a DiscordUser.");
            }

            if (CachedEntities.TryGetValue(usr.Id, out var entity))
            {
                _db.Logger.Print(LogLevel.Debug, "Database", $"GET_FROM_CACHE://user/{usr.Id}");
                return(entity);
            }

            entity = Get(usr.Id);

            if (!(entity is null))
            {
                if (!CachedEntities.TryAdd(usr.Id, entity))
                {
                    throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen.");
                }

                return(entity);
            }

            Create(new UserEntity
            {
                Id             = usr.Id,
                RewardCooldown = DateTime.Now,
                Gold           = 0,
                IsBlacklisted  = false,
                Experience     = 0,
                Playlists      = new List <UserPlaylist>()
            });

            entity = Get(usr.Id);

            if (!CachedEntities.TryAdd(usr.Id, entity))
            {
                throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen.");
            }

            return(entity);
        }
Exemple #8
0
        public MemberEntity GetOrCreate(SnowflakeObject sf, ulong guildId)
        {
            if (!(sf is DiscordMember mbr))
            {
                throw new InvalidOperationException("Given entity was not a DiscordMember.");
            }

            if (CachedEntities.TryGetValue(mbr.Id, out var entity))
            {
                _db.Logger.Print(LogLevel.Debug, "Database", $"GET_FROM_CACHE://member/{mbr.Id}");
                return(entity);
            }

            entity = Get(mbr.Id);

            if (!(entity is null))
            {
                if (!CachedEntities.TryAdd(mbr.Id, entity))
                {
                    throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen.");
                }

                return(entity);
            }

            Create(new MemberEntity
            {
                Id             = mbr.Id,
                RewardCooldown = DateTime.Now,
                Experience     = 0,
                GuildId        = guildId
            });

            entity = Get(mbr.Id);

            if (!CachedEntities.TryAdd(mbr.Id, entity))
            {
                throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen.");
            }

            return(entity);
        }
Exemple #9
0
        public ChannelEntity GetOrCreate(SnowflakeObject sf)
        {
            if (!(sf is DiscordChannel chn))
            {
                throw new InvalidOperationException("Given entity was not a DiscordChannel.");
            }

            if (CachedEntities.TryGetValue(chn.Id, out var entity))
            {
                _db.Logger.Print(LogLevel.Debug, "Database", $"GET_FROM_CACHE://channel/{chn.Id}");
                return(entity);
            }

            entity = Get(chn.Id);

            if (!(entity is null))
            {
                if (!CachedEntities.TryAdd(chn.Id, entity))
                {
                    throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen.");
                }

                return(entity);
            }

            Create(new ChannelEntity
            {
                Id         = chn.Id,
                Modules    = new ConcurrentDictionary <string, ModuleConfiguration>(),
                RiotRegion = "EUW"
            });

            entity = Get(chn.Id);

            if (!CachedEntities.TryAdd(chn.Id, entity))
            {
                throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen.");
            }

            return(entity);
        }
Exemple #10
0
        public void OnEntityComponentRemoved(ComponentsChangedEvent args)
        {
            if (CachedEntities.ContainsKey(args.Entity.Id))
            {
                if (args.Entity.HasAllComponents(Token.LookupGroup.RequiredComponents))
                {
                    return;
                }

                CachedEntities.Remove(args.Entity.Id);
                entityRemoved.OnNext(args.Entity);

                return;
            }

            if (false == Token.LookupGroup.Matches(args.Entity))
            {
                return;
            }

            CachedEntities.Add(args.Entity.Id, args.Entity);

            entityAdded.OnNext(args.Entity);
        }
        public static void Refresh()
        {
            using (var db = new DatabaseContext())
            {
                db.Artists.Load();
                db.Characters.Load();
                db.Genres.Load();
                db.Roles.Load();
                db.Titles.Load();
                db.TitleArtists.Load();
                db.TitleArtistCharacters.Load();
                db.TitleGenres.Load();

                var entities = new CachedEntities
                {
                    Artists = db.Artists.Local,
                    Characters = db.Characters.Local,
                    Genres = db.Genres.Local,
                    Roles = db.Roles.Local,
                    Titles = db.Titles.Local,
                    TitleArtists = db.TitleArtists.Local,
                    TitleArtistCharacters = db.TitleArtistCharacters.Local,
                    TitleGenres = db.TitleGenres.Local
                };

                var json = JsonConvert.SerializeObject(
                    entities,
                    Formatting.Indented,
                    settings: new JsonSerializerSettings
                    {
                        ContractResolver = new CachedEntitiesContractResolver()
                    });

                MemoryCache.Default[nameof(EntityCache)] = json;
            }
        }
Exemple #12
0
 public bool HasEntity(int id) => CachedEntities.ContainsKey(id);
Exemple #13
0
        public bool Reify(EqGoal goal)
        {
            var lhsTerm = Lhs as Term;
            var rhsTerm = Rhs as Term;
            var lhsVar  = Lhs as Var;
            var rhsVar  = Rhs as Var;

            string strategy = "Reify equation's internal variable by substituing a given fact.";

            if (lhsVar != null)
            {
                var lhsNum = LogicSharp.Reify(lhsVar, goal.ToDict());
                if (lhsNum != null && !lhsNum.Equals(lhsVar))
                {
                    var cloneEq = Clone();
                    cloneEq.Lhs = lhsNum;

                    string rule        = SubstitutionRule.ApplySubstitute();
                    string appliedRule = SubstitutionRule.ApplySubstitute(this, cloneEq);
                    var    ts          = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                    cloneEq._innerLoop.Add(ts);
                    cloneEq.GenerateATrace(strategy);
                    CachedEntities.Add(cloneEq);
                    return(true);
                }
            }

            if (rhsVar != null)
            {
                var rhsNum = LogicSharp.Reify(rhsVar, goal.ToDict());
                if (rhsNum != null && !rhsNum.Equals(lhsVar))
                {
                    var cloneEq = Clone();
                    cloneEq.Rhs = rhsNum;
                    string rule        = SubstitutionRule.ApplySubstitute();
                    string appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                    var    ts          = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                    cloneEq._innerLoop.Add(ts);
                    cloneEq.GenerateATrace(strategy);
                    CachedEntities.Add(cloneEq);
                    return(true);
                }
            }

            if (lhsTerm != null)
            {
                var term1 = lhsTerm.Reify(goal) as Term;
                if (lhsTerm.Equals(term1) || term1 == null)
                {
                    return(false);
                }
                var obj = term1.Eval();

                var cloneEq = Clone();
                cloneEq.Lhs = obj;
                string rule        = SubstitutionRule.ApplySubstitute();
                string appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                var    ts          = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                cloneEq._innerLoop.Add(ts);
                cloneEq.GenerateATrace(strategy);
                CachedEntities.Add(cloneEq);

                return(true);
            }

            if (rhsTerm != null)
            {
                object obj = rhsTerm.Reify(goal);
                if (rhsTerm.Equals(obj))
                {
                    return(false);
                }
                var cloneEq = Clone();
                cloneEq.Rhs = obj;
                string rule        = SubstitutionRule.ApplySubstitute();
                string appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                var    ts          = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                cloneEq._innerLoop.Add(ts);
                cloneEq.GenerateATrace(strategy);

                CachedEntities.Add(cloneEq);
                return(true);
            }
            return(false);
        }