-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainPage.xaml.cs
195 lines (179 loc) · 6.82 KB
/
MainPage.xaml.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
using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace Ice_Breaking
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
DataInitializer datainit = new DataInitializer();
ContentDialog no_student = new ContentDialog
{
Title = "数据库异常",
Content = "未找到任何数据。请参阅 GitHub 项目仓库中的 README.md 修复!",
PrimaryButtonText = "前往 GitHub",
DefaultButton = ContentDialogButton.Primary
};
private async void Grid_Loaded(object sender, RoutedEventArgs e)
{
await datainit.InitDataAsync();
if (datainit.person.Count == 0)
{
await no_student.ShowAsync();
await Windows.System.Launcher.LaunchUriAsync(new Uri("https://github.com/HoraceHuang-ui/HDU_Ice_Breaking"));
}
}
private void RefreshFlyoutState()
{
new_name_box.Text = "";
new_id_box.Text = "";
dropdown_gender.Content = "性别";
new_photo_box.Text = "";
add_save.Content = "保存";
add_save.IsEnabled = true;
gender_expected.Visibility = Visibility.Collapsed;
user_existed.Visibility = Visibility.Collapsed;
}
private int DivisionSearchByID(int l, int r, string arg)
{
int m = (l + r) / 2;
bool found = false;
while (l <= r)
{
m = (l + r) / 2;
if (datainit.person[m].id.CompareTo(arg) > 0) // id < m
{
r = m - 1;
}
else if (datainit.person[m].id.CompareTo(arg) < 0)
{
l = m + 1;
}
else
{
found = true;
break;
}
}
if (found)
return m;
else
return -1;
}
private async void start_btn_Click(object sender, RoutedEventArgs e)
{
var serializer = new SerializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build();
StorageFolder storageFolder = Windows.Storage.KnownFolders.DocumentsLibrary;
StorageFile file = await storageFolder.GetFileAsync("ice_breaking\\student_data.txt");
string name = name_box.Text;
string id = id_box.Text;
int m = DivisionSearchByID(0, datainit.person.Count - 1, id);
if (m == -1 || datainit.person[m].name != name)
{
invalid_name_or_id.Visibility = Visibility.Visible;
return;
}
if (anonym.IsChecked == true)
{
if (datainit.person[m].anonym == "F")
{
datainit.person[m].anonym = "T";
datainit.s_list[m] = serializer.Serialize(datainit.person[m]);
datainit.raw_str = "";
foreach (string str in datainit.s_list)
{
datainit.raw_str += str;
}
await FileIO.WriteTextAsync(file, datainit.raw_str);
}
}
else
{
if (datainit.person[m].anonym == "T")
{
datainit.person[m].anonym = "F";
datainit.s_list[m] = serializer.Serialize(datainit.person[m]);
datainit.raw_str = "";
foreach (string str in datainit.s_list)
{
datainit.raw_str += str;
}
await FileIO.WriteTextAsync(file, datainit.raw_str);
}
}
invalid_name_or_id.Visibility = Visibility.Collapsed;
file = await storageFolder.CreateFileAsync("ice_breaking\\selected.txt", CreationCollisionOption.OpenIfExists);
await FileIO.WriteTextAsync(file, m.ToString());
Frame.Navigate(typeof(quiz));
}
private async void add_save_Click(object sender, RoutedEventArgs e)
{
gender_expected.Visibility = Visibility.Collapsed;
user_existed.Visibility = Visibility.Collapsed;
string gender = "";
if (dropdown_gender.Content.ToString() == "男")
gender = "T";
else
gender = "F";
int m = DivisionSearchByID(0, datainit.person.Count - 1, new_id_box.Text);
if (m == -1)
{
if (new_name_box.Text == "")
{
new_name_box.Text = "请输入姓名!";
return;
}
bool valid_id = true;
if (new_id_box.Text == "")
valid_id = false;
for (int i = 0; i < new_id_box.Text.Length; i++)
if (!(new_id_box.Text[i] >= '0' && new_id_box.Text[i] <= '9'))
{
valid_id = false;
break;
}
if (!valid_id)
{
new_id_box.Text = "请输入正确学号!";
return;
}
if (dropdown_gender.Content.ToString() == "性别")
{
gender_expected.Visibility = Visibility.Visible;
return;
}
if (new_photo_box.Text == "")
{
new_photo_box.Text = "请输入图片地址!";
return;
}
await datainit.AddPersonAsync(new_name_box.Text, new_id_box.Text, gender, new_photo_box.Text);
add_save.Content = "已保存。";
add_save.IsEnabled = false;
}
else
{
user_existed.Visibility = Visibility.Visible;
}
}
private void dropdown_male_Click(object sender, RoutedEventArgs e)
{
dropdown_gender.Content = "男";
}
private void dropdown_female_Click(object sender, RoutedEventArgs e)
{
dropdown_gender.Content = "女";
}
private void add_user_Click(object sender, RoutedEventArgs e)
{
RefreshFlyoutState();
}
}
}