-
Notifications
You must be signed in to change notification settings - Fork 2
/
FishingDBSyncForm.cs
238 lines (213 loc) · 7.25 KB
/
FishingDBSyncForm.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
using System;
using System.Threading;
using System.Windows.Forms;
using Fishing.Properties;
namespace Fishing
{
internal partial class FishingDBSyncForm : Form, IFishDBStatusDisplay
{
private const string MessageFormatFishBaitOrZone = "{0} - {1}";
// 50% upload 50% download
// Rods or fish divide that half evenly
// Fish divide rods further (in download)
private int uploadFish;
private int uploadingFish = 0;
private int downloadRods;
private int downloadingRod = 0;
private int downloadFish;
private int downloadingFish = 0;
private int renameFish;
private int renamingFish = 0;
public FishingDBSyncForm()
{
InitializeComponent();
Thread syncThread = new Thread(new ThreadStart(DoSync));
syncThread.IsBackground = true;
FishSQL.StatusDisplay = this;
syncThread.Start();
}
~FishingDBSyncForm()
{
FishSQL.CloseConnection();
FishSQL.CloseAllConnections();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
}
private void DoSync()
{
if (!FishSQL.OpenConnection())
{
return;
}
try
{
StartDBTransaction(Resources.DBSyncMessageStart);
FishDB.MarkAllFishNew();
FishDB.GetUpdates();
FishSQL.BackgroundUpload();
FishSQL.DoDownloadFish();
}
catch (Exception e)
{
Error(e.ToString());
}
EndDBTransaction(Resources.DBSyncMessageDone);
}
public bool StartDBTransaction(string message)
{
bool ret = false;
this.UIThreadInvoke(delegate
{
lblRod.Text = message;
lblFish.Text = String.Empty;
progress.Value = progress.Minimum;
ret = true;
});
return ret;
}
public void EndDBTransaction(string message)
{
this.UIThread(delegate
{
lblRod.Text = message;
lblFish.Text = String.Empty;
progress.Value = progress.Maximum;
});
}
public void SetUploadFishNumber(int fish)
{
uploadFish = fish;
}
public void SetUploadRodAndFish(string rod, string fish)
{
this.UIThread(delegate
{
lblRod.Text = string.Format(Resources.DBSyncMessageFormatUploading, rod);
lblFish.Text = fish;
uploadingFish++;
ProgressUpdate();
});
}
public void SetUploadRenameRodAndFish(string rod, string fromName, string toName)
{
this.UIThread(delegate
{
lblRod.Text = string.Format(Resources.DBSyncMessageFormatUploading, rod);
lblFish.Text = string.Format(Resources.DBSyncMessageFormatRenaming, fromName, toName);
uploadingFish++;
ProgressUpdate();
});
}
public void SetDownloadRodNumber(int rods)
{
downloadRods = rods;
}
public void SetDownloadRodFish(int fish)
{
downloadingFish = 0;
downloadFish = fish;
}
public void SetDownloadRenameRodFish(int fish)
{
renamingFish = 0;
renameFish = fish;
}
public void SetDownloadRod(string rod)
{
this.UIThread(delegate
{
lblRod.Text = string.Format(Resources.DBSyncMessageFormatDownloading, rod);
downloadingRod++;
ProgressUpdate();
});
}
public void SetDownloadFish(string fish)
{
this.UIThread(delegate
{
lblFish.Text = fish;
downloadingFish++;
ProgressUpdate();
});
}
public void SetDownloadRenameFish(string fromName, string toName)
{
this.UIThread(delegate
{
lblFish.Text = string.Format(Resources.DBSyncMessageFormatRenaming, fromName, toName);
renamingFish++;
ProgressUpdate();
});
}
public void SetFishBaitOrZone(string fish, string baitOrZone)
{
this.UIThread(delegate
{
lblFish.Text = string.Format(MessageFormatFishBaitOrZone, fish, baitOrZone);
});
}
private void ProgressUpdate()
{
int prog = 0;
int half = 0;
this.UIThreadInvoke(delegate
{
half = progress.Maximum / 2;
});
if (uploadingFish > 0)
{
prog = (int)Math.Ceiling(Math.Min((double)(uploadingFish - 1) / uploadFish, 1.0D) * half);
}
if (downloadingRod > 0)
{
prog = half / 2;
prog += (int)Math.Ceiling(Math.Min((double)(downloadingRod - 1) / downloadRods, 1.0D) * half / downloadRods / 2);
if (downloadingFish > 0)
{
prog += (int)Math.Ceiling(Math.Min((double)(downloadingFish - 1) / downloadFish, 1.0D) * half / downloadRods / 2);
}
if (renamingFish > 0)
{
prog += (int)Math.Ceiling(Math.Min((double)(renamingFish - 1) / renameFish, 1.0D) * half / downloadRods / 2);
}
}
this.UIThread(delegate
{
progress.Value = prog;
});
}
public void Error(string message, params object[] args)
{
this.UIThread(delegate
{
lblRod.Text = Resources.MessageError + string.Format(message, args);
lblFish.Text = string.Empty;
MessageBox.Show(message, Resources.MessageTitleSyncFormError);
Thread.Sleep(500);
});
}
public void Warning(string message, params object[] args)
{
this.UIThread(delegate
{
lblRod.Text = Resources.MessageWarning + string.Format(message, args);
lblFish.Text = string.Empty;
MessageBox.Show(message, Resources.MessageTitleSyncFormWarning);
Thread.Sleep(500);
});
}
public void Info(string message, params object[] args)
{
this.UIThread(delegate
{
lblRod.Text = Resources.MessageInfo + string.Format(message, args);
lblFish.Text = string.Empty;
MessageBox.Show(message, Resources.MessageTitleSyncFormInfo);
Thread.Sleep(500);
});
}
}
}