/**
  * 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;
 }
 public function actionShowPostcombat()
 {
     $output = array('errno' => 1, 'html' => '');
     //Valid input
     if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) {
         //Load combat
         if ($combat = Combats::model()->with('fromKnight', 'toKnight')->findByPk($_GET['id'])) {
             //Check if precombat exit
             $postcombats = CombatsPostcombat::model()->findAll('combats_id = :combats_id', array(':combats_id' => $_GET['id']));
             if (count($postcombats) == 2) {
                 //Set data from knight and to knight
                 if ($combat->from_knight == $postcombats[0]->knights_id) {
                     $data = array('combat' => $combat, 'from_knight_postcombat' => &$postcombats[0], 'from_knight_automatic_object_repairs' => array(), 'from_knight_downgrades' => array(), 'to_knight_postcombat' => &$postcombats[1], 'to_knight_automatic_object_repairs' => array(), 'to_knight_downgrades' => array(), 'injuryLabels' => Constants::getLabelsTypeInjuries(), 'knight_card_labels' => array());
                 } else {
                     $data = array('combat' => $combat, 'from_knight_postcombat' => &$postcombats[1], 'from_knight_automatic_object_repairs' => array(), 'from_knight_downgrades' => array(), 'to_knight_postcombat' => &$postcombats[0], 'to_knight_automatic_object_repairs' => array(), 'to_knight_downgrades' => array(), 'injuryLabels' => Constants::getLabelsTypeInjuries(), 'knight_card_labels' => array());
                 }
                 //Load repairs
                 $result = ObjectRepairs::model()->findAll('combats_id = :combats_id', array(':combats_id' => $combat->id));
                 //sort result by knight
                 if (count($result)) {
                     foreach ($result as $repair) {
                         if ($repair['knights_id'] == $combat->from_knight) {
                             //Add to from knight
                             array_push($data['from_knight_automatic_object_repairs'], $repair);
                         } else {
                             array_push($data['to_knight_automatic_object_repairs'], $repair);
                         }
                     }
                 }
                 //Load downgrades!!
                 $downgrades = KnightsEvolution::model()->findAll('combats_id = :combats_id', array(':combats_id' => $combat->id));
                 //sort result by knight
                 if (count($downgrades)) {
                     foreach ($downgrades as $downgrade) {
                         if ($downgrade->knights_id == $combat->from_knight) {
                             array_push($data['from_knight_downgrades'], $downgrade);
                         } else {
                             array_push($data['to_knight_downgrades'], $downgrade);
                         }
                     }
                     //Load knight card labels
                     $data['knight_card_labels'] = KnightsCard::model()->attributeLabelsById();
                 }
                 //	var_dump($data);die;
                 $output['html'] = $this->renderFile(Yii::app()->basePath . '/views/character/dialog_post_combat.php', $data, true);
                 $output['errno'] = 0;
             } else {
                 $output['html'] = 'Se ha producido un error al cargar el postcombate ' . count($postcombats);
             }
         } else {
             $output['html'] = 'No se ha encontrado al combate.';
         }
     } else {
         $output['html'] = 'El identificador del combate no es válido.';
     }
     echo CJSON::encode($output);
 }