Gentlemen!
I am hoping to find a more efficient (less hairy) way to do the following code.
The public interface will call calculateListItemValue and pass the following variables
- $source_list: stdClass() with a structure similar to List JSON below.
- $target_list: stdClass() with a structure similar to List JSON below.
- $item_index: integer index pointing to the index of the array (list)->items[$item_index]
- $type: string value containing the controlling type $source_list->attribute->type to be compared to
calculateListItemValue should check the value of attribute->value and calculate the final value of the operation based on range and operator
- If value >= 0 result should be positive
- If value < 0 result should be negative
- If range is 0 apply Operator() to self
- If range is 4 apply Operator() to all items in source_list (or target_list if value < 0)
- Else apply to items with attribut->attrNo == range
Code follows.
/* List JSON example
* {
* "attributes": {
* "operator": "+",
* "value": "10",
* "range": "2,3",
* "type": "ATK",
* "leader": true,
* "attrNo": 2
* }
* }
*
*/
private function calculateListItemValue(&$source_list, &$target_list, $item_index, $type = 'ATK')
{
$power = array();
$operator = $source_list->items[$item_index]->attribute->operator;
$value = intval($source_list->items[$item_index]->attribute->value);
$leader = $source_list->items[$item_index]->attribute->leader ? 1 : .3;
$item_type = $source_list->items[$item_index]->attribute->affect;
$range = explode(',', $source_list->items[$item_index]->attribue->range);
if($value >= 0)
{
if(array_search('0', $range) !== false)
{
if($item_type == $type)
{
$power['power_up'] = $this->Operator($operator, ($type == 'ATK' ? $source_list->items[$item_index]->ATK : $source_list->items[$item_index]->DEF), $value, $leader);
}
}
elseif(array_search('4', $range) !== false)
{
foreach($source_list->items as $item)
{
if($item_type == $type)
{
$power['power_up'] += $this->Operator($operator, ($type == 'ATK' ? $item->ATK : $item->DEF), $value, $leader);
}
}
}
else
{
foreach($source_list->items as $item)
{
if(isset($item->attribute->attrNo))
{
if(array_search($item->attribute->attrNo, $range) !== false)
{
if($item_type == $type)
{
$power['power_up'] = $this->Operator($operator, ($type == 'ATK' ? $item->ATK : $item->DEF), $value, $leader);
}
}
}
}
}
}
elseif($value < 0)
{
if(array_search('0', $range) !== false)
{
return $power;
}
elseif(array_search('4', $range) !== false)
{
foreach($target_list->items as $item)
{
if($item_type == $type)
{
$power['power_down'] += $this->Operator($operator, ($type == 'ATK' ? $item->ATK : $item->DEF), $value, $leader);
}
}
}
else
{
foreach($target_list->items as $item)
{
if(isset($item->attribute->attrNo))
{
if(array_search($item->attribute->attrNo, $range) !== false)
{
if($item_type == $type)
{
$power['power_down'] += $this->Operator($operator, ($type == 'ATK' ? $item->ATK : $item->DEF), $value, $leader);
}
}
}
}
}
}
return $power;
}
private function Operator($operator, $base, $value, $mul)
{
$result = 0;
if($operator == '*')
{
$result = abs(ceil(($base * ($value * $mul)) - $base));
}
elseif($operator == '+')
{
$result = abs(ceil(($base + ($value * $mul)) - $base));
}
return $result;
}
Edit: Let me say that the code itself works fine, no problems.. but it's a nightmare to look at and explain to other people.