package com.dwarfholm.activitystats.braizhauler;

import java.util.HashMap;

import org.bukkit.entity.Player;


public class ASData {
	private HashMap <String, ASPlayer> playerlist;
	private ASDatabase database;
	private ActivityStats plugin;
	
	public ASData(ActivityStats plugin)	{
		this.plugin = plugin;
		playerlist = new HashMap<String, ASPlayer>();
		database = new ASDatabase(plugin);
	}
	
	public void saveAll()	{
		for (ASPlayer player:playerlist.values())
			database.updatePlayer(player);
	}
	
	public void createDatabase() {
		database.createTables();
	}
	
	public void recordOnline() {
		if (playerlist!=null && playerlist.size() > 0)	{
			for (ASPlayer player:playerlist.values())
				if ( plugin.getServer().getPlayer(player.getName()).isOnline() )
					player.curPeriod.addOnline();
			if( plugin.PeriodRolloverDue())	{
				plugin.info("Paying all Players");
				for (ASPlayer player:playerlist.values())	{
					plugin.info(player.getName());
					plugin.payPlayer(player);
				}
				if( plugin.DayRolloverDue())
					rolloverDay();
				if( plugin.WeekRolloverDue())
					rolloverWeek();
				if( plugin.MonthRolloverDue())
					rolloverMonth();
				rolloverPeriod();
				Player pPlayer;
				for (ASPlayer player:playerlist.values())	{
					pPlayer = plugin.getServer().getPlayer(player.getName());
					if ( pPlayer.isOnline() )
						plugin.autoPromoterCheck(pPlayer);
					else
						playerlist.remove(player.getName());
				}
						
				saveAll();
			}
		}
	}
	

	
	public void loadPlayer(String player)	{
		database.loadPlayer(player);
	}
	

	public void savePlayer(String name) {
		savePlayer(playerlist.get(name));
	}
	
	public void savePlayer(ASPlayer player) {
		database.updatePlayer(player);
	}
	
	
	public void setPlayer(ASPlayer player)	{
		playerlist.put(player.getName(), player);
	}
	

	
	public ASPlayer getPlayer(String name)	{
		return playerlist.get(name);
	}
	
	public void rolloverPeriod()	{
		for(String player: playerlist.keySet())	{
			playerlist.get(player).rolloverPeriod();
		}
		plugin.rolledoverPeriod();
	}
	public void rolloverDay()	{
		for(String player: playerlist.keySet())
			playerlist.get(player).rolloverDay();
		plugin.rolledoverDay();
	}
	public void rolloverWeek()	{
		for(String player: playerlist.keySet())
			playerlist.get(player).rolloverWeek();
		plugin.rolledoverWeek();
	}
	public void rolloverMonth()	{
		for(String player: playerlist.keySet())
			playerlist.get(player).rolloverMonth();
		plugin.rolledoverMonth();
	}

	public void loadOnlinePlayers() {
		for (Player player: plugin.getServer().getOnlinePlayers() )
			loadPlayer(player.getName());
	}





}

@@ -593,16 +593,60 @@
 ine() %7B%0A
+%09%09plugin.info(%22recordOnline fired%22);//DEBUG%0A
 %09%09if (pl
@@ -688,16 +688,70 @@
  %3E 0)%09%7B%0A
+%09%09%09plugin.info(%22recordOnline has playerList%22);//DEBUG%0A
 %09%09%09for (
@@ -852,24 +852,24 @@
 sOnline() )%0A
-
 %09%09%09%09%09player.
@@ -891,16 +891,78 @@
 line();%0A
+%09%09%09plugin.info(%22recordOnline looking for rollovers%22); //DEBUG%0A
 %09%09%09if( p
@@ -1588,24 +1588,31 @@
 .getName());
+//DEBUG
 %0A%09%09%09%09%7D%0A%09%09%09%09%09
@@ -1632,16 +1632,159 @@
 ();%0A%09%09%09%7D
+ else %7B%0A%09%09%09%09plugin.info(%22recordOnline not rolling over%22);//DEBUG%0A%09%09%09%7D%0A%09%09%7D else%09%7B%0A%09%09%09plugin.info(%22recordOnline failed due to empty playerlist%22);
 %0A%09%09%7D%0A%09%7D%0A

Debug tags for recordOnline