public override void Run() { DateTime now = DateTime.UtcNow; NextPollTime = CalculateNextPollTime(now, ERROR_RETRY_INTERVAL); string fes_info = null, fes_result = null, recent_results = null, contribution_ranking = null; using (WebClient wc = new WebClient()) { wc.Encoding = Encoding.UTF8; try { fes_info = wc.DownloadString("http://s3-ap-northeast-1.amazonaws.com/splatoon-data.nintendo.net/fes_info.json"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } try { fes_result = wc.DownloadString("http://s3-ap-northeast-1.amazonaws.com/splatoon-data.nintendo.net/fes_result.json"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } try { recent_results = wc.DownloadString("http://s3-ap-northeast-1.amazonaws.com/splatoon-data.nintendo.net/recent_results.json"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } try { contribution_ranking = wc.DownloadString("http://s3-ap-northeast-1.amazonaws.com/splatoon-data.nintendo.net/contribution_ranking.json"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } try { FesInfoRecord prevRecord = LastRecord; LastRecord = JsonConvert.DeserializeObject<FesInfoRecord>(fes_info); if (!freshShortUpdate) { if (prevRecord != null && prevRecord.fes_state == -1 && LastRecord.fes_state != -1) { Console.WriteLine("New Splatfest announced: {0} vs. {1}", LastRecord.team_alpha_name, LastRecord.team_bravo_name); } switch (LastRecord.fes_state) { case 0: Console.Write("Upcoming Splatfest at "); break; case 1: Console.Write("Ongoing Splatfest at "); break; case -1: Console.Write("Completed Splatfest at "); break; } Console.WriteLine("{0:G} to {1:G}.", LastRecord.datetime_fes_begin, LastRecord.datetime_fes_end); } } catch { LastRecord = null; } bool recordValid = IsRecordValid(LastRecord); using (MySqlConnection conn = Database.CreateConnection()) { conn.Open(); using (MySqlTransaction tran = conn.BeginTransaction()) { if (fes_info != null) Database.LogFesInfo(tran, fes_info, recordValid); if (fes_result != null) Database.LogFesResult(tran, fes_result, false); if (recent_results != null) ProcessRecentResults(tran, recent_results); if (contribution_ranking != null) ProcessContributionRanking(tran, contribution_ranking); tran.Commit(); } conn.Close(); } NextPollTime = CalculateNextPollTime(now, AMBIENT_POLL_INTERVAL); if (recordValid) { DateTime startTimeUtc = TokyoToUtc((DateTime)LastRecord.datetime_fes_begin); DateTime startTimePreEmpt = startTimeUtc.AddSeconds(-PRE_EMPT); int fesState = LastRecord.fes_state; if (fesState == 0 && startTimePreEmpt < now) { // received data is stale so we are polling NextPollTime = now.AddSeconds(FAST_POLL_INTERVAL); if (freshShortUpdate) Console.Write("."); else Console.Write("Waiting for Splatfest data."); freshShortUpdate = true; } else if (fesState == 0 && startTimePreEmpt < NextPollTime) { // end of rotation comes sooner than ambient polling NextPollTime = startTimePreEmpt; Console.WriteLine("Polling for Splatfest data at {0:G}.", NextPollTime.ToLocalTime()); freshShortUpdate = false; } else if (fesState == 1) { NextPollTime = CalculateNextPollTime(now, RAPID_POLL_INTERVAL); Console.WriteLine("Next Splatfest poll at {0:G}.", NextPollTime.ToLocalTime()); freshShortUpdate = false; } else { Console.WriteLine("Next Splatfest poll at {0:G}.", NextPollTime.ToLocalTime()); freshShortUpdate = false; } } else { Console.WriteLine("Splatfest information unavailable."); Console.WriteLine("Next Splatfest poll at {0:G}.", NextPollTime.ToLocalTime()); freshShortUpdate = false; } }
private bool IsRecordValid(FesInfoRecord record) { return record != null //&& new int[] { -1, 0, 1}.Contains(record.fes_state) && record.fes_state <= 1 && record.fes_state >= -1 && record.datetime_fes_begin != null && record.datetime_fes_end != null; }