Skip to content

Commit 6cf5272

Browse files
committed
Handles all test cases in Notifications.docx
1 parent 20514b0 commit 6cf5272

File tree

3 files changed

+107
-99
lines changed

3 files changed

+107
-99
lines changed
438 KB
Binary file not shown.

Source/Menu/Notifications/NotificationManager.cs

Lines changed: 99 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@
2020
using System.IO;
2121
using System.Linq;
2222
using System.Net;
23+
using System.Security.Policy;
2324
using System.Text;
2425
using Newtonsoft.Json;
2526
using ORTS.Common;
2627
using ORTS.Settings;
2728
using ORTS.Updater;
2829
using SharpDX;
30+
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
2931
using static ORTS.Common.SystemInfo;
3032
using static ORTS.NotificationPage;
3133

34+
//TODO Add caching of criteria
35+
//TODO Add "includeIf" for individual notifications
36+
3237
namespace ORTS
3338
{
3439
class NotificationManager
@@ -64,7 +69,6 @@ public void CheckNotifications()
6469
{
6570
Error = null;
6671
Notifications = GetNotifications();
67-
DropUnusedUpdateNotifications();
6872
ParameterDictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
6973

7074
// To support testing, add any overriding values to the ValueDictionary
@@ -128,7 +132,7 @@ private string GetRemoteJson()
128132
// Helpful to supply server with data for its log file.
129133
client.Headers[HttpRequestHeader.UserAgent] = $"{System.Windows.Forms.Application.ProductName}/{VersionInfo.VersionOrBuild}";
130134

131-
return client.DownloadString(new Uri("https://wepp.co.uk/openrails/notifications/menu.json"));
135+
return client.DownloadString(new Uri("https://wepp.co.uk/openrails/notifications2/menu.json"));
132136
}
133137

134138
public void PopulatePageList()
@@ -144,7 +148,7 @@ public void PopulatePageList()
144148
/// Ultimately there will be a list of notifications downloaded from https://static.openrails.org/api/notifications/menu.json .
145149
/// Until then, there is a single notification announcing either that a new update is available or the installation is up to date.
146150
/// </summary>
147-
void SetUpdateNotificationPage()
151+
private void SetUpdateNotificationPage()
148152
{
149153
MainForm.UpdateNotificationPageAlert();
150154
PageList.Clear();
@@ -181,94 +185,60 @@ void SetUpdateNotificationPage()
181185
var skipPage = false;
182186
new NTitleControl(page, Index + 1, list.Count, n.Date, n.Title).Add();
183187

184-
// Check constraints for the MetList.
185-
//var failingCheck = (n.Met.ItemList?.Count > 0 && n.Met.CheckIdList?.Count > 0)
186-
// ? CheckConstraints(n)
187-
// : null;
188-
189-
// If any check fails then its UnmetList is added to the page, otherwise the MetList is added.
190-
//n.PrefixItemList?.ForEach(item => AddItemToPage(page, item));
191-
//if (failingCheck == null)
192-
//{
193-
// n.Met.ItemList?.ForEach(item => AddItemToPage(page, item));
194-
//}
195-
//else
196-
//{
197-
// if (failingCheck.UnmetItemList == null) // Omit this section to skip the notification entirely.
198-
// {
199-
// // Don't skip if there is only one notification.
200-
// if (list.Count > 1) skipPage = true;
201-
// }
202-
// else
203-
// {
204-
// failingCheck.UnmetItemList?.ForEach(item => AddItemToPage(page, item));
205-
// }
206-
//}
207-
//n.SuffixItemList?.ForEach(item => AddItemToPage(page, item));
208-
n.ItemList?.ForEach(item => AddItemToPage(page, item));
209-
if (skipPage == false) PageList.Add(page);
210-
}
211-
212-
/// <summary>
213-
/// CheckConstraints() checks the constraints in sequence, but all parts are optional.
214-
/// If all the constraints in any Includes are met, then the whole check is met.
215-
/// If all the constraints in any Excludes are met, then the whole check is not met.
216-
/// Returns null if the whole check is met else returns the check that failed.
217-
/// </summary>
218-
/// <param name="n"></param>
219-
private Check CheckConstraints(Notification n)
220-
{
221-
Check failingCheck = null;
222-
foreach (var checkName in n.CheckIdList) // CheckIdList is optional
188+
// Check constraints for each item
189+
foreach(var item in n.ItemList)
223190
{
224-
LogChecks(checkName);
225-
226-
// Find the matching check
227-
var check = Notifications.CheckList.Where(c => c.Id == checkName).FirstOrDefault();
228-
if (check != null && check.AnyOfList.Count() > 0)
229-
{
230-
foreach(var anyOf in check.AnyOfList)
231-
{
232-
//if (anyOf is Excludes)
233-
//{
234-
// if (CheckAllMatch(anyOf.AllOfList) == true) return check; // immediate fail so quit
235-
//}
236-
//if (anyOf is Includes)
237-
//{
238-
// if (CheckAllMatch(anyOf.AllOfList) == false) failingCheck = check; // fail but continue testing other Includes
239-
//}
240-
}
241-
}
191+
if (AreChecksMet(item)) AddItemToPage(page, item);
242192
}
243-
return failingCheck;
193+
if (skipPage == false) PageList.Add(page);
244194
}
245195

246-
private void AddItemToPage(NotificationPage page, Item item)
196+
#region Process Criteria
197+
private bool AreChecksMet(Item item)
247198
{
248-
if (item is Record record)
199+
if (item.IncludeIf != null || item.IncludeIfNot != null)
249200
{
250-
new NRecordControl(page, item.Label, item.Indent, record.Value).Add();
201+
AppendToLog($"Label: {item.Label}");
251202
}
252-
else if (item is Link link)
253-
{
254-
new NLinkControl(page, item.Label, item.Indent, link.Value, MainForm, link.Url).Add();
255-
}
256-
else if (item is Update update)
203+
if (item.IncludeIf != null)
257204
{
258-
new NUpdateControl(page, item.Label, item.Indent, update.Value, MainForm).Add();
205+
foreach (var checkName in item.IncludeIf)
206+
{
207+
// Include if A=true AND B=true AND ...
208+
if (IsCheckMet(checkName) == false) return false;
209+
}
259210
}
260-
else if (item is Heading heading)
211+
if (item.IncludeIfNot != null)
261212
{
262-
new NHeadingControl(page, item.Label, heading.Color).Add();
213+
foreach (var checkName in item.IncludeIfNot)
214+
{
215+
// Include if C=false AND D=false AND ...
216+
if (IsCheckMet(checkName) == true) return false;
217+
}
263218
}
264-
else if (item is Text text)
219+
220+
return true;
221+
}
222+
223+
private bool IsCheckMet(string checkname)
224+
{
225+
foreach (var check in Notifications.CheckList)
265226
{
266-
new NTextControl(page, item.Label, text.Color).Add();
227+
if (check.Id == checkname)
228+
{
229+
if (CheckAnyMatch(check.AnyOfList)) return true;
230+
}
267231
}
268-
else if (item is Item item2)
232+
return false;
233+
}
234+
235+
private bool CheckAnyMatch(List<AnyOf> anyOfList)
236+
{
237+
foreach (var anyOf in anyOfList)
269238
{
270-
new NTextControl(page, item.Label).Add();
239+
if (CheckAllMatch(anyOf.AllOfList)) return true;
271240
}
241+
return false;
272242
}
273243

274244
private bool CheckAllMatch(List<Criteria> criteriaList)
@@ -304,29 +274,61 @@ private bool CheckContains(Criteria criteria, bool sense)
304274
LogCheckContains(criteria.Value, sense, content, result);
305275
return result;
306276
}
277+
#endregion
307278

