2016-09-25 44 views
2

私はチッピングゲームを作成しました。ユーザーは値を推測でき、12時間後に現在のUSD/EURレートがAPIリクエストによって取得され、 (1人の勝者は、n人のユーザーが同じ金額を払って、すべてが勝つ)。(laravel eloquent)最も近い値を取得する(チッピングゲーム)

データベースから「最も近い」ヒントを取得するにはどうすればよいですか?

  foreach ($rounds_closed as $round_closed) 
     { 
      //get latest rate 
      $amount = 458.12; 

      //set transaction as processed and set latest rate 
      $round_closed->update([ 
       'status' => 2, 
       'win_tip' => $amount, 

       ]); 

      //get lucky winners 
    //here I would need eloquent code to choose the "nearest" winners 
      $winners_transactions = Transaction::where('round_id', $round_closed->id)->where('assessment', $round_closed->win_tip)->get(); 

      //if we have at least one winner 
      if ($winners_transactions->count() > 0) 
      { 
       //calculate their win amount 
       $sum = $round_closed->transactions->sum('amount'); 
       $fee = $sum * 0.10; 

       $sum = $sum - $fee; 

       $sum = number_format($sum/$winners_transactions->count(), 8, '.', ''); 

       foreach ($winners_transactions as $winner_transaction) 
       { 
        //mark transaction as payed out 
        $winner_transaction->update([ 
         'payout' => 1, 
         ]); 

        //payout user 


       } 

      }     
     } 

答えて

0

私はあなたの枠組みの中で構文に慣れていないんだけど、ここにあなたがリファクタリング可能性がSQLで可能なアプローチである:

あなたのサイズに応じて
SET @amount = 458.12; 

#Order tips by smallest difference and return the first result 
SET @winning_tip = (SELECT tip FROM tips ORDER BY ABS([email protected]) ASC LIMIT 1); 

#Return all users with the winning tip value 
SELECT userid, tip, ABS([email protected]) AS delta FROM tips WHERE tip = @winning_tip 

http://sqlfiddle.com/#!9/fac65/2

データを使用すると、すべてのトランザクションを取り戻し、上記のロジックをSQL以外でコーディングすることもできます。

主なものは、金額に最も近いチップを特定し、それをパラメータとして使用して勝者を選択/フィルタする必要があります。

関連する問題