### ---------------------------------------------------------------------------
### <history>
###		Abaddon	4/12/2005	Created
### </history>
### ---------------------------------------------------------------------------

### Settings
NPC         = [12260]
QuestId     = 6007
QuestName   = "NewbieSwordsWeapon"
QuestDesc   = "exchange"
#QuestDesc   = "NewbieSwordsWeapon"
InitialHtml = "1.htm"
SuccessMsg  = ""
FailureMsg  = "You do not have enough materials."
CancelMsg   = "1.htm"

### Items - Format [name, eventId, [giveItems], [takeitems], [teleLocation x, teleLocation y, teleLocation z]]
### giveItems - Format [itemId, qty]
### takeItems - Format [itemId, qty]
### example: 
### Items = [
###     ["MyItem1", 1001, [[ 234,   10], [ 333,    1]], [[ 563,  100], [ 363,  150]], [-80826,149775,-3043]],
###     ["MyItem2", 1002, [[ 453,    1], [  63,    1]], [[  23,   10], [ 774,  100]], [-80826,149775,-3043]]
### ]
###EXCHANGE
Items       = [
["Sword of Solidarity -> Orcish Sword", 738, [[67, 1]], [[738, 1], [57, 35600]], []],
["Orcish Sword -> Long Sword", 67, [[2, 1]], [[67, 1], [57, 81900]], []],
["Long Sword -> Falchion", 2, [[68, 1]], [[2, 1], [57, 108000]], []],
["Falchion -> Saber", 68, [[123, 1]], [[68, 1], [57, 165000]], []],
["Saber -> Artisan's Sword", 123, [[126, 1]], [[123, 1], [57, 235000]], []],
["Artisan's Sword -> Elven Sword", 126, [[130, 1]], [[126, 1], [57, 323000]], []],
["Elven Sword -> Sword of Revolution", 130, [[129, 1]], [[130, 1], [57, 433000]], []],
["Sword of Revolution -> Elven Long Sword", 129, [[2499, 1]], [[129, 1], [57, 400000]], []],
["Elven Long Sword -> Stormbringer", 2499, [[72, 1]], [[2499, 1], [57, 490000]], []],
["Stormbringer -> Shamshir", 72, [[73, 1]], [[72, 1], [57, 580000]], []],
["Shamshir -> Katana", 73, [[74, 1]], [[73, 1], [57, 0]], []],
["Katana -> Raid Sword", 74, [[133, 1]], [[74, 1], [57, 0]], []],
["Raid Sword -> Spirit Sword", 133, [[131, 1]], [[133, 1], [57, 0]], []],
["Spirit Sword -> Caliburs", 131, [[75, 1]], [[131, 1], [57, 1430000]], []],
["Caliburs -> Homunkulus's Sword", 75, [[84, 1]], [[75, 1], [57, 0]], []],
["Homunkulus's Sword -> Sword of Delusion", 84, [[76, 1]], [[84, 1], [57, 0]], []],
["Sword of Delusion -> Sword of Nightmare", 76, [[134, 1]], [[76, 1], [57, 0]], []],
["Sword of Nightmare -> Samurai Longsword", 134, [[135, 1]], [[134, 1], [57, 1830000]], []],
]###PY_TEMPLATE###
###EXCHANGE
### ---------------------------------------------------------------------------
### DO NOT MODIFY BELOW THIS LINE
### ---------------------------------------------------------------------------

print "importing " + str(QuestId) + ": " + QuestDesc,
import sys
from net.sf.l2j.gameserver.model.quest import State
from net.sf.l2j.gameserver.model.quest import QuestState
from net.sf.l2j.gameserver.model.quest.jython import QuestJython as JQuest

### Events
def do_Validate(st, items) :
    if len(items) > 0 :
        for item in items:
            if st.getQuestItemsCount(item[0]) < item[1] :
                return False
    return True

def do_GiveItems(st, items) :
    if len(items) > 0 :
        for item in items:
            st.giveItems(item[0], item[1])

def do_TakeItems(st, items) :
    if len(items) > 0 :
        for item in items:
            st.takeItems(item[0], item[1])

def do_Teleport(st, items) :
    if len(items) > 0 :
        st.player.teleToLocation(items[0], items[1], items[2])

def do_RequestedEvent(event, st, item) :
    if do_Validate(st, item[3]) :
        do_TakeItems(st, item[3])
        do_GiveItems(st, item[2])
        do_Teleport(st, item[4])
        if SuccessMsg != "" :
            return SuccessMsg
        return event + ".htm"
    else :
        if FailureMsg != "" :
            return FailureMsg
        return event + "-0.htm"

def do_RequestEvent(event,st) :
    htmltext = event

    if event == "0":
        if CancelMsg != "" :
            return CancelMsg
        return "Transaction has been canceled."

    for item in Items:
        if event == str(item[1]):
            return do_RequestedEvent(event, st, item)

	if htmltext != event:
		st.setState(COMPLETED)
		st.exitQuest(1)

    return htmltext

### main code
class Quest (JQuest) :

 def __init__(self,id,name,descr): JQuest.__init__(self,id,name,descr)

 def onEvent (self,event,st) :
    return do_RequestEvent(event,st)

 def onTalk (Self,npc,st):

   npcId = npc.getNpcId()
   htmltext = "<html><head><body>I have nothing to say with you</body></html>"
   st.setState(STARTED)
   if InitialHtml == "onEvent" :
     return do_RequestEvent(str(npcId),st)
   elif InitialHtml != "" :
     return InitialHtml
   return htmltext

### Quest class and state definition
QUEST       = Quest(QuestId, str(QuestId) + "_" + QuestName, QuestDesc)
CREATED     = State('Start',     QUEST)
STARTED     = State('Started',   QUEST)
COMPLETED   = State('Completed', QUEST)

### Quest initialization
QUEST.setInitialState(CREATED)

for item in NPC:
### Quest NPC starter initialization
   QUEST.addStartNpc(item)

### Quest NPC initialization
   STARTED.addTalkId(item)

print  ": Loaded " + str(len(Items)) + " item(s)"
