public AdminTasksController(IDAFactory daFactory, JWTFactory jwt, IGluonHostPool hostPool) : base("/admin")
        {
            JWTTokenAuthentication.Enable(this, jwt);

            Get["/tasks"] = _ =>
            {
                this.DemandAdmin();

                using (var da = daFactory.Get)
                {
                    var offset = Request.Query["offset"];
                    var limit  = Request.Query["limit"];

                    if (offset == null)
                    {
                        offset = 0;
                    }
                    if (limit == null)
                    {
                        limit = 20;
                    }

                    if (limit > 100)
                    {
                        limit = 100;
                    }

                    var result = da.Tasks.All((int)offset, (int)limit);
                    return(Response.AsPagedList(result));
                }
            };

            Post["/tasks/request"] = x =>
            {
                var task = this.Bind <TaskRequest>();

                var taskServer = hostPool.GetByRole(Database.DA.Hosts.DbHostRole.task).FirstOrDefault();
                if (taskServer == null)
                {
                    return(Response.AsJson(-1));
                }
                else
                {
                    try {
                        var id = taskServer.Call(new RequestTask()
                        {
                            TaskType      = task.task_type.ToString(),
                            ParameterJson = JsonConvert.SerializeObject(task.parameter),
                            ShardId       = (task.shard_id == null || !task.shard_id.HasValue) ? -1 : task.shard_id.Value
                        }).Result;
                        return(Response.AsJson(id));
                    }catch (Exception ex)
                    {
                        return(Response.AsJson(-1));
                    }
                }
            };
        }
Example #2
0
        public void Run(TaskContext context)
        {
            var cityServers = HostPool.GetByRole(Database.DA.Hosts.DbHostRole.city);

            foreach (var city in cityServers)
            {
                city.Write(new CityNotify(CityNotifyType.NhoodUpdate));
            }
        }
