//@unloadnpc buy_sell
//@loadnpc npc/thor/buysell.txt

-	script	buy_sell	-1,{
OnBuyEvent:
	set .@markettime,gettimetick(2);
	set .@marketcd,(60*.marketcdtime);
	if(.@markettime - #marketannouncetimer < .@marketcd) {
		set .@marketk,#marketannouncetimer - (gettimetick(2)-.@marketcd);
		set .@marketm,.@marketk % 3600 / 60;
		set .@markets,.@marketk % 60;
		set .@marketm$, .@marketm+" minute"+((.@marketm <= 1)?"":"s");
		set .@markets$, .@markets+" second"+((.@markets <= 1)?"":"s");
		set .@markettime$,""+.@marketm$+" "+.@markets$+"";
		mes "[^ff0000 System ^000000]"; 
		mes "I'm sorry but you have to wait"; 
		mes "^FF0000"+.@markettime$+"^000000 before you can announce again.";
		close;	
	}
	mes "[^ff0000 System ^000000]";
	mes "This service is";
	mes "FREE if your character";
	mes "is below BaseLevel 76"; 
	mes "What would you like to ^ff0000BUY^000000?";
	if( input(.@keyword$,3,10) ){
		mes "[^ff0000 System ^000000]"; 
		mes "Keyword must be at least^FF0000 3 - 10^000000 characters long and NO SPACES.";	
		close;
	}
	// check allowed characters
	for (set .@i,0; .@i<getstrlen(.@keyword$); set .@i,.@i+1) {
		if (!charisalpha(.@keyword$,.@i)) {
			mes "[^ff0000 System ^000000]"; 
			mes "You have entered an ^FF0000invalid^000000 character. Use only letters from ^FF0000a-z or A-Z^000000";
			close;
		}
	}
	// query database
	set @nb,query_sql("select `id`,`name_japanese`,`slots`, `refineable`, `equip_locations`, `elemental` from `trading` where `name_japanese` LIKE '%"+.@keyword$+"%' AND instr('"+.nobuylist$+"',id) = 0 ORDER BY `name_japanese` ASC LIMIT 60",.@itemid,.@itemname$,.@slots,.@refineable,.@location,.@elemental);
	set $@menu$,"Cancel";
	if (!.@itemid) {
		mes "^FF0000No Item matches that keyword. Please try again^000000";
		close;
	}
	for(set .@i, 0;.@i < getarraysize(.@itemid);set .@i, .@i + 1){
		if (.@elemental[.@i]) {
			set .@menu$,.@menu$+":"+.@itemname$[.@i]+" [ ^28bf00Elemental^000000 ]";
		} else {
			set .@menu$,.@menu$+":"+.@itemname$[.@i]+" ["+.@slots[.@i]+"]";
		}
  	}
	set .@part,select(.@menu$) - 2;
	set .@itemname$,.@itemname$[.@part];
	set .@location,.@location[.@part];
	set .@slots,.@slots[.@part];
	set .@elemental,.@elemental[.@part];
	set .@element$,"None";
	set .@strong,"None";
	set .@card1$,"No Card";
	set .@card2$,"No Card";
	set .@card3$,"No Card";
	set .@card4$,"No Card";
	set .@price,0;
	while(1) {
		switch(select(
			((.@refineable[.@part])?"^777777^ff0000Refine^000000   [ +"+.@refine+" ]:":":")+
			((.@elemental[.@part])?"^777777^ff0000Element^000000   [ "+.@element$+" ]:":":")+
			((.@elemental[.@part])?"^777777^ff0000Strong^000000   [ "+.@strong+" ]:":":")+	
		  	((.@slots[.@part] >= 1)?"^777777^ff0000Card #1^000000 [ "+.@card1$+" ]:":":")+
		  	((.@slots[.@part] >= 2)?"^777777^ff0000Card #2^000000 [ "+.@card2$+" ]:":":")+
		 	((.@slots[.@part] >= 3)?"^777777^ff0000Card #3^000000 [ "+.@card3$+" ]:":":")+
		 	((.@slots[.@part] >= 4)?"^777777^ff0000Card #4^000000 [ "+.@card4$+" ]:":":")+
		 	 "^ff0000Send^000000"
		)) {
	
			case 1: 
				set .@refine,0;
				if( input(.@refine,0,10) ){
					break;
				}
				break;
			case 2: 
				switch(select("Wind","Ice","Fire","Earth","None")) {
					case 1:
						set .@element$,"Wind";
						break;
					case 2:
						set .@element$,"Ice";
						break;
					case 3:
						set .@element$,"Fire";
						break;
					case 4:
						set .@element$,"Earth";
						break;
					default:
						set .@element$,"None";
						break;
				}
				break;
			case 3: 
				set .@strong,0;
				if( input(.@strong,0,2) ){
					break;
				}
				break;

			case 4: 
				set .@card1menu$,"";	
				set .@card1,query_sql("select `id`,`name_japanese`,`equip_locations` from `trading` where `id` > 3992 AND `id` < 5000 AND `equip_locations` = "+.@location+" AND instr('"+.nobuylist$+"',id) = 0 ORDER BY `name_japanese` ASC LIMIT 100",.@cardid,.@cardname$,.@cardloc);
				for(set .@i, 0;.@i < .@card1;set .@i, .@i + 1){
					set .@card1menu$,.@card1menu$+":"+.@cardname$[.@i];
  				}
				set .@Select,select (.@card1menu$) - 2;
				set .@card1$,.@cardname$[.@Select];
				break;
			case 5: 
				set .@card2menu$,"";
				set .@card2,query_sql("select `id`,`name_japanese`,`equip_locations` from `trading` where `id` > 3992 AND `id` < 5000 AND `equip_locations` = "+.@location+" AND instr('"+.nobuylist$+"',id) = 0 ORDER BY `name_japanese` ASC LIMIT 100",.@cardid,.@cardname$,.@cardloc);
				for(set .@i, 0;.@i < .@card2;set .@i, .@i + 1){
					set .@card2menu$,.@card2menu$+":"+.@cardname$[.@i];
  				}
				set .@Select,select (.@card2menu$) - 2;
				set .@card2$,.@cardname$[.@Select];
				break;
			case 6: 
				set .@card3menu$,"";
				set .@card3,query_sql("select `id`,`name_japanese`,`equip_locations` from `trading` where `id` > 3992 AND `id` < 5000 AND `equip_locations` = "+.@location+" AND instr('"+.nobuylist$+"',id) = 0 ORDER BY `name_japanese` ASC LIMIT 100",.@cardid,.@cardname$,.@cardloc);
				for(set .@i, 0;.@i < .@card3;set .@i, .@i + 1){
					set .@card3menu$,.@card3menu$+":"+.@cardname$[.@i];
  				}
				set .@Select,select (.@card3menu$) - 2;
				set .@card3$,.@cardname$[.@Select];
				break;
			case 7:
				set .@card4menu$,"";
				set .@card4,query_sql("select `id`,`name_japanese`,`equip_locations` from `trading` where `id` > 3992 AND `id` < 5000 AND `equip_locations` = "+.@location+" AND instr('"+.nobuylist$+"',id) = 0 ORDER BY `name_japanese` ASC LIMIT 100",.@cardid,.@cardname$,.@cardloc);
				for(set .@i, 0;.@i < .@card4;set .@i, .@i + 1){
					set .@card4menu$,.@card4menu$+":"+.@cardname$[.@i];
  				}
				set .@Select,select (.@card4menu$) - 2;
				set .@card4$,.@cardname$[.@Select];
				break;
			case 8: 
				if (.@card1$ == "No Card") { set .@card1$,"*"; }
				if (.@card2$ == "No Card") { set .@card2$,"*"; }
				if (.@card3$ == "No Card") { set .@card3$,"*"; }
				if (.@card4$ == "No Card") { set .@card4$,"*"; }
				if (.@strong == 1) { set .@strong$,"VS";}
				if (.@strong == 2) { set .@strong$,"VVS";}
				if (.@strong == 3) { set .@strong$,"VVVS";}
				if ( BaseLevel > 75 ) {
					if (select("Pay 5,000z to announce:Nevermind") == 1) {
						if(Zeny < 5000) { mes "You don't have enough zeny"; close; }
						Zeny -= 5000;
					} else {
						close;
					}
				}
					set #marketannouncetimer,gettimetick(2);
					if (.@elemental) {
						if (.@refine) {
							if (.@price) {
								chanmsg("#buying",""+strcharinfo(0)+": [Buying] +"+.@refine+" "+.@strong$+" [ "+.@element$+" ] "+.@itemname$+" "+callfunc("F_InsertComma",.@price)+" z");
							} else {
								chanmsg("#buying",""+strcharinfo(0)+": [Buying] +"+.@refine+" "+.@strong$+" [ "+.@element$+" ] "+.@itemname$+" pm me.");
							}
						} else {
							if (.@price) {
								chanmsg("#buying",""+strcharinfo(0)+": [Buying] "+.@strong$+" [ "+.@element$+" ] "+.@itemname$+" "+callfunc("F_InsertComma",.@price)+" z");
							} else {
								chanmsg("#buying",""+strcharinfo(0)+": [Buying] "+.@strong$+" [ "+.@element$+" ] "+.@itemname$+" pm me.");
							}
						}
						close;
					}

					if (.@slots) {
						// 1 Slot
						if (.@slots == 1) {
							if (.@refine) {
								if (.@price) {
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] +"+.@refine+" "+.@itemname$+" [ "+.@card1$+" ] "+callfunc("F_InsertComma",.@price)+" z");
								} else {
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] +"+.@refine+" "+.@itemname$+" [ "+.@card1$+" ] pm me.");
								}
							} else {
								if (.@price) {
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] "+.@itemname$+" [ "+.@card1$+" ] "+callfunc("F_InsertComma",.@price)+" z");
								} else {
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] "+.@itemname$+" [ "+.@card1$+" ] pm me.");
								}
							}
						}
						
						// 2 Slots
						if (.@slots == 2) {
							if (.@refine) {
								if (.@price) {
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] +"+.@refine+" "+.@itemname$+" [ "+.@card1$+" ] [ "+.@card2$+" ] "+callfunc("F_InsertComma",.@price)+" z");
								} else {
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] +"+.@refine+" "+.@itemname$+" [ "+.@card1$+" ] [ "+.@card2$+" ] pm me.");
								}

							} else {
								if (.@price) {
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] "+.@itemname$+" [ "+.@card1$+" ] [ "+.@card2$+" ] "+callfunc("F_InsertComma",.@price)+" z");
								} else {
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] "+.@itemname$+" [ "+.@card1$+" ] [ "+.@card2$+" ] pm me.");
								}
							}
						}
						// 3 slots
						if (.@slots == 3) {
							if (.@refine) {
								if (.@price) {
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] +"+.@refine+" "+.@itemname$+" [ "+.@card1$+" ] [ "+.@card2$+" ] [ "+.@card3$+" ] "+callfunc("F_InsertComma",.@price)+" z");
								} else {
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] +"+.@refine+" "+.@itemname$+" [ "+.@card1$+" ] [ "+.@card2$+" ] [ "+.@card3$+" ] pm me.");
								}
							} else {
								if (.@price) {
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] "+.@itemname$+" [ "+.@card1$+" ] [ "+.@card2$+" ] [ "+.@card3$+" ] "+callfunc("F_InsertComma",.@price)+" z");
								} else {	
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] "+.@itemname$+" [ "+.@card1$+" ] [ "+.@card2$+" ] [ "+.@card3$+" ]  pm me.");
								}
							}
						}
						if (.@slots == 4) {
							if (.@refine) {
								if (.@price) {
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] +"+.@refine+" "+.@itemname$+" [ "+.@card1$+" ] [ "+.@card2$+" ] [ "+.@card3$+" ] [ "+.@card4$+" ] "+callfunc("F_InsertComma",.@price)+" z");
								} else {	
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] +"+.@refine+" "+.@itemname$+" [ "+.@card1$+" ] [ "+.@card2$+" ] [ "+.@card3$+" ] [ "+.@card4$+" ] pm me.");
								}
							} else {
								if (.@price) {
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] "+.@itemname$+" [ "+.@card1$+" ] [ "+.@card2$+" ] [ "+.@card3$+" ] [ "+.@card4$+" ] "+callfunc("F_InsertComma",.@price)+" z");
								} else {
									chanmsg("#buying",""+strcharinfo(0)+": [Buying] "+.@itemname$+" [ "+.@card1$+" ] [ "+.@card2$+" ] [ "+.@card3$+" ] [ "+.@card4$+" ]   pm me.");
								}
							}
						}

					
					} else {
						//no slots
						if (.@refine) {
							if (.@price) {
								chanmsg("#buying",""+strcharinfo(0)+": [Buying] +"+.@refine+" "+.@itemname$+" [ "+.@card1$+" ] "+callfunc("F_InsertComma",.@price)+" z");
							} else {
								chanmsg("#buying",""+strcharinfo(0)+": [Buying] +"+.@refine+" "+.@itemname$+" [ "+.@card1$+" ] pm me.");
							}
						} else {
							if (.@price) {
								chanmsg("#buying",""+strcharinfo(0)+": [Buying] "+.@itemname$+" "+callfunc("F_InsertComma",.@price)+" z");
							} else {
								chanmsg("#buying",""+strcharinfo(0)+": [Buying] "+.@itemname$+" pm me.");
							}
						}
					}						
				close;
			}
		}


