#!/bin/bash
#===============================================================================
#
#          FILE: frauder_finder
#
#         USAGE: frauder_finder <IP>
#
#   DESCRIPTION: Check the accounts mail in-game that come from <IP>
#
#       OPTIONS: <IP>
#
#  REQUIREMENTS:
#
#         NOTES:
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Thu Mar  8 18:49:35 KST 2012
#      REVISION: 1.0
#
#          TODO:
#
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
#set -m                                      # Enable job control

source ~/.gamerc

[ "$1" == "" ] && exec echo "Usage: $0 <IP>"

mkdir -p ~/frauder/
cd ~/frauder/

echo "Okay, the IP is $1" | colorize green

rm -f tmp*

echo "Get the Mail RID list from LIVE server ... " | colorize yellow
[ "$(ls GAMEDB* 2> /dev/null|wc -l)" == "$(wc -l <<< "$SET_NUMBER_LIST")" ] \
&& echo "Mail RID list already got" | colorize green \
|| sendscript allgamedb -p -s . <<< 'xgrep -h -v ",G:0," ~/servers*/Log/$(date -d "-1 month" +%Y%m)*/*/Mail.log.* ~/servers*/Log/$(date +%Y%m)*/*/Mail.log.* | awk -F, "{print \$5}"|sed "s/^RID://g"'
echo

echo -n "Get the login data from account database ... " | colorize yellow
ssh accountdb "psql $MEMBER_DB_NAME <<< \"copy (select distinct mid,strcharid,char_id,serverid from game_log where clientip='$1' order by mid) to stdout csv;\"" > data-$1
echo "OK" | colorize green

echo -n "Get the set numbers list from the login data ... " | colorize yellow
for serverid in $(awk -F, '{print $4}' data-$1 |sort|uniq);do 
   set=$(get_set $serverid)
   grep ",$serverid$" data-$1 > tmpdatafile-$set
done
echo "OK" | colorize green

echo -n "Get the character id list from the login data ... " | colorize yellow
for set in $SET_NUMBER_LIST ; do 
   touch tmpdatafile-$set
   awk -F, '{print "^"$3"$"}' tmpdatafile-$set|sort|uniq > tmpcharidlist-$set
done 
echo "OK" | colorize green

echo -n "Get the receiver list ... " | colorize yellow
for set in $SET_NUMBER_LIST ; do 
   echo -n "$set "
   egrep "$(cat tmpcharidlist-$set|tr '\n' '|'|sed 's/|$//g')" GAMEDB$set|sort|uniq -c|sort -gr > tmpreceivers-$set
done
echo "ALL DONE" | colorize green

echo

for set in $SET_NUMBER_LIST ; do
   echo -e " Count | CharacterID\n--------------------" > tmpreceivers-top-10-$set
   cat tmpreceivers-$set | head -10 >> tmpreceivers-top-10-$set

   echo "select id,given_name,gold,account_name from player_characters where $(for char_id in $(cat tmpreceivers-$set|awk '{print $2}');do echo -n " id = $char_id or " ;done|sed 's/ or $//g') order  by gold desc limit 10;"|ssh gamedb$set "source ~/.gamerc;game_db 2> /dev/null" |grep -v " rows" > frauder-top-10-$set
done 

echo "That's the cash box:" | colorize green
for set in $SET_NUMBER_LIST ; do 
   echo "Set $set($(cat tmpcharidlist-$set|sort|uniq|wc -l|colorize cyan) Characters from one IP):"
   paste tmpreceivers-top-10-$set frauder-top-10-$set | \
   highlight - $(head -10 tmpreceivers-$set | awk '{print $2}')
   echo
done

echo "^^^^^^^^^^^^^^^^^^^^    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Gold transfer count     The characters who got most of gold(current)
(Mail receive only)
"


