private void saveNewPlayerData(Player p)
 {
     string saveString = p.toSaveString(true);
     StreamWriter w = new StreamWriter(Properties.Settings.Default.LogFilename, true);
     w.Write("PlayerAdded\t" + saveString + "\r\n");
     w.Close();
 }
 private void addPlayerButton_Click(object sender, EventArgs e)
 {
     string playerName = newPlayerNameTextBox.Text.Trim();
     DateTime time = DateTime.Now;
     if (playerName != "" && playerName!="Party Average")
     {
         bool exists = false;
         for (int i = 0; i < playerSelectComboBox.Items.Count; i++)
             if (((Player)playerSelectComboBox.Items[i]).Name == playerName)
                 exists = true;
         if (!exists)
         {
             Player p = new Player(playerName);
             p.InitBAC = (double)initialBACNumericUpDown.Value;
             p.Height = (double)heightNumeric.Value;
             p.Weight = (double)weightNumeric.Value;
             p.HoursSinceEaten = (double)lastEatenNumeric.Value;
             p.Image = captureImage();
             p.BAC = p.InitBAC;
             p.PlayerAddedTime = time;
             addNewPlayer(p,true);
             if (playerSelectComboBox.Items.Count == 1)
                 playerSelectComboBox.SelectedIndex = 0;
         }
     }
 }
        public MainForm()
        {
            #region Flicker Fix

            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, false);
            this.SetStyle(ControlStyles.Opaque, false);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);

            #endregion

            #region Monitor Setting and Sizing

            if (Properties.Settings.Default.Monitor >= Screen.AllScreens.Length || Properties.Settings.Default.Monitor < 0)
                Properties.Settings.Default.Monitor = -1;
            this.Visible = false;
            InitializeComponent();
            if (Properties.Settings.Default.Monitor == -1)
            {
                this.Width = Screen.PrimaryScreen.Bounds.Width;
                this.Height = Screen.PrimaryScreen.Bounds.Height;
                this.Location = Screen.PrimaryScreen.Bounds.Location;
            }
            else
            {
                this.Width = Screen.AllScreens[Properties.Settings.Default.Monitor].Bounds.Width;
                this.Height = Screen.AllScreens[Properties.Settings.Default.Monitor].Bounds.Height;
                this.Location = Screen.AllScreens[Properties.Settings.Default.Monitor].Bounds.Location;
            }

            #endregion

            #region Restore Last Log

            if (File.Exists(Properties.Settings.Default.LogFilename) && Properties.Settings.Default.LoadPreviousLogFile)
            {
                StreamReader reader = new StreamReader(Properties.Settings.Default.LogFilename);
                string oldLog = reader.ReadToEnd();
                string[] logItems = oldLog.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < logItems.Length; i++)
                {
                    string[] itemSplit = logItems[i].Split(new char[] { '\t' });
                    string action = itemSplit[0];
                    string name;
                    DateTime dt;
                    Player p;
                    try
                    {
                        switch (action)
                        {
                            case "PointAdded":
                                name = itemSplit[1];
                                dt = DateTime.Parse(itemSplit[2]);
                                double val = double.Parse(itemSplit[3]);
                                int found = -1;
                                for (int j = 0; j < chart.Series.Count; j++)
                                    if (chart.Series[j].Name == name)
                                        found = j;
                                if (found == -1)
                                {
                                    p = new Player(name);
                                    p.InitBAC = val;
                                    addNewPlayer(p, false);
                                }
                                else
                                {
                                    addDataToPlayer(name, dt, val, false);
                                }
                                break;
                            case "PlayerAdded":
                                p = new Player(itemSplit[1]);
                                p.loadDataFromSaveString(logItems[i].Substring(12).Trim());
                                addNewPlayer(p, false);
                                break;
                            case "PlayerRemoved":
                                name = itemSplit[1];
                                dt = DateTime.Parse(itemSplit[2]);
                                removePlayer(name, dt, false);
                                break;
                            default:
                                continue;
                        }
                    }
                    catch (Exception) { continue; }
                }
                if (playerSelectComboBox.Items.Count > 0)
                    playerSelectComboBox.SelectedIndex = 0;

                reader.Close();
            }
            else
            {
                File.Open(Properties.Settings.Default.LogFilename,FileMode.Create).Close();
            }

            #endregion

            #region Dynamic Sizing and Locating

            this.background.Width = this.Width;
            this.background.Height = this.Height;
            this.background.Image = Properties.Resources.CliffhangerBackground;
            this.climberPictureBox.Image = Properties.Resources.Climber;
            this.climberPictureBox.Width = this.climberPictureBox.Image.Width;
            this.climberPictureBox.Height = this.climberPictureBox.Image.Height;
            this.climberPictureBox.Parent = this.background;
            setClimberState(0, 0.0, Properties.Settings.Default.BACTarget);

            chart.Width = this.Width;
            chart.Height = this.Height;
            chart.ChartAreas[0].AxisX.LabelStyle.Format = "MM/dd/yy HH:mm:ss";
            controlPanel.Location = new Point(this.Width / 2 - controlPanel.Width / 2, this.Height / 2 - controlPanel.Height / 2);

            #endregion

            #region Initialize State Variables

            screenState = 0;
            PiRState = 0;
            CliffState = 0;
            hasGroupLost = false;

            #endregion

            #region Audio Initialization

            musicAbsolutePath = Application.StartupPath + Properties.Resources.MusicPath;
            player = new System.Media.SoundPlayer();
            wmp = new MediaPlayer();

            #endregion

            #region Register Event Handlers

            this.Click += new EventHandler(MainForm_Click);
            this.newPlayerNameTextBox.KeyDown += new KeyEventHandler(newPlayerNameTextBox_KeyDown);
            this.bacPercentNumericUpDown.KeyDown += new KeyEventHandler(bacPercentNumericUpDown_KeyDown);
            this.initialBACNumericUpDown.KeyDown += new KeyEventHandler(initialBACNumericUpDown_KeyDown);

            #endregion

            #region Image Capture Initialization

            reactivateCamera();

            #endregion

            //Show The Application
            this.Visible = true;
        }
 private void addNewPlayer(Player p, bool save)
 {
     playerSelectComboBox.Items.Add(p);
     newPlayerNameTextBox.Text = "";
     initialBACNumericUpDown.Value = 0;
     if (chart.Series.IndexOf(p.Name) < 0)
     {
         chart.Series.Add(p.Name);
         chart.Series[chart.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
         chart.Series[chart.Series.Count - 1].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
         chart.Series[chart.Series.Count - 1].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
         chart.Series[chart.Series.Count - 1].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime;
     }
     if (save)
     {
         saveNewPlayerData(p);
         reactivateCamera();
     }
     addDataToPlayer(p.Name, p.PlayerAddedTime, p.InitBAC, save);
 }