Example #1
0
        public async Task <double> GetVotingDurationByPollType(PollTypes pollType, string tenantId)
        {
            var val = await GetSettingValueByType(Settings.VotingDuration, tenantId);

            var votingDuration = Convert.ToDouble(val.Value);

            return(pollType == PollTypes.AuthorityPoll ? 2 * votingDuration : votingDuration);
        }
Example #2
0
        public override void Run()
        {
            DateTime now = DateTime.UtcNow;
            // set the next time to the error time first off, so if an
            // exception happens, this is what we use.
            NextPollTime = CalculateNextPollTime(now, ERROR_RETRY_INTERVAL);

            StagesInfoRecord[] records;
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = Encoding.UTF8;
                string data = wc.DownloadString("http://s3-ap-northeast-1.amazonaws.com/splatoon-data.nintendo.net/stages_info.json");
                records = ProcessStagesInfo(data, pollType);
            }

            pollType = PollTypes.Ambient;
            NextPollTime = CalculateNextPollTime(now, AMBIENT_POLL_INTERVAL);

            if (records != null && records.Length > 0)
            {
                StagesInfoRecord record = records[0];
                if (IsRecordValid(record))
                {
                    DateTime endTimeUtc = TokyoToUtc((DateTime)record.datetime_term_end);
                    DateTime endTimePreEmpt = endTimeUtc.AddSeconds(-PRE_EMPT);
                    DateTime startTimeUtc = TokyoToUtc((DateTime)record.datetime_term_begin);
                    if (endTimePreEmpt < now)
                    {
                        // received data is stale so we are polling
                        // xxx: we need a rapid give-up time so that if the
                        // response format changes or something, we don't end up
                        // hammering the server indefinitely.
                        NextPollTime = now.AddSeconds(FAST_POLL_INTERVAL);
                        if (freshShortUpdate)
                            Console.Write(".");
                        else
                            Console.Write("Waiting for new maps.");
                        pollType = PollTypes.Fresh;

                        freshShortUpdate = true;
                    }
                    else if (endTimePreEmpt < NextPollTime)
                    {
                        // end of rotation comes sooner than ambient polling
                        NextPollTime = endTimePreEmpt;
                        Console.WriteLine("Polling for fresh maps at {0:G}.", NextPollTime.ToLocalTime());
                        pollType = PollTypes.Fresh;
                        freshShortUpdate = false;
                    }
                    else if (scrapeGear && startTimeUtc.AddHours(1) > now)
                    {
                        // This is the first hour of a new leaderboard.
                        // The leaderboard changes more rapidly early on, so we
                        // poll more frequently to increase our chance of picking
                        // up weapons/gear not already in the database.
                        NextPollTime = CalculateNextPollTime(now, RAPID_POLL_INTERVAL);
                        Console.WriteLine("Next poll at {0:G}.", NextPollTime.ToLocalTime());
                        freshShortUpdate = false;
                    }
                    else
                    {
                        Console.WriteLine("Next poll at {0:G}.", NextPollTime.ToLocalTime());
                        freshShortUpdate = false;
                    }
                }
                else if (FesInfoTask != null && FesInfoTask.LastRecord != null)
                {
                    FesInfoRecord lastRecord = FesInfoTask.LastRecord;
                    DateTime endTimeUtc = TokyoToUtc((DateTime)lastRecord.datetime_fes_end);
                    DateTime endTimePreEmpt = endTimeUtc.AddSeconds(-PRE_EMPT);

                    if (endTimePreEmpt < now)
                    {
                        // Splatfest just ended so get back on the ball asap.
                        NextPollTime = now.AddSeconds(FAST_POLL_INTERVAL);
                        if (freshShortUpdate)
                            Console.Write(".");
                        else
                            Console.Write("Waiting for new maps.");
                        pollType = PollTypes.Fresh;

                        freshShortUpdate = true;
                    }
                    else if (endTimePreEmpt < NextPollTime)
                    {
                        // end of splatfest comes sooner than ambient polling
                        NextPollTime = endTimePreEmpt;
                        Console.WriteLine("Polling for fresh maps at {0:G}.", NextPollTime.ToLocalTime());
                        pollType = PollTypes.Fresh;
                        freshShortUpdate = false;
                    }
                    else
                    {
                        Console.WriteLine("Server isn't providing data.");
                        Console.WriteLine("Next poll at {0:G}.", NextPollTime.ToLocalTime());
                        freshShortUpdate = false;
                    }
                }
                else
                {
                    Console.WriteLine("Server isn't providing data.");
                    Console.WriteLine("Next poll at {0:G}.", NextPollTime.ToLocalTime());
                    freshShortUpdate = false;
                }
            }
            else
            {
                Console.WriteLine("Server isn't providing data.");
                Console.WriteLine("Next poll at {0:G}.", NextPollTime.ToLocalTime());
                freshShortUpdate = false;
            }
        }
