//@unloadnpc Online Gaming Time
//@loadnpc npc/amatsu/online.txt

office,130,38,4	script	Online Points	4_F_KAFRA7,{
	if(questprogress(31001) == 1 && countitem(30103) > 0 ) {
		cutin "kafra_07",2;
		mes .npc$;
		mes "It's nice to see you here ^003366"+strcharinfo(PC_NAME)+"^000000? I give good reward to anyone who is staying online.";
		next;
		mes .npc$;
		mes "For every hour you spend online, I give 1 ^003366"+getitemname(Coupon)+"^000000.  You can use ^003366"+getitemname(Coupon)+"^000000 to rent selected gears from the ^ff0000Rental Guru^000000!";
		next;
		mes .npc$;
		mes "Come back to me any time you like in your adventure and i'll take it from there!";
		nov_train1++;
		switch (BaseLevel){
			case 1: getexp 9,0; break;
			case 2: getexp 16,0; break;
			case 3: getexp 25,0; break;
			case 4: getexp 36,0; break;
			case 5: getexp 77,0; break;
			case 6: getexp 112,0; break;
			case 7: getexp 153,0; break;
			case 8: getexp 200,0; break;
			case 9: getexp 253,0; break;
			case 10: getexp 320,0; break;
			case 11: getexp 385,0; break;
			case 12: getexp 490,0; break;
			case 13: getexp 585,0; break;
			case 14: getexp 700,0; break;
			case 15: getexp 830,0; break;
			case 16: getexp 970,0; break;
			case 17: getexp 1120,0; break;
			case 18: getexp 1260,0; break;
			case 19: getexp 1420,0; break;
		}
		delitem 30103,countitem(30103);
		if(nov_train1 == 6){
			next;
			mes .npc$;
			mes "Go back and talk to ^ff0000Percy^000000 now, I bet she will be happy that you learned so many NPC at this room already.";
			completequest(31001);
			setquest(31002);
			cutin "",255;
			close;
		}
		cutin "",255;	
		close;				
	}
	mes .npc$;
	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  "[ "+.@days+" day "+.@hours+" hour "+.@minutes+" min "+.@seconds+" sec ]";
        @tick = .@tick/3600;
	if(#PCTIME) mes "Available ^ff0000Online Points^000000: ^ff0000"+#PCTIME+"^000000.";
	mes "Stay online and accumulate more coins. We will give ^ff00001^000000 ^003366"+getitemname(Coupon)+"^000000 for every hour you are online.";
	next;
	switch(select( "[ Redeem Coins ]",(#PCTIME)?"[ Convert Points ]":"","[ Open Shop ]","[ Close ]")) {
	case 1:
        	mes .npc$;
         	if (!@tick) { 
			mes "You must have atleast 1 hour time to proceed.";
			cutin "",255;
			close;
		}
		@thistick = 3600*@tick;
	 	getitem Coupon,@tick;
		mes "You successfully redeemed ^3399FF"+@tick+"^000000 ^003366"+getitemname(Coupon)+"^000000.";
		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;
	case 2:
		mes .npc$;
		if(#PCTIME) {
			mes "You are about to convert ^3399FF"+#PCTIME+"^000000 Online Points to ^003366"+getitemname(Coupon)+"^000000.";
			next;
			if(select("No, Thanks", "Yes please.")== 2){
				mes .npc$;
				mes "There you go!";
				getitem Coupon,#PCTIME;
				#PCTIME = 0;
				cutin "",255;
				close;
			} else {
				mes .npc$;
				mes "Okay, come back when you are ready.";
				cutin "",255;
				close;
			}
		} else {
			mes "You don't have Online Points to convert.";
			cutin "",255;
			close;
		}
	case 3:
		cutin "",255;
		close2;
		openshop();							
		end;		
	default:
		cutin "",255;
		close;

	}
	close2;
	cutin "",255;
	end;

OnPCLoginEvent:
	.@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(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;
	questinfo(QTYPE_JOB, 1);
	setquestinfo(QINFO_JOB, Job_Novice);
	setquestinfo(QINFO_QUEST, 31001, 1);
	setquestinfo(QINFO_ITEM, 30103, 1,1);

	set .npc$,"[ ^ff0000Online Points^000000 ]";
	tradertype(NST_CUSTOM);
	sellitem 13515,4;	
	sellitem 30007,4;
	sellitem 604,2;
	sellitem 30008,10;
	sellitem 12209,10;
	sellitem 12299,10;	
	sellitem 5361,150;
	end;

OnCountFunds:
	setcurrency(countitem(Coupon));
	end;

OnPayFunds:
	if((countitem(Coupon)) < @points) end;
	delitem Coupon,@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;
}

//new_1-1,61,108,2	duplicate(Online Gaming Time)	Online Gaming Time#n	4_F_KAFRA7