Skip to content

Commit 0a956f5

Browse files
committed
Adds IncludesAllOf() and ExcludesAnyOf().
Adds local menu_test.json for testing changes.
1 parent 96eff08 commit 0a956f5

File tree

2 files changed

+74
-49
lines changed

2 files changed

+74
-49
lines changed

Source/Menu/Notifications/NotificationManager.cs

Lines changed: 68 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ public void CheckNotifications()
7676
public Notifications GetNotifications()
7777
{
7878
String notificationsSerial;
79-
// To support testing of a new menu.json file, GetNotifications tests for a local file first and uses that if present.
79+
// To support testing of a new menu.json file, GetNotifications tests for a local file test.json first and uses that if present.
8080

81-
var filename = @"menu.json";
81+
var filename = @"menu_test.json";
8282
if (System.IO.File.Exists(filename))
8383
{
8484
// Input from local file into a string
@@ -97,7 +97,8 @@ public Notifications GetNotifications()
9797
}
9898

9999
/// <summary>
100-
/// Fetch the Notifications from https://static.openrails.org/notifications/menu.json
100+
/// Fetch the Notifications from https://static.openrails.org/api/notifications/menu.json
101+
/// This file is copied hourly from Github openrails/notifications/
101102
/// </summary>
102103
private string GetRemoteJson()
103104
{
@@ -109,12 +110,6 @@ private string GetRemoteJson()
109110
// Helpful to supply server with data for its log file.
110111
client.Headers[HttpRequestHeader.UserAgent] = $"{System.Windows.Forms.Application.ProductName}/{VersionInfo.VersionOrBuild}";
111112

112-
// Trial for glTD and PBR graphics
113-
//return client.DownloadString(new Uri("https://wepp.co.uk/openrails/notifications/menu_testing_direct3d.json"));
114-
115-
// Trial for UserSettings
116-
//return client.DownloadString(new Uri("https://wepp.co.uk/openrails/notifications/menu_testing_user_settings.json"));
117-
118113
return client.DownloadString(new Uri("https://wepp.co.uk/openrails/notifications/menu.json"));
119114
}
120115

@@ -128,7 +123,7 @@ public void PopulatePageList()
128123
}
129124

130125
/// <summary>
131-
/// Ultimately there will be a list of notifications downloaded from openrails/content.
126+
/// Ultimately there will be a list of notifications downloaded from https://static.openrails.org/api/notifications/menu.json .
132127
/// Until then, there is a single notification announcing either that a new update is available or the installation is up to date.
133128
/// </summary>
134129
void SetUpdateNotificationPage()
@@ -186,61 +181,79 @@ void SetUpdateNotificationPage()
186181
}
187182

188183
/// <summary>
189-
/// IncludesAnyOf() implements D == d OR E == e OR ...
184+
/// ExcludesAnyOf() implements A != a OR B != b AND ...
190185
/// ExcludesAllOf() implements A != a AND B != b AND ...
191-
/// CheckConstraints() implements ExcludesAnyOf() AND IncludesAnyOf(), but all parts are optional.
186+
/// IncludesAnyOf() implements D == d OR E == e OR ...
187+
/// IncludesAllOf() implements D == d AND E == e OR ...
188+
/// CheckConstraints() implements ExcludesAnyOf() AND ExcludesAllOf() AND IncludesAnyOf() AND IncludesAllOf(), but all parts are optional.
192189
/// </summary>
193190
/// <param name="page"></param>
194191
/// <param name="n"></param>
195192
private void CheckConstraints(NotificationPage page, Notification n)
196193
{
197-
var excludesMet = true;
198-
var includesMet = true;
199194
foreach (var nc in n.MetLists.CheckIdList)
200195
{
201-
foreach (var c in Notifications.CheckList.Where(c => c.Id == nc.Id))
196+
// Find the matching check
197+
var c = Notifications.CheckList.Where(check => check.Id == nc.Id).FirstOrDefault();
198+
if (c != null)
202199
{
203-
if (c.ExcludesAllOf == null) // ExcludesAnyOf is optional
204-
excludesMet = true;
205-
else
200+
// Check the ALL constraints
201+
if (c.ExcludesAllOf != null) // ExcludesAllOf is optional
206202
{
207-
var checkFailed = CheckExcludes(c);
208-
excludesMet = (checkFailed == null);
209-
if (excludesMet == false)
203+
var checkFailed = CheckMatch(c, c.ExcludesAllOf);
204+
if (checkFailed != null)
210205
{
211206
foreach (var item in checkFailed.UnmetItemList)
212207
{
213208
AddItemToPage(page, item);
214209
}
215-
break;
210+
return;
216211
}
217212
}
218213

219-
if (c.IncludesAnyOf == null) // IncludesAnyOf is optional
220-
includesMet = true;
221-
else
214+
// NOT TESTED YET
215+
//if (c.IncludesAllOf != null) // IncludesAllOf is optional
216+
//{
217+
// var checkFailed = CheckAll(c, c.IncludesAllOf);
218+
// if (checkFailed != null)
219+
// {
220+
// foreach (var item in checkFailed.UnmetItemList)
221+
// {
222+
// AddItemToPage(page, item);
223+
// }
224+
// return;
225+
// }
226+
//}
227+
228+
// Check the ANY constraints
229+
if (c.ExcludesAnyOf != null) // ExcludesAnyOf is optional
222230
{
223-
var checkPassed = CheckIncludes(c);
224-
includesMet = (checkPassed != null);
225-
if (includesMet == false)
231+
if (CheckMatch(c, c.ExcludesAnyOf) != null)
226232
{
227233
foreach (var item in c.UnmetItemList)
228234
{
229235
AddItemToPage(page, item);
230236
}
231-
break;
237+
return;
238+
}
239+
}
240+
241+
if (c.IncludesAnyOf != null) // IncludesAnyOf is optional
242+
{
243+
if (CheckMatch(c, c.IncludesAnyOf) == null)
244+
{
245+
foreach (var item in c.UnmetItemList)
246+
{
247+
AddItemToPage(page, item);
248+
}
249+
return;
232250
}
233251
}
234252
}
235-
if (excludesMet == false || includesMet == false)
236-
break;
237253
}
238-
if (excludesMet && includesMet)
254+
foreach (var item in n.MetLists.ItemList)
239255
{
240-
foreach (var item in n.MetLists.ItemList)
241-
{
242-
AddItemToPage(page, item);
243-
}
256+
AddItemToPage(page, item);
244257
}
245258
}
246259

