Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaning up some of the issues #1271

Merged
merged 12 commits into from
Oct 27, 2024
7 changes: 7 additions & 0 deletions ansible/runonce/vpngw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,13 @@
- { cmd: "{{APP_DIR}}/backend/yii cron/pf" }
- { cmd: "mkdir -p /var/log/cron" }

- name: "Create configuration files with contents"
copy:
dest: "{{item.dest}}"
content: "{{item.val}}\n"
with_items:
- { dest: "/etc/registry_clients.conf", val: "10.0.0.0/24"}

- name: "Create targets network interface hostname.{{targets_if}}"
copy:
content: "inet {{targets_if_ipv4}} {{targets_netmask}} NONE group targets\n"
Expand Down
8 changes: 6 additions & 2 deletions backend/components/columns/AcademicColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public function init()
if (!$this->attribute) {
throw new InvalidConfigException('No {attribute} provided.');
}
if (\Yii::$app->sys->academic_grouping !== false) {
if (intval(\Yii::$app->sys->academic_grouping)>0) {
$filter=[];
for ($i = 0; $i < intval(\Yii::$app->sys->academic_grouping); $i++) {
$filter[] = \Yii::$app->sys->{"academic_" . $i . "short"};
$filter[] = $i.': '.\Yii::$app->sys->{"academic_" . $i . "short"};
}
$this->filter=$filter;
$this->visible=true;
Expand All @@ -32,4 +32,8 @@ public function init()
}
$this->filterAttribute = $this->attribute;
}

