Jump to content

Create a Pilot Earned award question.


XPBaz
 Share

Recommended Posts

G'day

I'm looking to create an award that is automatically applied to set amounts of pilot money earned for example when looking at my pilot dashboard "Your Balance" and I reach $2000 the system gives an award in the same way you can have awards provided for a number of flights.

Editing a copy of the PilotHoursAward.php, it seems to me I need the code that looks at "Your Balance" value.

In the PilotHoursAward.php I think I'm looking to change the code in bold in the snippet of code below however I don't know what to change it to... (if I'm on the right track.)

If anyone can provide direction it would be greatly appreciated.

Baz

 

   public function check($flight_minutes = null): bool
    {
        if (!is_numeric($flight_minutes)) {
            Log::error('PilotHourAwards: Flight time "'.$flight_minutes.'" is not a valid flight time');
            return false;
        }

        return $this->user->flight_time >= (int) $flight_minutes;
    }

Link to comment
Share on other sites

8 hours ago, XPBaz said:

G'day

I'm looking to create an award that is automatically applied to set amounts of pilot money earned for example when looking at my pilot dashboard "Your Balance" and I reach $2000 the system gives an award in the same way you can have awards provided for a number of flights.

Editing a copy of the PilotHoursAward.php, it seems to me I need the code that looks at "Your Balance" value.

In the PilotHoursAward.php I think I'm looking to change the code in bold in the snippet of code below however I don't know what to change it to... (if I'm on the right track.)

If anyone can provide direction it would be greatly appreciated.

Baz

 

   public function check($flight_minutes = null): bool
    {
        if (!is_numeric($flight_minutes)) {
            Log::error('PilotHourAwards: Flight time "'.$flight_minutes.'" is not a valid flight time');
            return false;
        }

        return $this->user->flight_time >= (int) $flight_minutes;
    }

 

Yeah, you are on the right track but missing some more :) Technically you need to change more things. The $flight_minutes you mentioned is the value you define at admin > awards page, you also need to check the user's journal for current balance against it. Also changing the class name is needed (to avoid duplicate names and errors), so in simple terms what you need will be something like the below example.

 

// Creating a new Award class
class PilotBalanceAwards extends Award
{
    // Award definitions 
    public $name = 'Pilot Balance';
    public $param_description = 'Amount of balance at which to give this award';

    // Award method
    public function check($balance = null): bool
    {
        // Checking our own value to be sure
        if (!is_numeric($balance)) {
            Log::error('PilotBalanceAwards: Provided value "'.$balance.'" is not valid');
            return false;
        }

        // Here we check the user's balance and return the result (true/false)
        return $this->user->journal->balance >= (int) $balance;
    }
}

 

Hope this helps, good luck

 

Edited by DisposableHero
Link to comment
Share on other sites

In the above example

 

$balance is the amount we wish to give the award, entered manually by us at admin > awards page (it was $flight_minutes in your example)

$this->user->journal->balance is the user's balance (it was $this->user->flight_time in your example)

 

When a user sends a pilot report and when it gets accepted (either manually or automatically) award checks are done too. So to test your new award class, you need to file / accept a pirep (no need to fly, just file a valid manual pirep and accept it)

 

Also, if something goes wrong you probably will not see an error page, but the pireps will not be accepted. You need to check your laravel logs to see where it fails (Most of the time it is awards, a missing one or a faulty one)

Link to comment
Share on other sites

16 hours ago, DisposableHero said:

In the above example

 

$balance is the amount we wish to give the award, entered manually by us at admin > awards page (it was $flight_minutes in your example)

$this->user->journal->balance is the user's balance (it was $this->user->flight_time in your example)

 

When a user sends a pilot report and when it gets accepted (either manually or automatically) award checks are done too. So to test your new award class, you need to file / accept a pirep (no need to fly, just file a valid manual pirep and accept it)

 

Also, if something goes wrong you probably will not see an error page, but the pireps will not be accepted. You need to check your laravel logs to see where it fails (Most of the time it is awards, a missing one or a faulty one)

Thank you for your help so far.

 

I made the suggested edits however I got an error. Then I started again editing the Flight time Award ending up with this below.

 

Obviously I've missed something because this didn't tigger an award after passing the monetary amount set in the Awards interface.

Can you see where I slipped up?

 

Thanks

Baz

 

 

 

 

 

 

<?php

namespace Modules\Awards\Awards;

use App\Contracts\Award;
use Illuminate\Support\Facades\Log;

/**
 * All award classes need to extend Award and implement the check() method
 * This award is based on the original PilotFlightAwards.php file but
 * checks the Pilots money balance.
 * This award means you can create an award for a pilot that completes any
 * amount of money.
 *
 * See: https://docs.phpvms.net/developers/awards
 */
class PilotMoneyAwards extends Award
{
    /**
     * Set the name of this award class to make it easier to see when
     * assigning to a specific award
     *
     * @var string
     */
    public $name = 'Pilot Earnings Award';

    /**
     * The description to show under the parameters field, so the admin knows
     * what the parameter actually controls. You can leave this blank if there
     * isn't a parameter.
     *
     * @var string
     */
    public $param_description = 'Money Earned by pilot as $$$$';

    /**
     * If the user has over N dollar, then we can give them this award.
     *
     * @param int|null $balance The parameters passed in from the UI
     *
     * @return bool
     */
    public function check($balance = null): bool
    {
        if (!is_numeric($balance)) {
            Log::error('PilotMoneyAwards: Flight time "'.$balance.'" is not a valid amount');
            return false;
        }

        return $this->user-> balance >= (int) $balance;
    }
}
 

 

Link to comment
Share on other sites

12 hours ago, DisposableHero said:

There is no such thing called `$this->user->balance` , check my example carefully ;) Looks like your problem is there @XPBaz

 

