Sorting arrays with uncertain data Sorting arrays with uncertain data laravel laravel

Sorting arrays with uncertain data


Here are some optimizations to your code, there are comments inline to describe what was done. This is obviously not finished as there are some things Marcin pointed out in his answer that would be better.

 // Determine number of markings    $num = mt_rand(1,10);    // Removed redundent $num > X as the conditions were already meet that it was > X by the previous if statement    if ($num == 1) {        $markingNum = 0;    } else if ($num < 4) {        $markingNum = 1;    } else if ($num < 6) {        $markingNum = 2;    } else if ($num < 8) {        $markingNum = 3;    } else if ($num < 10) {        $markingNum = 4;    } else {        $markingNum = 5;    }    // Calculate Marking type and color    $markings = array();    if ($markingNum > 0) {        for ($m = 1 ; $m <= $markingNum; $m++) { // incrimented to 1 and <= so we can dynamically select elements            // Set color values (pulls from the "pallet" selected earlier in the code, which will determine the range of color that marking can be)            $pal = $pallet->{'marking' . $m}; // Removed if/else and replaced with a dynamic variable            // Uncommon marking (10% chance)            $r = mt_rand(1, 10);            // removed duplicate database selections for a simple $rarity variable that accomplishes the same task            if ($r == 10) {                $rarity = 1;            } else {                $rarity = 0;            }            $marking = DataMarking::where('rarity', $rarity)                                  ->where('public', 1)                                  ->whereNotIn('name', array_keys($markings))                                  ->whereNotIn('region', $regions)                                  ->orderByRaw("RAND()")                                  ->first();            // Colors marking            if ($pal == 0) {                $markingColor = rand_color();            } else {                $range = ColorRange::where('id', $pal)->firstOrFail();                $markingColor = "#" . mixRange(substr($range->start_hex, 1), substr($range->end_hex, 1));            }            $markings[$marking->name] = $marking; // adds all of the marking data, this is where you could have a z-index in the database            $markings[$marking->name] = $markingColor; // add your color to your marking data            $regions[] = $marking->region;        }    }


I won't answer your question, but looking at your code there are a lot of space for improvements.

Consider this:

if ($m == 1) {        $pal = $pallet->marking1;} elseif ($m == 2) {    $pal = $pallet->marking2;} elseif ($m == 3) {    $pal = $pallet->marking3;} elseif ($m == 4) {    $pal = $pallet->marking4;} else {    $pal = $pallet->marking5;}

It could be changed for something much simpler:

$pa1 = (in_array($m,range(1,4))) ? $pallet->marking{$m} : $pallet->marking5;

Same for:

if ($r == 10) {    $marking = DataMarking::where('rarity', 1)                          ->where('public', 1)                          ->whereNotIn('name', array_keys($markings))                          ->whereNotIn('region', array_keys($regions))                          ->orderByRaw("RAND()")                          ->first();// Common markings} else {    $marking = DataMarking::where('rarity', 0)                          ->where('public', 1)                          ->whereNotIn('name', array_keys($markings))                          ->whereNotIn('region', array_keys($regions))                          ->orderByRaw("RAND()")                          ->first();}

if could be rewritten to:

$marking = DataMarking::where('rarity', ($r == 10) ? 1 : 0)                      ->where('public', 1)                      ->whereNotIn('name', array_keys($markings))                      ->whereNotIn('region', array_keys($regions))                      ->orderByRaw("RAND()")                      ->first();

Of course in above ORDER BY RAND() might be not the best solution because of performance reasons.

You should really care about amount of your code and duplication or you'll soon be lost in what you are doing