<?php

namespace loyalsoft;

/**
 * 公会数据结构
 */
class Data_Guild extends HashSaver {

    /**
     * 公会的ID
     * @var int
     */
    public $guildId;

    /**
     * 公会名称
     * @var string
     */
    public $name;

    /**
     * 公会的等级
     * @var int
     */
    public $level = 1;

//    /**
//     * 奖杯积分
//     * @var int
//     */
//    public $cupScore = 0;

    /**
     * 玩家申请加入公会列表
     * @var object
     */
    public $applylist;

    /**
     * 会长的OpenID
     * @var string
     */
    public $chairman_id = "";

    /**
     * 会长 name
     * @var string
     */
    public $chairman_name = "";

//    /**
//     * @var array 副会长集合
//     */
//    public $viceChairManArr = array();
//
//    /**
//     * @var array 干部
//     */
//    public $officerManArr = array();

    /**
     * 公会成员
     * @var array
     */
    public $members = array();

    /**
     * 公会的宗旨/公告
     * @var string
     */
    public $declare = "";

    /**
     * 创建时间戳
     * @var type
     */
    public $createts = 0;

    /**
     * 允许一键(快速)加入, 默认开启,1是需要审核,2,不允许任何人加入
     * @var int
     */
    public $enableQuickIn = 0;

    /**
     * 徽章
     * @var int
     */
//    public $guild_img = 0;

    /**
     * 加入公会所需条件
     * @var int
     */
//    public $condition;
//    /**
//     * 弹劾 同意
//     * @var type
//     */
//    public $accuse_agreed = array();
//
//    /**
//     * 弹劾 拒接
//     *  @var type
//     */
//    public $accuse_refuse = array();
//    /**
//     * 弹劾 时间
//     * @var int
//     */
//    public $accuseTs = 0;
//    /**
//     * 弹劾 冷却时间
//     * @var int
//     */
//    public $accuse_coolTime = 0;
//    /**
//     * 公会总贡献
//     * @var int
//     */
//    public $allContribute = 0;
//    /**
//     * 公会公告编辑冷却时间
//     * @var int
//     */
//    public $announcement_CoolingTs = 0;
//
//    /**
//     * 公会公告内容
//     * @var type
//     */
//    public $announcement_content = array();
//    /**
//     * 公会礼包购买数量记录
//     * @var type
//     */
//    public $giftnum_record;

    /**
     * 检察公会最大人数 
     * @return boolean
     */
    function checkmemberfull() { //根据成员的 贡献度
        $levelInfo = GameConfig::guildlevel_getItem($this->level);
        return count($this->members) >= $levelInfo->maxPersoNum;
    }

    function updateGuildMemberCount() {
        daoInst()->update(GuildProc::tabGuild)
                ->data(array('membercount' => count($this->members)))
                ->where('guildId')->eq($this->guildId)
                ->andWhere('zoneid')->eq(req()->zoneid)
                ->exec();
    }

    /**
     * 创建新公会
     * @param type $mem
     * @return Data_Guild
     */
    public static function NewGuildInit($creator_uid, $creator_name, $guildName, $declare = "公告", $guild_img = "-") {
        $guildInfo = new Data_Guild();
        $guildInfo->guildId = self::CreateNewGuildID();
        $guildInfo->createts = now();
        $guildInfo->members = array($creator_uid);
        $guildInfo->level = 1;
//        $guildInfo->cupScore = 0;
        $guildInfo->applylist = ObjectInit();
//        $guildInfo->condition = ObjectInit();
//        $guildInfo->giftnum_record = ObjectInit();

        $guildInfo->chairman_id = $creator_uid;
        $guildInfo->chairman_name = $creator_name;
        $guildInfo->name = $guildName;

        $guildInfo->declare = $declare;
//        $guildInfo->guild_img = $guild_img;
        $guildInfo->enableQuickIn = 0;                                          # 0 可以直接加入, 1 需要审批, 2 不允许任何人加入
//        $guildInfo->chairmanLastLogin = $base->lastLogin;
        return $guildInfo;
    }

    private static function CreateNewGuildID() {
        $key_id = MemKey_Guild::constGetCurrentMaxGuildID(req()->zoneid);
        return gMem()->increment($key_id);
    }

}

// <editor-fold defaultstate="collapsed" desc="闲置">

/**
 * 玩家请求加入公会的消息结构体
 */
class UserApplyForGuildModel {

    public $uid;
    public $ts;
    public $approved = false;
    public $name;
    public $level;

    /**
     * 创建一条新的请求信息
     * @param type $oid
     * @return UserApplyForGuildModel Description
     */
    public static function CreateApplyInfo($uid, $name, $level) {
        $ret = new UserApplyForGuildModel();
        $ret->uid = $uid;
        $ret->ts = now();
        $ret->name = $name;
        $ret->level = $level;
        $ret->approved = false;

        return $ret;
    }

}

/**
 * 公会值日消息类型
 */
class GuildJournalType {

    const Join = 1; //加入
    const requestDonate = 2; //请求捐献
    const Quit = 3; //退会
    const ChangeChairMan = 4; //改变会长
    const BuyGift = 5; //购买礼包

}

/**
 * 公会日志数据体
 */
class GuildJournalModel {

    public $ts;
    public $uid;
    public $type;
    public $datas;

    public static function CreateJoin($uid, $name) {
        $journal = new GuildJournalModel();
        $journal->ts = now();
        $journal->type = GuildJournalType::Join;
        $journal->uid = $uid;
        $journal->datas = array();
        $journal->datas [] = $uid;
        $journal->datas [] = $name;
        return $journal;
    }

    public static function RequestDonate($uid, $name, $cardId, $cardCount) {
        $journal = new GuildJournalModel();
        $journal->ts = now();
        $journal->type = GuildJournalType::requestDonate;
        $journal->uid = $uid;
        $journal->datas = array();
        $journal->datas [] = $uid;
        $journal->datas [] = $name;
        $journal->datas [] = $cardId;
        $journal->datas [] = $cardCount;
        $journal->datas [] = 0; //现在已得到的捐献数量
        return $journal;
    }

    public static function CreateQuit($uid, $name) {
        $journal = new GuildJournalModel();
        $journal->ts = now();
        $journal->type = GuildJournalType::Quit;
        $journal->uid = $uid;
        $journal->datas = array();
        $journal->datas [] = $uid;
        $journal->datas [] = $name;
        return $journal;
    }

    public static function CreateChangeChairman($uid, $name, $transferedid, $transferedname) {
        $journal = new GuildJournalModel();
        $journal->ts = now();
        $journal->type = GuildJournalType::ChangeChairMan;
        $journal->uid = $uid;
        $journal->datas = array();
        $journal->datas [] = $uid;
        $journal->datas [] = $name;
        $journal->datas [] = $transferedid;
        $journal->datas [] = $transferedname;
        return $journal;
    }

    public static function BuyGift($uid, $name) {
        $journal = new GuildJournalModel();
        $journal->ts = now();
        $journal->type = GuildJournalType::BuyGift;
        $journal->uid = $uid;
        $journal->datas = array();
        $journal->datas [] = $uid;
        $journal->datas [] = $name;

        return $journal;
    }

}

class GuildAnnouncementModel extends Object_ext {

    public $ts;
    public $title;
    public $content;

    function __construct($tag, $ts, $title, $content) {
        if (1 == func_num_args() && $tag) {                                     # 默认的Object初始化方法
            parent::__construct($tag);
        } else {
            $this->ts = $ts;
            $this->title = $title;
            $this->content = $content;
        }
    }

}

// </editor-fold>