我正在为一个浏览器游戏制作一个脚本,它将生成一个随机动物,供玩家在0-5标记的任何地方进行战斗.该动物上的标记是随机生成的,并被输入到自定义的imagick函数中,该函数将按照它们出现在数组中的顺序添加它们.
虽然标记是随机决定的,但它们应该如何出现在动物上有很多规则,例如“全身”区域中的标记显示在“腹部”区域的标记之上.为了更好地解释,到目前为止我将附上测试仪的图像:
因此要打破这个随机生成的动物的5个标记,眼影标记属于眼睛区域,属于尾巴,条纹属于全身,阿帕卢萨属于背部,霍加皮属于腿.现在的订单只是在脚本循环通过数据库和随机选择的标记时添加,所以okapi(腿上的条纹)位于顶部,因为它是数组中的最后一个,并且添加了最后一个.但是遵循顺序规则,数组中的最后一个应该是条纹(身体上的水平条纹),因为全身标记在顶部.
// Determine number of markings $num = mt_rand(1,10); if ($num == 1) { $markingNum = 0; } elseif ($num > 1 && $num < 4) { $markingNum = 1; } elseif ($num > 4 && $num < 6) { $markingNum = 2; } elseif ($num > 6 && $num < 8) { $markingNum = 3; } elseif ($num > 8 && $num < 10) { $markingNum = 4; } else { $markingNum = 5; } // Calculate Marking type and color $markings = array(); if ($markingNum > 0) { for ($m = 0 ; $m < $markingNum; $m++) { // Set color values (pulls from the "pallet" selected earlier in the code,which will determine the range of color that marking can be) 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; } // Pull prevIoUs marking info if (count($markings) != 0) { $prevIoUs = DataMarking::whereIn('name',array_keys($markings))->get(); // This pulls the regions of the current markings in the array so it won't select a region that already has a marking. foreach ($prevIoUs as $p) { $regions[$p->region] = $p->name; } // Uncommon marking (10% chance) $r = mt_rand(1,10); 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',array_keys($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] = $markingColor; } else { // Uncommon marking (10% chance) $r = mt_rand(1,10); if ($r == 10) { $marking = DataMarking::where('rarity',1) ->where('public',1) ->orderByRaw("RAND()")->first(); // Common marking } else { $marking = DataMarking::where('rarity',0) ->where('public',1) ->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)); } $markings[$marking->name] = $markingColor; } } }
我想我可以用很多复杂的if语句来完成这个,但它对我来说似乎不是一个优雅的解决方案.此外,还有一个例外:’渐变’,一个全身标记,即使它是一个全身标记,也会在所有内容下面.到目前为止,它是唯一具有此类异常的标记.
我尝试过使用PHP提供的各种排序功能,但我没有太多运气. uksort似乎是最有希望的,但是因为我们排序的值存在于数据库中而不是数组中我们正在排序(imagick函数必须被提供一个标记=>颜色数组格式),它证明很难使用.
Tl; dr:我需要根据数据库中存在的键(标记区域)中存在的不确定数据量对数组进行重新排序.实现这一目标最优雅的方法是什么?
以下是对代码的一些优化,内联的注释用于描述所执行的操作.这显然没有完成,因为Marcin在他的回答中指出了一些更好的东西.
// 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)); } $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; } }