@@ -6,83 +6,188 @@ class LiveSyncDataController extends DataController {
66
77 public async function genGenerateData (): Awaitable <void > {
88 $data = array ();
9+ await tr_start ();
10+ $input_auth_key = idx (Utils :: getGET(), ' auth' , ' ' );
11+ $livesync_awaits = Map {
12+ ' livesync_enabled' => Configuration :: gen(' livesync' ),
13+ ' livesync_auth_key' => Configuration :: gen(' livesync_auth_key' ),
14+ };
15+ $livesync_awaits_results = await \HH \Asio \ m ($livesync_awaits );
16+ $livesync_enabled = $livesync_awaits_results [' livesync_enabled' ];
17+ $livesync_auth_key = $livesync_awaits_results [' livesync_auth_key' ];
918
10- $teams_array = array ();
11- $all_teams = await Team :: genAllTeams();
12- foreach ($all_teams as $team ) {
13- $team_livesync_exists =
14- await Team :: genLiveSyncExists($team -> getId(), " fbctf" );
15- if ($team_livesync_exists === true ) {
16- $team_livesync_key =
17- await Team :: genGetLiveSyncKey($team -> getId(), " fbctf" );
18- $teams_array [$team -> getId()] = strval ($team_livesync_key );
19- }
20- }
19+ if ($livesync_enabled -> getValue() === ' 1' &&
20+ hash_equals (
21+ strval ($livesync_auth_key -> getValue()),
22+ strval ($input_auth_key ),
23+ )) {
24+
25+ $livesync_enabled_awaits = Map {
26+ ' all_teams' => Team :: genAllTeams(),
27+ ' all_scores' => ScoreLog :: genAllScores(),
28+ ' all_hints' => HintLog :: genAllHints(),
29+ ' all_levels' => Level :: genAllLevels(),
30+ };
31+ $livesync_enabled_awaits_results =
32+ await \HH \Asio \ m ($livesync_enabled_awaits );
33+ $all_teams = $livesync_enabled_awaits_results [' all_teams' ];
34+ invariant (
35+ is_array ($all_teams ),
36+ ' all_teams should be an array and not null' ,
37+ );
38+
39+ $all_scores = $livesync_enabled_awaits_results [' all_scores' ];
40+ invariant (
41+ is_array ($all_scores ),
42+ ' all_scores should be an array and not null' ,
43+ );
44+
45+ $all_hints = $livesync_enabled_awaits_results [' all_hints' ];
46+ invariant (
47+ is_array ($all_hints ),
48+ ' all_hints should be an array and not null' ,
49+ );
2150
22- $scores_array = array ();
23- $scored_teams = array ();
24- $all_scores = await ScoreLog :: genAllScores();
25- foreach ($all_scores as $score ) {
26- if (in_array ($score -> getTeamId(), array_keys ($teams_array )) === false ) {
27- continue ;
51+ $all_levels = $livesync_enabled_awaits_results [' all_levels' ];
52+ invariant (
53+ is_array ($all_levels ),
54+ ' all_levels should be an array and not null' ,
55+ );
56+
57+ $data = array ();
58+ $teams_array = array ();
59+ $team_livesync_exists = Map {};
60+ $team_livesync_key = Map {};
61+ foreach ($all_teams as $team ) {
62+ $team_id = $team -> getId();
63+ $team_livesync_exists -> add(
64+ Pair {$team_id , Team :: genLiveSyncExists($team_id , " fbctf" )},
65+ );
2866 }
29- $scores_array [$score -> getLevelId()][$teams_array [$score -> getTeamId()]][' timestamp' ] =
30- $score -> getTs();
31- $scores_array [$score -> getLevelId()][$teams_array [$score -> getTeamId()]][' capture' ] =
32- true ;
33- $scores_array [$score -> getLevelId()][$teams_array [$score -> getTeamId()]][' hint' ] =
34- false ;
35- $scored_teams [$score -> getLevelId()][] = $score -> getTeamId();
36- }
37- $all_hints = await HintLog :: genAllHints();
38- foreach ($all_hints as $hint ) {
39- if ($hint -> getPenalty()) {
40- if (in_array ($hint -> getTeamId(), array_keys ($teams_array )) ===
67+ $team_livesync_exists_results = await \HH \Asio \ m ($team_livesync_exists );
68+ foreach ($team_livesync_exists_results as $team_id => $livesync_exists ) {
69+ if ($livesync_exists === true ) {
70+ $team_livesync_key -> add(
71+ Pair {$team_id , Team :: genGetLiveSyncKey($team_id , " fbctf" )},
72+ );
73+ }
74+ }
75+ $team_livesync_key_results = await \HH \Asio \ m ($team_livesync_key );
76+ $teams_array = $team_livesync_key_results -> toArray();
77+
78+ $scores_array = array ();
79+ $scored_teams = array ();
80+
81+ foreach ($all_scores as $score ) {
82+ if (in_array ($score -> getTeamId(), array_keys ($teams_array )) ===
4183 false ) {
4284 continue ;
4385 }
44- $scores_array [$hint -> getLevelId()][$teams_array [$hint -> getTeamId()]][' hint' ] =
86+ $scores_array [$score -> getLevelId()][$teams_array [$score -> getTeamId()]][' timestamp' ] =
87+ $score -> getTs();
88+ $scores_array [$score -> getLevelId()][$teams_array [$score -> getTeamId()]][' capture' ] =
4589 true ;
46- if (in_array (
47- $hint -> getTeamId(),
48- $scored_teams [$hint -> getLevelId()],
49- ) ===
50- false ) {
51- $scores_array [$hint -> getLevelId()][$teams_array [$hint -> getTeamId()]][' capture' ] =
52- false ;
53- $scores_array [$hint -> getLevelId()][$teams_array [$hint -> getTeamId()]][' timestamp' ] =
54- $hint -> getTs();
90+ $scores_array [$score -> getLevelId()][$teams_array [$score -> getTeamId()]][' hint' ] =
91+ false ;
92+ $scored_teams [$score -> getLevelId()][] = $score -> getTeamId();
93+ }
94+ foreach ($all_hints as $hint ) {
95+ if ($hint -> getPenalty()) {
96+ if (in_array ($hint -> getTeamId(), array_keys ($teams_array )) ===
97+ false ) {
98+ continue ;
99+ }
100+ $scores_array [$hint -> getLevelId()][$teams_array [$hint -> getTeamId()]][' hint' ] =
101+ true ;
102+ if (in_array (
103+ $hint -> getTeamId(),
104+ $scored_teams [$hint -> getLevelId()],
105+ ) ===
106+ false ) {
107+ $scores_array [$hint -> getLevelId()][$teams_array [$hint -> getTeamId()]][' capture' ] =
108+ false ;
109+ $scores_array [$hint -> getLevelId()][$teams_array [$hint -> getTeamId()]][' timestamp' ] =
110+ $hint -> getTs();
111+ }
55112 }
56113 }
57- }
58114
59- $levels_array = array ();
60- $all_levels = await Level :: genAllLevels();
61- foreach ($all_levels as $level ) {
62- $entity = await Country :: gen($level -> getEntityId());
63- $category = await Category :: genSingleCategory($level -> getCategoryId());
64- if (array_key_exists ($level -> getId(), $scores_array )) {
65- $score_level_array = $scores_array [$level -> getId()];
66- } else {
67- $score_level_array = array ();
115+ $levels_array = array ();
116+ $entities = Map {};
117+ $categories = Map {};
118+ foreach ($all_levels as $level ) {
119+ $level_id = $level -> getId();
120+ $entities -> add(Pair {$level_id , Country :: gen($level -> getEntityId())});
121+ $categories -> add(
122+ Pair {
123+ $level_id ,
124+ Category :: genSingleCategory($level -> getCategoryId()),
125+ },
126+ );
68127 }
69- $one_level = array (
70- ' active' => $level -> getActive(),
71- ' type' => $level -> getType(),
72- ' title' => $level -> getTitle(),
73- ' description' => $level -> getDescription(),
74- ' entity_iso_code' => $entity -> getIsoCode(),
75- ' category' => $category -> getCategory(),
76- ' points' => $level -> getPoints(),
77- ' bonus' => $level -> getBonusFix(),
78- ' bonus_dec' => $level -> getBonusDec(),
79- ' penalty' => $level -> getPenalty(),
80- ' teams' => $score_level_array ,
128+ $entities_results = await \HH \Asio \ m ($entities );
129+ invariant (
130+ $entities_results instanceof Map ,
131+ ' entities_results should of type Map and not null' ,
81132 );
82- $levels_array [] = $one_level ;
83- }
84133
85- $data = $levels_array ;
134+ $categories_results = await \HH \Asio \ m ($categories );
135+ invariant (
136+ $categories_results instanceof Map ,
137+ ' categories_results should of type Map and not null' ,
138+ );
139+
140+ foreach ($all_levels as $level ) {
141+ $level_id = $level -> getId();
142+ $entity = $entities_results -> get($level_id );
143+ invariant (
144+ $entity instanceof Country ,
145+ ' entity should of type Country and not null' ,
146+ );
147+
148+ $category = $categories_results -> get($level_id );
149+ invariant (
150+ $category instanceof Category ,
151+ ' category should of type Category and not null' ,
152+ );
153+
154+ if (array_key_exists ($level -> getId(), $scores_array )) {
155+ $score_level_array = $scores_array [$level_id ];
156+ } else {
157+ $score_level_array = array ();
158+ }
159+ $one_level = array (
160+ ' active' => $level -> getActive(),
161+ ' type' => $level -> getType(),
162+ ' title' => $level -> getTitle(),
163+ ' description' => $level -> getDescription(),
164+ ' entity_iso_code' => $entity -> getIsoCode(),
165+ ' category' => $category -> getCategory(),
166+ ' points' => $level -> getPoints(),
167+ ' bonus' => $level -> getBonusFix(),
168+ ' bonus_dec' => $level -> getBonusDec(),
169+ ' penalty' => $level -> getPenalty(),
170+ ' teams' => $score_level_array ,
171+ );
172+ $levels_array [] = $one_level ;
173+ }
174+
175+ $data = $levels_array ;
176+ } else if ($livesync_enabled -> getValue() === ' 0' ) {
177+ $data [' error' ] = tr (
178+ ' LiveSync is disabled, please contact the administrator for access.' ,
179+ );
180+ } else if (strval ($input_auth_key ) !==
181+ strval ($livesync_auth_key -> getValue())) {
182+ $data [' error' ] =
183+ tr (
184+ ' LiveSync auth key is invalid, please contact the administrator for access.' ,
185+ );
186+ } else {
187+ $data [' error' ] = tr (
188+ ' LiveSync failed, please contact the administrator for assistance.' ,
189+ );
190+ }
86191 $this -> jsonSend($data );
87192 }
88193
0 commit comments