public function generateGameResult($uuid) {
    /** @var \Drupal\openy_campaign\Entity\MemberGame $game */
    $game = $this->entityRepository->loadEntityByUuid('openy_campaign_member_game', $uuid);

    $result = $game->result->value;
    /** @var \Drupal\Node\Entity\Node $campaign */
    $campaign = $game->member->entity->campaign->entity;

    if (!empty($result)) {
      return [
        'already_used_chance' => TRUE,
      ];
    }

    $expected = $campaign->field_campaign_expected_visits->value;
    $coefficient = $campaign->field_campaign_prize_coefficient->value;
    $ranges = [];

    $previousRange = 0;
    foreach ($campaign->field_campaign_prizes as $target) {
      /** @var \Drupal\paragraphs\Entity\Paragraph $prize */
      $prize = $target->entity;
      $amount = $prize->field_prgf_prize_amount->value;
      if ($amount == 0) {
        continue;
      }
      $nextRange = $previousRange + ceil($prize->field_prgf_prize_amount->value * $coefficient);

      $ranges[] = [
        'min' => $previousRange,
        'max' => $nextRange,
        'description' => $prize->field_prgf_prize_description->value,
        'prize' => $prize,
      ];

      $previousRange = $nextRange;
    }

    $isWinner = FALSE;
    $randomNumber = mt_rand(0, $expected);
    $result = $campaign->field_campaign_prize_nowin->value;
    if (empty($result)) {
      $result = $this->t('Did not win.');
    }
    foreach ($ranges as $range) {
      if ($randomNumber >= $range['min'] && $randomNumber < $range['max']) {
        $result = $range['description'];
        $isWinner = TRUE;

        // Decrease an amount of the prizes.
        $prize = $range['prize'];
        $amount = $prize->field_prgf_prize_amount->value;
        $prize->set('field_prgf_prize_amount', $amount - 1);
        $prize->save();

        break;
      }
    }

    $logMessage = json_encode([
      'number' => $randomNumber,
      'expected' => $expected,
      'coeff' => $coefficient,
      'ranges' => array_map(function ($item) {
        //return $item['min'] . '-' . $item['max'] . ':' . substr($item['description'], 0, 10);
        return $item['min'] . '-' . $item['max'];
      }, $ranges),
    ]);

    $game->result->value = $result;
    $game->date = $this->time->getRequestTime();
    $game->log->value = $logMessage;
    $game->save();

    return [
      'is_winner' => $isWinner,
      'result' => $result,
    ];

  }
public function generateGameResult($uuid) {
    /** @var \Drupal\openy_campaign\Entity\MemberGame $game */
    $game = $this->entityRepository->loadEntityByUuid('openy_campaign_member_game', $uuid);

    $result = $game->result->value;
    /** @var \Drupal\Node\Entity\Node $campaign */
    $campaign = $game->member->entity->campaign->entity;

    if (!empty($result)) {
      return [
        'already_used_chance' => TRUE,
      ];
    }

    $expected = $campaign->field_campaign_expected_visits->value;
    $coefficient = $campaign->field_campaign_prize_coefficient->value;
    $ranges = [];

    $previousRange = 0;
    foreach ($campaign->field_campaign_prizes as $target) {
      /** @var \Drupal\paragraphs\Entity\Paragraph $prize */
      $prize = $target->entity;
      $amount = $prize->field_prgf_prize_amount->value;
      if ($amount == 0) {
        continue;
      }
      $nextRange = $previousRange + ceil($prize->field_prgf_prize_amount->value * $coefficient);

      $ranges[] = [
        'min' => $previousRange,
        'max' => $nextRange,
        'description' => $prize->field_prgf_prize_description->value,
        'prize' => $prize,
      ];

      $previousRange = $nextRange;
    }

    $isWinner = FALSE;
    $randomNumber = mt_rand(0, $expected);
    $result = $campaign->field_campaign_prize_nowin->value;
    if (empty($result)) {
      $result = $this->t('Did not win.');
    }
    foreach ($ranges as $range) {
      if ($randomNumber >= $range['min'] && $randomNumber < $range['max']) {
        $result = $range['description'];
        $isWinner = TRUE;

        // Decrease an amount of the prizes.
        $prize = $range['prize'];
        $amount = $prize->field_prgf_prize_amount->value;
        $prize->set('field_prgf_prize_amount', $amount - 1);
        $prize->save();

        break;
      }
    }

    $logMessage = json_encode([
      'number' => $randomNumber,
      'expected' => $expected,
      'coeff' => $coefficient,
      'ranges' => array_map(function ($item) {
        //return $item['min'] . '-' . $item['max'] . ':' . substr($item['description'], 0, 10);
        return $item['min'] . '-' . $item['max'];
      }, $ranges),
    ]);

    $game->result->value = $result;
    $game->date = $this->time->getRequestTime();
    $game->log->value = $logMessage;
    $game->save();

    return [
      'is_winner' => $isWinner,
      'result' => $result,
    ];

  }
