#!/usr/bin/perl -w

# squad_payout.pl
# (c) CWZ, LLC

# Squad and upgrade payouts

use strict;
use lib '.';
use List::Util qw(min max);
use CWZ;



Exec: {
	print "squad_payout.pl\n";
	my $d = (localtime())[4];

	# Probability tables for the RNG payouts
	my @rng_newb = qw( 7 6 6 5 5 5 4 4 4 3 3 2 );
	my @rng_hodler = qw( 5 4 4 3 3 3 2 2 1 );
	my @rng_whale = qw( 2 1 1 1 );
	my @rng_f_lo = qw( 2 1 1 0.8 0.6 0.5 0.4 0.2 0.1 );
	my @rng_f_mid = qw( 1 0.8 0.6 0.5 0.4 0.4 0.2 0.1 );
	my @rng_f_hi = qw( 0.8 0.6 0.5 0.4 0.2 0.2 0.1 );

	# Handle node/minefac/ad/rally payouts per location
	foreach my $loc_cd ( 1..9 ) {
		# New RNG multiplier for each location
		# ..use the probability tables
		my %mult = ( node => $Conf{NODE_PAYOUT},
				newb => $rng_newb[rand @rng_newb], hodler => $rng_hodler[rand @rng_hodler], whale => $rng_whale[rand @rng_whale],
				youtuber => $rng_f_lo[rand @rng_f_lo], redditor => $rng_f_mid[rand @rng_f_mid], anon => $rng_f_hi[rand @rng_f_hi],
				karen => $rng_f_lo[rand @rng_f_lo], reporter => $rng_f_mid[rand @rng_f_mid], politician => $rng_f_hi[rand @rng_f_hi] );

		# Modify multipliers based on some round conditions
		$mult{node} += min($d - 10, 10) * ($Conf{NODE_PAYOUT} / 10) if $d  > 10;	# Increase payout by 10% between days 10 and 20
		$mult{f_newb} *= 2 if $Conf{EOR_ACTIVE};			# ..
		$mult{f_hodler} *= 2 if $Conf{EOR_ACTIVE};		# Double spits during EOR!
		$mult{f_whale} *= 2 if $Conf{EOR_ACTIVE};			# ..

		# Get all eligible players from this location (not max turns or turfed)
		print "Location $loc_cd: ";
		my $local_plyrs = qDb_ar(
			qq{SELECT pid, sid, turns, payouts, sk_marketing, sk_techanal, sk_intuition, sk_cunning, sk_debate, sk_re, upg_advert, upg_rally, cash, bank,
							f_newb, f_hodler, f_whale,
							f_youtuber, f_redditor, f_anon,
							f_karen, f_reporter, f_politician,
							(SELECT amt FROM Squad_Holding WHERE sid = p.sid AND loc = p.loc AND cd = 1) AS nodes,
							(SELECT amt FROM Squad_Holding WHERE sid = p.sid AND loc = p.loc AND cd = 2) AS minefacs
					FROM Player p
					WHERE loc = ? AND fud_exp < ?}, $loc_cd, time());

					### Add turns to stop payouts - WHERE turns < 5000 AND loc = ? AND fud_exp < ?}, $loc_cd, time());

		# Skip if no eligible players in this location
		print "no players found!\n" unless $local_plyrs;
		next unless $local_plyrs;

		# Loop through all the data and figure out where payouts go
		print "Found $#$local_plyrs players..processing..";
		foreach ( @$local_plyrs ) {
			# Staking Node payout spills to cash if set to personal bank and bank is maxed
			$_->{bank_up} = ($_->{payouts} == 1 && $_->{bank} < $Conf{MAX_BANK}) ? $_->{nodes} * $mult{node} : 0;
			$_->{cash_up} = ($_->{payouts} == 2 || ($_->{payouts} == 1 && $_->{bank} >= $Conf{MAX_BANK})) ? $_->{nodes} * $mult{node} : 0;
			$_->{squad_up} = ($_->{payouts} == 3) ? $_->{nodes} * $mult{node} : 0;

			# Mining Facilities produce Investor followers
			$_->{newb_up} = $_->{f_newb} < $Conf{MAX_NEWB} ? $_->{minefacs} * $mult{newb} : 0;
			$_->{hodler_up} = $_->{f_hodler} < $Conf{MAX_HODLER} ? $_->{minefacs} * $mult{hodler} : 0;
			$_->{whale_up} = $_->{f_whale} < $Conf{MAX_WHALE} ? $_->{minefacs} * $mult{whale} : 0;

			# Advertising upgrades attract Influencer followers
			$_->{youtuber_up} = $_->{f_youtuber} < $Conf{MAX_YOUTUBER} && ($_->{sk_marketing} + $_->{sk_techanal} + $_->{sk_intuition}) > 200 ? $_->{upg_advert} * $mult{youtuber} : 0;
			$_->{redditor_up} = $_->{f_redditor} < $Conf{MAX_REDDITOR} && ($_->{sk_marketing} + $_->{sk_techanal} + $_->{sk_intuition}) > 250 ? $_->{upg_advert} * $mult{redditor} : 0;
			$_->{anon_up} = $_->{f_anon} < $Conf{MAX_ANON} && ($_->{sk_marketing} + $_->{sk_techanal} + $_->{sk_intuition}) > 300 ? $_->{upg_advert} * $mult{anon} : 0;

			# Rally upgrades attract Doubter followers
			$_->{karen_up} = $_->{f_karen} < $Conf{MAX_KAREN} && ($_->{sk_cunning} + $_->{sk_debate} + $_->{sk_re}) > 200 ? $_->{upg_rally} * $mult{karen} : 0;
			$_->{reporter_up} = $_->{f_reporter} < $Conf{MAX_REPORTER} && ($_->{sk_cunning} + $_->{sk_debate} + $_->{sk_re}) > 250 ? $_->{upg_rally} * $mult{reporter} : 0;
			$_->{politician_up} = $_->{f_politician} < $Conf{MAX_POLITICIAN} && ($_->{sk_cunning} + $_->{sk_debate} + $_->{sk_re}) > 300 ? $_->{upg_rally} * $mult{politician} : 0;
		}

		# We have all our data now, so prepare a big UPDATE statement handle to loop
		print "UPDATE Player..";
		my $sth = $DBH->prepare(
			qq{UPDATE Player
					SET cash = cash + ?,	bank = bank + ?, donated = donated + ?,
						f_newb = f_newb + ?, f_hodler = f_hodler + ?, f_whale = f_whale + ?,
						f_youtuber = f_youtuber + ?, f_redditor = f_redditor + ?, f_anon = f_anon + ?,
						f_karen = f_karen + ?, f_reporter = f_reporter + ?, f_politician = f_politician + ?
					WHERE pid = ?});
		$sth->execute($_->{cash_up}, $_->{bank_up}, $_->{squad_up},
				$_->{newb_up}, $_->{hodler_up}, $_->{whale_up},
				$_->{youtuber_up}, $_->{redditor_up}, $_->{anon_up},
				$_->{karen_up}, $_->{reporter_up}, $_->{politician_up},
				$_->{pid}) foreach ( @$local_plyrs );
		$sth->finish();

		# One last loop to catch the payouts to squad bank
		print "UPDATE Squad..";
		$sth = $DBH->prepare("UPDATE Squad SET bank = bank + ? WHERE sid = ?");
		foreach ( @$local_plyrs ) {
			next unless $_->{squad_up};					# Skip if nothing to donate
			$sth->execute($_->{squad_up}, $_->{sid});
		}
		$sth->finish();

		print "done.\n";
	}	# Done looping through locations


	# SHILLS
	# ..first, decrease shills when over whale threshold
	# ..and eliminate shills when not enough crypto
	qDb_do("UPDATE Player SET f_shill = $Conf{MAX_SHILL} WHERE f_shill > $Conf{MAX_SHILL} AND f_whale >= $Conf{MAX_WHALE_4ALT}");
	qDb_do("UPDATE Player SET f_shill = 0 WHERE f_shill > 0 AND market < f_shill * $Conf{SHILL_EAT}");

	# Man this is going to be complicated
	# ..lets just get list of who's eating
	# ..and the current market prices (both game and live)
	my $shill_eaters = qDb_ar("SELECT pid, f_shill FROM Player WHERE f_shill > 0") || [ ( ) ];
	my $crypto_data = qDb_ar("SELECT cd, price_now FROM Crypto_Game UNION SELECT cd, price_now FROM Crypto_Live");

	# Unroll the SQL data into an easier to lookup table for crypto prices
	my @crypto_price;
	$crypto_price[$_->{cd}] = $_->{price_now} foreach ( @$crypto_data );

	# Loop through shill_eaters and see what we need to do
	foreach ( @$shill_eaters ) {
	}



	print "\n\n";
}
