|
10 | 10 | // |
11 | 11 |
|
12 | 12 | using System; |
| 13 | +using System.Collections.Generic; |
| 14 | +using Allow2_SimpleJSON; |
| 15 | + |
13 | 16 | namespace Allow2 |
14 | 17 | { |
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 |
16 | 80 | { |
17 | 81 | //public Allow2CheckResult() |
18 | 82 | //{ |
19 | 83 | //} |
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 | + } |
21 | 98 |
|
22 | 99 | public DateTime Expires { |
23 | 100 | 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(); |
26 | 184 | } |
27 | 185 | } |
28 | 186 | } |
| 187 | + |
29 | 188 | } |
0 commit comments