public function generateGameResult ( $ uuid ) { /** @var \Drupal\openy_campaign\Entity\MemberGame $game */ $ game = $ this -> entityRepository -> loadEntityByUuid ( 'openy_campaign_member_game' , $ uuid ) ; $ result = $ game -> result -> value ; /** @var \Drupal\Node\Entity\Node $campaign */ $ campaign = $ game -> member -> entity -> campaign -> entity ; if ( ! empty ( $ result ) ) { return [ 'already_used_chance' => TRUE , ] ; } $ expected = $ campaign -> field_campaign_expected_visits -> value ; $ coefficient = $ campaign -> field_campaign_prize_coefficient -> value ; $ ranges = [ ] ; $ previousRange = 0 ; foreach ( $ campaign -> field_campaign_prizes as $ target ) { /** @var \Drupal\paragraphs\Entity\Paragraph $prize */ $ prize = $ target -> entity ; $ amount = $ prize -> field_prgf_prize_amount -> value ; if ( $ amount == 0 ) { continue ; } $ nextRange = $ previousRange + ceil ( $ prize -> field_prgf_prize_amount -> value * $ coefficient ) ; $ ranges [ ] = [ 'min' => $ previousRange , 'max' => $ nextRange , 'description' => $ prize -> field_prgf_prize_description -> value , 'prize' => $ prize , ] ; $ previousRange = $ nextRange ; } $ isWinner = FALSE ; $ randomNumber = mt_rand ( 0 , $ expected ) ; $ result = $ campaign -> field_campaign_prize_nowin -> value ; if ( empty ( $ result ) ) { $ result = $ this -> t ( 'Did not win.' ) ; } foreach ( $ ranges as $ range ) { if ( $ randomNumber >= $ range [ 'min' ] && $ randomNumber < $ range [ 'max' ] ) { $ result = $ range [ 'description' ] ; $ isWinner = TRUE ; // Decrease an amount of the prizes. $ prize = $ range [ 'prize' ] ; $ amount = $ prize -> field_prgf_prize_amount -> value ; $ prize -> set ( 'field_prgf_prize_amount' , $ amount - 1 ) ; $ prize -> save ( ) ; break ; } } $ logMessage = json_encode ( [ 'number' => $ randomNumber , 'expected' => $ expected , 'coeff' => $ coefficient , 'ranges' => array_map ( function ( $ item ) { //return $item['min'] . '-' . $item['max'] . ':' . substr($item['description'], 0, 10); return $ item [ 'min' ] . '-' . $ item [ 'max' ] ; } , $ ranges ) , ] ) ; $ game -> result -> value = $ result ; $ game -> date = $ this -> time -> getRequestTime ( ) ; $ game -> log -> value = $ logMessage ; $ game -> save ( ) ; return [ 'is_winner' => $ isWinner , 'result' => $ result , ] ; }
Draw instant-win game result.

@param $uuid

@return array
@throws \Drupal\Core\Entity\EntityStorageException
Draw instant - win game result .