-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVong2.cs
261 lines (248 loc) · 10.4 KB
/
Vong2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using LibVLCSharp.Shared;
using Microsoft.AspNetCore.Mvc;
using MinigameOlympia.Models;
using MinigameOlympia;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MinigameOlympia {
public partial class Vong2 : Form {
public TcpClient client;
public string roomCode;
public Player player;
public List<string> players = new List<string>();
public List<Image> avatars = new List<Image>();
private Thread receive;
private int question = 0;
private int total = 0;
private bool isVVW = false;
private bool isCountdown = false;
private bool isAnswer = false;
private bool isEnd = false;
private SoundPlayer sound;
private DateTime startTime = new DateTime();
private VerticalProgressbar progressbar = new VerticalProgressbar();
private ManualResetEvent suspendEvent = new ManualResetEvent(true);
private System.Windows.Forms.Timer startTimer = new System.Windows.Forms.Timer();
private System.Windows.Forms.Timer countdownTimer = new System.Windows.Forms.Timer();
public Vong2() {
InitializeComponent();
progressbar.Location = new Point(286, 12);
progressbar.Size = new Size(34, 434);
progressbar.Minimum = 0;
progressbar.ForeColor = Color.Blue;
Controls.Add(progressbar);
startTimer.Interval = 5000;
countdownTimer.Interval = 1000;
startTimer.Tick += startTimer_Tick;
countdownTimer.Tick += countdownTimer_Tick;
}
private void Round2_FormClosing(object sender, FormClosingEventArgs e) {
if (receive.IsAlive) {
receive.Abort();
}
}
private void Round2_Load(object sender, EventArgs e) {
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Normal;
receive = new Thread(() => ReceiveMessage(client));
receive.IsBackground = true;
receive.Start();
LoadScreen();
SendData($"GET_POINT_FN:{roomCode}", client);
Thread.Sleep(2000);
SendData($"ROUND2:{roomCode}", client);
}
private void LoadScreen() {
lblPlayer1.Text = players[0];
lblPlayer2.Text = players[1];
lblPlayer3.Text = players[2];
lblPlayer4.Text = players[3];
ptbPlayer1.Image = avatars[0];
ptbPlayer2.Image = avatars[1];
ptbPlayer3.Image = avatars[2];
ptbPlayer4.Image = avatars[3];
if (player.Username == players[0]) {
lblPlayer1.ForeColor = Color.Yellow;
} else if (player.Username == players[1]) {
lblPlayer2.ForeColor = Color.Yellow;
} else if (player.Username == players[2]) {
lblPlayer3.ForeColor = Color.Yellow;
} else {
lblPlayer4.ForeColor = Color.Yellow;
}
}
private void ReceiveMessage(TcpClient tcpClient) {
while (true) {
suspendEvent.WaitOne();
if (client == null || !client.Connected)
continue;
else {
if (client.Available > 0) {
string message = "";
while (client.Available > 0) {
byte[] buffer = new byte[1024];
try {
NetworkStream stream = client.GetStream();
int bytesRead = stream.Read(buffer, 0, buffer.Length);
message = Encoding.UTF8.GetString(buffer, 0, bytesRead);
AnalyzingMessage(message, tcpClient);
} catch (Exception) {
}
}
}
}
}
}
private void AnalyzingMessage(string mess, TcpClient tcpClient) {
string[] Payload = mess.Split(':');
switch (Payload[0]) {
case "POINT":
string[] data = Payload[1].Split('-');
//Invoke(new MethodInvoker(delegate {
for (int i = 0; i < data.Length; i++) {
if (i == 0) {
lblPointPlayer1.Text = data[i];
} else if (i == 1) {
lblPointPlayer2.Text = data[i];
} else if (i == 2) {
lblPointPlayer3.Text = data[i];
} else {
lblPointPlayer4.Text = data[i];
}
}
//}));
break;
case "QUEST_R2":
Invoke(new MethodInvoker(delegate {
sound = new SoundPlayer(Properties.Resources.TT_Open);
sound.Play();
Payload[1] += $":{Payload[2]}";
data = Payload[1].Split('^');
int t = int.Parse(data[0]);
question = t;
gbQuestion.Text = $"Câu hỏi {t + 1}: {data[1]}";
isEnd = false;
isAnswer = false;
tbAnswer.Text = "";
btnAnswer.Enabled = true;
if (t < 3) {
isEnd = true;
ptbQuestion.Visible = true;
vvwQuestion.Visible = false;
ptbQuestion.Load(data[2]);
isVVW = false;
} else {
ptbQuestion.Visible = false;
vvwQuestion.Visible = true;
var libVLC = new LibVLC();
var media = new Media(libVLC, new Uri(data[2]));
vvwQuestion.MediaPlayer = new LibVLCSharp.Shared.MediaPlayer(media) {
EnableHardwareDecoding = true
};
vvwQuestion.MediaPlayer.EndReached += OnEndReached;
isVVW = true;
vvwQuestion.MediaPlayer.Play();
}
int time = int.Parse(data[3]);
progressbar.Maximum = time;
if (!isVVW) {
if (time == 10)
sound = new SoundPlayer(Properties.Resources.TT_10s);
else if (time == 20)
sound = new SoundPlayer(Properties.Resources.TT_20s);
else if (time == 30) {
sound = new SoundPlayer(Properties.Resources.TT_30s);
}
} else
sound = null;
}));
Invoke(new MethodInvoker(delegate {
startTimer.Start();
}));
break;
}
}
private void SendData(string message, TcpClient client) {
NetworkStream stream = client.GetStream();
byte[] buffer = Encoding.UTF8.GetBytes(message);
stream.Write(buffer, 0, buffer.Length);
Thread.Sleep(100);
}
private void countdownTimer_Tick(object sender, EventArgs e) {
if (progressbar.Value < progressbar.Maximum) {
progressbar.Value++;
isCountdown = true;
} else {
if (!isAnswer) {
btnAnswer.Enabled = false;
SendData($"ANSWER_R2:{roomCode}-{player.Username}---{question}", client);
}
if (isEnd) {
total++;
isCountdown = false;
countdownTimer.Stop();
progressbar.Value = 0;
suspendEvent.Reset();
Answer show = new Answer();
show.client = client;
show.roomCode = roomCode;
show.round = 2;
show.WindowState = FormWindowState.Normal;
show.Activate();
Visible = false;
show.ShowDialog();
suspendEvent.Set();
if (total < 4) {
SendData($"GET_POINT_FN:{roomCode}", client);
Thread.Sleep(1000);
SendData($"QUEST_R2:{roomCode}-{total}", client);
} else {
KetQua result = new KetQua();
result.client = client;
result.roomCode = roomCode;
result.player = player;
result.Show();
Close();
}
}
}
}
private void OnEndReached(object sender, EventArgs e) {
isEnd = true;
}
private void startTimer_Tick(object sender, EventArgs e) {
startTimer.Stop();
startTime = DateTime.Now;
btnAnswer.Visible = true;
countdownTimer.Start();
if (sound != null) {
sound.Play();
}
}
private void btnAnswer_Click(object sender, EventArgs e) {
if (tbAnswer.Text != "") {
btnAnswer.Enabled = false;
isAnswer = true;
TimeSpan time = DateTime.Now - startTime;
string str = tbAnswer.Text.Replace(":", "");
SendData($"ANSWER_R2:{roomCode}-{player.Username}-{str}-" +
$"{time.TotalSeconds.ToString("0.00")}-{question}", client);
}
}
private void Guide_Click(object sender, EventArgs e) {
InstructionRound2 form = new InstructionRound2();
form.Show();
}
}
}