<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Xml;
using System.Text.RegularExpressions;
using System.Xml.Serialization;

namespace LiveResults.Client
{
    public partial class OEForm : Form
    {
        List<EmmaMysqlClient> m_Clients;
        OSParser m_OSParser;
        OEParser m_OEParser;

        enum Format { IOFXML, OECSV, OSCSV, OECSVTEAM, OECSVPAR }
        class FormatItem
        {
            public string Name { get; set; }
            public string Description { get; set; }
            public Format Format { get; set; }
            public FormatItem(string Name, string Description, Format format)
            {
                this.Name = Name;
                this.Description = Description;
                this.Format = format;
            }

            public override string ToString()
            {
                return Name;
            }
        }

        List<FormatItem> supportedFormats = new List<FormatItem>();

        public OEForm()
        {
            InitializeComponent();
            Text = Text += ", " + Encoding.Default.EncodingName + "," + Encoding.Default.CodePage;
            fileSystemWatcher1.Changed += new FileSystemEventHandler(fileSystemWatcher1_Changed);
            m_Clients = new List<EmmaMysqlClient>();

            supportedFormats.Add(new FormatItem("IOF-XML", "Export files in IOF-XML (version 2 supported)", Format.IOFXML));
            supportedFormats.Add(new FormatItem("OE-csv", "CSV files exported from OLEinzel 10.3 and 11", Format.OECSV));
            supportedFormats.Add(new FormatItem("OS-csv", "CSV files exported from OLStaffel 10.3 and 11", Format.OSCSV));
            supportedFormats.Add(new FormatItem("OS-csv (Team)", "Team-CSV files exported from OSStaffel 10.3", Format.OECSVTEAM));
            supportedFormats.Add(new FormatItem("OE-csv (Par)", "Special format for DalaDubbeln from OLEinzel 10.3", Format.OECSVPAR));
            cmbFormat.DataSource = supportedFormats;
            cmbFormat.SelectedIndex = 0;

            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "EmmaClient");
            

