PRoConEvents.MULTIbalancer.UpdateTicketLossRateLog C# (CSharp) Method

UpdateTicketLossRateLog() private method

private UpdateTicketLossRateLog ( System.DateTime now, int strong, int weak ) : void
now System.DateTime
strong int
weak int
return void
        private void UpdateTicketLossRateLog(DateTime now, int strong, int weak)
        {
            /*
            Log will be log rolled at midnight, so date is built into the log name
            Log will be log rolled by round-map-mode
            Sequence number follows date to disambiguate round-map-mode
            Log name template: YYYYMMDD_Seq_Round-Map-ModeCode_tlr.csv
            Example: 20130713_09_2-Caspian_Border-CL0_tlr.csv
            Time: HH:MM:SS
            Round: Number
            Map: Text
            Mode: Text
            Max Players: Number
            US Players: Number
            RU Players: Number
            US Tickets: Number
            RU Tickets: Number
            Samples: Number
            US Average Ticket Loss: Number (looking backward for Samples, normalized to a positive value)
            RU Average Ticket Loss: Number (looking backward for Samples, normalized to a positive value)
            Ratio%: Number (as a percentage)
            Strong unstacked to: Number (0 means no unstack this entry, 1 means to US team, 2 means to RU team)
            Weak unstacked to: Number (0 means no unstack this entry, 1 means to US team, 2 means to RU team)
            */

            if (fServerInfo == null || TotalPlayerCount() < 4 || fGameState != GameState.Playing) return;

            String path = String.Empty;

            try {
            String date = now.ToString("yyyyMMdd");
            String suffix = "tlr.csv";
            String map = GetRoundMapMode();
            String log = String.Join("_", new String[]{date, String.Format("{0:D3}", fRoundsEnabled), map, suffix});
            path = Path.Combine(Path.Combine("Logs", fHost + "_" + fPort), log);
            DebugWrite("^9^bDEBUG^n: UpdateTicketLossRateLog " + path + " at " + now, 8);

            PerModeSettings perMode = GetPerModeSettings();
            String[] row = new String[18]; // index of array is column number
            row[0] = now.ToString("HH:mm:ss");
            row[1] = (fServerInfo.CurrentRound + 1).ToString();
            row[2] = FriendlyMap;
            row[3] = FriendlyMode;
            row[4] = fServerInfo.MaxPlayerCount.ToString();
            row[5] = fTeam1.Count.ToString();
            row[6] = fTeam2.Count.ToString();
            row[7] = fTickets[1].ToString();
            row[8] = ((IsRush()) ? Convert.ToInt32(Math.Max(fTickets[1]/2, fMaxTickets - (fRushMaxTickets - fTickets[2]))) : fTickets[2]).ToString();
            row[9] = perMode.TicketLossSampleCount.ToString();
            double a1 = GetAverageTicketLossRate(1, true);
            row[10] = a1.ToString("F3");
            double a2 = GetAverageTicketLossRate(2, true);
            row[11] = a2.ToString("F3");
            double ratio = (a1 > a2) ? (a1/Math.Max(1, a2)) : (a2/Math.Max(1, a1));
            ratio = Math.Min(ratio, 50.0); // cap at 50x
            ratio = ratio * 100.0;
            row[12] = ratio.ToString("F0");
            row[13] = strong.ToString();
            row[14] = weak.ToString();
            // Spares for future expansion
            row[15] = String.Empty;
            row[16] = String.Empty;
            row[17] = String.Empty;

            if (!Path.IsPathRooted(path)) path = Path.Combine(Directory.GetParent(Application.ExecutablePath).FullName, path);

            // Add newline
            String entry = String.Join(",", row) + "\n";

            lock (fAverageTicketLoss) { // mutex access to log file
            using (FileStream fs = File.Open(path, FileMode.Append)) {
                Byte[] info = new UTF8Encoding(true).GetBytes(entry);
                fs.Write(info, 0, info.Length);
            }
            }
            } catch (Exception ex) {
            ConsoleError("Unable to append to log file: " + path);
            ConsoleError(ex.ToString());
            }
        }
MULTIbalancer