#!/usr/bin/perl

use Template;
use CGI::Lite;
use FCGI;
use Compress::Zlib;

require "modules/globals.pm";
require "modules/sqldb.pm";
require "modules/tt.pm";

my $ret_template = tt::init($globals::fs_dir.'/templates');
my $tt = $ret_template->{template};
my $dbh = sqldb::init($globals'mysql->{database},$globals'mysql->{interface},$globals'mysql->{user}, $globals'mysql->{password});
my $cgi = CGI::Lite->new();


my $cached_data;

my $request = FCGI::Request();

#$main::get_maps->execute();
#my $maps = $main::get_maps->fetchall_hashref('map_id');

#$main::get_maps_zones->execute();
#my $maps_zones = $main::get_maps_zones->fetchall_hashref('col_0');

#$main::get_zones->execute();
#my $zones = $main::get_zones->fetchall_hashref('zone_id');
#$main::get_zones->finish();

$main::get_primary_skills->execute();
my $primary_skills = $main::get_primary_skills->fetchall_hashref('skill_id');
$main::get_primary_skills->finish();

warn "!";

while($request->Accept() >= 0) {

   $dbh = &sqldb::check($dbh);
   if ($dbh eq 0) {
      warn "sql connection problem";
      print "Location: /static.html\n\n";
      next;

   } else {

warn "2!";
      my ($html,$page,$template);
      my $hf = 0;
      my $q = $cgi->parse_new_form_data();

      if ($q->{page} eq "") {$q->{page} = "init";}

      if (!defined($q->{lang_id}) || $q->{lang_id} !~ /^\d+$/) {
         $q->{lang_id} = 1;
      }
      if (!defined($q->{server_id}) || $q->{server_id} !~ /^\d+$/) {
         $q->{server_id} = 100;
      }

      if (!defined($q->{page_id}) || $q->{page_id} !~ /^\d+$/) {
         $q->{page_id} = 6;
      }

      my $usegzip;
      foreach my $enc ( split( /\s*,\s*/, $ENV{HTTP_ACCEPT_ENCODING} )) {
         $enc =~ s/;.*$//s;
         $usegzip = $enc if ( $enc =~ /^(x-)?gzip$/ );
      }
                                    

      my $template = $tt->{$q->{lang_id}};

      my $vars = {
		query => $q, 
      };

      if ($q->{page} eq 'init') {
warn "3!";
         $hf = 1;
         $page = 'init';

         $vars->{servers} = $main::servers;

         my ($total,$counts,$ra,$cl,$fraction,$online_total,$online_counts,$online_ra,$online_cl,$online_fraction,$tmp_levels,$levels,$lera_counts,$lera_percents);

         if ($q->{page_id} == 6) {
            $vars->{include} = 'all_races_classes';
            $vars->{title} = 1;

            my ($total,$counts,$ra,$cl,$fraction);

warn "3.1";
#            $main::get_players->execute($q->{server_id});
#            foreach my $player (@{$main::get_players->fetchall_arrayref({})}) {
#               ++$total->{$q->{server_id}};
#               ++$counts->{$q->{server_id}}->{$player->{race}}->{$player->{class}};
#               ++$ra->{$q->{server_id}}->{$player->{race}};
#               ++$cl->{$q->{server_id}}->{$player->{class}};
#               if ($player->{race} == 1 || $player->{race} == 3 || $player->{race} == 4 || $player->{race} == 7 || $player->{race} == 11) {
#                  ++$fraction->{$q->{server_id}}->{a};
#               } else {
#                  ++$fraction->{$q->{server_id}}->{h};
#               }
#            }
warn "3.2";
            $vars->{total} = $total->{$q->{server_id}};
            $vars->{ra} = $ra->{$q->{server_id}};
            $vars->{cl} = $cl->{$q->{server_id}};
            $vars->{counts} = $counts->{$q->{server_id}};
            $vars->{fraction} = $fraction->{$q->{server_id}};

         } elsif ($q->{page_id} == 7) {
            $vars->{include} = 'online_races_classes';
            $vars->{title} = 2;

            my ($online_total,$online_counts,$online_ra,$online_cl,$online_fraction);

#            $main::get_online_players->execute($q->{server_id});
#            foreach my $player (@{$main::get_online_players->fetchall_arrayref({})}) {
#               ++$online_total->{$q->{server_id}};
#               ++$online_counts->{$q->{server_id}}->{$player->{race}}->{$player->{class}};
#               ++$online_ra->{$q->{server_id}}->{$player->{race}};
#               ++$online_cl->{$q->{server_id}}->{$player->{class}};
#               if ($player->{race} == 1 || $player->{race} == 3 || $player->{race} == 4 || $player->{race} == 7) {
#                  ++$online_fraction->{$q->{server_id}}->{a};
#               } else {
#                  ++$online_fraction->{$q->{server_id}}->{h};
#               }
#            }

            $vars->{total} = $online_total->{$q->{server_id}};
            $vars->{ra} = $online_ra->{$q->{server_id}};
            $vars->{cl} = $online_cl->{$q->{server_id}};
            $vars->{counts} = $online_counts->{$q->{server_id}};
            $vars->{fraction} = $online_fraction->{$q->{server_id}};

         } elsif ($q->{page_id} == 8) {
            $vars->{include} = 'all_level_race';
            $vars->{title} = 3;

            my ($lera_counts,$lera,$tmp_levels,$levels,$lera_percents);

            $main::get_players->execute($q->{server_id});
            my $all_players = $main::get_players->fetchall_arrayref({});
            my $num_all_players = scalar(@{$all_players});
            foreach my $player (@{$all_players}) {
               ++$lera_counts->{$q->{server_id}}->{$player->{level}}->{$player->{race}};
               ++$lera->{$q->{server_id}}->{$player->{level}};
               if ($tmp_levels->{$player->{level}} != 1) {
                  $tmp_levels->{$player->{level}} = 1;
                  push @{$levels->{$q->{server_id}}}, $player->{level};
               }
            }

            foreach my $level (@{$levels->{$q->{server_id}}}) {
               if ($lera->{$q->{server_id}}->{$level} > 0) {
                  $lera_percents->{$q->{server_id}}->{$level} = sprintf("%.2f", ($lera->{$q->{server_id}}->{$level}/$num_all_players)*100);
               }
            }

            $vars->{lera} = $lera->{$q->{server_id}};
            $vars->{lera_counts} = $lera_counts->{$q->{server_id}};
            $vars->{lera_percents} = $lera_percents->{$q->{server_id}};
            $vars->{levels} = $levels->{$q->{server_id}};

         } elsif ($q->{page_id} == 9) {
            $vars->{include} = 'online_level_race';
            $vars->{title} = 4;

            my ($lera_counts,$lera,$tmp_levels,$levels,$lera_percents);

            $main::get_online_players->execute($q->{server_id});
            my $online_players = $main::get_online_players->fetchall_arrayref({});
            foreach my $player (@{$online_players}) {
               ++$lera_counts->{$q->{server_id}}->{$player->{level}}->{$player->{race}};
               ++$lera->{$q->{server_id}}->{$player->{level}};
               if ($tmp_levels->{$player->{level}} != 1) {
                  $tmp_levels->{$player->{level}} = 1;
                  push @{$levels->{$q->{server_id}}}, $player->{level};
               }
            }

            $vars->{lera} = $lera->{$q->{server_id}};
            $vars->{lera_counts} = $lera_counts->{$q->{server_id}};
            $vars->{lera_percents} = $lera_percents->{$q->{server_id}};
            $vars->{levels} = $levels->{$q->{server_id}};



         } elsif ($q->{page_id} == 13) {
            $vars->{include} = 'all_level_class';
            $vars->{title} = 5;

            my ($lecl_counts,$lecl,$tmp_levels,$levels,$lecl_percents,$lecl_total);

            $main::get_players->execute($q->{server_id});
            my $all_players = $main::get_players->fetchall_arrayref({});
            my $num_all_players = scalar(@{$all_players});
            foreach my $player (@{$all_players}) {
               ++$lecl_counts->{$player->{level}}->{$player->{class}};
               ++$lecl_total->{$player->{class}};
               ++$lecl->{$player->{level}};
               if ($tmp_levels->{$player->{level}} != 1) {
                  $tmp_levels->{$player->{level}} = 1;
                  push @{$levels}, $player->{level};
               }
            }

            foreach my $level (@{$levels}) {
               if ($lecl->{$level} > 0) {
                  $lecl_percents->{$level} = sprintf("%.2f", ($lecl->{$level}/$num_all_players)*100);
               }
            }

            $vars->{lecl} = $lecl;
            $vars->{lecl_counts} = $lecl_counts;
            $vars->{lecl_percents} = $lecl_percents;
            $vars->{levels} = $levels;
            $vars->{lecl_total} = $lecl_total;
            $vars->{total_players} = scalar(@{$all_players});

         } elsif ($q->{page_id} == 14) {
            $vars->{include} = 'online_level_class';
            $vars->{title} = 6;

            my ($lecl_counts,$lecl,$tmp_levels,$levels,$lecl_percents);

            $main::get_online_players->execute($q->{server_id});
            my $online_players = $main::get_online_players->fetchall_arrayref({});
            foreach my $player (@{$online_players}) {
               ++$lecl_counts->{$q->{server_id}}->{$player->{level}}->{$player->{class}};
               ++$lecl->{$q->{server_id}}->{$player->{level}};
               if ($tmp_levels->{$player->{level}} != 1) {
                  $tmp_levels->{$player->{level}} = 1;
                  push @{$levels->{$q->{server_id}}}, $player->{level};
               }
            }

            $vars->{lecl} = $lecl->{$q->{server_id}};
            $vars->{lecl_counts} = $lecl_counts->{$q->{server_id}};
            $vars->{lecl_percents} = $lecl_percents->{$q->{server_id}};
            $vars->{levels} = $levels->{$q->{server_id}};

         } elsif ($q->{page_id} == 10) {
            $vars->{include} = 'online_map_zone';
            $vars->{title} = 7;

            $main::get_online_players->execute($q->{server_id});
            my $online_players = $main::get_online_players->fetchall_arrayref({});

            my ($num_map_zone_continent,$num_map_zone_instance,$players_map_zone,$players_instances,$tmp_map_zone,$tmp_map,$continent_zones,$continents,$instances);

            foreach my $rec (@{$online_players}) {
               if ($rec->{map} == 609) {$rec->{map} = 0;}
               if ($rec->{map} == 0 || $rec->{map} == 1 || $rec->{map} == 530 || $rec->{map} == 571) {
                  if (!defined($tmp_map_zone->{$rec->{map}}->{$rec->{zone}})) {
                     $tmp_map_zone->{$rec->{map}}->{$rec->{zone}} = 1;
                     push @{$continent_zones->{$rec->{map}}},$rec->{zone};
                  }
                  if (!defined($tmp_map->{$rec->{map}})) {
                     $tmp_map->{$rec->{map}} = 1;
                     push @{$continents},$rec->{map};
                  }
                  ++$num_map_zone_continent->{$rec->{map}}->{$rec->{zone}};
                  push @{$players_map_zone->{$rec->{map}}->{$rec->{zone}}}, $rec;
               } else {
                  if (!defined($tmp_map->{$rec->{zone}})) {
                     $tmp_map->{$rec->{zone}} = 1;
                     push @{$instances},$rec->{zone};
                  }
                  ++$num_map_zone_instance->{$rec->{zone}};
                  push @{$players_instances->{$rec->{zone}}}, $rec;
               }
            }


            $vars->{num_map_zone_continent} = $num_map_zone_continent;
            $vars->{num_map_zone_instance} = $num_map_zone_instance;
            $vars->{players_map_zone} = $players_map_zone;
            $vars->{players_instances} = $players_instances;
            $vars->{continent_zones} = $continent_zones;
            $vars->{continents} = $continents;
            $vars->{instances} = $instances;
#            $vars->{maps} = $maps;
#            $vars->{zones} = $zones;
            $vars->{maps_zones} = $maps_zones;
            $vars->{online_players} = $online_players;







         } elsif ($q->{page_id} == 11) {
            $vars->{include} = 'guilds';
            $vars->{title} = 8;


            $main::get_guilds->execute($q->{server_id});
            my $guilds;
            foreach (@{$main::get_guilds->fetchall_arrayref({})}) {
               push @{$guilds->{$_->{incline}}}, $_;
            }

            $vars->{guilds} = $guilds;


            $main::get_guilded_players->execute($q->{server_id});
            my $num = $main::get_guilded_players->fetchall_hashref('guild_id');
            $main::get_guilded_players->finish();

            $vars->{num} = $num;

         } elsif ($q->{page_id} == 12) {
            $vars->{include} = 'honor';
            $vars->{title} = 9;


            $main::get_honor->execute($q->{server_id});
            my $honor = $main::get_honor->fetchall_arrayref({});

            my ($alliance,$horde,$pom);

            foreach (@{$honor}) {
               if ($player->{race} == 1 || $player->{race} == 3 || $player->{race} == 4 || $player->{race} == 7 || $player->{race} == 11) {
                  $alliance->{$pom} = $_;
               } else {
                  $horde->{$pom} = $_;
               }
               ++$pom;
            }

            $vars->{honor} = $honor;
            $vars->{alliance} = $alliance;
            $vars->{horde} = $horde;


         } elsif ($q->{page_id} == 15) {
            $vars->{include} = 'graph';
            $vars->{type} = 'hvsa-hday';
            $vars->{title} = 10;

         } elsif ($q->{page_id} == 16) {
            $vars->{include} = 'graph';
            $vars->{type} = 'races-hday';
            $vars->{title} = 11;

         } elsif ($q->{page_id} == 17) {
            $vars->{include} = 'graph';
            $vars->{type} = 'classes-hday';
            $vars->{title} = 12;

         } elsif ($q->{page_id} == 18) {
            $vars->{include} = 'records';
            $vars->{title} = 13;

            $main::get_server_records->execute($q->{server_id});
            my $records = $main::get_server_records->fetchrow_hashref();
            $records->{max_uptime_datestamp} = globals::ut2d($records->{max_uptime});
            $records->{max_downtime_datestamp} = globals::ut2d($records->{max_downtime});

            $vars->{records} = $records;
            $main::get_server_records->finish();



         } elsif ($q->{page_id} == 19) {
            $vars->{include} = 'items_level';
            $vars->{title} = 14;

            $main::get_items_level->execute($q->{server_id},1);
            my $players = $main::get_items_level->fetchall_arrayref({});
            $main::get_items_level->finish();

            $vars->{players} = $players;


         } elsif ($q->{page_id} == 20) {
            $vars->{include} = 'all_tradeskills';
            $vars->{title} = 15;

            my ($ts,$total_ts);

            $main::get_tradeskill_players->execute($q->{server_id});
            foreach my $trade (@{$main::get_tradeskill_players->fetchall_arrayref({})}) {
               if ($trade->{race} == 1 || $trade->{race} == 3 || $trade->{race} == 4 || $trade->{race} == 7 || $trade->{race} == 11) {
                  ++$ts->{$trade->{max}}->{$trade->{skill_id}}->{a};
                  ++$total_ts->{$trade->{max}}->{a};
               } else {
                  ++$ts->{$trade->{max}}->{$trade->{skill_id}}->{h};
                  ++$total_ts->{$trade->{max}}->{h};
               }
            }
            $main::get_tradeskill_players->finish();
            $vars->{ts} = $ts;
            $vars->{primary_skills} = $primary_skills;
            $vars->{total_ts} = $total_ts;

         } elsif ($q->{page_id} == 21) {
            $vars->{include} = 'online_tradeskills';
            $vars->{title} = 16;

            my ($ts,$total_ts);

            $main::get_tradeskill_online_players->execute($q->{server_id});
            foreach my $trade (@{$main::get_tradeskill_online_players->fetchall_arrayref({})}) {
               if ($trade->{race} == 1 || $trade->{race} == 3 || $trade->{race} == 4 || $trade->{race} == 7 || $trade->{race} == 11) {
                  ++$ts->{$trade->{max}}->{$trade->{skill_id}}->{a};
                  ++$total_ts->{$trade->{max}}->{a};
               } else {
                  ++$ts->{$trade->{max}}->{$trade->{skill_id}}->{h};
                  ++$total_ts->{$trade->{max}}->{h};
               }
            }
            $main::get_tradeskill_online_players->finish();
            $vars->{ts} = $ts;
            $vars->{primary_skills} = $primary_skills;
            $vars->{total_ts} = $total_ts;

         } elsif ($q->{page_id} == 22) {
            $vars->{include} = 'guild_members';
            $vars->{title} = 17;
                                 
                                 

            $main::get_guild_members->execute($q->{guild_id});
            my $members = $main::get_guild_members->fetchall_arrayref({});
            $main::get_guild_members->finish();
            
            my $rank;
            $main::get_guild_ranks->execute($q->{guild_id});
            foreach (@{$main::get_guild_ranks->fetchall_arrayref({})}) {
               $rank->{$_->{rid}} = $_->{rname};
            }
            $main::get_guild_ranks->finish();
            
            $main::get_guild->execute($q->{guild_id});
            my $guild = $main::get_guild->fetchrow_hashref();
            $main::get_guild->finish();
            
            $vars->{guild} = $guild;
            $vars->{members} = $members;
            $vars->{rank} = $rank;
         }


warn "3a!";


      } elsif ($q->{page} eq 'stat') {
warn "4!";
         $hf = 0;
         $page = 'stat';

         $main::get_stats->execute($q->{server_id});
         $vars->{stats} = $main::get_stats->fetchrow_hashref();
         $main::get_stats->finish();

      } elsif ($q->{page} eq 'stats') {
warn "4!";
         $hf = 0;
         $page = 'stats';

         $main::get_stats->execute($q->{server_id});
         $vars->{stats} = $main::get_stats->fetchrow_hashref();
         $main::get_stats->finish();

      }

warn "5!";

      if (-f "templates/".$page.".tt2" || -f "templates/".$temp_lang."/".$page.".tt2") {
         if ($usegzip) {print 'Content-Encoding: '.$usegzip."\n";}
         print 'Content-type: text/html'."\n\n";
         if ($hf == 1) {$template->process('header.tt2', $vars, \$html) || warn $template->error();}
         $template->process($page.'.tt2', $vars, \$html) || warn $template->error();
         if ($hf == 1) {$template->process('footer.tt2', $vars, \$html) || warn $template->error();}
         if ($usegzip) {$html = Compress::Zlib::memGzip( $html );}
         print $html;
         $html = '';
      } else {
         print 'Content-type: text/html'."\n\n";
         print "bububu";
      }
   }
}

