<?php
class Jobs{
static private $instance = NULL;
public $data = array();
public $userData = array();
public $userDataOld = array();
public $userJob = array();
static function a($data){
if(self::$instance==NULL)self::$instance=new Jobs($data);
return self::$instance;
}
public function __construct($data){
$this->userData = User::a()->user;
$this->userDataOld = User::a()->user;
//Если нет задания,записываем первое
if($this->userData['dataJobs']==''){
$this->userJob = array('id' => 1 , 'count' => array(0,0,0,0,0,0,0,0,0,0));
}
//Получаем инфу о задании
else{
$this->userJob = json_decode($this->userData['dataJobs'],TRUE);
}
$dataJobs = DataJobs::a()->data;
foreach($dataJobs as $key => $value){
$this->data[$key] = $value;
}
$this->update();
}
public function getJob() {
$data = json_decode(User::a()->user['dataJobs'],true);
return array('userData' => $this->userJob ,'jobData' =>$this->data[$data['id']]);
}
public function updateCount($key,$count) {
$data = json_decode(User::a()->user['dataJobs'],true);
$data['count'][$key]+=$count;
User::a()->user['dataJobs'] = json_encode($data);
}
public function searchJob($type,$action,$count) {
$job = $this->getJob();
if($job['jobData']==NULL)return false;
$keys = array_keys($job['jobData']['type'],$type);
if(count($keys)>1){
foreach($keys as $key=>$value){
if($job['jobData']['action'][$value]==$action)$this->updateCount($value,$count);
}
}else{
if(array_search($type, $job['jobData']['type'])!==FALSE)$key = array_search($type, $job['jobData']['type']);
if($key!==FALSE){
if($job['jobData']['action'][$key]==$action)$this->updateCount($key,$count);
}
}
}
public function progressJob() {
$job = $this->getJob();
if($job['jobData']==false)return false;
$fact=0;
$count = count($job['jobData']['action']);
for($i=0;$i<$count;$i++){
if($job['userData']['count'][$i] >= $job['jobData']['count'][$i])$fact++;
}
if($fact == $count)return true;
return false;
}
public function getFeed(){
$job = $this->getJob();
if($job['jobData']==false)return false;
if($this->progressJob()==true)return "Задание \"".$job['jobData']['name']."\" выполнено!<br>Перейдите в <a class='link4' href='/Jobs/'>Задания</a> ,чтобы получить награду!";
return false;
}
public function getStar(){
if($this->progressJob()== true)return '<span class="minor">(*)</span>';
}
public function getBonusView(){
$job = $this->getJob();
$count=0;
foreach($job['jobData']['price'] as $key=>$value){
if($key=='donate')$value=User::a()->format($value);
if($key=='money')$value=User::a()->format($value);
if($key=='rating')$value=num($value);
if($count==0)$bonus="<img src='/img/icons/".$key.".png' style='margin-right:2px;width:16px'>".$value;
else $bonus.=" и <img src='/img/icons/".$key.".png' style='margin-right:2px;width:16px'>".$value;
$count++;
}
return $bonus;
}
public function counts($count,$count_need){
if($count>$count_need) return $count_need;
return $count;
}
public function getIcon($count,$count_need){
if($count>=$count_need)return '<img src="/img/jobs/clean.png" style="vertical-align:middle;margin-right:2px;width:18px">';
return '<img src="/img/icons/cancel.png" style="vertical-align:middle;margin-right:2px;width:18px">';
}
public function endJob(){
$job = $this->getJob();
if($job['jobData']==false)return false;
foreach($job['jobData']['price'] as $key=>$value){
User::a()->user[$key]+=$value;
}
$_SESSION['info']= $this->getBonusView();
$this->userJob['id']++;
$this->userJob['count'] = array(0,0,0,0,0,0,0,0,0,0);
User::a()->user['countJobs']++;
$this->update();
return true;
}
public function nextJob(){
$job = $this->getJob();
if($job['jobData']==false)return false;
if($job['jobData']['next']==false){
return $this->nextJobDonate();
}
$this->userJob['id']++;
$this->userJob['count'] = array(0,0,0,0,0,0,0,0,0,0);
$this->update();
return true;
}
public function nextJobDonate(){
$job = $this->getJob();
if($job['jobData']===false)return false;
if($job['jobData']['next']===true)return false;
if(User::a()->user['donate'] < 15){
$_SESSION['info']="Не хватает <img src='/img/icons/donate.png' style='margin-right:2px;width:16px'>".(15-User::a()->user['donate']);
return false;
}
$this->userJob['id']++;
$this->userJob['count'] = array(0,0,0,0,0,0,0,0,0,0);
User::a()->user['donate']-=15;
$this->update();
return true;
}
public function loc(){
header("Location:/Jobs/");
}
public function update() {
User::a()->user['dataJobs'] = json_encode($this->userJob);
}
}