//method: ResultPanel //description: initializes a new Result panel //params: QuestonResult result - question result // int questionNum - question number //returns: ResultPanel public ResultPanel(QuestionResult result, int questionNum) : base() { scoreLabel.AutoSize = true; scoreLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); scoreLabel.ForeColor = System.Drawing.Color.DodgerBlue; scoreLabel.Location = new System.Drawing.Point(252, 86); scoreLabel.Name = "scoreLabel"; scoreLabel.Size = new System.Drawing.Size(44, 13); scoreLabel.TabIndex = 9; scoreLabel.Text = "Score:"; Controls.Add(scoreLabel); correctAnswerLabel.AutoSize = true; correctAnswerLabel.Location = new System.Drawing.Point(5, 94); correctAnswerLabel.Name = "correctAnswerLabel"; correctAnswerLabel.Size = new System.Drawing.Size(81, 13); correctAnswerLabel.TabIndex = 6; correctAnswerLabel.Text = "Correct answer:"; Controls.Add(correctAnswerLabel); yourAnswerLabel.AutoSize = true; yourAnswerLabel.Location = new System.Drawing.Point(5, 50); yourAnswerLabel.Name = "yourAnswerLabel"; yourAnswerLabel.Size = new System.Drawing.Size(69, 13); yourAnswerLabel.TabIndex = 5; yourAnswerLabel.Text = "Your answer:"; Controls.Add(yourAnswerLabel); scoreValueLabel.AutoSize = true; scoreValueLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); scoreValueLabel.Location = new System.Drawing.Point(265, 99); scoreValueLabel.Name = "scoreValueLabel"; scoreValueLabel.Size = new System.Drawing.Size(49, 25); scoreValueLabel.TabIndex = 10; scoreValueLabel.Text = "+"+result.score; Controls.Add(scoreValueLabel); correctAnswerTextBox.BackColor = System.Drawing.Color.White; correctAnswerTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None; correctAnswerTextBox.Location = new System.Drawing.Point(18, 110); correctAnswerTextBox.Name = "correctAnswerTextBox"; correctAnswerTextBox.ReadOnly = true; correctAnswerTextBox.Size = new System.Drawing.Size(230, 30); correctAnswerTextBox.TabIndex = 8; correctAnswerTextBox.Text = result.correctAnswer; Controls.Add(correctAnswerTextBox); yourAnswerTextBox.BackColor = System.Drawing.Color.White; yourAnswerTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None; yourAnswerTextBox.Location = new System.Drawing.Point(18, 66); yourAnswerTextBox.Name = "yourAnswerTextBox"; yourAnswerTextBox.ReadOnly = true; yourAnswerTextBox.Size = new System.Drawing.Size(230, 30); yourAnswerTextBox.TabIndex = 7; yourAnswerTextBox.Text = result.userAnswer; Controls.Add(yourAnswerTextBox); questionTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None; questionTextBox.Location = new System.Drawing.Point(18, 20); questionTextBox.Name = "questionTextBox"; questionTextBox.ReadOnly = true; questionTextBox.Size = new System.Drawing.Size(293, 30); questionTextBox.TabIndex = 4; questionTextBox.Text = result.questionText; Controls.Add(questionTextBox); questionResultLabel.AutoSize = true; questionResultLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); questionResultLabel.ForeColor = System.Drawing.Color.DodgerBlue; questionResultLabel.Location = new System.Drawing.Point(4, 4); questionResultLabel.Name = "questionResultLabel"; questionResultLabel.Size = new System.Drawing.Size(68, 13); questionResultLabel.TabIndex = 0; questionResultLabel.Text = "Question "+questionNum; Controls.Add(questionResultLabel); }
//method: ConnectionReader //description: reads data from the server //params: none //returns: void private void ConnectionReader() { try { while(!stopped) { if(reader.PeekLine().StartsWith("QUESTION:")) {//QUESTION //get question QuestionEventArgs question = new QuestionEventArgs(); question.questionText = reader.ReadLine().Substring("QUESTION:".Length); //get answers question.answers = new List<string>(); while(reader.PeekLine().StartsWith("ANSWER:")) { question.answers.Add(reader.ReadLine().Substring("ANSWER:".Length)); } question.timeLimit = int.Parse(reader.ReadLine().Substring("TIME:".Length)); owner.Invoke((MethodInvoker)( () => { QuestionReceived(this, question); } )); } else if(reader.PeekLine().StartsWith("RESULT:")) {//QUESTION RESULTS List<QuestionResult> results = new List<QuestionResult>(); while(reader.PeekLine().StartsWith("RESULT:")) { QuestionResult r = new QuestionResult(); r.questionText = reader.ReadLine().Substring("RESULT:".Length); r.correctAnswer = reader.ReadLine(); r.userAnswer = reader.ReadLine(); r.score = int.Parse(reader.ReadLine()); results.Add(r); } owner.Invoke((MethodInvoker)( () => { ResultsReceived(this, results); } )); } else if(reader.PeekLine().StartsWith("SESSIONID:")) {//LEADERBOARD List<UserSession> rows = new List<UserSession>(); while(reader.PeekLine() != null && reader.PeekLine().StartsWith("SESSIONID:")) { UserSession r = new UserSession(); r.sessionId = int.Parse(reader.ReadLine().Substring("SESSIONID:".Length)); r.userName = reader.ReadLine().Substring("USERNAME:"******"SCORE:".Length)); rows.Add(r); } owner.Invoke((MethodInvoker)( () => { LeaderboardReceived(this, rows); } )); } else if(reader.PeekLine().StartsWith("YOUR_SESSIONID:")) { int sessionId = int.Parse(reader.ReadLine().Substring("YOUR_SESSIONID:".Length)); owner.Invoke((MethodInvoker)( () => { SessionIdReceived(this, sessionId); } )); } else { //nothing we can handle reader.ReadLine();//consume } Thread.Sleep(50); } } catch (Exception ex) { if(ex is ArgumentException || ex is SocketException || ex is IOException || ex is NullReferenceException) { stopped = true; owner.Invoke((MethodInvoker)( () => { ConnectionLost(this, new EventArgs()); } )); } else { throw; } } finally { socket.Close(); } }