#!/bin/bash
[ "$1" == "" ] && exec echo "Usage: $0 <TYPE: 1 => LOCAL, 2 => LIVE Loop>"
source $HOME/.gamerc

ATKDEF_CHEAT_COUNT=5
TYPE=$1

# Local
if [ "${TYPE}" == "1" ]; then
    if [ "$HOST_NAME" == "TEST" -o "$HOST_NAME" == "SUB" -o "$HOST_NAME" == "SUBMIT" -o "$HOST_NAME" == "DATA" -o "$HOST_NAME" == "DLC" -o "$HOST_NAME" == "TIME1" -o "$HOST_NAME" == "MEDIA" ] ; then
        ENVIRONMENT="TEST"
    else
        ENVIRONMENT="LIVE"
    fi
    DBNAME=$(psql -U postgres -c "COPY (SELECT datname FROM pg_database WHERE datname ~ '^${GAME_DB_NAME}[0-9]+$') TO STDOUT;")
    for DB in ${DBNAME}; do
        SET=$(echo ${DB}|sed 's/N1DB//')
        COUNT=$(psql -U postgres ${DB} -c "COPY (SELECT COUNT(*) FROM player_cheat WHERE atkdef_cheat_count >= ${ATKDEF_CHEAT_COUNT} AND char_id IN (SELECT id FROM player_characters WHERE (flag & 64) = 0)) TO STDOUT;")
        echo "${DB} => ${SET} ( ${COUNT} )"
        if [ ! -z ${COUNT} ] && [ ${COUNT} -gt 0 ]; then
            send_chatbot_text_only_by_thread_warning "[監控] ATKDEF Cheat Count" "[$ENVIRONMENT][$HOST_NAME] SET${SET} ATKDEF Cheat Count 大於等於 ${ATKDEF_CHEAT_COUNT} 的 UnCheat 角色數量 ( ${COUNT} )" hour 1
        fi
    done
# LIVE all loops
elif [ "${TYPE}" == "2" ]; then
    sendscript allms <<< "$HOME/bin/atkdef_cheat_count_monitor 1"
fi

