#!/usr/bin/perl -w

# rank_update.pl
# (c) CWZ, LLC

# All Smash Stats Udate

use strict;
use lib '.';
use CWZ;

Exec: {

   #
   # UPDATE Smash Stats with all characters found in squad)
   #
   qDb_do("UPDATE Squad s
LEFT JOIN (
  SELECT sid,
         COALESCE(SUM(whales_smashed), 0) AS total_whales_smashed,
         COALESCE(SUM(shills_smashed), 0) AS total_shills_smashed,
         COALESCE(SUM(def_count), 0) AS total_def_count,
         COALESCE(SUM(loot_stole), 0) AS total_loot_stole,
         COALESCE(SUM(influencers_stole), 0) AS total_influencers_stole,
         COALESCE(SUM(doubters_stole), 0) AS total_doubters_stole,
         COALESCE(SUM(att_won), 0) AS total_att_won,
         COALESCE(SUM(att_lost), 0) AS total_att_lost
  FROM Player
  GROUP BY sid
) p ON s.sid = p.sid
SET s.whales_smashed = p.total_whales_smashed,
    s.shills_smashed = p.total_shills_smashed,
    s.def_count = p.total_def_count,
    s.loot_stole = p.total_loot_stole,
    s.influencers_stole = p.total_influencers_stole,
    s.doubters_stole = p.total_doubters_stole,
    s.att_won = p.total_att_won,
    s.att_lost = p.total_att_lost;");

#Removal of Debuffs
    my $now = time();
      qDb_do("UPDATE Player SET it_by = ''
            				WHERE it_exp < $now");
      qDb_do("UPDATE Player SET a51_by = ''
                    WHERE it_exp < $now");
      qDb_do("UPDATE Player SET fud_by = ''
                    WHERE it_exp < $now");
      qDb_do("UPDATE Player SET bog_by = ''
                    WHERE it_exp < $now");
}
