<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit;

function Mirai_expOnRegister($uid) {
    $value = Mirai_expGetTaskValue('sign_up');
    if ($value <= 0) return;
    Mirai_expAdd($uid, $value, 'sign_up', 'user', $uid, '首次注册', 1);
}

function Mirai_expOnLogin($uid) {
    $value = Mirai_expGetTaskValue('sign_in');
    if ($value <= 0) return;
    Mirai_expAdd($uid, $value, 'sign_in', 'daily', 0, '每日登录', 1);
}

function Mirai_expOnPostPublish($cid, $authorId) {
    $value = Mirai_expGetTaskValue('post_new');
    if ($value <= 0) return;
    if (!$authorId) return;
    Mirai_expAdd($authorId, $value, 'post_new', 'post', $cid, '发布文章', 1);
}

function Mirai_expOnComment($coid, $authorId) {
    $value = Mirai_expGetTaskValue('comment_new');
    if ($value <= 0) return;
    if (!$authorId) return;
    Mirai_expAdd($authorId, $value, 'comment_new', 'comment', $coid, '发表评论', 1);
}

function Mirai_expOnLike($cid, $postAuthorId, $operatorUid) {
    $value = Mirai_expGetTaskValue('post_like');
    if ($value <= 0) return;
    if (!$postAuthorId || $operatorUid == $postAuthorId) return;

    $db = \Typecho\Db::get();
    $expLogTable = Mirai_pointsTable('mirai_exp_logs');
    $count = (int)$db->fetchObject($db->select(['COUNT(*)' => 'cnt'])->from($expLogTable)
        ->where('action = ? AND ref_id = ? AND uid = ?', 'post_like', $cid, $postAuthorId))->cnt;
    if ($count >= 5) return;

    Mirai_expAdd($postAuthorId, $value, 'post_like', 'post', $cid, '文章获赞', 5);
}

function Mirai_expOnCollect($cid, $postAuthorId, $operatorUid) {
    $value = Mirai_expGetTaskValue('post_collect');
    if ($value <= 0) return;
    if (!$postAuthorId || $operatorUid == $postAuthorId) return;

    $db = \Typecho\Db::get();
    $expLogTable = Mirai_pointsTable('mirai_exp_logs');
    $count = (int)$db->fetchObject($db->select(['COUNT(*)' => 'cnt'])->from($expLogTable)
        ->where('action = ? AND ref_id = ? AND uid = ?', 'post_collect', $cid, $postAuthorId))->cnt;
    if ($count >= 5) return;

    Mirai_expAdd($postAuthorId, $value, 'post_collect', 'post', $cid, '文章被收藏', 5);
}
