#!/bin/bash
#source $HOME/.gamerc
source /home/ffdev/.gamerc

[ "$1" == "" ] && exec echo "Usage: $0 <Environment (TEST => 0, LIVE => 1)>"

env=$1
output_filename="/tmp/fb_probability_monitor.log"
stop_check=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (SELECT status FROM xl_system_table_check WHERE name = 'fortune_bag') TO STDOUT;")

if [ "$stop_check" == "1" ]; then
echo "Checking Fortune Bag ..."

# 一般福袋
COUNT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH tb AS (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag WHERE set NOT IN (98, 101) GROUP BY id, set HAVING COUNT(set) > 1 ORDER BY id, set) SELECT id, set, SUM(probability) FROM fortune_bag WHERE (id, set) IN (SELECT id, set FROM t1 ORDER BY id, set) GROUP BY id, set HAVING SUM(probability)::integer <> 100 ORDER BY id, set) SELECT COUNT(*) FROM tb) TO STDOUT")

if [ "${COUNT}" -ne 0 ]; then
    OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag WHERE set NOT IN (98, 101) GROUP BY id, set HAVING COUNT(set) > 1 ORDER BY id, set) SELECT id, set, SUM(probability) FROM fortune_bag WHERE (id, set) IN (SELECT id, set FROM t1 ORDER BY id, set) GROUP BY id, set HAVING SUM(probability)::integer <> 100 ORDER BY id, set) TO '${output_filename}' CSV HEADER")

    if [ "${env}" == "0" ]; then
        ENV_TAG="(測試)"
    elif [ "${env}" == "1" ]; then
        ENV_TAG="(正式)"
    fi
    send_messages ERROR fb_probability "${ENV_TAG} 福袋表道具 (一般) 設定機率異常"
fi

# 特殊福袋1
COUNT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH tb AS (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag WHERE set = 98 GROUP BY id, set HAVING COUNT(set) > 1 ORDER BY id, set) SELECT id, set, SUM(probability) FROM fortune_bag WHERE (id, set) IN (SELECT id, set FROM t1 ORDER BY id, set) GROUP BY id, set HAVING SUM(probability)::integer >= 100 ORDER BY id, set) SELECT COUNT(*) FROM tb) TO STDOUT")

if [ "${COUNT}" -ne 0 ]; then
    OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag WHERE set = 98 GROUP BY id, set HAVING COUNT(set) > 1 ORDER BY id, set) SELECT id, set, SUM(probability) FROM fortune_bag WHERE (id, set) IN (SELECT id, set FROM t1 ORDER BY id, set) GROUP BY id, set HAVING SUM(probability)::integer >= 100 ORDER BY id, set) TO '${output_filename}' CSV HEADER")

    if [ "${env}" == "0" ]; then
        ENV_TAG="(測試)"
    elif [ "${env}" == "1" ]; then
        ENV_TAG="(正式)"
    fi
    send_messages ERROR fb_probability "${ENV_TAG} 福袋表道具 (一般非必得) 設定機率異常"
fi

# 特殊福袋2
COUNT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH tb AS (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag WHERE set = 101 GROUP BY id, set HAVING COUNT(set) > 1 ORDER BY id, set) SELECT id, set, SUM(probability) FROM fortune_bag WHERE (id, set) IN (SELECT id, set FROM t1 ORDER BY id, set) GROUP BY id, set HAVING SUM(probability)::integer % 100 <> 0 ORDER BY id, set) SELECT COUNT(*) FROM tb) TO STDOUT")

if [ "${COUNT}" -ne 0 ]; then
    OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag WHERE set = 101 GROUP BY id, set HAVING COUNT(set) > 1 ORDER BY id, set) SELECT id, set, SUM(probability) FROM fortune_bag WHERE (id, set) IN (SELECT id, set FROM t1 ORDER BY id, set) GROUP BY id, set HAVING SUM(probability)::integer % 100 <> 0 ORDER BY id, set) TO '${output_filename}' CSV HEADER")

    if [ "${env}" == "0" ]; then
        ENV_TAG="(測試)"
    elif [ "${env}" == "1" ]; then
        ENV_TAG="(正式)"
    fi
    send_messages ERROR fb_probability "${ENV_TAG} 福袋表道具 (自選) 設定機率異常"
fi

## 新增規則1
COUNT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag GROUP BY id, set HAVING COUNT(set) >= 1), t2 AS (SELECT id, SUM(CASE WHEN set <= 100 THEN 1 ELSE 0 END) AS f1, SUM(CASE WHEN set > 100 THEN 1 ELSE 0 END) AS f3 FROM t1 GROUP BY id) SELECT COUNT(id) FROM t2 WHERE f1 > 0 AND f3 > 0) TO STDOUT")

if [ "${COUNT}" -ne 0 ]; then
    OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag GROUP BY id, set HAVING COUNT(set) >= 1), t2 AS (SELECT id, SUM(CASE WHEN set <= 100 THEN 1 ELSE 0 END) AS f1, SUM(CASE WHEN set > 100 THEN 1 ELSE 0 END) AS f3 FROM t1 GROUP BY id) SELECT * FROM t1 WHERE id IN (SELECT id FROM t2 WHERE f1 > 0 AND f3 > 0)) TO '${output_filename}' CSV HEADER")

    if [ "${env}" == "0" ]; then
        ENV_TAG="(測試)"
    elif [ "${env}" == "1" ]; then
        ENV_TAG="(正式)"
    fi
    send_messages ERROR fb_probability "${ENV_TAG} 福袋表道具 一般 <=> 自選 衝突"
fi

fi
