Skip to content

Commit

Permalink
group sync fix progress
Browse files Browse the repository at this point in the history
minecraft <-> website working as expected finally, will test discord later
  • Loading branch information
tadhgboyle committed Feb 22, 2021
1 parent e738035 commit 5880e06
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 19 deletions.
8 changes: 8 additions & 0 deletions core/classes/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,12 @@ public static function loadEndpoints($path, $endpoints) {
}
}
}

public static function getIngameRankName($website_group_id) {
$data = DB::getInstance()->get('group_sync', array('website_group_id', '=', $website_group_id));
if ($data->count()) {
return $data->first()->ingame_rank_name;
}
return null;
}
}
2 changes: 1 addition & 1 deletion custom/panel_templates/Default/core/api_endpoints.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<div class="card-body">
<div class="row">
<div class="col-md-9">
<h5 style="margin-top: 7px; margin-bottom: 7px;">{$ENDPOINTS_INFO}</h5>
<p style="margin-top: 7px; margin-bottom: 7px;">{$ENDPOINTS_INFO}</p>
</div>
<div class="col-md-3">
<span class="float-md-right"><a class="btn btn-warning" href="{$BACK_LINK}">{$BACK}</a></span>
Expand Down
2 changes: 2 additions & 0 deletions custom/panel_templates/Default/core/groups.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<div class="card shadow mb-4">
<div class="card-body">
<a class="btn btn-primary" style="margin-bottom: 10px" href="{$NEW_GROUP_LINK}">{$NEW_GROUP}</a>
<a class="btn btn-primary" style="margin-bottom: 10px" href="{$GROUP_SYNC_LINK}"><i class="fas fa-external-link-alt"></i> {$GROUP_SYNC}</a>

<!-- Success and Error Alerts -->
{include file='includes/alerts.tpl'}
Expand All @@ -48,6 +49,7 @@
<th>{$NAME}</th>
<th>{$USERS}</th>
<th>{$STAFF}</th>
<th></th>
</tr>
</thead>
<tbody id="sortable">
Expand Down
59 changes: 41 additions & 18 deletions modules/Core/includes/endpoints/ServerInfoEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,31 +110,54 @@ public function execute(Nameless2API $api) {
}

$group_sync_updates[strtolower($item->ingame_rank_name)] = array(
'website' => $item->website_group_id
'website' => $item->website_group_id,
'discord' => $item->discord_role_id
);
}

if (count($_POST['players'])) {
foreach ($_POST['players'] as $uuid => $player) {
$user = new User($uuid, 'uuid');
if ($user->data()) {
// Any synced groups to remove?
foreach ($user->getGroups() as $group) {
$group_name = strtolower($group->name);
if (array_key_exists($group_name, $group_sync_updates) && in_array($group_name, $player['groups'])) {
$user->removeGroup($group->id);
}
foreach ($_POST['players'] as $uuid => $player) {
$user = new User($uuid, 'uuid');
if ($user->data()) {

// Never edit root user
if ($user->data()->id == 1) {
continue;
}

// Any synced groups to remove?
foreach ($user->getGroups() as $group) {
// Convert user group ID to minecraft group name. exit if this isnt set
$ingame_rank_name = Util::getIngameRankName($group->id);
if ($ingame_rank_name == null) {
continue;
}

// Any synced groups to add?
foreach ($player['groups'] as $group) {
$group_name = strtolower($group);
if (array_key_exists($group_name, $group_sync_updates)) {
$group_info = $group_sync_updates[$group_name];
// Check that this website group is setup to sync
if (!array_key_exists($ingame_rank_name, $group_sync_updates)) {
continue;
}

$user->addGroup($group_info['website']);
}
// If they currently have this rank ingame, dont remove it
if (in_array($ingame_rank_name, $player['groups'])) {
continue;
}

$user->removeGroup($group->id);
Discord::removeDiscordRole($user, $group->id, $api->getLanguage(), false);
}

// Any synced groups to add?
foreach ($player['groups'] as $group) {
$ingame_rank_name = strtolower($group);
// Check that this ingame group is setup to sync
if (!array_key_exists($ingame_rank_name, $group_sync_updates)) {
continue;
}

$group_info = $group_sync_updates[$ingame_rank_name];

$user->addGroup($group_info['website']);
Discord::addDiscordRole($user, $group_info['discord'], $api->getLanguage(), false);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions modules/Core/pages/panel/groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@
'INFO' => $language->get('general', 'info'),
'WARNING' => $language->get('general', 'warning'),
'FORCE_TFA_WARNING' => $language->get('admin', 'force_tfa_warning'),
'GROUP_SYNC' => $language->get('admin', 'group_sync'),
'GROUP_SYNC_LINK' => URL::build('/panel/core/api/', 'view=group_sync')
));

$page_load = microtime(true) - $start;
Expand Down

0 comments on commit 5880e06

Please sign in to comment.