-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrmMain.cs
More file actions
185 lines (162 loc) · 5.28 KB
/
Copy pathfrmMain.cs
File metadata and controls
185 lines (162 loc) · 5.28 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.IO;
namespace BaiTapLon_GameBox
{
public partial class frmMain : Form
{
private string username;
public bool isLogedIn=false;
frmLogin login;
string dataPath = Application.StartupPath + @"/data.json";
public UserData[] totalData;
public UserData currentData;
public frmMain()
{
InitializeComponent();
}
public string UserName
{
set { username = value; }
get { return username; }
}
void loadData()
{
var totalDataJson = File.ReadAllText(dataPath);
if(totalDataJson!="")
{
totalData = JsonConvert.DeserializeObject<UserData[]>(totalDataJson);
}
else
{
}
}
public void loadUI()
{
changeHeader();
if(isLogedIn == true)
{
for(int i=0; i<currentData.history.GetLength(0); i++)
{
ListViewItem item = new ListViewItem(currentData.history[i][ 0]);
item.SubItems.Add(currentData.history[i][ 1]);
item.SubItems.Add(currentData.history[i][ 2]);
lvHistory.Items.Add(item);
}
}
else
{
lvHistory.Items.Clear();
}
}
public void saveData()
{
string jsonResult = JsonConvert.SerializeObject(totalData);
File.WriteAllText(dataPath, jsonResult);
}
public void setHistory(string name, int score)
{
ListViewItem item = new ListViewItem(DateTime.Now.ToString("dd/MM/yyyy hh:mm tt"));
item.SubItems.Add(name);
item.SubItems.Add(score.ToString());
lvHistory.Items.Add(item);
int newIndex = currentData.history.GetLength(0);
string[][] newArray = new string[currentData.history.Length+1][];
Array.Copy(currentData.history, newArray, currentData.history.Length);
newArray[newIndex] = new string[] { DateTime.Now.ToString("dd/MM/yyyy hh:mm tt"), name, score.ToString() };
currentData.history = newArray;
saveData();
}
public void changeHeader()
{
if(isLogedIn == true)
{
btnLogout.Show();
lblUserName.Show();
btnLogin.Hide();
btnRegister.Hide();
lblUserName.Text = currentData.userName;
lblGreeting.Text = "Xin chào " + currentData.userName + "!";
}
else
{
btnLogout.Hide();
lblUserName.Hide();
btnLogin.Show();
btnRegister.Show();
lblGreeting.Text = "Welcome to GameBOX";
}
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
}
private void openGames(object sender, EventArgs e)
{
if (isLogedIn)
{
PictureBox pic = (PictureBox)sender;
switch (pic.Name) {
case"picSnake":
frmSnakeGame snakeGame = new frmSnakeGame(this);
snakeGame.Show();
break;
case "picFlappyBird":
frmFlappyBird flappyBird = new frmFlappyBird(this);
flappyBird.Show();
break;
case "picGameAnXu":
frmGameAnXu gameAnXu = new frmGameAnXu(this);
gameAnXu.Show();
break;
case "picGameDuaXe":
frmDuaXe gameDuaXe = new frmDuaXe(this);
gameDuaXe.Show();
break;
default:
return;
}
}
else
{
DialogResult result = MessageBox.Show("Đăng nhập trước khi chơi!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
if(result == DialogResult.OK)
{
login = new frmLogin(this);
login.Show();
}
}
}
private void frmMain_Load(object sender, EventArgs e)
{
lblUserName.Hide();
loadData();
}
private void btnLogin_Click(object sender, EventArgs e)
{
login = new frmLogin(this);
login.Show();
}
private void btnRegister_Click(object sender, EventArgs e)
{
login = new frmLogin(this, true);
login.Show();
}
private void btnLogout_Click(object sender, EventArgs e)
{
isLogedIn = false;
loadUI();
}
private void tabGames_Click(object sender, EventArgs e)
{
}
}
}