<?php


namespace freedom\utils;


use think\facade\Cache;

/**
 * TODO Redis操作工具类
 * Class RedisUtil
 * @package freedom\utils
 */
class RedisUtil
{

    /**
     * TODO 卡牌保存
     * @param int $numberTabId 局ID
     * @param int $card 卡牌点数
     * @param int $timeout 过期时间，默认3天
     * @return bool;
     */
    public static function saveCardPosition(int $numberTabId, int $position, int $card, $timeout = 60*60*24*3):bool{
        $res = Cache::store('redis')->set('card_'.$numberTabId.'_'.$position,$card,$timeout);
        if ($res){
            return true;
        }else{
            return false;
        }
    }

    /**
     * TODO 卡牌获取
     * @param int $numberTabId 局ID
     * @return array;
     */
    public static function getCardPosition(int $numberTabId): array
    {
        $card11 = Cache::store('redis')->get('card_'.$numberTabId.'_11');
        $card12 = Cache::store('redis')->get('card_'.$numberTabId.'_12');
        $card13 = Cache::store('redis')->get('card_'.$numberTabId.'_13');
        $card21 = Cache::store('redis')->get('card_'.$numberTabId.'_21');
        $card22 = Cache::store('redis')->get('card_'.$numberTabId.'_22');
        $card23 = Cache::store('redis')->get('card_'.$numberTabId.'_23');
        $cardInfoRedis = [];
        if ($card11) $cardInfoRedis['player_1'] = $card11;
        if ($card12) $cardInfoRedis['player_2'] = $card12;
        if ($card13) $cardInfoRedis['player_3'] = $card13;
        if ($card21) $cardInfoRedis['banker_1'] = $card21;
        if ($card22) $cardInfoRedis['banker_2'] = $card22;
        if ($card23) $cardInfoRedis['banker_3'] = $card23;
        return $cardInfoRedis;
    }

    /**
     * TODO 卡牌缓存删除
     * @param int $numberTabId
     * @return bool
     */
    public static function deleteCardPosition(int $numberTabId): bool
    {
        Cache::store('redis')->delete('card_'.$numberTabId.'_11');
        Cache::store('redis')->delete('card_'.$numberTabId.'_12');
        Cache::store('redis')->delete('card_'.$numberTabId.'_13');
        Cache::store('redis')->delete('card_'.$numberTabId.'_21');
        Cache::store('redis')->delete('card_'.$numberTabId.'_22');
        Cache::store('redis')->delete('card_'.$numberTabId.'_23');
        return true;
    }

    /**
     * TODO 卡牌保存
     * @param int $numberTabId 局ID
     * @param array $array 卡牌数组
     * @param int $timeout 过期时间，默认3天
     * @return bool;
     */
    public static function saveCard(int $numberTabId, array $array, $timeout = 60*60*24*3): bool{
        $res = Cache::store('redis')->set('card_'.$numberTabId,json_encode($array),$timeout);
        if ($res){
            return true;
        }else{
            return false;
        }
    }

    /**
     * TODO 卡牌获取
     * @param int $numberTabId 局ID
     * @return array;
     */
    public static function getCard(int $numberTabId): array{
        $cardInfoRedis = Cache::store('redis')->get('card_'.$numberTabId);
        if ($cardInfoRedis){
            return json_decode($cardInfoRedis,true);
        }else{
            return [];
        }
    }

    /**
     * TODO 设置缓存
     * @param string $name 名称
     * @param string|int|bool $value 名称
     * @param int $waitTime 过期时间
     * @return bool;
     */
    public static function save(string $name, $value, int $waitTime): bool
    {
        $res = Cache::store('redis')->set($name,$value,$waitTime);
        if ($res){
            return true;
        }else{
            return false;
        }
    }

    /**
     * TODO 删除缓存
     * @param string $name 名称
     * @return bool;
     */
    public static function delete(string $name): bool
    {
        $res = Cache::store('redis')->delete($name);
        if ($res){
            return true;
        }else{
            return false;
        }
    }

    /**
     * TODO 获取缓存
     * @param string $name 名称
     * @return string;
     */
    public static function get(string $name): string
    {
        return Cache::store('redis')->get($name);
    }
}