@@ -269,34 +282,39 @@ private void AddItemToPage(NotificationPage page, Item item)
269282
}
270283

271284
/// <summary>
272-
/// Returns any check that fails.
285+
/// Returns null or the first check that matches.
273286
/// </summary>
274287
/// <param name="check"></param>
275288
/// <returns></returns>
276-
private Check CheckExcludes(Check check)
289+
private Check CheckMatch(Check check, List<Criteria> criteriaList)
277290
{
278-
foreach (var c in check.ExcludesAllOf)
291+
foreach (var c in criteriaList)
279292
{
280-
if (c is Contains) // Other criteria might be added such as greater_than and less_than
293+
if (c is Contains) // Other criteria might be added such as NoLessThan and NoMoreThan.
281294
{
282-
return CheckContains(check, c);
295+
var matchedCheck = CheckContains(check, c);
296+
if (matchedCheck != null)
297+
return matchedCheck;
283298
}
284299
}
285300
return null;
286301
}
287302

288303
/// <summary>
289-
/// Returns any check that succeeds.
304+
/// Returns null or the first check that does not match.
290305
/// </summary>
291306
/// <param name="check"></param>
292307
/// <returns></returns>
293-
private Check CheckIncludes(Check check)
308+
private Check CheckNoMatch(Check check, List<Criteria> criteriaList)
294309
{
295-
foreach (var c in check.IncludesAnyOf)
310+
foreach (var c in criteriaList)
296311
{
297-
if (c is Contains)
312+
if (c is Contains) // Other criteria might be added such as NoLessThan and NoMoreThan.
298313
{
299-
return CheckContains(check, c);
314+
if (CheckContains(check, c) == null)
315+
{
316+
return check;
317+
}
300318
}
301319
}
302320
return null;
@@ -311,8 +329,7 @@ private Check CheckContains(Check check, Criteria c)
311329
return check;
312330
break;
313331
case "installed_version":
314-
if (c.Value == "none" // as Update Mode == "none"
315-
|| SystemInfo.Application.Version.IndexOf(c.Value, StringComparison.OrdinalIgnoreCase) > -1)
332+
if (SystemInfo.Application.Version.IndexOf(c.Value, StringComparison.OrdinalIgnoreCase) > -1)
316333
return check;
317334
break;
318335
case "system":
@@ -321,7 +338,7 @@ private Check CheckContains(Check check, Criteria c)
321338
if (system.IndexOf(c.Value, StringComparison.OrdinalIgnoreCase) > -1)
322339
return check;
323340
break;
324-
default:
341+
default:
325342
if (GetSetting(c.Name).IndexOf(c.Value, StringComparison.OrdinalIgnoreCase) > -1)
326343
{
327344
return check;
@@ -388,6 +405,8 @@ public void ReplaceParameters()
388405
foreach (var c in Notifications.CheckList)
389406
{
390407
c.ExcludesAllOf?.ForEach(criteria => ReplaceCriteriaParameter(criteria));
408+
c.IncludesAllOf?.ForEach(criteria => ReplaceCriteriaParameter(criteria));
409+
c.ExcludesAnyOf?.ForEach(criteria => ReplaceCriteriaParameter(criteria));
391410
c.IncludesAnyOf?.ForEach(criteria => ReplaceCriteriaParameter(criteria));
392411
}
393412
}

Source/Menu/Notifications/Notifications.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,20 @@ public class Check
8282
{
8383
public string Id { get; set; }
8484
public List<Criteria> IncludesAnyOf { get; set; }
85+
public List<Criteria> IncludesAllOf { get; set; }
86+
public List<Criteria> ExcludesAnyOf { get; set; }
8587
public List<Criteria> ExcludesAllOf { get; set; }
8688
public List<Item> UnmetItemList { get; set; }
8789
public bool IsChecked { get; set; } = false;
8890
public bool IsMet { get; set; } = false;
8991
}
9092
class Contains : Criteria { }
93+
94+
// Not implemented yet
95+
// String comparison, not numerical
9196
class NoLessThan : Criteria { }
9297
class NoMoreThan : Criteria { }
98+
9399
public class Criteria
94100
{
95101
// System Information "examples"

0 commit comments

Comments
 (0)