/**
  * knight's equipment are repaired automatically. Looking for equipment with pde loosed, calculate repair cost, substract to knight coins and add pde points. 
  * Return if error 
  * @param unknown_type $combat
  * @param unknown_type $knight
  * @return multitype:number boolean
  */
 public static function autorepairObjectsEquipment(&$combat, &$knight)
 {
     //
     $output = array('errno' => 0, 'error' => '', 'automatic_repair' => true, 'repair_cost' => 0, 'not_enought_money' => false);
     //Prepared statement
     $sql = '';
     //Load all items of inventory of knight
     $inventory = Inventory::model()->findAll('knights_id = :knights_id AND (type = ' . Inventory::EQUIPMENT_TYPE_ARMOUR . ' OR type = ' . Inventory::EQUIPMENT_TYPE_SPEAR . ' )', array(':knights_id' => $knight->id));
     if (count($inventory)) {
         //For each item check maximun pde
         foreach ($inventory as $item) {
             //Load class of item
             if ($item['type'] == Inventory::EQUIPMENT_TYPE_ARMOUR) {
                 $object = ArmoursObjects::model()->findByPk($item['identificator']);
                 $classItem = Armours::model()->findByPk($object->armours_id);
             } else {
                 $object = SpearsObjects::model()->findByPk($item['identificator']);
                 $classItem = Spears::model()->findByPk($object->spears_id);
             }
             //echo "\n".$knight->name.": ".$classItem->name." PDE MAX (".$classItem->pde.") object pde (".$object->current_pde.") ";
             //Check if need a repair
             if ($object->current_pde < $classItem->pde) {
                 //Check prize of reparation
                 $percentPDEloose = 1 - $object->current_pde / $classItem->pde;
                 $repair_cost = ceil($classItem->prize * $percentPDEloose);
                 //echo "coins (".$knight->coins.") percent ($percentPDEloose) prize class item(".$classItem->prize.") repair cost (".$repair_cost.")";
                 //Check coins and cost
                 if ($knight->coins > $repair_cost) {
                     Yii::log('Reparando ' . $classItem->name . ' por ' . $repair_cost . ' MO');
                     //Add autorepair row
                     $objectRepair = new ObjectRepairs();
                     $objectRepair->attributes = array('knights_id' => $knight->id, 'inventory_type' => $item['type'], 'combats_id' => $combat->id, 'object_identificator' => $item['identificator'], 'class_identificator' => $classItem->id, 'current_pde' => $object->current_pde, 'maximum_pde' => $classItem->pde, 'repair_cost' => $repair_cost, 'date' => date('Y-m-d H:m:s'));
                     if (!$objectRepair->save()) {
                         $output['errno'] = 2;
                         $output['error'] .= $objectRepair->getErrors();
                     }
                     //We can repair the object
                     $object->current_pde = $classItem->pde;
                     if (!$object->save()) {
                         $output['errno'] = 4;
                     }
                     //Subtract coins
                     $knight->coins -= $repair_cost;
                     $output['repair_cost'] += $repair_cost;
                 } else {
                     //Upsss, knight has not enought money!! we break loop
                     Yii::log('¡¡No se puede reparar!!');
                     $output['not_enought_money'] = true;
                     break;
                 }
             }
         }
     } else {
         $output['errno'] = 1;
     }
     //var_dump($output);
     return $output;
 }