// Formats the results. Sorry Chris, couldn't think of a more descriptive comment.
 // I suppose I could go into what the arguments are. But then again, I could just leave it be.
 void formatResults(ref string data, List<MatchResult> results, Tags tag, Tournament tournament)
 {
     var rank = 1;
     foreach (var result in results)
     {
         if (chkExcludeForfeits.Checked && result.Forfeited) continue;
         var player = Config.Settings.GetPlayer(result.PlayerID);
         var rankString = result.Forfeited ? "F" : rank++.ToString();
         data += tag.rowStart + tag.cellStart + rankString + tag.cellEnd + tag.cellStart + player.Name + tag.cellEnd;
         if (chkShowFactions.Checked) data += tag.cellStart + tournament.PlayerFaction[player.ID] + tag.cellEnd;
         if (tournament.Format == EventFormat.Domination)
             data += tag.cellStart + result.TournamentPoints + tag.cellEnd + tag.cellStart +
                     result.Differential + tag.cellEnd + tag.cellStart +
                     result.VictoryPoints + tag.cellEnd;
         else if (tournament.Format == EventFormat.Disparity)
             data += tag.cellStart + result.Differential + tag.cellEnd + tag.cellStart +
                     result.VictoryPoints + tag.cellEnd + tag.cellStart +
                     result.TournamentPoints + tag.cellEnd;
         else //if(tournament.Format == EventFormat.Victory)
             data += tag.cellStart + result.VictoryPoints + tag.cellEnd + tag.cellStart +
                     result.TournamentPoints + tag.cellEnd +  tag.cellStart +
                     result.Differential + tag.cellEnd;
         data += tag.rowEnd + "\r\n";
     }
 }
        void formatTable(ref string data, Tags tag, TournamentRound round, Tournament tournament, int roundNumber)
        {
            var matchNumber = 1;
            foreach (var match in round.Matches)
            {
                var player1 = Config.Settings.GetPlayer(match.Players[0]);
                var player2 = match.Players.Count > 1 ? Config.Settings.GetPlayer(match.Players[1]) : null;
                data += tag.rowStart + tag.cellStart + roundNumber + tag.cellEnd + tag.cellStart + matchNumber++ + tag.cellEnd + tag.cellStart + player1.Name +
                        tag.cellEnd;
                if (chkShowFactions.Checked) data += tag.cellStart + tournament.PlayerFaction[player1.ID] + tag.cellEnd;

                Scores scores;
                scores.vps = match.Results[player1.ID].VictoryPoints;
                scores.tps = match.Results[player1.ID].TournamentPoints;
                scores.diff = match.Results[player1.ID].Differential;
                formatScores(ref data, ref tag, tournament.Format, scores);

                if (player2 != null)
                {
                    data += tag.cellStart + player2.Name + tag.cellEnd;
                    if (chkShowFactions.Checked) data += tag.cellStart + tournament.PlayerFaction[player2.ID] + tag.cellEnd;
                    scores.vps = match.Results[player2.ID].VictoryPoints;
                    scores.tps = match.Results[player2.ID].TournamentPoints;
                    scores.diff = match.Results[player2.ID].Differential;
                    formatScores(ref data, ref tag, tournament.Format, scores);
                }
                else
                    data += (tag.cellStart + "BYE ROUND" + tag.cellEnd + tag.cellStart + tag.cellEnd + tag.cellStart + tag.cellEnd + tag.cellStart + tag.cellEnd);
                data += tag.rowEnd + "\r\n";
            }
        }
 private void formatScores(ref string data, ref Tags tag, EventFormat eventFormat, Scores scores)
 {
     if (eventFormat == EventFormat.Domination)
         data += tag.cellStart + scores.tps + tag.cellEnd + tag.cellStart +
                 scores.diff + tag.cellEnd + tag.cellStart +
                 scores.vps + tag.cellEnd;
     else if (eventFormat == EventFormat.Disparity)
         data += tag.cellStart + scores.diff + tag.cellEnd + tag.cellStart +
                 scores.vps + tag.cellEnd + tag.cellStart +
                 scores.tps + tag.cellEnd;
     else //if(tournament.Format == EventFormat.Victory)
         data += tag.cellStart + scores.vps + tag.cellEnd + tag.cellStart +
                 scores.tps + tag.cellEnd + tag.cellStart +
                 scores.diff + tag.cellEnd;
 }
        private void Export(Tournament tournament, Tags tag, string filename = null)
        {
            string data = "";
            if (chkShowName.Checked) data += tag.headlineStart + tournament.Name + tag.headlineEnd + "\r\n";
            data += tag.docStart + "\r\n";

            string scoreLabels;
            if (tournament.Format == EventFormat.Domination)
                scoreLabels = tag.thStart + "TPs" + tag.thEnd + tag.thStart + "Diff" + tag.thEnd + tag.thStart 
                    + "VPs" + tag.thEnd;
            else if (tournament.Format == EventFormat.Disparity)
                scoreLabels = tag.thStart + "Diff" + tag.thEnd + tag.thStart + "VPs" + tag.thEnd + tag.thStart
                    + "TPs" + tag.thEnd;
            else //if( tournament.Format == EventFormat.Victory)
                scoreLabels = tag.thStart + "VPs" + tag.thEnd + tag.thStart + "TPs" + tag.thEnd + tag.thStart 
                    + "Diff" + tag.thEnd;

            var factionLabel = "";
            if (chkShowFactions.Checked) factionLabel = tag.thStart + "Faction" + tag.thEnd;

            if (rdoExportPlayer.Checked)
                data += tag.rowStart + tag.thStart + "Round" + tag.thEnd + tag.thStart
                    + "Player Name" + tag.thEnd + factionLabel + scoreLabels + tag.thStart + "Opponent" + tag.thEnd 
                    + tag.rowEnd + "\r\n";
            else if (rdoExportTable.Checked)
                data += tag.rowStart + tag.thStart + "Round" + tag.thEnd + tag.thStart + "Table" + tag.thEnd
                    + tag.thStart + "Player 1" + tag.thEnd + factionLabel + scoreLabels +
                        tag.thStart + "Player 2" + tag.thEnd + factionLabel + scoreLabels + tag.thEnd + tag.rowEnd + "\r\n";
            else
                data += tag.rowStart + tag.thStart + "Rank" + tag.thEnd + tag.thStart + "Player Name" + tag.thEnd
                    + factionLabel + scoreLabels + tag.rowEnd + "\r\n";

            if (rdoExportResults.Checked)
            {
                var results = tournament.PlayerIDs.Select(tournament.GetPlayerResults).ToList();
                sortResults(ref results, tournament.Format);

                formatResults(ref data, results, tag, tournament);
            }
            else
            {
                var roundNumber = 1;
                foreach (var round in tournament.Rounds)
                {
                    if (rdoExportTable.Checked)
                    {
                        formatTable(ref data, tag, round, tournament, roundNumber);
                    }
                    else
                    {
                        if (!rdoOrderTable.Checked)
                        {
                            var players = new List<string>(tournament.Players.Keys);
                            orderPlayers(ref players);
                            foreach (var id in players)
                            {
                                var player = Config.Settings.GetPlayer(id);
                                foreach (var match in round.Matches.Where(match => match.Players.Contains(player.ID)))
                                {
                                    data += tag.rowStart + tag.cellStart + roundNumber + tag.cellEnd + tag.cellStart 
                                        + player.Name + tag.cellEnd;
                                    if (chkShowFactions.Checked) data += tag.cellStart + tournament.PlayerFaction[player.ID] + tag.cellEnd;
                                    Scores scores;
                                    scores.diff = match.Results[id].Differential;
                                    scores.tps = match.Results[id].TournamentPoints;
                                    scores.vps = match.Results[id].VictoryPoints;
                                    formatScores(ref data, ref tag, tournament.Format, scores);

                                    if (match.Players.Count > 1)
                                    {
                                        var id2 = match.Players[0] == id ? match.Players[1] : match.Players[0];
                                        var player2 = Config.Settings.GetPlayer(id2);
                                        data += tag.cellStart + player2.Name + tag.cellEnd;
                                    }
                                    else
                                        data += tag.cellStart + "BYE ROUND" + tag.cellEnd;
                                    data += tag.rowEnd + "\r\n";
                                    break;
                                }
                            }
                        }
                        else
                        {
                            foreach (var match in round.Matches)
                            {
                                var player1 = Config.Settings.GetPlayer(match.Players[0]);
                                var player2 = match.Players.Count > 1
                                                  ? Config.Settings.GetPlayer(match.Players[1])
                                                  : null;
                                data += tag.rowStart + tag.cellStart + roundNumber + tag.cellEnd + tag.cellStart 
                                    + player1.Name + tag.cellEnd;
                                if (chkShowFactions.Checked) data += tag.cellStart + tournament.PlayerFaction[player1.ID] 
                                    + tag.cellEnd;

                                Scores scores;
                                scores.diff = match.Results[player1.ID].Differential;
                                scores.tps = match.Results[player1.ID].TournamentPoints;
                                scores.vps = match.Results[player1.ID].VictoryPoints;
                                formatScores(ref data, ref tag, tournament.Format, scores);

                                if (player2 != null)
                                {
                                    data += tag.cellStart + player2.Name + tag.cellEnd;
                                    if (chkShowFactions.Checked) data += tag.cellStart + tournament.PlayerFaction[player2.ID] 
                                        + tag.cellEnd;
                                    scores.diff = match.Results[player2.ID].Differential;
                                    scores.tps = match.Results[player2.ID].TournamentPoints;
                                    scores.vps = match.Results[player2.ID].VictoryPoints;
                                    formatScores(ref data, ref tag, tournament.Format, scores);

                                }
                                else
                                    data += tag.cellStart + "BYE ROUND" + tag.cellEnd;
                                data += tag.rowEnd + "\r\n";
                            }
                        }
                    }
                    roundNumber++;
                }
            }
            data += tag.docEnd + "\r\n";

            if (rdoWriteFile.Checked)
                using (var sw = new StreamWriter(new FileStream(filename, FileMode.Create, FileAccess.Write,
                                                                FileShare.None)))
                    sw.Write(data);
            else
                Clipboard.SetText(data, TextDataFormat.Text);
        }