public function getFilter(){
return $this->filter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

use yii\db\Migration;

/**
* Class m241027_110007_update_calculate_ranks_procedure_to_make_dynamic
*/
class m241027_110007_update_calculate_ranks_procedure_to_make_dynamic extends Migration
{
public $DROP_SQL="DROP PROCEDURE IF EXISTS {{%calculate_ranks}}";
public $CREATE_SQL="CREATE PROCEDURE {{%calculate_ranks}} ()
BEGIN
DECLARE v_max INT unsigned DEFAULT 0;
DECLARE v_counter INT unsigned DEFAULT 0;
SET v_max=(SELECT IFNULL(memc_get('sysconfig:academic_grouping'),0));

DROP TABLE IF EXISTS pr_ranking;

IF v_max = 0 THEN
CREATE TEMPORARY TABLE `pr_ranking` (id int primary key AUTO_INCREMENT,player_id int) ENGINE=MEMORY CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
START TRANSACTION;
delete from player_rank;
insert into pr_ranking select NULL,t.player_id from player_score as t left join player as t2 on t.player_id=t2.id where t2.active=1 and t2.status=10 order by points desc,t.ts asc, t.player_id asc;
insert IGNORE into player_rank select * from pr_ranking;
COMMIT;
DROP TABLE `pr_ranking`;
ELSE
REPEAT
CREATE TEMPORARY TABLE `pr_ranking` (id int primary key AUTO_INCREMENT,player_id int) ENGINE=MEMORY CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
START TRANSACTION;
delete from player_rank where player_id in (select id from player where academic=v_counter) OR player_id NOT IN (select id from player);
insert into pr_ranking select NULL,t.player_id from player_score as t left join player as t2 on t.player_id=t2.id where t2.active=1 and t2.status=10 and t2.academic=v_counter order by points desc,t.ts asc, t.player_id asc;
insert IGNORE into player_rank select * from pr_ranking;
COMMIT;
DROP TABLE `pr_ranking`;
SET v_counter=v_counter+1;
UNTIL v_counter >= v_max
END REPEAT;
END IF;
END";
// Use up()/down() to run migration code without a transaction.
public function up()
{
$this->db->createCommand($this->DROP_SQL)->execute();
$this->db->createCommand($this->CREATE_SQL)->execute();
}

public function down()
{
$this->db->createCommand($this->DROP_SQL)->execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use yii\db\Migration;

/**
* Class m241027_110150_update_calculate_team_ranks_procedure_to_make_dynamic
*/
class m241027_110150_update_calculate_team_ranks_procedure_to_make_dynamic extends Migration
{
public $DROP_SQL="DROP PROCEDURE IF EXISTS {{%calculate_team_ranks}}";
public $CREATE_SQL="CREATE PROCEDURE {{%calculate_team_ranks}} ()
BEGIN
DECLARE v_max INT unsigned DEFAULT 0;
DECLARE v_counter INT unsigned DEFAULT 0;

SET v_max=(SELECT IFNULL(memc_get('sysconfig:academic_grouping'),0));

DROP TABLE IF EXISTS `tr_ranking`;

IF v_max = 0 THEN
CREATE TEMPORARY TABLE `tr_ranking` (id int primary key AUTO_INCREMENT,team_id int) ENGINE=MEMORY CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
START TRANSACTION;
delete from team_rank;
insert into tr_ranking select NULL,t.team_id from team_score as t left join team as t2 on t.team_id=t2.id ORDER BY points desc,t.ts asc, t.team_id asc;
insert IGNORE into team_rank select * from tr_ranking;
COMMIT;
DROP TABLE `tr_ranking`;
ELSE
IF (SELECT count(*) FROM sysconfig WHERE id='teams')>0 AND (SELECT val FROM sysconfig WHERE id='teams')=1 THEN
REPEAT
CREATE TEMPORARY TABLE `tr_ranking` (id int primary key AUTO_INCREMENT,team_id int) ENGINE=MEMORY CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
START TRANSACTION;
-- DELETE team of a given academic category or no longer exist in the table
delete from team_rank where team_id in (select id from team where academic=v_counter) OR team_id NOT IN (select id from team);
insert into tr_ranking select NULL,t.team_id from team_score as t left join team as t2 on t.team_id=t2.id WHERE t2.academic=v_counter order by points desc,t.ts asc, t.team_id asc;
insert IGNORE into team_rank select * from tr_ranking;
COMMIT;
DROP TABLE `tr_ranking`;
SET v_counter=v_counter+1;
UNTIL v_counter >= v_max
END REPEAT;
END IF;
END IF;
END";

public function up()
{
$this->db->createCommand($this->DROP_SQL)->execute();
$this->db->createCommand($this->CREATE_SQL)->execute();
}

public function down()
{
$this->db->createCommand($this->DROP_SQL)->execute();
}
}
14 changes: 11 additions & 3 deletions backend/modules/activity/controllers/NotificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,23 @@ public function actionCreate()
{

$model = new Notification();
if ($model->load(Yii::$app->request->post())) {
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->player_id == null) {
$err = false;
$connection=\Yii::$app->db;
$transaction = $connection->beginTransaction();

$query=Player::find()->where(['active' => 1, 'status' => 10]);
if($model->online)
{
$query->andHaving(['!=','online',0]);
}
if($model->ovpn)
{
$query->andHaving(['!=','ovpn',0]);
}
// Send notification to all players
try {
foreach (Player::find()->where(['active' => 1, 'status' => 10])->all() as $player) {
foreach ($query->all() as $player) {
$notif = new Notification;
$notif->load(Yii::$app->request->post());
$notif->player_id = $player->id;
Expand Down
6 changes: 5 additions & 1 deletion backend/modules/activity/models/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
class Notification extends \yii\db\ActiveRecord
{
public $online;
public $ovpn;

/**
* {@inheritdoc}
*/
Expand All @@ -46,9 +49,10 @@ public function behaviors()
public function rules()
{
return [
// [['player_id'], 'required'],
[['player_id'], 'integer'],
[['archived'], 'boolean'],
[['online','ovpn'], 'boolean'],
[['online','ovpn'],'filter', 'filter' => 'boolval'],
[['body'], 'string'],
[['created_at', 'updated_at'], 'safe'],
[['title'], 'string', 'max' => 255],
Expand Down
16 changes: 12 additions & 4 deletions backend/modules/activity/views/notification/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,26 @@

<?php $form = ActiveForm::begin(); ?>
<div class="row">
<div class="col-md-5">
<div class="col-md-6">
<?= $form->field($model, 'player_id',['inputOptions' => ['autofocus' => 'autofocus',]])->widget(AutocompleteAjax::class, [
'multiple' => false,
'url' => ['/frontend/player/ajax-search'],
'options' => ['placeholder' => 'Find player by email, username, id or profile.']
])->hint('The player that we will send the notification.'); ?>
</div>
<div class="col-md-5">
<div class="col-md-6">
<?= $form->field($model, 'category')->dropDownList($model->supportedCategories())->hint('Choose the notification type. <code>swal:</code> prefixed notifications invoke a modal popup.') ?>
</div>
<div class="col-md-2">
<?= $form->field($model, 'archived')->checkBox(['label'=>false])->label('Archived')->hint('Should the notification be archived? (default: no)') ?>
</div>
<div class="row">
<div class="col-md-4">
<?= $form->field($model, 'archived')->checkBox(['label'=>false])->label('Archived')->hint('Should the notification be archived? (default: no)') ?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'online')->checkBox(['label'=>false])->label('Online')->hint('Notify only online players') ?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'ovpn')->checkBox(['label'=>false])->label('On VPN')->hint('Notify only players on the VPN') ?>
</div>
</div>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
Expand Down
4 changes: 2 additions & 2 deletions backend/modules/frontend/views/player/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
<?= $form->field($model, 'fullname')->textInput(['maxlength' => true])->hint('The fullname of the player') ?>

<?php if (\Yii::$app->sys->academic_grouping !== false): ?>
<?= $form->field($model, 'academic')->dropDownList($filter)->hint('Whether the player is gov, edu or pro') ?>
<?= $form->field($model, 'academic')->dropDownList((new \app\components\columns\AcademicColumn(['attribute'=>'academic']))->filter)->hint('Academic grouping number for the player (if any)') ?>
<?php else:?>
<?= $form->field($model, 'academic')->textInput()->hint('Whether the player is gov, edu or pro') ?>
<?= $form->field($model, 'academic')->textInput()->hint('Academic grouping number for the player (if any)') ?>
<?php endif;?>
<?= $form->field($model, 'email')->textInput(['maxlength' => true])->hint('The email address of the player') ?>

Expand Down
10 changes: 1 addition & 9 deletions backend/modules/frontend/views/team/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@
/* @var $model app\modules\frontend\models\Team */
/* @var $form yii\widgets\ActiveForm */

if (Yii::$app->sys->academic_grouping !== false) {
for ($i = 0; $i < intval(Yii::$app->sys->academic_grouping); $i++) {
$filter[] = Yii::$app->sys->{"academic_" . $i . "short"};
}
} else {
$filter = [];
}

?>

<div class="team-form">
Expand All @@ -35,7 +27,7 @@
</div>
<div class="row">
<?php if (\Yii::$app->sys->academic_grouping !== false): ?>
<div class="col-md-6"><?= $form->field($model, 'academic')->dropDownList($filter)->hint('Whether the player is gov, edu or org') ?></div>
<div class="col-md-6"><?= $form->field($model, 'academic')->dropDownList((new \app\components\columns\AcademicColumn(['attribute'=>'academic']))->filter)->hint('Whether the player is gov, edu or org') ?></div>
<?php else: ?>
<div class="col-md-6"><?= $form->field($model, 'academic')->textInput()->hint('Whether the player is gov, edu or org') ?></div>
<?php endif; ?>
Expand Down
8 changes: 4 additions & 4 deletions docs/Sysconfig-Keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ Or instead if not DSN then use the following
* `stripe_automatic_tax_enabled` Enable Stripe automatic TAX

## Player Specific
* `academic_N` Name for academic value `N` (starting at 0)
* `academic_Nshort` Short name for academic value `N`
* `academic_grouping` (1)Enable/(0)Disable support for academic grouping of activity stream
* `academic_Nlong` Name for academic value `N` (starting at 0)
* `academic_Nshort` Short name for academic value `N` (starting at 0)
* `academic_grouping` The number for the supported groups or (0) to Disable support for academic grouping of player activity
example:
```sh
backend/yii sysconfig/set academic_grouping 1
backend/yii sysconfig/set academic_grouping 2
backend/yii sysconfig/set academic_0 "SuperSite.com"
backend/yii sysconfig/set academic_1 "AnotherSite.com"
backend/yii sysconfig/set academic_0short "supersite"
Expand Down
2 changes: 1 addition & 1 deletion frontend/controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function actionIndex()
$dashboardStats=new \stdClass();
$active_targets=intval(\app\modules\target\models\Target::find()->active()->count());
$active_challenges=intval(\app\modules\challenge\models\Challenge::find()->alias('t')->active()->count());
$dashboardStats->countries=(int) Profile::find()->select(['country'])->distinct()->count();
$dashboardStats->countries=(int) Profile::find()->joinWith(['owner'])->select(['country'])->distinct()->where(['player.status'=>10])->count();
if(Yii::$app->sys->academic_grouping!==false)
$dashboardStats->claims=(int) PlayerTreasure::find()->where(['player.academic'=>Yii::$app->user->identity->academic])->joinWith(['player'])->count();
else
Expand Down
15 changes: 15 additions & 0 deletions frontend/models/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,21 @@ public function getAcademicIcon()
}
}


/**
* Send a notification to current user
*/
public function notify($type="info",$title,$body)
{
$n=new \app\models\Notification;
$n->player_id=$this->id;
$n->archived=0;
$n->category=$type;
$n->title=$title;
$n->body=$body;
return $n->save();
}

/**
* Send mail to player with
* @param string $subject
Expand Down
2 changes: 1 addition & 1 deletion frontend/modules/help/controllers/CreditsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function behaviors()
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => Credits::find()->orderBy('id ASC'),
'query' => Credits::find()->orderBy('weight ASC, id ASC'),
'pagination' =>false,
]);
return $this->render('index',['dataProvider'=>$dataProvider]);
Expand Down
6 changes: 6 additions & 0 deletions frontend/modules/target/actions/SpinRestAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ public function run($id)
$playerSpin->counter=intval($playerSpin->counter) + 1;
$playerSpin->total=intval($playerSpin->total) + 1;
if($SQ->save() !== false && $playerSpin->save() !== false)
{
Yii::$app->session->setFlash('success', sprintf($msg, $target->name));
if(\Yii::$app->sys->spins_per_day!==false && intval($playerSpin->counter)>=intval(\Yii::$app->sys->spins_per_day))
{
Yii::$app->user->identity->notify("swal:info",\Yii::t('app',"Max spins reached"),\Yii::t('app','You have reached the maximum number of spins for the day. Feel free to reach out to our support to reset your counter!'));
}
}
else
throw new NotFoundHttpException(\Yii::t('app','Failed to queue target for restart.'));

Expand Down
Loading