Skip to content
This repository was archived by the owner on Mar 3, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ INSERT INTO `configuration` (field, value, description) VALUES("password_type",
INSERT INTO `configuration` (field, value, description) VALUES("default_bonus", "30", "(Integer) Default value for bonus in levels");
INSERT INTO `configuration` (field, value, description) VALUES("default_bonusdec", "10", "(Integer) Default bonus decrement in levels");
INSERT INTO `configuration` (field, value, description) VALUES("language", "en", "(String) Language of the system");
INSERT INTO `configuration` (field, value, description) VALUES("livesync", "0", "(Boolean) LiveSync functionality");
INSERT INTO `configuration` (field, value, description) VALUES("livesync_auth_key", "", "(String) Optional LiveSync Auth Key");
UNLOCK TABLES;

--
Expand Down
2 changes: 2 additions & 0 deletions database/test_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ INSERT INTO `configuration` (field, value, description) VALUES("password_type",
INSERT INTO `configuration` (field, value, description) VALUES("default_bonus", "30", "(Integer) Default value for bonus in levels");
INSERT INTO `configuration` (field, value, description) VALUES("default_bonusdec", "10", "(Integer) Default bonus decrement in levels");
INSERT INTO `configuration` (field, value, description) VALUES("language", "en", "(String) Language of the system");
INSERT INTO `configuration` (field, value, description) VALUES("livesync", "0", "(Boolean) LiveSync functionality");
INSERT INTO `configuration` (field, value, description) VALUES("livesync_auth_key", "", "(String) Optional LiveSync Auth Key");
UNLOCK TABLES;

--
Expand Down
43 changes: 43 additions & 0 deletions src/controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ class="fb-cta cta--yellow"
'autorun_cycle' => Configuration::gen('autorun_cycle'),
'start_ts' => Configuration::gen('start_ts'),
'end_ts' => Configuration::gen('end_ts'),
'livesync' => Configuration::gen('livesync'),
'livesync_auth_key' => Configuration::gen('livesync_auth_key'),
};

$results = await \HH\Asio\m($awaitables);
Expand All @@ -318,6 +320,8 @@ class="fb-cta cta--yellow"
$autorun_cycle = $results['autorun_cycle'];
$start_ts = $results['start_ts'];
$end_ts = $results['end_ts'];
$livesync = $results['livesync'];
$livesync_auth_key = $results['livesync_auth_key'];

$registration_on = $registration->getValue() === '1';
$registration_off = $registration->getValue() === '0';
Expand All @@ -337,6 +341,8 @@ class="fb-cta cta--yellow"
$gameboard_off = $gameboard->getValue() === '0';
$timer_on = $timer->getValue() === '1';
$timer_off = $timer->getValue() === '0';
$livesync_on = $livesync->getValue() === '1';
$livesync_off = $livesync->getValue() === '0';

$game_start_array = array();
if ($start_ts->getValue() !== '0' && $start_ts->getValue() !== 'NaN') {
Expand Down Expand Up @@ -887,6 +893,43 @@ class="fb-cta cta--yellow"
</div>
</div>
</section>
<section class="admin-box">
<header class="admin-box-header">
<h3>{tr('LiveSync')}</h3>
<div class="admin-section-toggle radio-inline">
<input
type="radio"
name="fb--conf--livesync"
id="fb--conf--livesync--on"
checked={$livesync_on}
/>
<label for="fb--conf--livesync--on">
{tr('On')}
</label>
<input
type="radio"
name="fb--conf--livesync"
id="fb--conf--livesync--off"
checked={$livesync_off}
/>
<label for="fb--conf--livesync--off">
{tr('Off')}
</label>
</div>
</header>
<div class="fb-column-container">
<div class="col col-pad col-1-4">
<div class="form-el el--block-label el--full-text">
<label>{tr('Optional LiveSync Auth Key')}</label>
<input
type="text"
value={$livesync_auth_key->getValue()}
name="fb--conf--livesync_auth_key"
/>
</div>
</div>
</div>
</section>
<section class="admin-box">
<header class="admin-box-header">
<h3>{tr('Language')}</h3>
Expand Down
151 changes: 86 additions & 65 deletions src/data/livesync.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,104 @@ class LiveSyncDataController extends DataController {

public async function genGenerateData(): Awaitable<void> {
$data = array();
await tr_start();
$input_auth_key = idx(Utils::getGET(), 'auth', '');
$livesync_enabled = await Configuration::gen('livesync');
$livesync_auth_key = await Configuration::gen('livesync_auth_key');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combine these two awaits into one await with https://docs.hhvm.com/hack/reference/function/HH.Asio.va/.

Also, can you create a function in utils called genva which calls \HH\Asio\va? This is what we use internally at FB and is easier to type.

if ($livesync_enabled->getValue() === '1' &&
strval($input_auth_key) === strval($livesync_auth_key->getValue())) {

$teams_array = array();
$all_teams = await Team::genAllTeams();
foreach ($all_teams as $team) {
$team_livesync_exists =
await Team::genLiveSyncExists($team->getId(), "fbctf");
if ($team_livesync_exists === true) {
$team_livesync_key =
await Team::genGetLiveSyncKey($team->getId(), "fbctf");
$teams_array[$team->getId()] = strval($team_livesync_key);
}
}
$data = array();

$scores_array = array();
$scored_teams = array();
$all_scores = await ScoreLog::genAllScores();
foreach ($all_scores as $score) {
if (in_array($score->getTeamId(), array_keys($teams_array)) === false) {
continue;
$teams_array = array();
$all_teams = await Team::genAllTeams();
foreach ($all_teams as $team) {
$team_livesync_exists =
await Team::genLiveSyncExists($team->getId(), "fbctf");
if ($team_livesync_exists === true) {
$team_livesync_key =
await Team::genGetLiveSyncKey($team->getId(), "fbctf");
$teams_array[$team->getId()] = strval($team_livesync_key);
}
}
$scores_array[$score->getLevelId()][$teams_array[$score->getTeamId()]]['timestamp'] =
$score->getTs();
$scores_array[$score->getLevelId()][$teams_array[$score->getTeamId()]]['capture'] =
true;
$scores_array[$score->getLevelId()][$teams_array[$score->getTeamId()]]['hint'] =
false;
$scored_teams[$score->getLevelId()][] = $score->getTeamId();
}
$all_hints = await HintLog::genAllHints();
foreach ($all_hints as $hint) {
if ($hint->getPenalty()) {
if (in_array($hint->getTeamId(), array_keys($teams_array)) ===

$scores_array = array();
$scored_teams = array();
$all_scores = await ScoreLog::genAllScores();
foreach ($all_scores as $score) {
if (in_array($score->getTeamId(), array_keys($teams_array)) ===
false) {
continue;
}
$scores_array[$hint->getLevelId()][$teams_array[$hint->getTeamId()]]['hint'] =
$scores_array[$score->getLevelId()][$teams_array[$score->getTeamId()]]['timestamp'] =
$score->getTs();
$scores_array[$score->getLevelId()][$teams_array[$score->getTeamId()]]['capture'] =
true;
if (in_array(
$hint->getTeamId(),
$scored_teams[$hint->getLevelId()],
) ===
false) {
$scores_array[$hint->getLevelId()][$teams_array[$hint->getTeamId()]]['capture'] =
false;
$scores_array[$hint->getLevelId()][$teams_array[$hint->getTeamId()]]['timestamp'] =
$hint->getTs();
$scores_array[$score->getLevelId()][$teams_array[$score->getTeamId()]]['hint'] =
false;
$scored_teams[$score->getLevelId()][] = $score->getTeamId();
}
$all_hints = await HintLog::genAllHints();
foreach ($all_hints as $hint) {
if ($hint->getPenalty()) {
if (in_array($hint->getTeamId(), array_keys($teams_array)) ===
false) {
continue;
}
$scores_array[$hint->getLevelId()][$teams_array[$hint->getTeamId()]]['hint'] =
true;
if (in_array(
$hint->getTeamId(),
$scored_teams[$hint->getLevelId()],
) ===
false) {
$scores_array[$hint->getLevelId()][$teams_array[$hint->getTeamId()]]['capture'] =
false;
$scores_array[$hint->getLevelId()][$teams_array[$hint->getTeamId()]]['timestamp'] =
$hint->getTs();
}
}
}
}

$levels_array = array();
$all_levels = await Level::genAllLevels();
foreach ($all_levels as $level) {
$entity = await Country::gen($level->getEntityId());
$category = await Category::genSingleCategory($level->getCategoryId());
if (array_key_exists($level->getId(), $scores_array)) {
$score_level_array = $scores_array[$level->getId()];
} else {
$score_level_array = array();
$levels_array = array();
$all_levels = await Level::genAllLevels();
foreach ($all_levels as $level) {
$entity = await Country::gen($level->getEntityId());
$category =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combine these two awaits.

await Category::genSingleCategory($level->getCategoryId());
if (array_key_exists($level->getId(), $scores_array)) {
$score_level_array = $scores_array[$level->getId()];
} else {
$score_level_array = array();
}
$one_level = array(
'active' => $level->getActive(),
'type' => $level->getType(),
'title' => $level->getTitle(),
'description' => $level->getDescription(),
'entity_iso_code' => $entity->getIsoCode(),
'category' => $category->getCategory(),
'points' => $level->getPoints(),
'bonus' => $level->getBonusFix(),
'bonus_dec' => $level->getBonusDec(),
'penalty' => $level->getPenalty(),
'teams' => $score_level_array,
);
$levels_array[] = $one_level;
}
$one_level = array(
'active' => $level->getActive(),
'type' => $level->getType(),
'title' => $level->getTitle(),
'description' => $level->getDescription(),
'entity_iso_code' => $entity->getIsoCode(),
'category' => $category->getCategory(),
'points' => $level->getPoints(),
'bonus' => $level->getBonusFix(),
'bonus_dec' => $level->getBonusDec(),
'penalty' => $level->getPenalty(),
'teams' => $score_level_array,
);
$levels_array[] = $one_level;
}

$data = $levels_array;
$data = $levels_array;
} else if ($livesync_enabled->getValue() === '0') {
$data['error'] =
tr('LiveSync is disabled, please contact the administrator for access.');
} else if (strval($input_auth_key) !==
strval($livesync_auth_key->getValue())) {
$data['error'] =
tr('LiveSync auth key is invalid, please contact the administrator for access.');
} else {
$data['error'] =
tr('LiveSync failed, please contact the administrator for assistance.');
}
$this->jsonSend($data);
}

Expand Down
4 changes: 4 additions & 0 deletions src/scripts/liveimport.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class LiveSyncImport {
$json = await self::genDownloadData($url, $check_certificates);
$data = json_decode($json);
if (empty($data) === false) {
if ((!is_array($data)) && (property_exists($data, 'error'))) {
self::debug(true, $url, '!!!', strval($data->error));
continue;
}
foreach ($data as $level) {
$mandatories_set = await self::genMandatoriesSet($level);
if ($mandatories_set === false) {
Expand Down