Korero – Blame information for rev 2
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.Linq; |
||
4 | using System.Threading; |
||
5 | using System.Threading.Tasks; |
||
6 | using System.Windows.Forms; |
||
7 | using Korero.Communication; |
||
8 | using Korero.Properties; |
||
9 | using Korero.Serialization; |
||
10 | using Korero.Utilities; |
||
11 | using Serilog; |
||
12 | |||
13 | namespace Korero.Teleport |
||
14 | { |
||
15 | public partial class TeleportForm : Form |
||
16 | { |
||
17 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
||
18 | |||
19 | private readonly CancellationToken _cancellationToken; |
||
20 | |||
21 | private readonly CancellationTokenSource _cancellationTokenSource; |
||
22 | |||
23 | private readonly Task _loadInventoryLandmarksTask; |
||
24 | |||
25 | private readonly MainForm _mainForm; |
||
26 | |||
27 | private readonly MqttCommunication _mqttCommunication; |
||
28 | |||
29 | #endregion |
||
30 | |||
31 | #region Constructors, Destructors and Finalizers |
||
32 | |||
33 | public TeleportForm() |
||
34 | { |
||
35 | InitializeComponent(); |
||
36 | |||
37 | _cancellationTokenSource = new CancellationTokenSource(); |
||
38 | _cancellationToken = _cancellationTokenSource.Token; |
||
39 | } |
||
40 | |||
41 | public TeleportForm(MainForm mainForm, MqttCommunication mqttCommunication) : this() |
||
42 | { |
||
43 | _mainForm = mainForm; |
||
44 | _mqttCommunication = mqttCommunication; |
||
45 | |||
46 | _loadInventoryLandmarksTask = LoadInventoryLandmarks(_cancellationToken); |
||
47 | _mqttCommunication.NotificationReceived += MqttCommunication_NotificationReceived; |
||
48 | } |
||
49 | |||
50 | /// <summary> |
||
51 | /// Clean up any resources being used. |
||
52 | /// </summary> |
||
53 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
54 | protected override void Dispose(bool disposing) |
||
55 | { |
||
56 | if (disposing && components != null) |
||
57 | { |
||
58 | components.Dispose(); |
||
59 | } |
||
60 | |||
61 | _mqttCommunication.NotificationReceived -= MqttCommunication_NotificationReceived; |
||
62 | base.Dispose(disposing); |
||
63 | } |
||
64 | |||
65 | #endregion |
||
66 | |||
67 | #region Event Handlers |
||
68 | |||
2 | office | 69 | private void TeleportForm_Load(object sender, EventArgs e) |
70 | { |
||
71 | Utilities.WindowState.FormTracker.Track(this); |
||
72 | } |
||
73 | |||
1 | office | 74 | private void MqttCommunication_NotificationReceived(object sender, MqttNotificationEventArgs e) |
75 | { |
||
76 | switch (e.Notification["notification"]) |
||
77 | { |
||
78 | case "teleport": |
||
79 | toolStripStatusLabel1.Text = $"Teleport status: {e.Notification["status"]}"; |
||
80 | break; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | private async void Button1_Click(object sender, EventArgs e) |
||
85 | { |
||
86 | if (!await CheckTeleportRegion(textBox1.Text)) |
||
87 | { |
||
88 | return; |
||
89 | } |
||
90 | |||
91 | if (!int.TryParse(textBox2.Text, out var x)) |
||
92 | { |
||
93 | x = 128; |
||
94 | } |
||
95 | |||
96 | if (!int.TryParse(textBox3.Text, out var y)) |
||
97 | { |
||
98 | y = 128; |
||
99 | } |
||
100 | |||
101 | if (!int.TryParse(textBox4.Text, out var z)) |
||
102 | { |
||
103 | z = 25; |
||
104 | } |
||
105 | |||
106 | await TeleportManual(textBox1.Text, x, y, z); |
||
107 | } |
||
108 | |||
109 | private async void TextBox1_KeyPress(object sender, KeyPressEventArgs e) |
||
110 | { |
||
111 | if (e.KeyChar != (char) Keys.Enter) |
||
112 | { |
||
113 | return; |
||
114 | } |
||
115 | |||
116 | if (!await CheckTeleportRegion(textBox1.Text)) |
||
117 | { |
||
118 | return; |
||
119 | } |
||
120 | |||
121 | if (!int.TryParse(textBox2.Text, out var x)) |
||
122 | { |
||
123 | x = 128; |
||
124 | } |
||
125 | |||
126 | if (!int.TryParse(textBox3.Text, out var y)) |
||
127 | { |
||
128 | y = 128; |
||
129 | } |
||
130 | |||
131 | if (!int.TryParse(textBox4.Text, out var z)) |
||
132 | { |
||
133 | z = 25; |
||
134 | } |
||
135 | |||
136 | await TeleportManual(textBox1.Text, x, y, z); |
||
137 | } |
||
138 | |||
139 | private async void Button2_Click(object sender, EventArgs e) |
||
140 | { |
||
141 | var index = comboBox1.SelectedIndex; |
||
142 | if (index == -1) |
||
143 | { |
||
144 | return; |
||
145 | } |
||
146 | |||
147 | var item = (LandmarkComboBoxItem) comboBox1.SelectedItem; |
||
148 | |||
149 | toolStripStatusLabel1.Text = string.Empty; |
||
150 | |||
151 | await TeleportLandmark(item.Id); |
||
152 | } |
||
153 | |||
154 | private async void Button3_Click(object sender, EventArgs e) |
||
155 | { |
||
156 | var data = new Dictionary<string, string> |
||
157 | { |
||
158 | {"command", "gohome"}, |
||
159 | {"group", Settings.Default.Group}, |
||
160 | {"password", Settings.Default.Password} |
||
161 | }; |
||
162 | |||
163 | var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken); |
||
164 | |||
165 | if (callback == null || !callback.Success) |
||
166 | { |
||
167 | if (callback != null) |
||
168 | { |
||
169 | Log.Warning("Command {Command} has failed with {Error}.", |
||
170 | callback.Command, callback.Error); |
||
171 | } |
||
172 | |||
173 | toolStripStatusLabel1.Text = "Could not teleport home."; |
||
174 | |||
175 | return; |
||
176 | } |
||
177 | |||
178 | toolStripStatusLabel1.Text = "Teleported home."; |
||
179 | } |
||
180 | |||
181 | private void TeleportForm_FormClosing(object sender, FormClosingEventArgs e) |
||
182 | { |
||
183 | _cancellationTokenSource.Cancel(); |
||
184 | } |
||
185 | |||
186 | #endregion |
||
187 | |||
188 | #region Private Methods |
||
189 | |||
190 | private async Task LoadInventoryLandmarks(CancellationToken cancellationToken) |
||
191 | { |
||
192 | var data = new Dictionary<string, string> |
||
193 | { |
||
194 | {"command", "inventory"}, |
||
195 | {"group", Settings.Default.Group}, |
||
196 | {"password", Settings.Default.Password}, |
||
197 | {"action", "ls"}, |
||
198 | {"path", "/My Inventory/Landmarks"} |
||
199 | }; |
||
200 | |||
201 | var callback = await _mqttCommunication.SendCommand(new Command(data), cancellationToken); |
||
202 | |||
203 | if (callback == null || !callback.Success) |
||
204 | { |
||
205 | if (callback != null) |
||
206 | { |
||
207 | Log.Warning("Command {Command} has failed with {Error}.", |
||
208 | callback.Command, callback.Error); |
||
209 | } |
||
210 | |||
211 | return; |
||
212 | } |
||
213 | |||
214 | comboBox1.InvokeIfRequired(comboBox => { comboBox.Items.Clear(); }); |
||
215 | |||
216 | foreach (var landmark in CsvStride(callback.Data, 10)) |
||
217 | { |
||
218 | var index = Array.IndexOf(landmark, "name"); |
||
219 | var name = landmark[index + 1]; |
||
220 | index = Array.IndexOf(landmark, "type"); |
||
221 | var type = landmark[index + 1]; |
||
222 | |||
223 | if (type != "Landmark") |
||
224 | { |
||
225 | continue; |
||
226 | } |
||
227 | |||
228 | index = Array.IndexOf(landmark, "item"); |
||
229 | |||
230 | var item = landmark[index + 1]; |
||
231 | |||
232 | if (!Guid.TryParse(item, out var id)) |
||
233 | { |
||
234 | continue; |
||
235 | } |
||
236 | |||
237 | var landmarkComboBoxItem = new LandmarkComboBoxItem(name, id); |
||
238 | |||
239 | comboBox1.InvokeIfRequired(comboBox => { comboBox.Items.Add(landmarkComboBoxItem); }); |
||
240 | } |
||
241 | } |
||
242 | |||
243 | private async Task TeleportManual(string region, int x, int y, int z) |
||
244 | { |
||
245 | var data = new Dictionary<string, string> |
||
246 | { |
||
247 | {"command", "teleport"}, |
||
248 | {"group", Settings.Default.Group}, |
||
249 | {"password", Settings.Default.Password}, |
||
250 | {"entity", "region"}, |
||
251 | {"region", region}, |
||
252 | {"position", $"<{x}, {y}, {z}>"} |
||
253 | }; |
||
254 | |||
255 | var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken); |
||
256 | |||
257 | if (callback == null || !callback.Success) |
||
258 | { |
||
259 | if (callback != null) |
||
260 | { |
||
261 | Log.Warning("Command {Command} has failed with {Error}.", |
||
262 | callback.Command, callback.Error); |
||
263 | } |
||
264 | } |
||
265 | } |
||
266 | |||
267 | private async Task<bool> CheckTeleportRegion(string name) |
||
268 | { |
||
269 | // Check for region name roundtrip. |
||
270 | var data = new Dictionary<string, string> |
||
271 | { |
||
272 | {"command", "getgridregiondata"}, |
||
273 | {"group", Settings.Default.Group}, |
||
274 | {"password", Settings.Default.Password}, |
||
275 | {"entity", "region"}, |
||
276 | {"region", textBox1.Text}, |
||
277 | {"data", "Name"} |
||
278 | }; |
||
279 | |||
280 | var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken); |
||
281 | |||
282 | if (callback == null || !callback.Success) |
||
283 | { |
||
284 | if (callback != null) |
||
285 | { |
||
286 | Log.Warning("Command {Command} has failed with {Error}.", |
||
287 | callback.Command, callback.Error); |
||
288 | } |
||
289 | |||
290 | return false; |
||
291 | } |
||
292 | |||
293 | if (!string.Equals(name, callback["Name"].FirstOrDefault(), StringComparison.OrdinalIgnoreCase)) |
||
294 | { |
||
295 | return false; |
||
296 | } |
||
297 | |||
298 | return true; |
||
299 | } |
||
300 | |||
301 | private static IEnumerable<string[]> CsvStride(string input, int stride) |
||
302 | { |
||
303 | var i = 0; |
||
304 | return new CSV(input).Select(s => new {s, num = i++}) |
||
305 | .GroupBy(t => t.num / stride, t => t.s) |
||
306 | .Select(g => g.ToArray()); |
||
307 | } |
||
308 | |||
309 | private async Task TeleportLandmark(Guid id) |
||
310 | { |
||
311 | var data = new Dictionary<string, string> |
||
312 | { |
||
313 | {"command", "teleport"}, |
||
314 | {"group", Settings.Default.Group}, |
||
315 | {"password", Settings.Default.Password}, |
||
316 | {"entity", "landmark"}, |
||
317 | {"item", $"{id.ToString()}"} |
||
318 | }; |
||
319 | |||
320 | var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken); |
||
321 | |||
322 | if (callback == null || !callback.Success) |
||
323 | { |
||
324 | if (callback != null) |
||
325 | { |
||
326 | Log.Warning("Command {Command} has failed with {Error}.", |
||
327 | callback.Command, callback.Error); |
||
328 | } |
||
329 | } |
||
330 | } |
||
331 | |||
332 | #endregion |
||
2 | office | 333 | |
334 | |||
1 | office | 335 | } |
336 | } |