Example #3
0
        public async Task NotifyUsers(PollTypes pollType, PollNotificationTypes pollNotificationType, Poll poll)
        {
            var tenant = await _tenantService.GetTenant(poll.TenantId);

            if (tenant == null)
            {
                return;
            }
            var msg     = "";
            var msgEn   = "";
            var baseUrl = await _genericService.GetBaseUrl(poll.TenantId);

            var langDic       = new Dictionary <string, EmailDetailModel>();
            var pollTypeStr   = pollType.DescriptionLang("tr");
            var pollTypeStrEn = pollType.DescriptionLang("en");

            switch (pollNotificationType)
            {
            case PollNotificationTypes.Started:
                msg = $"<b>{poll.Name}</b> adlı oylama başlamıştır. "
                      + $"Oylamaya <a target =\"_blank\" href=\"{baseUrl}/polls\" style=\"font-weight: bold; color: #2F2F2F; cursor: pointer;\">"
                      + "buraya</a> tıklayarak ulaşabilirsiniz. ";


                msgEn = $"<b>{poll.Name}</b> has started. "
                        + $"In order to see poll details <a target =\"_blank\" href=\"{baseUrl}/polls\" style=\"font-weight: bold; color: #2F2F2F; cursor: pointer;\">"
                        + "click here</a> ";
                break;

            case PollNotificationTypes.Ended when pollType == PollTypes.AuthorityPoll:
                msg =
                    $" <b>{poll.Name}</b> adlı oylama sonuçlanmıştır.<br/> Sonuçlara <a target=\"_blank\" href=\""
                    + $"{baseUrl}/polls\" style=\"font-weight: bold; color: #2F2F2F; cursor: pointer;\">buraya</a> tıklayarak ulaşabilirsiniz.";

                msgEn =
                    $"Poll titled <b>{poll.Name}</b> has ended.<br/> In order to see the result <a target=\"_blank\" href=\""
                    + $"{baseUrl}/polls\" style=\"font-weight: bold; color: #2F2F2F; cursor: pointer;\"> click here</a>";
                break;

            case PollNotificationTypes.Ended:
            {
                string pollResEn;
                var    pollRes = pollResEn = poll.Result;


                var res = Enum.TryParse(poll.Result, out PollResults result);
                if (res)
                {
                    pollRes   = result.DescriptionLang("tr");
                    pollResEn = result.DescriptionLang("en");
                }

                msg = $"<strong>{poll.Name}</strong> adlı oylama, aşağıdaki şekilde sonuçlanmıştır. <br/>"
                      + $"<b>{pollRes}</b>";

                msgEn = $"<strong>{poll.Name}</strong> named poll is resulted as " + $"<b>{pollResEn}</b>";
                break;
            }

            case PollNotificationTypes.AboutToEnd:
                msg = $"<strong>{poll.Name}</strong> adlı oylama yakında sonlanacaktır.<br/>"
                      + $"Oyunuzu kullanmak için <a target =\"_blank\" href=\"{baseUrl}/polls\" style=\"font-weight: bold; color: #2F2F2F; cursor: pointer;\">"
                      + "buraya</a> tıklayınız.";

                msgEn = $"<strong>{poll.Name}</strong> named poll soon to be completed. <br/>"
                        + $"In order to vote <a target =\"_blank\" href=\"{baseUrl}/polls\" style=\"font-weight: bold; color: #2F2F2F; cursor: pointer;\">"
                        + "click here</a> .";
                break;
            }

            langDic.Add("en", new EmailDetailModel {
                Subject = pollTypeStrEn, Message = msgEn
            });
            langDic.Add("tr", new EmailDetailModel {
                Subject = pollTypeStr, Message = msg
            });

            if (pollNotificationType != PollNotificationTypes.AboutToEnd)
            {
                if (pollType == PollTypes.AuthorityPoll)
                {
                    BackgroundJob.Enqueue(() => _userService.SendEmailToAllUsers(poll.TenantId, langDic));
                }
                else
                {
                    BackgroundJob.Enqueue(() => _userService.SendEmailToVoters(poll.TenantId, langDic));
                }
            }
            else
            {
                BackgroundJob.Enqueue(() => SendEmailToNotVotedUsers(poll.TenantId, langDic, poll.Id));
            }
        }
