//===== eAthena Script =======================================
//= Kafra Expres - Stat/Skill Market Module
//===== By: ==================================================
//= Skotlex
//===== Current Version: =====================================
//= 2.0
//===== Compatible With: =====================================
//= eAthena SVN R3579+
//===== Description: =========================================
//= Part of the Kafra Express Script Package.
//= Lets players buy/sell skill points/stat points 
//===== Additional Comments: =================================
//= See config.txt for configuration.
//============================================================

-	script	keInit_statmarket	{
OnInit:	//Load Config
	donpcevent "keConfig::OnLoadStatMarket";
	end;
}

function	script	F_keStatMarket	{
	set @discount,callfunc("F_keCost",100,$@kesm_discount);
	do {
		set @kmenu, select (
			"- Return",
			"- Buy Stat points ("+($@kesm_stBuyPrice*@discount/100)+"z each)",
			"- Sell Stat points (up to "+StatusPoint+"/"+$@kesm_stSellPrice+"z each)",
			"- Buy Skill points ("+($@kesm_skBuyPrice*@discount/100)+"z each)",
			"- Sell Skill points (up to "+SkillPoint+"/"+$@kesm_skSellPrice+"z each)",
			"- Trade Stats -> Skill ("+$@kesm_skTradePrice+" stats/skill)",
			"- Trade Skills -> Stats ("+$@kesm_stTradePrice+" stats/skill)"
		);
		if (@kmenu > 1)
			input @qty;
		switch (@kmenu) {
		case 2:	//Buy Stat
			set @min, 1;
			set @max, 9999;
			set @cost, @qty*$@kesm_stBuyPrice;
			break;
		case 3:	//Sell Stat
			input @qty;
			set @min, 1;
			set @max, StatusPoint;
			set @cost, @qty*$@kesm_stSellPrice;
			break;
		case 4: //Buy Skill
			set @min, 1;
			set @max, 9999;
			set @cost, @qty*$@kesm_skBuyPrice;
			break;
		case 5: //Sell Skill
			set @min, 1;
			set @max, SkillPoint;
			set @cost, @qty*$@kesm_skSellPrice;
			break;
		case 6: //Convert stats -> skills
			set @min, $@kesm_skTradePrice;
			set @max, StatusPoint;
			set @cost, @qty/$@kesm_skTradePrice;
			break;
		case 7: //Convert skills -> stats
			set @min, 1;
			set @max, SkillPoint;
			set @cost, @qty*$@kesm_stTradePrice;
			break;
		default:
			return;
		}
		if (@qty < @min) {
			if (@min == 1)
				callfunc "F_keIntro", e_dots, "Was that supposed to be funny?";
			else
				callfunc "F_keIntro", e_dots, "That is not enough to buy a single skill...";
		} else 
		if (@qty > @max) {
			if (@max == 9999)
				callfunc "F_keIntro", e_X, "You can't buy that many!";
			else
				callfunc "F_keIntro", e_X, "You don't have that many to sell...";
		} else
		if (@cost < 0) {
			callfunc "F_keIntro", e_swt2, "That is too much for a single transaction! Try a smaller quantity... please?";
		} else {
			switch(@kmenu) {
			case 2: //Buy Stat
				if (!(callfunc("F_keCharge",@cost,$@kesm_discount,1))) {
					callfunc "F_keIntro", e_X, "You do not have enough zeny to buy that many.";
					break;
				}
				set StatusPoint,StatusPoint+@qty;
				emotion e_oh;
				break;
			case 3: //Sell Stat
				set StatusPoint,StatusPoint-@qty;
				set Zeny,Zeny+@cost;
				emotion e_oh;
				break;
			case 4: //Buy Skill
				if (!(callfunc("F_keCharge",@cost,$@kesm_discount,1))) {
					callfunc "F_keIntro", e_X, "You do not have enough zeny to buy that many.";
					break;
				}
				set SkillPoint,SkillPoint+@qty;
				emotion e_oh;
				break;
			case 5: //Sell Skill
				set SkillPoint,SkillPoint-@qty;
				set Zeny,Zeny+@cost;
				emotion e_oh;
				break;
			case 6: //Convert stats -> skills
				set @qty, @cost*$@kesm_skTradePrice;
				set StatusPoint,StatusPoint-@qty;
				set SkillPoint,SkillPoint+@cost;
				emotion e_oh;
				break;
			case 7: //Convert skills -> stats
				set SkillPoint,SkillPoint-@qty;
				set StatusPoint,StatusPoint+@cost;
				emotion e_oh;
				break;
			}
		}
	} while (@kmenu > 1);
	return;
}