OnSellEvent:
	set .@markettime,gettimetick(2);
	set .@marketcd,(60*.marketcdtime);
	if(.@markettime - #marketannouncetimer < .@marketcd) {
		set .@marketk,#marketannouncetimer - (gettimetick(2)-.@marketcd);
		set .@marketm,.@marketk % 3600 / 60;
		set .@markets,.@marketk % 60;
		set .@marketm$, .@marketm+" minute"+((.@marketm <= 1)?"":"s");
		set .@markets$, .@markets+" second"+((.@markets <= 1)?"":"s");
		set .@markettime$,""+.@marketm$+" "+.@markets$+"";
		mes "[^ff0000 System ^000000]"; 
		mes "I'm sorry but you have to wait"; 
		mes "^FF0000"+.@markettime$+"^000000 before you can announce again.";
		close;	
	}
	mes "[^ff0000 System ^000000]";
	mes "This service is";
	mes "FREE if your character";
	mes "is below Base Level 76"; 
	mes "Item must be in your inventory";
	mes "in order to sell it.";
	mes "What would you like to ^ff0000SELL^000000?";
	next;

	getinventorylist();
	for (.@i = 0; .@i < @inventorylist_count; .@i++) {
		/* Filter only items that is not bound */
		if (@inventorylist_bound[.@i] || getiteminfo(@inventorylist_id[.@i],ITEMINFO_LOC) >= 1024 ){

		} else {
			.@itemname$ = callfunc( "getitemname2", @inventorylist_id[.@i], @inventorylist_identify[.@i], @inventorylist_refine[.@i], @inventorylist_attribute[.@i], @inventorylist_card1[.@i], @inventorylist_card2[.@i], @inventorylist_card3[.@i], @inventorylist_card4[.@i], @inventorylist_bound[.@i]);
			.@menu$ += sprintf("%s %s:", @itemname2_info$, .@itemname$);
			.@inventorylist_id[.@j] = @inventorylist_id[.@i];
			.@inventorylist_identify[.@j] = @inventorylist_identify[.@i];
			.@inventorylist_refine[.@j] = @inventorylist_refine[.@i];
			.@inventorylist_attribute[.@j] = @inventorylist_attribute[.@i];
			.@inventorylist_card1[.@j] = @inventorylist_card1[.@i];
			.@inventorylist_card2[.@j] = @inventorylist_card2[.@i];
			.@inventorylist_card3[.@j] = @inventorylist_card3[.@i];
			.@inventorylist_card4[.@j] = @inventorylist_card4[.@i];
			.@inventorylist_bound[.@j] = @inventorylist_bound[.@i];
			.@j++;
		}
	}
	.@menu$ += "Cancel";
	.@s = select(.@menu$) -1;
	if (.@s == .@j)
		close;

	if ( BaseLevel > 75 ) {
		if (select("Pay 5,000z to announce:Nevermind") == 1) {
			if(Zeny < 5000) { mes "You don't have enough zeny"; close; }
			Zeny -= 5000;
		} else {
			close;
		}
	}
	set #marketannouncetimer,gettimetick(2);
	/* Now display ItemID of selected inventory */
	chanmsg("#selling",""+strcharinfo(0)+": [Selling] "+@itemname2_info$[0] + callfunc( "getitemname2", .@inventorylist_id[.@s], .@inventorylist_identify[.@s], .@inventorylist_refine[.@s], .@inventorylist_attribute[.@s], .@inventorylist_card1[.@s], .@inventorylist_card2[.@s], .@inventorylist_card3[.@s], .@inventorylist_card4[.@s], .@inventorylist_bound[.@s] ));
	close2;

OnInit:
	set .@allowed$,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	bindatcmd("buy","buy_sell::OnBuyEvent",0,99);
	bindatcmd("sell","buy_sell::OnSellEvent",0,99);
	set .marketcdtime,3;		// 5 = 5 minutes Town
	set .nobuylist$, "4128,4144,4147,4142,4132,4143,4137,4123,4146,4131,4121,4135,4047,4054";	// MvP Cards
	end;
}