Also the code in my first message is almost complete (except the top definition part before the class line), what happens when you try that ?

 

 

G'day and thank you for your help. I greatly appreciate it. I used the code below and got a Server Error 500.

 

<?php

namespace Modules\Awards\Awards;

use App\Contracts\Award;

/**
 * Simple example of an awards class, where you can apply an award when a user
 * has 100 flights. All award classes need to extend Award and implement the check() method
 *
 * See: https://docs.phpvms.net/developers/awards
 */
// Creating a new Award class
class PilotBalanceAwards extends Award
{
    // Award definitions 
    public $name = 'Pilot Balance';
    public $param_description = 'Amount of balance at which to give this award';

    // Award method
    public function check($balance = null): bool
    {
        // Checking our own value to be sure
        if (!is_numeric($balance)) {
            Log::error('PilotBalanceAwards: Provided value "'.$balance.'" is not valid');
            return false;
        }

        // Here we check the user's balance and return the result (true/false)
        return $this->user->journal->balance >= (int) $balance;
    }
}

 

 

The Laravel logged the following error...

[2022-01-05 22:22:17] production.INFO: Bid for user: VAT0001 on flight VAT24/C.108/L.1  
[2022-01-05 22:22:17] production.INFO: NotificationEvents::onPirepFile: jW2pzepPYjxQ0q34 filed  
[2022-01-05 22:22:17] production.ERROR: Object of class App\Support\Money could not be converted to int {"userId":1,"exception":"[object] (ErrorException(code: 0): Object of class App\\Support\\Money could not be converted to int at /home/bnasro5/AussieAirToursVirtual.bnasroberts.net/modules/Awards/Awards/PilotMoneyAwards.php:51)
[stacktrace]
Edited...
#23 /home/bnasro5/AussieAirToursVirtual.bnasroberts.net/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#24 /home/bnasro5/AussieAirToursVirtual.bnasroberts.net/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))

Link to comment
Share on other sites

You may need to convert the "money object" ($this->user->journal->balance) to an integer, or the opposite like converting your definition to a "money object" ($balance).

 

// Casting as integer
return (int) $this->user->journal->balance >= (int) $balance;

// Getting the integer value with php core functions
return intval($this->user->journal->balance) >= (int) $balance;

// Coverting an integer to a money object
return $this->user->journal->balance >= Money::createFromAmount($balance);

 

PS: Last one needs the declaration of `use App\Support\Money;` beforehand.

 

Hope you figure it out ;)

Edited by DisposableHero
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...