<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class UseAction extends BaseAction
{
    public function run()
    {
        $time = time();
        $this->getController()->isLogin();
        $this->getController()->isVerify();
        
        if (isset($_SESSION['user']['wa']['lastvist']) && ($time - $_SESSION['user']['wa']['lastvist'] < 5))
        {
            //throw new iSystemException('您的动作太快了，歇下喘口气^_^');
        }
        
        $id = S::gp('id', 'G', 2);
        if (!in_array($id, array(1,2,3)))
        {
            throw new iSystemException('非法操作');
        }
        $money = $this->getController()->priceMap[$id];
        $this->getController()->hasMoney($this->uid(),$this->userName(),$money);
        $icredit = iCredits::single($this->uid(), $this->userName());
        $icredit->consume($money);

        $data = array(
            'uid' => $this->uid(),
            'username' => $this->userName(),
            'amount' => 1,
            'price' => $money,
        );
        PropertyLog::single()->create(16,$data);

        Dynamic::single()->createWaDynamic('{{'.$this->uid().'}}耐不住绝世珍宝的诱惑!出手{{Tag}}了,快抢啊');
        
        $badMessage = 'Sorry!这次手气太差了,珍宝没挖到!<br/>我不甘心,'.ilink('继续',url('system',array('s'=>'props','c'=>'wa'))).'挖到为止!';
        $propList = $this->getController()->propMap[$id];
        $result = $this->getController()->proRand($propList);
        if (empty($result) || -1 == $result[0][0] || !isset($result[0][1]))
        {
            $this->showMessage($badMessage);
        }
        
        $_SESSION['user']['wa']['lastvist'] = $time;
        $re = $result[0][0];
        if (0 == $re)
        {
            $array = array(1=>20, 2=>100, 3=>500);
            $icredit->addMoney($array[$id]);
            Dynamic::single()->createWaDynamic('恭喜!{{'.$this->uid().'}}成功挖到了'.$array[$id].'{{'.setting('creditname').'}}');
            $this->showMessage('哇!好手气,您挖到了'.$array[$id].setting('creditname').',我要'.ilink('继续',url('system',array('s'=>'props','c'=>'wa'))).'挖');
        }
        else if (in_array($re, array(1,2,3,4)))
        {
            $array = array(1=>'flower',2=>'brick',3=>'recommend',4=>'frecommend');
            $property = $this->getController()->getProp($array[$re]);
            
            if (false !== ($prop = $this->getController()->hasPropMemberLog($this->uid(), $array[$re])))
            {
                $option = unserialize($prop['option']);
                3 == $re ? $option['300'] : $option['amount'] += 1;
                $this->getController()->updatePropMemberLog($this->uid(), $this->userName(), $re, serialize($option));
            }
            else
            {
                3 == $re ? $option = array('class'=>3,'time'=>300) : array('class'=>1,'amount'=>1);
                $this->getController()->createPropMemberLog($this->uid(),$this->userName(), $re, serialize($option));
            }
            Dynamic::single()->createWaDynamic('恭喜!{{'.$this->uid().'}}成功挖到了{{'.$property['propertyname'].'}}');
            $this->showMessage('哇!好手气,您挖到一个绝世珍宝: '.$property['propertyname'].'('.ilink('查看',url('system',array('s'=>'member','c'=>'my','a'=>'props'))).'),我要'.ilink('继续',url('system',array('s'=>'props','c'=>'wa'))).'挖');
        }
        else if (in_array($re, array(5, 12, 13)))
        {
            if (in_array($re, array(12, 13)))
            {
                $array = array(12 => 'noad', 13 => 'auto');
                $days = 2 == $id ? 7 : 30;
                $expire = (u('autoprogress') && u('autoprogress') > $time ? u('autoprogress') : $time) + ($days * 24 * 3600);
                $key = $re == 12 ? 'noad' : 'autoprogress';
                if (false !== ($prop = $this->getController()->hasPropMemberLog($this->uid(), $array[$re])))
                {
                    $option = unserialize($prop['option']);
                    $option['expire'] = $expire;
                    $this->getController()->updatePropMemberLog($this->uid(), $this->userName(), $re, serialize($option));
                }
                else
                {
                    $option = array('class' => 2, 'expire' => $expire);
                    $this->getController()->createPropMemberLog($this->uid(),$this->userName(), $re, serialize($option));
                }
                $this->db()->update(MemberExts::table())->value(array($key=>$expire))->where('uid='.$this->uid())->q();
                
                $times = $id == 2 ? '1周' : '1月';
                $property = $this->getController()->getProp($array[$re]);
                Dynamic::single()->createWaDynamic('恭喜!{{'.$this->uid().'}}成功挖到了'.'('.$times.'){{'.$property['propertyname'].'}}');
                $this->showMessage('哇!好手气,您挖到一个绝世珍宝: '.$property['propertyname'].'('.$times.')('.ilink('查看',url('system',array('s'=>'member','c'=>'my','a'=>'props'))).'),我要'.ilink('继续',url('system',array('s'=>'props','c'=>'wa'))).'挖');
            }
            else if ($re == 5)
            {
                $expire = strtotime('+7 day');
                $medalList = $this->getController()->medalListMap;
                $keys = array_keys($medalList);
                shuffle($keys);
                $medalName = $medalList[$keys[0]];
                $medals = $this->getController()->getMedal($medalName, 100);
                if (empty($medals['medals']))
                {
                    $this->showMessage($badMessage);
                }
                $medal = $medals['medals'][rand(0, (count($medals['medals']) - 1))];
                $medalImgSrc = iMedals::single()->getImage($medal['image']);
                $check = $this->db()->select(array('medals'))->from(Memberfields::table())->where(array('uid'=>$this->uid()))->find(true);
                if (!empty($check['medals']))
                {
                    $existsMedals = iMedals::single()->getMedalsString($check['medals']);
                    if (!empty($existsMedals))
                    {
                        $_SESSION['user']['wa'] = array('id'=>$medal['medalid'],'expire'=>$expire);
                        $message[] = '哇!好手气,您挖到一个勋章:<img src="'.$medalImgSrc.'" alt="wa" />';
                        $message[] = '但系统检测到您当前已经有'.count($existsMedals['images']).'枚勋章:'.implode('', $existsMedals['images']);
                        $message[] = '如果要领取奖品，之前的将被删除，是否'.ilink('领取',url('system',array('s'=>'props','c'=>'wa','a'=>'medal'))).'？';
                        $this->showMessage($message);
                    }
                }
                else
                {
                    $this->db()->update(Memberfields::table())->value(array('medals'=>$medal['medalid'].'|'.$expire))->where('uid='.$this->uid())->q();
                    $this->showMessage('哇!好手气,您挖到一枚勋章: <img src="'.$medalImgSrc.'" alt="wa" />,我要'.ilink('继续',url('system',array('s'=>'props','c'=>'wa'))).'挖');
                }
                Dynamic::single()->createWaDynamic('恭喜!{{'.$this->uid().'}}成功挖到了一枚{{勋章}}');
            }
        }
        else
        {
            $this->showMessage($badMessage);
        }
    }
}