<?php
/**
 * Created by PhpStorm.
 * User: guanxl
 * Date: 2018/3/25
 * Time: 15:30
 */

namespace App\Http\Controllers\Console;
use App\Services\BrainstormingService;
use App\Services\TopService;
use App\Services\UserService;
use Laravel\Lumen\Routing\Controller as BaseController;

use Illuminate\Http\Request;
use App\Models\PkRoom;
use App\Models\User;
use Illuminate\Support\Facades\Redis;

class PkProcessController extends BaseController
{

    /**
     * PK结果处理接口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getResultProcess(Request $request){
        $params = $request->all();

        if(empty($params['room_id'])){
            return response()->json([
                "error" => 1,
                "message" => "无房间号码,无法获取PK结果"
            ]);
        }

        $roomId = $params['room_id'];

        //获取PK房间信息
        $roomInfo = PkRoom::where('room_id', $roomId)->where('status', 0)->first();

        if(!$roomInfo || empty($roomInfo)){
            return response()->json([
                "error" => 1,
                "message" => "无法获取PK结果"
            ]);
        }

        if($roomInfo['is_dispose'] == 1){
            return response()->json([
                "error" => 1,
                "message" => "结果已处理，请勿重复操作"
            ]);
        }

        $sponsorUserId    = $roomInfo['sponsor_user_id'];
        $challengerUserId = $roomInfo['challenger_user_id'];
        $sponsorUserInfo    = UserService::getInstance()->getUser($sponsorUserId);
        $challengerUserInfo = UserService::getInstance()->getUser($challengerUserId);
        $activityInfo       = BrainstormingService::getInstance()->get($roomInfo['activity_id']);
        $levelStarConfig    = json_decode($activityInfo['level_star_config'], true);

        $data = [];
        $data['sponsor_user_level_ago']         = intval($sponsorUserInfo['user_level']);
        $data['sponsor_user_star_ago']          = intval($sponsorUserInfo['user_star']);
        $data['sponsor_user_level_name_ago']    = $levelStarConfig[$sponsorUserInfo['user_level']]['name'];
        $data['challenger_user_level_ago']         = intval($challengerUserInfo['user_level']);
        $data['challenger_user_star_ago']          = intval($challengerUserInfo['user_star']);
        $data['challenger_user_level_name_ago']    = $levelStarConfig[$challengerUserInfo['user_level']]['name'];
        if($roomInfo['win_user_id'] == 0) {                                                                             //平局
            $data['sponsor_user_level_now']         = intval($sponsorUserInfo['user_level']);
            $data['sponsor_user_star_now']          = intval($sponsorUserInfo['user_star']);
            $data['sponsor_user_level_name_now']    = $levelStarConfig[$sponsorUserInfo['user_level']]['name'];

            $data['challenger_user_level_now']         = intval($challengerUserInfo['user_level']);
            $data['challenger_user_star_now']          = intval($challengerUserInfo['user_star']);
            $data['challenger_user_level_name_now']    = $levelStarConfig[$challengerUserInfo['user_level']]['name'];

            $data['sponsor_level_need_star']           = intval($levelStarConfig[$sponsorUserInfo['user_level']]['star']);
            $data['challenger_level_need_star']        = intval($levelStarConfig[$challengerUserInfo['user_level']]['star']);

            User::where('user_id', $sponsorUserId)->update(['win_continuity'=>0]);
            User::where('user_id', $challengerUserId)->update(['win_continuity'=>0]);

            $data['sponsor_win_continuity']     = 0;
            $data['challenger_win_continuity']  = 0;
        } else {                                                                                                        //有胜负
            $winUserId      = $roomInfo['win_user_id'];
            $failUserId     = $roomInfo['win_user_id'] == $roomInfo['sponsor_user_id'] ? $roomInfo['challenger_user_id'] : $roomInfo['sponsor_user_id'];
            $winUserInfo    = $roomInfo['win_user_id'] == $roomInfo['sponsor_user_id'] ? $sponsorUserInfo : $challengerUserInfo;
            $failUserInfo   = $roomInfo['win_user_id'] == $roomInfo['sponsor_user_id'] ? $challengerUserInfo : $sponsorUserInfo;

            $winUserQuery = [];
            if ($winUserInfo['user_star'] >= $levelStarConfig[$winUserInfo['user_level']]['star']) {                    //需要提升PK段位
                //小于最高段位则增加段位，重置星级
                if($winUserInfo['user_level'] < count($levelStarConfig)){
                    $winUserQuery['user_level'] = $winUserInfo['user_level'] + 1;
                    $winUserQuery['user_star']  = 1;
                } else {
                    $winUserQuery['user_star']  = $winUserInfo['user_star'] + 1;
                }
                $winUserQuery['win_continuity'] = $winUserInfo['win_continuity'] + 1;
            } else {                                                                                                    //只需要提升星级
                $winUserQuery['user_star']      = $winUserInfo['user_star'] + 1;
                $winUserQuery['win_continuity'] = $winUserInfo['win_continuity'] + 1;
            }

            $failUserQuery = [];
            if ($failUserInfo['user_star'] <= 0) {                                                                      //星级为0时，不扣减段位，不扣减星级
                $failUserQuery['user_star'] = 0;
            } else {
                $failUserQuery['user_star'] = $failUserInfo['user_star'] - 1;
            }
            $failUserQuery['win_continuity'] = 0;

            //修改PK双方的星段
            User::where('user_id', $winUserId)->update($winUserQuery);
            User::where('user_id', $failUserId)->update($failUserQuery);
            Redis::del(UserService::REDIS_USERS.':'.$sponsorUserId);
            Redis::del(UserService::REDIS_USERS.':'.$challengerUserId);

            $sponsorUserInfo_new    = UserService::getInstance()->getUser($sponsorUserId);
            $challengerUserInfo_new = UserService::getInstance()->getUser($challengerUserId);
            $data['sponsor_user_level_now']         = intval($sponsorUserInfo_new['user_level']);
            $data['sponsor_user_star_now']          = intval($sponsorUserInfo_new['user_star']);
            $data['sponsor_user_level_name_now']    = $levelStarConfig[$sponsorUserInfo_new['user_level']]['name'];

            $data['challenger_user_level_now']         = intval($challengerUserInfo_new['user_level']);
            $data['challenger_user_star_now']          = intval($challengerUserInfo_new['user_star']);
            $data['challenger_user_level_name_now']    = $levelStarConfig[$challengerUserInfo_new['user_level']]['name'];

            $data['sponsor_level_need_star']        = intval($levelStarConfig[$sponsorUserInfo_new['user_level']]['star']);
            $data['challenger_level_need_star']     = intval($levelStarConfig[$challengerUserInfo_new['user_level']]['star']);

            $data['sponsor_win_continuity']     = $sponsorUserInfo_new['win_continuity'];
            $data['challenger_win_continuity']  = $challengerUserInfo_new['win_continuity'];
        }

        PkRoom::where('room_id', $roomId)->update(['is_dispose'=>1]);

        //更新redis
        TopService::getInstance()->updateUserTop($challengerUserId);
        TopService::getInstance()->updateUserTop($sponsorUserId);

        return response()->json([
            'error'=>0,
            'message'=>"OK",
            'data'=>$data
        ]);
    }
}
?>