Example #3
0
        public void Run(TaskContext context)
        {
            //ensure the task is configured
            var tuning = Tuning.BirthdayGift;

            if (tuning == null)
            {
                return;
            }

            using (var da = DAFactory.Get())
            {
                const float dayLength = (24 * 60 * 60);
                //get a list of all avatars and their ages
                var avatars   = da.Avatars.All().ToList();
                var now       = Epoch.Now;
                var mailItems = new List <MessageItem>();

                //iterate all the gift items
                foreach (var item in tuning.items)
                {
                    var targAge = item.age;

                    var eventName           = "bday_" + item.age;
                    var alreadyAwardedUsers = new HashSet <uint>(da.Events.GetGenericParticipatingAvatars(eventName));
                    var toBeAwarded         = avatars.Where(x => {
                        var daysOld = (now - x.date) / dayLength;
                        return(daysOld > targAge && !alreadyAwardedUsers.Contains(x.avatar_id));
                    });

                    //attempt to give all 'to be awarded' avatars their awards
                    foreach (var avatar in toBeAwarded)
                    {
                        if (da.Events.GenericAvaTryParticipate(new DbGenericAvatarParticipation()
                        {
                            participation_name = eventName, participation_avatar = avatar.avatar_id
                        }))
                        {
                            LOG.Info($"Sending {item.age} day birthday gift to {avatar.name}.");
                            //award the object
                            da.Objects.Create(new Database.DA.Objects.DbObject()
                            {
                                type         = item.guid,
                                shard_id     = avatar.shard_id,
                                owner_id     = avatar.avatar_id,
                                lot_id       = null, //to inventory
                                dyn_obj_name = ""
                            });

                            //send email
                            if (item.mail_message != null)
                            {
                                mailItems.Add(new MessageItem()
                                {
                                    Subject    = item.mail_subject ?? "Age Gift",
                                    Body       = item.mail_message,
                                    SenderID   = uint.MaxValue,
                                    SenderName = item.mail_sender_name ?? "The Sims Online",
                                    TargetID   = avatar.avatar_id,
                                    Type       = 4,
                                    Subtype    = 0
                                });
                            }
                        }
                    }
                }

                if (mailItems.Count > 0)
                {
                    //TODO: MULTI-CITY - select mail target by shard id
                    var cityServers = HostPool.GetByRole(Database.DA.Hosts.DbHostRole.city);

                    foreach (var city in cityServers)
                    {
                        city.Write(new SendCityMail(mailItems));
                    }
                }
            }
        }
        public void Run(TaskContext context)
        {
            var tuning = Tuning.JobBalance;

            if (tuning == null)
            {
                tuning = new JobBalanceTuning();
            }

            //obtain aggregate for all skills
            using (var db = DAFactory.Get())
            {
                var days      = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalDays;
                var limitdays = days - tuning.days_to_aggregate;
                var aggregate = db.DynPayouts.GetSummary(limitdays);

                var missing = TransactionToType.Keys.Where(x => !aggregate.Any(y => y.transaction_type == x));
                var baseSum = (aggregate.Count == 0) ? 1 : 0;
                foreach (var miss in missing)
                {
                    aggregate.Add(new DbTransSummary()
                    {
                        transaction_type = miss, sum = baseSum
                    });
                }
                aggregate = aggregate.OrderBy(x => x.transaction_type).ToList();

                var totalPayouts = aggregate.Sum(x => x.sum);
                //get the percent of all single money payouts that each object gave out
                //since some jobs take longer to complete, their payout counts need to be rescaled,
                //so that one payout counts as like, 1.1.
                int i             = 0;
                var percentIncome = aggregate.Select(x => TypeCompletionTimes[(i++)] / BaseCompletionTime * ((float)x.sum) / totalPayouts);

                var rand = new Random();
                //convert this into our base multipler, with some variation
                var multipliers = percentIncome.Select(x => PercentToBaseMultiplier(x) + tuning.mul_variation * ((float)rand.NextDouble() - 0.5f)).ToList();

                //pick a job that recieves a notable boost. there is a chance that no job gets this boost.
                var bonusJob = rand.Next(TypeCompletionTimes.Length + 1);
                if (bonusJob < TypeCompletionTimes.Length)
                {
                    multipliers[bonusJob] -= tuning.mul_bonus_job + tuning.mul_variation * (float)rand.NextDouble();
                }

                //finally,  scale into the final range.
                var finalMultipliers = multipliers.Select(x => Math.Min(1f, Math.Max(0f, 1 - x)) * (tuning.max_multiplier - tuning.min_multiplier) + tuning.min_multiplier);

                //ace af. lets put these into the database. log the raw multipliers first...
                var dbEntries = new List <DbDynPayout>();
                i = 0;
                foreach (var mul in finalMultipliers)
                {
                    dbEntries.Add(new DbDynPayout()
                    {
                        day        = days,
                        multiplier = mul,
                        skilltype  = i,
                        flags      = (i == bonusJob) ? 1 : 0
                    });
                    i++;
                }
                db.DynPayouts.InsertDynRecord(dbEntries);

                //...then we calculate and replace the object tuning.
                var dbTuning = new List <DbTuning>();
                i = 0;
                foreach (var mul in finalMultipliers)
                {
                    //we need to scale the multiplier by the time it takes to get one payout.
                    //the longer it takes than the base, the more we pay out in one instance.
                    var timeScaledMulti = mul * (TypeCompletionTimes[i] / BaseCompletionTime);
                    dbTuning.Add(new DbTuning()
                    {
                        tuning_type  = "skillobjects.iff",
                        tuning_table = 8207 - 8192,
                        tuning_index = ToTuningIndex[i],
                        value        = (int)Math.Round((100 * timeScaledMulti) - 100),
                        owner_type   = DbTuningType.DYNAMIC,
                        owner_id     = 1
                    });
                    i++;
                }

                db.DynPayouts.ReplaceDynTuning(dbTuning);
                //tell lots tuning has changed
                var lots = HostPool.GetByRole(Database.DA.Hosts.DbHostRole.lot);
                foreach (var lot in lots)
                {
                    lot.Write(new TuningChanged()
                    {
                        UpdateInstantly = true
                    });
                }
            }
        }