#!/bin/bash

DBNAME="$1"

###
#成就16004
#完成 : completed_num = 1 && state = 2
#任務 5007
#狀態 3 : misOnGoing    = 3, // 進行

#更新狀態 4 : misOnReward    = 4, // 領獎
###

achievement_id="16004"
mission_id="5007"

# 成就 1 ~ 10
for achievement_table in $(seq 1 10); do
	echo ""
	echo "======== Achievement Table: ${achievement_table} ========"
	echo ""
	echo "SELECT COUNT(player_id) FROM player_characters_achievement${achievement_table} WHERE achievement_id = ${achievement_id} AND completed_num = 1 AND state = 2"
	psql -U postgres ${DBNAME} -c "SELECT COUNT(player_id) FROM player_characters_achievement${achievement_table} WHERE achievement_id = ${achievement_id} AND completed_num = 1 AND state = 2"

	#-- final_mission_state1
	echo ""
	echo "======== FM1 MIDs: ${mission_id}, STATE: 3 ========"
	echo "SELECT COUNT(player_id) FROM player_characters_achievement${achievement_table} WHERE (achievement_id = ${achievement_id} AND completed_num = 1 AND state = 2) AND (player_id IN (SELECT player_id FROM final_mission_state1 WHERE index=(${mission_id}-1)/10 AND state=(3<<(${mission_id}-1)%10*3) | (state&(~(7 << (${mission_id}-1)%10*3)))))"
	psql -U postgres ${DBNAME} -c "SELECT COUNT(player_id) FROM player_characters_achievement${achievement_table} WHERE (achievement_id = ${achievement_id} AND completed_num = 1 AND state = 2) AND (player_id IN (SELECT player_id FROM final_mission_state1 WHERE index=(${mission_id}-1)/10 AND state=(3<<(${mission_id}-1)%10*3) | (state&(~(7 << (${mission_id}-1)%10*3)))))"
	
	echo ""
	echo "WITH t1 AS (SELECT player_id FROM player_characters_achievement${achievement_table} WHERE (achievement_id = ${achievement_id} AND completed_num = 1 AND state = 2) AND (player_id IN (SELECT player_id FROM final_mission_state1 WHERE index=(${mission_id}-1)/10 AND state=(3<<(${mission_id}-1)%10*3) | (state&(~(7 << (${mission_id}-1)%10*3)))))) UPDATE final_mission_state1 SET state=(4<<(${mission_id}-1)%10*3) | (state&(~(7 << (${mission_id}-1)%10*3))) WHERE index=(${mission_id}-1)/10 AND player_id IN (SELECT player_id FROM t1)"
	psql -U postgres ${DBNAME} -c "WITH t1 AS (SELECT player_id FROM player_characters_achievement${achievement_table} WHERE (achievement_id = ${achievement_id} AND completed_num = 1 AND state = 2) AND (player_id IN (SELECT player_id FROM final_mission_state1 WHERE index=(${mission_id}-1)/10 AND state=(3<<(${mission_id}-1)%10*3) | (state&(~(7 << (${mission_id}-1)%10*3)))))) UPDATE final_mission_state1 SET state=(4<<(${mission_id}-1)%10*3) | (state&(~(7 << (${mission_id}-1)%10*3))) WHERE index=(${mission_id}-1)/10 AND player_id IN (SELECT player_id FROM t1)"

	#-- final_mission_state2
	echo ""
	echo "======== FM2 MIDs: ${mission_id}, STATE: 3 ========"
	echo "SELECT COUNT(player_id) FROM player_characters_achievement${achievement_table} WHERE (achievement_id = ${achievement_id} AND completed_num = 1 AND state = 2) AND (player_id IN (SELECT player_id FROM final_mission_state2 WHERE index=(${mission_id}-1)/10 AND state=(3<<(${mission_id}-1)%10*3) | (state&(~(7 << (${mission_id}-1)%10*3)))))"
	psql -U postgres ${DBNAME} -c "SELECT COUNT(player_id) FROM player_characters_achievement${achievement_table} WHERE (achievement_id = ${achievement_id} AND completed_num = 1 AND state = 2) AND (player_id IN (SELECT player_id FROM final_mission_state2 WHERE index=(${mission_id}-1)/10 AND state=(3<<(${mission_id}-1)%10*3) | (state&(~(7 << (${mission_id}-1)%10*3)))))"
	
	echo ""
	echo "WITH t1 AS (SELECT player_id FROM player_characters_achievement${achievement_table} WHERE (achievement_id = ${achievement_id} AND completed_num = 1 AND state = 2) AND (player_id IN (SELECT player_id FROM final_mission_state2 WHERE index=(${mission_id}-1)/10 AND state=(3<<(${mission_id}-1)%10*3) | (state&(~(7 << (${mission_id}-1)%10*3)))))) UPDATE final_mission_state2 SET state=(4<<(${mission_id}-1)%10*3) | (state&(~(7 << (${mission_id}-1)%10*3))) WHERE index=(${mission_id}-1)/10 AND player_id IN (SELECT player_id FROM t1)"
	psql -U postgres ${DBNAME} -c "WITH t1 AS (SELECT player_id FROM player_characters_achievement${achievement_table} WHERE (achievement_id = ${achievement_id} AND completed_num = 1 AND state = 2) AND (player_id IN (SELECT player_id FROM final_mission_state2 WHERE index=(${mission_id}-1)/10 AND state=(3<<(${mission_id}-1)%10*3) | (state&(~(7 << (${mission_id}-1)%10*3)))))) UPDATE final_mission_state2 SET state=(4<<(${mission_id}-1)%10*3) | (state&(~(7 << (${mission_id}-1)%10*3))) WHERE index=(${mission_id}-1)/10 AND player_id IN (SELECT player_id FROM t1)"
done

