Exemple #1
0
        private static void BackgroundStatistics()
        {
            int  resetTimer         = 600;
            long lastMinuteSessions = -1;

            while (true)
            {
                resetTimer -= 5;

                string serverJson = File.ReadAllText(DATA_FILE);
                ServerDataManifest.Rootobject serverDataValues =
                    JsonConvert.DeserializeObject <ServerDataManifest.Rootobject>(serverJson);

                DayOfWeek weekDay = DateTime.Today.DayOfWeek;
                DateTime  date    = DateTime.Now;

                // Reset weekly statistics
                if (weekDay.ToString() == "Monday" && resetWeek == false)
                {
                    resetWeek = true;

                    serverDataValues.DaysOpened  = resetDaysOpened;
                    serverDataValues.WeeklyOpens = 0;
                }
                else if (weekDay.ToString() != "Monday")
                {
                    resetWeek = false;
                }

                // Reset monthly statistics
                if (date.Month.ToString() != lastMonth)
                {
                    lastMonth = date.Month.ToString();
                    serverDataValues.MonthlyOpens = 0;
                }

                // Reset yearly statistics
                if (date.DayOfYear == 1)
                {
                    serverDataValues.YearlyOpens = 0;
                }

                // Reset active statistics
                if (resetTimer == 0)
                {
                    lastMinuteSessions = serverDataValues.CurrentlyOpenSessions;
                    serverDataValues.CurrentlyOpenSessions = 0;
                    onlineUsers.Clear();
                }

                if (serverDataValues.CurrentlyOpenSessions < 0)
                {
                    serverDataValues.CurrentlyOpenSessions = 0;
                }

                if (viewStatistics)
                {
                    Console.Clear();
                    Console.WriteLine("You're currently viewing statistics.");
                    Console.WriteLine("Press E to go back to the main console menu.");
                    Console.WriteLine("Press S to speed up/slow down statistics.\n");

                    int secondsUntilReset = resetTimer;
                    int minutesUntilReset = 0;

                    while (secondsUntilReset > 59)
                    {
                        minutesUntilReset++;
                        secondsUntilReset -= 60;
                    }

                    Console.WriteLine("Time until open sessions reset: {0}m {1}s\n", minutesUntilReset, secondsUntilReset);
                    Console.WriteLine("Currently open sessions: " + serverDataValues.CurrentlyOpenSessions);
                    if (lastMinuteSessions == -1)
                    {
                        Console.WriteLine("Last 10m open sessions: waiting for data...");
                    }
                    else
                    {
                        Console.WriteLine("Last 10m open sessions: " + lastMinuteSessions);
                    }


                    Console.WriteLine("\nTotal opens: " + serverDataValues.AmountOpened);
                    Console.WriteLine("Total projects: " + serverDataValues.ProjectsCount);
                    Console.WriteLine("Opens this year: " + serverDataValues.YearlyOpens);
                    Console.WriteLine("Opens this month: " + serverDataValues.MonthlyOpens);
                    Console.WriteLine("Opens this week: " + serverDataValues.WeeklyOpens);

                    Console.WriteLine("-----------------");
                    foreach (string day in serverDataValues.DaysOpened)
                    {
                        Console.WriteLine(day);
                    }
                }

                // Write new values
                StringBuilder sb = new StringBuilder();
                StringWriter  sw = new StringWriter(sb);

                using (JsonWriter js = new JsonTextWriter(sw)) {
                    js.Formatting = Formatting.Indented;

                    js.WriteStartObject();

                    js.WritePropertyName("AmountOpened");
                    js.WriteValue(serverDataValues.AmountOpened);

                    js.WritePropertyName("DaysOpened");
                    js.WriteStartArray();
                    foreach (string day in serverDataValues.DaysOpened)
                    {
                        js.WriteValue(day);
                    }
                    js.WriteEnd();

                    js.WritePropertyName("ProjectsCount");
                    js.WriteValue(serverDataValues.ProjectsCount);

                    js.WritePropertyName("YearlyOpens");
                    js.WriteValue(serverDataValues.YearlyOpens);

                    js.WritePropertyName("MonthlyOpens");
                    js.WriteValue(serverDataValues.MonthlyOpens);

                    js.WritePropertyName("WeeklyOpens");
                    js.WriteValue(serverDataValues.WeeklyOpens);

                    js.WritePropertyName("CurrentlyOpenSessions");
                    js.WriteValue(serverDataValues.CurrentlyOpenSessions);

                    js.WriteEndObject();
                }

                File.WriteAllText(DATA_FILE, sw.ToString());
                sb.Clear();
                sw.Close();

                if (resetTimer == 0)
                {
                    resetTimer = 600;
                }

                if (speedUpStatistics)
                {
                    Thread.Sleep(1000);
                }
                else
                {
                    Thread.Sleep(5000);
                }
            }
        }