Example #4
0
        private static StagesInfoRecord[] ProcessStagesInfo(string data, PollTypes pollType)
        {
            using (MySqlConnection conn = Database.CreateConnection())
            {
                conn.Open();

                StagesInfoRecord[] records = null;
                try
                {
                    records = GetStagesInfo(data);
                }
                catch { }

                bool isValid = records != null && records.Length > 0 &&
                    IsRecordValid(records[0]);
                using (MySqlTransaction tran = conn.BeginTransaction())
                {
                    Database.LogStagesInfo(tran, data, isValid);
                    if (!isValid)
                    {
                        if (records != null && records.Length > 0)
                            InsertMissingLeaderboard(tran);
                        return null;
                    }

                    tran.Commit();
                }

                int newStages = 0, newWeapons = 0,
                    newShoes = 0, newClothes = 0, newHead = 0;
                using (MySqlTransaction tran = conn.BeginTransaction())
                {
                    StagesInfoRecord record = records[0];
                    // scrape all the identifiers we can
                    foreach (StageRecord sr in record.stages)
                    {
                        bool isNew = false;
                        if (sr != null) Database.GetStageId(tran, sr, out isNew);
                        if (isNew) newStages++;
                    }

                    foreach (RankingRecord rr in record.ranking)
                    {
                        bool isNew = false;
                        if (rr.weapon_id != null) Database.GetWeaponId(tran, rr.weapon_id, out isNew);
                        if (isNew) newWeapons++;

                        isNew = false;
                        if (rr.gear_shoes_id != null) Database.GetShoesId(tran, rr.gear_shoes_id, out isNew);
                        if (isNew) newShoes++;

                        isNew = false;
                        if (rr.gear_clothes_id != null) Database.GetShirtId(tran, rr.gear_clothes_id, out isNew);
                        if (isNew) newClothes++;

                        isNew = false;
                        if (rr.gear_head_id != null) Database.GetHatId(tran, rr.gear_head_id, out isNew);
                        if (isNew) newHead++;
                    }
                    tran.Commit();
                }

                using (MySqlTransaction tran = conn.BeginTransaction())
                {
                    for (int x = 1; x < records.Length; x++)
                    {
                        int thisNewStages = 0, thisNewWeapons = 0,
                            thisNewShoes = 0, thisNewClothes = 0, thisNewHead = 0;
                        StagesInfoRecord record = records[x];
                        bool success = false;

                        if (IsRecordValid(record))
                        {
                            success = Database.InsertLeaderboard(tran, records[x],
                                out thisNewStages, out thisNewWeapons, out thisNewShoes, out thisNewClothes, out thisNewHead);
                        }

                        newStages += thisNewStages;
                        newWeapons += thisNewWeapons;
                        newShoes += thisNewShoes;
                        newClothes += thisNewClothes;
                        newHead += thisNewHead;

                        if (success)
                        {
                            if (pollType == PollTypes.Fresh) Console.WriteLine();
                            Console.WriteLine("Inserted leaderboard for {0} to {1}.", records[x].datetime_term_begin, records[x].datetime_term_end);
                        }
                    }
                    tran.Commit();
                }
                conn.Close();

                if (newStages == 1) Console.WriteLine("Inserted 1 new stage.");
                if (newStages > 1) Console.WriteLine("Inserted {0} new stages.", newStages);
                if (newWeapons == 1) Console.WriteLine("Inserted 1 new weapon.");
                if (newWeapons > 1) Console.WriteLine("Inserted {0} new weapons.", newWeapons);
                if (newShoes == 1) Console.WriteLine("Inserted 1 new shoe.");
                if (newShoes > 1) Console.WriteLine("Inserted {0} new shoes.", newShoes);
                if (newClothes == 1) Console.WriteLine("Inserted 1 new shirt.");
                if (newClothes > 1) Console.WriteLine("Inserted {0} new shirts.", newClothes);
                if (newHead == 1) Console.WriteLine("Inserted 1 new hat.");
                if (newHead > 1) Console.WriteLine("Inserted {0} new hats.", newHead);

                return records;
            }
        }
 public ActivePollByTypeSpecification(PollTypes pollType) : base(p => p.Active && p.PollType == pollType)
 {
 }