            string file = Path.Combine(path, "oesetts.xml");
            if (File.Exists(file))
            {
                try
                {
                    var fs = File.OpenRead(file);
                    XmlSerializer ser = new XmlSerializer(typeof(Settings));
                    Settings s = ser.Deserialize(fs) as Settings;
                    fs.Close();
                    if (s != null)
                    {
                        txtOEDirectory.Text = s.Location;
                        txtExtension.Text = s.extension;
                        txtCompID.Text = s.CompID.ToString();
                        for (int i = 0; i < supportedFormats.Count; i++)
                        {
                            if (supportedFormats[i].Name == s.Format)
                                cmbFormat.SelectedIndex = i;
                        }

                    }
                }
                catch
                {
                }
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            fileSystemWatcher1.EnableRaisingEvents = false;
            fileSystemWatcher1.Dispose();
            timer1.Enabled = false;
            timer1.Dispose();

            if (m_Clients != null)
            {
                foreach (EmmaMysqlClient c in m_Clients)
                {
                    c.Stop();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
            {
                txtOEDirectory.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(txtOEDirectory.Text))
            {
                MessageBox.Show(this, "Please select an existing OE Export directory", "Start OE Monitor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrEmpty(txtCompID.Text))
            {
                MessageBox.Show(this, "You must enter a competition-ID", "Start OE Monitor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            listBox1.Items.Clear();
            m_Clients.Clear();
            logit("Reading servers from config (eventually resolving online)");
            Application.DoEvents();
            EmmaMysqlClient.EmmaServer[] servers = EmmaMysqlClient.GetServersFromConfig();
            logit("Got servers from obasen...");
            Application.DoEvents();
            foreach (EmmaMysqlClient.EmmaServer server in servers)
            {
                EmmaMysqlClient client = new EmmaMysqlClient(server.host, 3306, server.user, server.pw, server.db, Convert.ToInt32(txtCompID.Text));

                client.OnLogMessage += new LogMessageDelegate(client_OnLogMessage);
                client.Start();
                m_Clients.Add(client);
            }

            timer1_Tick(null, null);

            FormatItem format = cmbFormat.SelectedItem as FormatItem;


            if ( format.Format == Format.OECSV || format.Format == Format.OECSVPAR || format.Format == Format.OECSVTEAM || format.Format == Format.OSCSV)
            {
                m_OSParser = new OSParser();
                m_OSParser.OnLogMessage +=
                delegate (string msg)
                {
                    logit(msg);
                };

                m_OSParser.OnResult += new ResultDelegate(m_OSParser_OnResult);

                m_OEParser = new OEParser();
                m_OEParser.OnLogMessage +=
                delegate(string msg)
                {
                    logit(msg);
                };

                m_OEParser.OnResult += new ResultDelegate(m_OSParser_OnResult);

                fsWatcherOS.Path = txtOEDirectory.Text;
                fsWatcherOS.Filter = txtExtension.Text;
                fsWatcherOS.EnableRaisingEvents = true;

            }
            else if (format.Format == Format.IOFXML)
            {
                fileSystemWatcher1.Path = txtOEDirectory.Text;
                fileSystemWatcher1.Filter = txtExtension.Text;
                fileSystemWatcher1.NotifyFilter = NotifyFilters.LastWrite;
                fileSystemWatcher1.IncludeSubdirectories = false;
                fileSystemWatcher1.EnableRaisingEvents = true;
            }
        }

        void m_OSParser_OnResult(Result newResult)
        {
            foreach (EmmaMysqlClient c in m_Clients)
            {
                if (!c.IsRunnerAdded(newResult.ID))
                {
                    c.AddRunner(new Runner(newResult.ID, newResult.RunnerName, newResult.RunnerClub, newResult.Class));
                }
                else
                    c.UpdateRunnerInfo(newResult.ID, newResult.RunnerName, newResult.RunnerClub, newResult.Class);


                if (newResult.StartTime >= 0)
                    c.SetRunnerStartTime(newResult.ID, newResult.StartTime);

                c.SetRunnerResult(newResult.ID, newResult.Time, newResult.Status);
                if (newResult.SplitTimes != null)
                {
                    foreach (ResultStruct r in newResult.SplitTimes)
                    {
                        c.SetRunnerSplit(newResult.ID, r.ControlCode, r.Time);
                    }
                }
            }<commit_msg>Fixes issue with OlStaffel RelayResults not beeing uploaded correctly..<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Xml;
using System.Text.RegularExpressions;
using System.Xml.Serialization;

namespace LiveResults.Client
{
    public partial class OEForm : Form
    {
        List<EmmaMysqlClient> m_Clients;
        OSParser m_OSParser;
        OEParser m_OEParser;

        enum Format { IOFXML, OECSV, OSCSV, OECSVTEAM, OECSVPAR }
        class FormatItem
        {
            public string Name { get; set; }
            public string Description { get; set; }
            public Format Format { get; set; }
            public FormatItem(string Name, string Description, Format format)
            {
                this.Name = Name;
                this.Description = Description;
                this.Format = format;
            }

            public override string ToString()
            {
                return Name;
            }
        }

        List<FormatItem> supportedFormats = new List<FormatItem>();

        public OEForm()
        {
            InitializeComponent();
            Text = Text += ", " + Encoding.Default.EncodingName + "," + Encoding.Default.CodePage;
            fileSystemWatcher1.Changed += new FileSystemEventHandler(fileSystemWatcher1_Changed);
            m_Clients = new List<EmmaMysqlClient>();

            supportedFormats.Add(new FormatItem("IOF-XML", "Export files in IOF-XML (version 2 supported)", Format.IOFXML));
            supportedFormats.Add(new FormatItem("OE-csv", "CSV files exported from OLEinzel 10.3 and 11", Format.OECSV));
            supportedFormats.Add(new FormatItem("OS-csv", "CSV files exported from OLStaffel 10.3 and 11", Format.OSCSV));
            supportedFormats.Add(new FormatItem("OS-csv (Team)", "Team-CSV files exported from OSStaffel 10.3", Format.OECSVTEAM));
            supportedFormats.Add(new FormatItem("OE-csv (Par)", "Special format for DalaDubbeln from OLEinzel 10.3", Format.OECSVPAR));
            cmbFormat.DataSource = supportedFormats;
            cmbFormat.SelectedIndex = 0;

            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "EmmaClient");
            

            string file = Path.Combine(path, "oesetts.xml");
            if (File.Exists(file))
            {
                try
                {
                    var fs = File.OpenRead(file);
                    XmlSerializer ser = new XmlSerializer(typeof(Settings));
                    Settings s = ser.Deserialize(fs) as Settings;
                    fs.Close();
                    if (s != null)
                    {
                        txtOEDirectory.Text = s.Location;
                        txtExtension.Text = s.extension;
                        txtCompID.Text = s.CompID.ToString();
                        for (int i = 0; i < supportedFormats.Count; i++)
                        {
                            if (supportedFormats[i].Name == s.Format)
                                cmbFormat.SelectedIndex = i;
                        }

                    }
                }
                catch
                {
                }
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            fileSystemWatcher1.EnableRaisingEvents = false;
            fileSystemWatcher1.Dispose();
            timer1.Enabled = false;
            timer1.Dispose();

            if (m_Clients != null)
            {
                foreach (EmmaMysqlClient c in m_Clients)
                {
                    c.Stop();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
            {
                txtOEDirectory.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(txtOEDirectory.Text))
            {
                MessageBox.Show(this, "Please select an existing OE Export directory", "Start OE Monitor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrEmpty(txtCompID.Text))
            {
                MessageBox.Show(this, "You must enter a competition-ID", "Start OE Monitor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            listBox1.Items.Clear();
            m_Clients.Clear();
            logit("Reading servers from config (eventually resolving online)");
            Application.DoEvents();
            EmmaMysqlClient.EmmaServer[] servers = EmmaMysqlClient.GetServersFromConfig();
            logit("Got servers from obasen...");
            Application.DoEvents();
            foreach (EmmaMysqlClient.EmmaServer server in servers)
            {
                EmmaMysqlClient client = new EmmaMysqlClient(server.host, 3306, server.user, server.pw, server.db, Convert.ToInt32(txtCompID.Text));

                client.OnLogMessage += new LogMessageDelegate(client_OnLogMessage);
                client.Start();
                m_Clients.Add(client);
            }

            timer1_Tick(null, null);

            FormatItem format = cmbFormat.SelectedItem as FormatItem;


            if ( format.Format == Format.OECSV || format.Format == Format.OECSVPAR || format.Format == Format.OECSVTEAM || format.Format == Format.OSCSV)
            {
                m_OSParser = new OSParser();
                m_OSParser.OnLogMessage +=
                delegate (string msg)
                {
                    logit(msg);
                };

                m_OSParser.OnResult += new ResultDelegate(m_OSParser_OnResult);

                m_OEParser = new OEParser();
                m_OEParser.OnLogMessage +=
                delegate(string msg)
                {
                    logit(msg);
                };

                m_OEParser.OnResult += new ResultDelegate(m_OSParser_OnResult);

                fsWatcherOS.Path = txtOEDirectory.Text;
                fsWatcherOS.Filter = txtExtension.Text;
                fsWatcherOS.EnableRaisingEvents = true;

            }
            else if (format.Format == Format.IOFXML)
            {
                fileSystemWatcher1.Path = txtOEDirectory.Text;
                fileSystemWatcher1.Filter = txtExtension.Text;
                fileSystemWatcher1.NotifyFilter = NotifyFilters.LastWrite;
                fileSystemWatcher1.IncludeSubdirectories = false;
                fileSystemWatcher1.EnableRaisingEvents = true;
            }
        }

        void m_OSParser_OnResult(Result newResult)
        {
            foreach (EmmaMysqlClient c in m_Clients)
            {
                if (!c.IsRunnerAdded(newResult.ID))
                {
                    c.AddRunner(new Runner(newResult.ID, newResult.RunnerName, newResult.RunnerClub, newResult.Class));
                }
                else
                    c.UpdateRunnerInfo(newResult.ID, newResult.RunnerName, newResult.RunnerClub, newResult.Class);


                if (newResult.StartTime >= 0)
                    c.SetRunnerStartTime(newResult.ID, newResult.StartTime);

                if (newResult is RelayResult)
                {
                    var rr = newResult as RelayResult;
                    c.SetRunnerResult(newResult.ID, rr.OverallTime, rr.OverallStatus);
                }
                else
                {
                    c.SetRunnerResult(newResult.ID, newResult.Time, newResult.Status);
                }

                if (newResult.SplitTimes != null)
                {
                    foreach (ResultStruct r in newResult.SplitTimes)
                    {
                        c.SetRunnerSplit(newResult.ID, r.ControlCode, r.Time);
                    }
                }
            }
{'fraction_non_alphanumeric': 0.08701396753491884, 'fraction_numerical': 0.005410846860450485, 'mean_word_length': 1.5562881955612737, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3063948', 'n_tokens_mistral': 4351, 'n_tokens_neox': 4018, 'n_words': 821}