Exemple #2
0
        private static void CopyData(string origin)
        {
            string clientJson = File.ReadAllText(origin);

            ClientDataManifest.Rootobject clientDataValues =
                JsonConvert.DeserializeObject <ClientDataManifest.Rootobject>(clientJson);

            string serverJson = File.ReadAllText(DATA_FILE);

            ServerDataManifest.Rootobject serverDataValues =
                JsonConvert.DeserializeObject <ServerDataManifest.Rootobject>(serverJson);

            serverDataValues.AmountOpened += clientDataValues.AmountOpened;

            for (int i = 0; i < serverDataValues.DaysOpened.Length; i++)
            {
                clientDataValues.DaysOpened[i] = Regex.Replace(clientDataValues.DaysOpened[i], @"\s+", "");

                string[] clientSides = clientDataValues.DaysOpened[i].Split(':');
                long     clientValue = Int64.Parse(clientSides[1]);

                serverDataValues.DaysOpened[i] = Regex.Replace(serverDataValues.DaysOpened[i], @"\s+", "");

                string[] serverSides = serverDataValues.DaysOpened[i].Split(':');
                long     serverValue = Int64.Parse(serverSides[1]);

                serverValue += clientValue;

                string rebuiltString = serverSides[0] + ": " + serverValue;
                serverDataValues.DaysOpened[i] = rebuiltString;
            }

            serverDataValues.ProjectsCount += clientDataValues.ProjectsCount;

            serverDataValues.YearlyOpens  += clientDataValues.YearlyOpens;
            serverDataValues.MonthlyOpens += clientDataValues.MonthlyOpens;
            serverDataValues.WeeklyOpens  += clientDataValues.WeeklyOpens;

            if (clientDataValues.IsOpen && !onlineUsers.Contains(clientDataValues.UserID))
            {
                serverDataValues.CurrentlyOpenSessions++;
                onlineUsers.Add(clientDataValues.UserID);
            }

            // Write new values
            StringBuilder sb = new StringBuilder();
            StringWriter  sw = new StringWriter(sb);

            using (JsonWriter js = new JsonTextWriter(sw)) {
                js.Formatting = Formatting.Indented;

                js.WriteStartObject();

                js.WritePropertyName("AmountOpened");
                js.WriteValue(serverDataValues.AmountOpened);

                js.WritePropertyName("DaysOpened");
                js.WriteStartArray();
                foreach (string day in serverDataValues.DaysOpened)
                {
                    js.WriteValue(day);
                }
                js.WriteEnd();

                js.WritePropertyName("ProjectsCount");
                js.WriteValue(serverDataValues.ProjectsCount);

                js.WritePropertyName("YearlyOpens");
                js.WriteValue(serverDataValues.YearlyOpens);

                js.WritePropertyName("MonthlyOpens");
                js.WriteValue(serverDataValues.MonthlyOpens);

                js.WritePropertyName("WeeklyOpens");
                js.WriteValue(serverDataValues.WeeklyOpens);

                js.WritePropertyName("CurrentlyOpenSessions");
                js.WriteValue(serverDataValues.CurrentlyOpenSessions);

                js.WriteEndObject();
            }

            File.WriteAllText(DATA_FILE, sw.ToString());
            sb.Clear();
            sw.Close();
        }