Example #1
0
        private static DatabaseContext AddPenalty(DatabaseContext context, EFPenalty penalty, int count, int commitCount, bool recreateContext)
        {
            context.Penalties.Add(penalty);
            if (count % commitCount == 0)
            {
                try
                {
                    context.SaveChanges();
                }

                catch (Exception)
                {
                }

                if (recreateContext)
                {
                    context.Dispose();
                    context = new DatabaseContext();
                    context.Configuration.AutoDetectChangesEnabled = false;
                    context.Configuration.LazyLoadingEnabled       = false;
                    context.Configuration.ProxyCreationEnabled     = false;
                }
            }

            return(context);
        }
Example #2
0
        public static void ImportPenalties(IList <Penalty> penalties)
        {
            DatabaseContext context = null;

            try
            {
                context = new DatabaseContext();
                context.Configuration.AutoDetectChangesEnabled = false;
                context.Configuration.LazyLoadingEnabled       = false;
                context.Configuration.ProxyCreationEnabled     = false;

                int count = 0;
                foreach (var entityToInsert in penalties)
                {
                    ++count;
                    var punisher = entityToInsert.Offender.NetworkId == entityToInsert.Punisher.NetworkId ?
                                   context.Clients.SingleOrDefault(c => c.ClientId == 1) :
                                   context.Clients.SingleOrDefault(c => c.NetworkId == entityToInsert.Punisher.NetworkId);
                    if (punisher == null)
                    {
                        continue;
                    }
                    var offender = context.Clients.Include("AliasLink").SingleOrDefault(c => c.NetworkId == entityToInsert.Offender.NetworkId);

                    if (offender == null)
                    {
                        continue;
                    }


                    var penalty = new EFPenalty()
                    {
                        Active   = true,
                        Expires  = entityToInsert.Expires.Year == 9999 ? DateTime.Parse(System.Data.SqlTypes.SqlDateTime.MaxValue.ToString()) : entityToInsert.Expires,
                        Offender = offender,
                        Punisher = punisher,
                        Offense  = entityToInsert.Offense,
                        Type     = entityToInsert.Type,
                        When     = entityToInsert.When == DateTime.MinValue ? DateTime.UtcNow : entityToInsert.When,
                        Link     = offender.AliasLink
                    };

                    context = AddPenalty(context, penalty, count, 1000, true);
                }

                context.SaveChanges();
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }
Example #3
0
 public abstract Task Kick(string reason, EFClient target, EFClient origin, EFPenalty originalPenalty);
        public string BuildFormattedMessage(IRConParserConfiguration config, EFPenalty currentPenalty, EFPenalty originalPenalty = null)
        {
            var isNewLineSeparator = config.NoticeLineSeparator == Environment.NewLine;
            var penalty            = originalPenalty ?? currentPenalty;
            var builder            = new StringBuilder();
            // build the top level header
            var header = _transLookup[$"SERVER_{penalty.Type.ToString().ToUpper()}_TEXT"];

            builder.Append(header);
            builder.Append(config.NoticeLineSeparator);
            // build the reason
            var reason = _transLookup["GAME_MESSAGE_PENALTY_REASON"].FormatExt(penalty.Offense);

            if (isNewLineSeparator)
            {
                foreach (var splitReason in SplitOverMaxLength(reason, config.NoticeMaxCharactersPerLine))
                {
                    builder.Append(splitReason);
                    builder.Append(config.NoticeLineSeparator);
                }
            }

            else
            {
                builder.Append(reason);
                builder.Append(config.NoticeLineSeparator);
            }

            if (penalty.Type == EFPenalty.PenaltyType.TempBan)
            {
                // build the time remaining if temporary
                var timeRemainingValue = penalty.Expires.HasValue
                    ? (penalty.Expires - DateTime.UtcNow).Value.HumanizeForCurrentCulture()
                    : "--";
                var timeRemaining = _transLookup["GAME_MESSAGE_PENALTY_TIME_REMAINING"].FormatExt(timeRemainingValue);

                if (isNewLineSeparator)
                {
                    foreach (var splitReason in SplitOverMaxLength(timeRemaining, config.NoticeMaxCharactersPerLine))
                    {
                        builder.Append(splitReason);
                        builder.Append(config.NoticeLineSeparator);
                    }
                }

                else
                {
                    builder.Append(timeRemaining);
                }
            }

            if (penalty.Type == EFPenalty.PenaltyType.Ban)
            {
                // provide a place to appeal the ban (should always be specified but including a placeholder just incase)
                builder.Append(_transLookup["GAME_MESSAGE_PENALTY_APPEAL"].FormatExt(_appConfig.ContactUri ?? "--"));
            }

            // final format looks something like:

            /*
             * You are permanently banned
             * Reason - toxic behavior
             * Visit example.com to appeal
             */

            return(builder.ToString());
        }