-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKetQua.cs
155 lines (145 loc) · 5.82 KB
/
KetQua.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
using MinigameOlympia.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MinigameOlympia {
public partial class KetQua : Form {
public TcpClient client;
public string roomCode;
public Player player;
private Thread receive;
private SoundPlayer sound;
private bool isDone = false;
private System.Windows.Forms.Timer endTimer = new System.Windows.Forms.Timer();
public KetQua() {
InitializeComponent();
endTimer.Interval = 10000;
endTimer.Tick += endTimer_Tick;
}
private void Result_Load(object sender, EventArgs e) {
sound = new SoundPlayer(Properties.Resources.Result);
sound.Play();
receive = new Thread(() => ReceiveMessage(client));
receive.IsBackground = true;
receive.Start();
SendData($"WINNER:{roomCode}-{player.Username}", client);
}
private void ReceiveMessage(TcpClient tcpClient) {
while (true) {
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 async void AnalyzingMessage(string mess, TcpClient tcpClient) {
string[] Payload = mess.Split(':');
switch (Payload[0]) {
case "WINNER":
string[] data = Payload[1].Split('-');
byte[] avatarByte = await getAvatar(data[0]);
pictureBox_avt.Image = LoadImage(avatarByte);
if (data[0] == player.Username) {
lblYourScore.Visible = false;
player.WinCount++;
string jsonContent = JsonConvert.SerializeObject(player);
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
using (HttpClient client = new HttpClient()) {
try {
await client.PutAsync("https://olympiawebservice.azurewebsites.net/api/Player", content);
isDone = true;
endTimer.Interval = 2000;
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
} else {
lblYourScore.Text = $"Điểm của bạn: {data[2]}";
isDone = true;
}
label_name.Text = data[0];
label_score.Text = data[1];
Invoke(new MethodInvoker(delegate {
endTimer.Start();
}));
break;
}
}
private void SendData(string message, TcpClient tcpClient) {
NetworkStream stream = client.GetStream();
byte[] buffer = Encoding.UTF8.GetBytes(message);
stream.Write(buffer, 0, buffer.Length);
}
private async Task<byte[]> getAvatar(string username) {
Player pTemp = new Player();
HttpClient httpClient = new HttpClient();
try {
string url = "https://olympiawebservice.azurewebsites.net/api/Player/username?lookup=" + username;
var response = await httpClient.GetAsync(url);
if (response.IsSuccessStatusCode) {
string jsonContent = await response.Content.ReadAsStringAsync();
pTemp = JsonConvert.DeserializeObject<Player>(jsonContent);
}
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
return pTemp.Avatar;
}
private Image LoadImage(byte[] b) {
if (b.Length == 0)
return Properties.Resources.anhdaidien;
Image res;
using (MemoryStream ms = new MemoryStream(b)) {
res = Image.FromStream(ms);
}
return res;
}
private void endTimer_Tick(object sender, EventArgs e) {
if (isDone) {
endTimer.Stop();
foreach (Form form in Application.OpenForms) {
if (form.Name == "GiaoDienChinh") {
form.Visible = true;
((GiaoDienChinh)form).suspendEvent.Set();
break;
}
}
Close();
}
}
private void Result_FormClosing(object sender, FormClosingEventArgs e) {
if (receive.IsAlive)
receive.Abort();
if (client.Connected) {
SendData($"END:{roomCode}-{player.Username}", client);
client.Close();
}
sound.Stop();
}
}
}