//@unloadnpc Online Gaming Time
//@loadnpc npc/thor/playtime.txt


office,118,32,6	script	Online Gaming Time	4_F_KAFRA7,{
	mes "^3399FF[ Online Time Reward ]^000000";
	cutin "kafra_07",2;
	.@tick = 0;
	query_sql "SELECT `total_sec` FROM `onlinetime` WHERE `account_id` = "+getcharid(CHAR_ID_ACCOUNT), .@tick;
	.@seconds = (.@tick % 60);
	.@minutes = (.@tick % 3600) / 60;
	.@hours = (.@tick % 86400) / 3600;
	.@days = (.@tick % (86400 * 30)) / 86400;


	mes "Online Time:";
	mes  "[ "+.@days+" day "+.@hours+" hour "+.@minutes+" min "+.@seconds+" sec ]";
        @tick = .@tick/3600;
	mes "Available ^ff0000Online Points^000000: ^ff0000"+#PCTIME+"^000000 points";
	mes "Stay online and accumulate points.";
	next;
	switch(select( "[ Open Shop ]","[ Convert to Points ]","[ Close ]")) {

	case 1:
		cutin "",255;
		close2;
		openshop();							
		end;

	case 2:
		mes "^3399FF[ Online Time Reward ]^000000";
        	mes .@npcname$; 
         	if (!@tick) { mes "You must have atleast 1 hour time to proceed."; close; }
		@thistick = 3600*@tick;
	 	#PCTIME += @tick;
		mes "You successfully converted "+@tick+" hours to points";
		query_sql "UPDATE `onlinetime` SET `total_sec` =  `total_sec` - "+@thistick+" WHERE `account_id` = "+getcharid(CHAR_ID_ACCOUNT);
		.@tick = 0;
      		@tick = 0;
        	@thistick = 0;
		cutin "",255;
		close;
	default:
		cutin "",255;
		close;

	}
	close2;
	cutin "",255;
	end;


OnPCLoginEvent:
	if (getgroupid() == 4) end;
	.@bool = 0;
	query_sql "SELECT `account_id` FROM `onlinetime` WHERE `account_id` = "+getcharid(CHAR_ID_ACCOUNT), .@bool;
	if( .@bool )
		query_sql "UPDATE `onlinetime` SET `start_time` = NOW() WHERE `account_id` = "+getcharid(CHAR_ID_ACCOUNT);
	else
		query_sql "INSERT INTO `onlinetime` VALUES("+getcharid(CHAR_ID_ACCOUNT)+", NOW(), 0)";
	end;



OnPCLogoutEvent:
	if (getgroupid() == 4) end;
	if(checkvending() >= 1) end;
	query_sql "UPDATE `onlinetime` SET `total_sec` = `total_sec` + TIMESTAMPDIFF(SECOND, `start_time`,  NOW()), `start_time` = NOW() WHERE `account_id` = "+getcharid(CHAR_ID_ACCOUNT);
	end;
OnInit:
	initnpctimer;
	//waitingroom "Online Bonus",0;
	set .npcname$,"^3399FF[ Online Time Reward ]^000000";


	tradertype(NST_CUSTOM);
	sellitem 7338,5;
	sellitem 14581,3;
	sellitem 14527,3;
	sellitem 30248,50;
	sellitem 5476,300;
	sellitem 18639,300;
	sellitem 13551,30;
	sellitem 12063,2;
	sellitem 12048,2;
	sellitem 12043,2;
	sellitem 12053,2;
	sellitem 12058,2;
	sellitem 12068,2;
	sellitem 14233,10;
	sellitem 12909,100;
	sellitem 13517,10;
	sellitem 30009,1;
	sellitem 30010,1;
	sellitem 30000,50;
	sellitem 5336,24;
	sellitem 5337,24;
	sellitem 5338,24;
	sellitem 5339,24;
	end;

OnCountFunds:
	setcurrency(#PCTIME);
	end;

OnPayFunds:
	if((#PCTIME) < @points) end;
	set #PCTIME,#PCTIME-@price;
	purchaseok();
	end;

OnTimer1800000:
	query_sql( "SELECT COUNT(`char_id`) FROM `char` WHERE `online` = 1 ", .@total );
	while( .@count < .@total ){
		query_sql( "SELECT `account_id`,`char_id`,`name` FROM `char` WHERE `online` = 1 ORDER BY `char_id` LIMIT 128 OFFSET "+.@offset, .@aid,.@cid,.@name$ );
		set .@i,0;
		set .@size,getarraysize( .@cid );
 		while( .@i < .@size ){
 			.@state = checkvending(.@name$[.@i]);
			if (isloggedin(.@aid[.@i], .@cid[.@i])) {
				if ( .@state == 1 || .@state == 4 || !.@state ) {
					attachrid(.@aid[.@i]);
					if(checkvending() >= 1) {
						//dispbottom "[System] You online points is on hold due to duplicated machineID logged in.";
						query_sql "UPDATE `onlinetime` SET `start_time` = NOW() WHERE `account_id` = "+.@aid[.@i];
					} else {
						//dispbottom "[System] Online points updated.";
						query_sql "UPDATE `onlinetime` SET `total_sec` = `total_sec` + TIMESTAMPDIFF(SECOND, `start_time`,  NOW()), `start_time` = NOW() WHERE `account_id` = "+.@aid[.@i];
					}
					detachrid();
				} else {
					query_sql "UPDATE `onlinetime` SET `start_time` = NOW() WHERE `account_id` = "+.@aid[.@i];
				}		
			}
			set .@count,.@count + 1;
			set .@i,.@i + 1;
		}
		set .@offset,.@offset + .@size;
		deletearray .@cid,.@size;
		deletearray .@name$,.@size;
	}
	stopnpctimer;
	initnpctimer;
	end;

}

