Korero – Blame information for rev 1

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