# linux script server logic status about functions encapsulation.
# Longwei Lai
###################################################################

source "${____FWK_SCRIPT_PATH}/lnx_fwk_path.sh"
source "${____FWK_SCRIPT_PATH}/lnx_fwk_server.sh"
source "${____LOGIC_SCRIPT_PATH}/lnx_logic_defs.sh"
source "${____LOGIC_SCRIPT_PATH}/lnx_logic_serverhttp.sh"
source "${____LOGIC_SCRIPT_PATH}/lnx_logic_serversql.sh"
source "${____LOGIC_SCRIPT_PATH}/lnx_logic_serverconf.sh"

# get gameserver logic status describe
# arguments:
#   arg1: logic status(1,2,...)
# returns:
#   echo style:
#       the logic status describe.
#   return style:
#       N/A
function get_gameserver_logic_status_desc()
{
    local logic_status="$1"
    if [ "${logic_status}" = 1 ]; then
        echo -n "Normal(正常)"
    elif [ "${logic_status}" = 2 ]; then
        echo -n "Maintain(维护)"
    elif [ "${logic_status}" = 3 ]; then
        echo -n "New(新服)"
    else
        echo -n "Unknown(未知):${logic_status}"
    fi
}

# get gameserver logic status
# arguments:
#   N/A
# returns:
#   echo style:
#       the gameserver all kingdoms status describe.
#   return style:
#       N/A
function get_gameserver_logic_status()
{
    # get all kingdom ids
    local gamenode_id="`get_serverconf_gamenode_id`"
    local kingdom_ids="`exec_sql_cmd_on_confdb "SELECT kid from conf_kingdom WHERE nodeid = ${gamenode_id}"`"

    for kingdom_id in ${kingdom_ids}; do
        local sql_cmd="SELECT status FROM conf_kingdom WHERE kid = ${kingdom_id}"
        local kingdom_status="`exec_sql_cmd_on_confdb "${sql_cmd}"`"
        echo "kingdom ${kingdom_id} status: `get_gameserver_logic_status_desc "${kingdom_status}"`"
    done
}

# set gameserver logic status
# arguments:
#   arg1: the new gameserver logic status(1, 2, ...)
# returns:
#   echo style:
#       N/A
#   return style:
#       0: success
#       <other>: failed
function set_gameserver_logic_status()
{
    # get gameserver all kingdom ids
    local gamenode_id="`get_serverconf_gamenode_id`"
    local kingdom_ids="`exec_sql_cmd_on_confdb "SELECT kid from conf_kingdom WHERE nodeid = ${gamenode_id}"`"

    local new_status="$1"
    log_info "set gameserver status to ${new_status}..."

    # if set status to maintain then enable ipwhitelist, otherwise disable ipwhitelist
    local new_ipwhitelist_status=0
    if [ "${new_status}" = 2 ]; then
        new_ipwhitelist_status=1
    fi
    log_info "set gameserver ipwhitelist status to ${new_ipwhitelist_status}..."

    local send_http_msg_failed=FALSE
    local loginnode_id="`get_serverconf_loginnode_id`"
    log_info "call HTTP API to set status(send to login node:${loginnode_id})..."
    for kingdom_id in ${kingdom_ids}; do
        # modify conf_kingdom table
        log_info "set kingdom ${kingdom_id} status to: ${new_status}..."
        local msg="{\"kingdoms\": [${kingdom_id}], \"status\": ${new_status}}"
        local res_msg="`send_httpmsg_to_node ${loginnode_id} 1 2 "${msg}"`"
        local send_succ=$?
        if [ ${send_succ} -ne 0 -o -z "`echo "${res_msg}" | egrep '"result"\s*:\s*1\s*,'`" ]; then
            log_warn "failed to set kingdom ${kingdom_id} status, http res:${res_msg}, skip..."
            send_http_msg_failed=TRUE
        else
            log_info "set kingdom ${kingdom_id} status to ${new_status} success, return: ${res_msg}"
        fi

        # modify conf_ipwhitelist table
        log_info "set kingdom ${kingdom_id} ipwhitelist status to: ${new_ipwhitelist_status}..."
        local msg="{\"kid\": ${kingdom_id}, \"status\": ${new_ipwhitelist_status}}"
        local res_msg="`send_httpmsg_to_node ${loginnode_id} 1 17 "${msg}"`"
        local send_succ=$?
        if [ ${send_succ} -ne 0 -o -z "`echo "${res_msg}" | egrep '"result"\s*:\s*true\s*,'`" ]; then
            log_warn "failed to set kingdom ${kingdom_id} ipwhitelist status, res:${res_msg}!, skip..."
            send_http_msg_failed=TRUE
        fi
    done

    if [ ${send_http_msg_failed} = TRUE ]; then
        log_info "set some kingdom status failed, try update sql some tables to set status..."
        for kingdom_id in ${kingdom_ids}; do
            log_info "set kingdom ${kingdom_id} status to: ${new_status}..."

            local sql_cmd="UPDATE conf_kingdom SET status = ${new_status} WHERE kid = ${kingdom_id}"
            exec_sql_cmd_on_confdb "${sql_cmd}"
            local sql_succ=$?
            if [ ${sql_succ} -ne 0 ]; then
                log_err "failed to set kingdom ${kingdom_id} status, res:${res_msg}!"
                return ${sql_succ}
            fi

            sql_cmd="UPDATE conf_ipwhitelist SET status = ${new_ipwhitelist_status} WHERE kid = ${kingdom_id}"
            exec_sql_cmd_on_confdb "${sql_cmd}"
            sql_succ=$?
            if [ ${sql_succ} -ne 0 ]; then
                log_err "failed to set kingdom ${kingdom_id} ipwhitelist status, res:${res_msg}!"
                return ${sql_succ}
            fi
        done
    fi
}

