Skip to content

Commit 247345b

Browse files
committed
implement Allow2Result helpers
1 parent dd7af38 commit 247345b

4 files changed

Lines changed: 200 additions & 5 deletions

File tree

Allow2/Allow2.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public static class Allow2
3939

4040
public static int childId; // ie: 34
4141

42+
public static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
43+
4244
public static string ApiUrl
4345
{
4446
get

Allow2/Allow2Ban.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Allow2Unity
3+
// Allow2Ban.cs
4+
//
5+
// Created by Andrew Longhorn in May 2019.
6+
// Copyright © 2019 Allow2 Pty Ltd. All rights reserved.
7+
//
8+
// LICENSE:
9+
// See LICENSE file in root directory
10+
//
11+
12+
using System;
13+
using Allow2_SimpleJSON;
14+
15+
namespace Allow2
16+
{
17+
public class Allow2Ban
18+
{
19+
public int Id { get; private set; }
20+
public string Title { get; private set; }
21+
public DateTime AppliedAt { get; private set; }
22+
public int Duration { get; private set; }
23+
public bool Selected { get; private set; }
24+
25+
Allow2Ban(string name, JSONNode val)
26+
{
27+
Id = val["id"].AsInt;
28+
Title = name;
29+
AppliedAt = Allow2.Epoch.AddSeconds(val["appliedAt"].AsInt).ToUniversalTime();
30+
Duration = val["durationMinutes"].AsInt;
31+
Selected = false;
32+
}
33+
}
34+
}

Allow2/Allow2CheckResult.cs

Lines changed: 163 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,179 @@
1010
//
1111

1212
using System;
13+
using System.Collections.Generic;
14+
using Allow2_SimpleJSON;
15+
1316
namespace Allow2
1417
{
15-
public class Allow2CheckResult : Allow2_SimpleJSON.JSONObject // shortcut implementation for now
18+
// Allow2Response JSON structure
19+
//{
20+
// "allowed":false,
21+
// "activities":{
22+
// "1":{
23+
// "id":1,
24+
// "name":"Internet",
25+
// "timed":true,
26+
// "units":"minutes",
27+
// "banned":false,
28+
// "remaining":0,
29+
// "cached":false,
30+
// "expires":1557067560,
31+
// "timeBlock":{
32+
// "allowed":true,
33+
// "remaining":495
34+
// }
35+
// },
36+
// "2":{
37+
// "id":2,
38+
// "name":"Computer",
39+
// "timed":true,
40+
// "units":"minutes",
41+
// "banned":false,
42+
// "remaining":0,
43+
// "cached":false,
44+
// "expires":1557067560,
45+
// "timeBlock":{
46+
// "allowed":true,
47+
// "remaining":375
48+
// }
49+
// }
50+
// },
51+
// "dayTypes":{
52+
// "today":{"id":57,"name":"Weekend"},
53+
// "tomorrow":{"id":61,"name":"School Day"}
54+
// },
55+
// "allDayTypes":[
56+
// {"id":57,"name":"Weekend"},
57+
// {"id":59,"name":"Weekday"},
58+
// {"id":61,"name":"School Day"},
59+
// {"id":64,"name":"Sick Day"},
60+
// {"id":66,"name":"Holiday"},
61+
// {"id":16997,"name":"No Limit"}
62+
// ],
63+
// "children":[
64+
// {"id":681,"name":"Bob","pin":"1234"},
65+
// {"id":639,"name":"Mary","pin":"4567"},
66+
// {"id":21423,"name":"Milly","pin":"5566"}
67+
// ],
68+
// "subscription":{
69+
// "active":false,
70+
// "type":1,
71+
// "maxChildren":6,
72+
// "childCount":3,
73+
// "deviceCount":12,
74+
// "serviceCount":0,
75+
// "financial":false
76+
// }
77+
//}
78+
79+
public class Allow2CheckResult : JSONObject // shortcut implementation for now
1680
{
1781
//public Allow2CheckResult()
1882
//{
1983
//}
20-
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
84+
85+
public JSONNode Activities {
86+
get {
87+
return this["activities"];
88+
}
89+
}
90+
91+
public JSONNode Subscription
92+
{
93+
get
94+
{
95+
return this["subscription"];
96+
}
97+
}
2198

2299
public DateTime Expires {
23100
get {
24-
int unixTimeStamp = this["activities"]["0"]["expires"];
25-
return Epoch.AddSeconds(unixTimeStamp).ToUniversalTime(); // nineteen70.AddSeconds(unixTimeStamp).ToLocalTime();
101+
int unixTimeStamp = ((Activities != null) && (Activities[0] != null)) ?
102+
this["activities"]["0"]["expires"].AsInt : 0;
103+
return Allow2.Epoch.AddSeconds(unixTimeStamp).ToUniversalTime(); // nineteen70.AddSeconds(unixTimeStamp).ToLocalTime();
104+
}
105+
}
106+
107+
public string NeedSubscription {
108+
get {
109+
if (!Subscription["financial"].AsBool) {
110+
int childCount = Subscription["childCount"].AsInt;
111+
int maxChildren = Subscription["maxChildren"].AsInt;
112+
//int serviceCount = subscription["serviceCount"].AsInt;
113+
//int deviceCount = subscription["deviceCount"].AsInt;
114+
int type = Subscription["type"].AsInt;
115+
116+
if ((maxChildren > 0 ) && (childCount > maxChildren) && (type == 1)) {
117+
return "Subscription Upgrade Required.";
118+
}
119+
120+
return "Subscription Required.";
121+
}
122+
return null;
123+
}
124+
}
125+
126+
public bool IsAllowed {
127+
get {
128+
return this["allowed"].AsBool;
129+
}
130+
}
131+
132+
public string Explanation {
133+
get {
134+
List<string> reasons = new List<string>();
135+
string subscriptionString = NeedSubscription;
136+
if (subscriptionString != null) {
137+
reasons.Add(subscriptionString);
138+
}
139+
foreach (JSONNode activity in Activities) {
140+
if (activity["banned"].IsBoolean && activity["banned"].AsBool)
141+
{
142+
reasons.Add("You are currently banned from " + activity["name"].ToString());
143+
}
144+
else
145+
{
146+
JSONNode timeblock = activity["timeblock"];
147+
if ((timeblock == null) || !timeblock["allowed"].IsBoolean || !timeblock["allowed"].AsBool)
148+
{
149+
reasons.Add("You cannot use " + activity["name"].ToString() + " at this time.");
150+
}
151+
else
152+
{
153+
// todo: reasons.append("You have \(activity["remaining"]) to use \(activity["name"]).")
154+
}
155+
}
156+
}
157+
return String.Join("/n", reasons.ToArray());
158+
}
159+
}
160+
161+
public Allow2Ban[] CurrentBans {
162+
get {
163+
List<Allow2Ban> bans = new List<Allow2Ban>();
164+
foreach (JSONNode activity in Activities) {
165+
166+
if (activity["banned"].AsBool) {
167+
//int id = activity.dictionary?["id"]?.uInt64Value,
168+
string name = activity["name"].ToString();
169+
if ((activity["bans"] != null) && (activity["bans"]["bans"] != null) && (activity["bans"]["bans"].IsArray))
170+
{
171+
JSONArray items = activity["bans"]["bans"].AsArray;
172+
foreach (JSONNode item in items)
173+
{
174+
//bans.Add(new Allow2Ban(name, item));
175+
}
176+
}
177+
else
178+
{
179+
// todo: reasons.append("You have \(activity["remaining"]) to use \(activity["name"]).")
180+
}
181+
}
182+
}
183+
return bans.ToArray();
26184
}
27185
}
28186
}
187+
29188
}

Allow2/Allow2Child.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Allow2
1313
{
14-
public class Allow2Child // shortcut implementation for now
14+
public class Allow2Child
1515
{
1616
public int Id { get; private set; }
1717
public string Name { get; private set; }

0 commit comments

Comments
 (0)