308-
/// <summary>
309-
/// Drop any notifications for the channel not selected
310-
/// </summary>
311-
/// <param name="updateManager"></param>
312-
public void DropUnusedUpdateNotifications()
279+
private void AddItemToPage(NotificationPage page, Item item)
313280
{
314-
var updateModeSetting = UpdateManager.ChannelName.ToLower();
315-
foreach (var n in Notifications.NotificationList)
281+
if (item is Record record)
316282
{
317-
if (n.UpdateMode == null) // Skip notifications which are not updates
318-
continue;
319-
320-
var lowerUpdateMode = n.UpdateMode.ToLower();
321-
322-
// If setting == "none", then keep just one update notification, e.g. the stable one
323-
if (updateModeSetting == "" && lowerUpdateMode == "stable")
324-
continue;
283+
new NRecordControl(page, item.Label, item.Indent, record.Value).Add();
284+
}
285+
else if (item is Link link)
286+
{
287+
var url = GetUrl(link);
288+
if (string.IsNullOrEmpty(url) == false)
289+
{
290+
new NLinkControl(page, item.Label, item.Indent, link.Value, MainForm, url).Add();
291+
}
292+
}
293+
else if (item is Update update)
294+
{
295+
new NUpdateControl(page, item.Label, item.Indent, update.Value, MainForm).Add();
296+
}
297+
else if (item is Heading heading)
298+
{
299+
new NHeadingControl(page, item.Label, heading.Color).Add();
300+
}
301+
else if (item is Text text)
302+
{
303+
new NTextControl(page, item.Label, text.Color).Add();
304+
}
305+
else if (item is Item item2)
306+
{
307+
new NTextControl(page, item.Label).Add();
308+
}
309+
}
325310

326-
// Mark unused updates for deletion outside loop
327-
//n.ToDelete = lowerUpdateMode != updateModeSetting;
311+
private string GetUrl(Link link)
312+
{
313+
var url = link.Url;
314+
if (string.IsNullOrEmpty(url))
315+
{
316+
switch (UpdateManager.ChannelName.ToLower())
317+
{
318+
case "stable":
319+
case "": // Channel None
320+
url = link.StableUrl;
321+
break;
322+
case "testing":
323+
url = link.TestingUrl;
324+
break;
325+
case "unstable":
326+
url = link.UnstableUrl;
327+
break;
328+
}
328329
}
329-
//Notifications.NotificationList.RemoveAll(n => n.ToDelete);
330+
331+
return url;
330332
}
331333

332334
public void ReplaceParameters()
@@ -523,6 +525,7 @@ public OverrideParameterList GetOverrideParameters()
523525
return null;
524526
}
525527

528+
#region Logging
526529
public void LogOverrideParameters()
527530
{
528531
if (Log == false) return;
@@ -566,7 +569,7 @@ public void LogChecks(string checkName)
566569
public void LogCheckContains(string value, bool sense, string content, bool result)
567570
{
568571
var negation = sense ? "" : "NOT ";
569-
AppendToLog($"Check: '{value}' {negation}contained in '{content}' = {result}");
572+
AppendToLog($"Check: {result} = '{value}' {negation}contained in '{content}'");
570573
}
571574

572575
public void AppendToLog(string record)
@@ -578,5 +581,6 @@ public void AppendToLog(string record)
578581
sw.WriteLine(record);
579582
}
580583
}
584+
#endregion
581585
}
582586
}

Source/Menu/Notifications/Notifications.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717

1818
using System.Collections.Generic;
19-
using ORTS.Updater;
19+
2020

2121
namespace ORTS
2222
{
@@ -38,7 +38,6 @@ public class Notification
3838
public string Title { get; set; }
3939
public string UpdateMode { get; set; }
4040
public List<Item> ItemList { get; set; }
41-
public List<string> CheckIdList { get; set; }
4241
}
4342
class Record : Item
4443
{
@@ -55,6 +54,9 @@ class Link : Item
5554
{
5655
public string Value { get; set; }
5756
public string Url { get; set; }
57+
public string StableUrl { get; set; }
58+
public string TestingUrl { get; set; }
59+
public string UnstableUrl { get; set; }
5860
}
5961
class Update : Item
6062
{
@@ -63,6 +65,8 @@ class Update : Item
6365
}
6466
public class Item
6567
{
68+
public List<string> IncludeIf { get; set; }
69+
public List<string> IncludeIfNot { get; set; }
6670
public string Label { get; set; }
6771
public string Color { get; set; } = "black";
6872
public int Indent { get; set; } = 140;
@@ -80,15 +84,15 @@ public class Item
8084
public class Check
8185
{
8286
public string Id { get; set; }
83-
public List<AllOf> AnyOfList { get; set; }
87+
public List<AnyOf> AnyOfList { get; set; }
8488
//public List<Item> UnmetItemList { get; set; }
8589
}
8690

8791
//public class CheckAllOf
8892
//{
8993
// public List<Criteria> AllOfList { get; set; }
9094
//}
91-
public class AllOf
95+
public class AnyOf
9296
{
9397
public List<Criteria> AllOfList { get; set; }
9498
}

0 commit comments

Comments
 (0)