# By Zombie_Killer
# 03.05.2012

from com.festina import Config
from com.festina.gameserver.model.quest import State
from com.festina.gameserver.model.quest import QuestState
from com.festina.gameserver.model.quest.jython import QuestJython as JQuest

qn = "617_GatherTheFlames"

TORCH = 7264

VULCAN = 8539
#ROONEY = 32049 - TODO

DROPLIST = {1381:51,1653:51,1387:53,1655:53,1390:56,1656:69,1389:55,1388:53,\
            1383:51,1392:56,1382:60,1654:52,1384:64,1394:51,1395:56,1385:52,\
            1391:55,1393:58,1657:57,1386:52,1652:49,1378:49,1376:48,1377:48,\
            1379:59,1380:49}

REWARDS = [ 6881,6883,6885,6887,6891,6893,6895,6897,6899,7580 ]

class Quest (JQuest) :

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

 def onEvent (self,event,st) :
     htmltext = event
     torches = st.getQuestItemsCount(TORCH)
     if event == "31539-03.htm" :
       if st.getPlayer().getLevel() >= 74 :
         st.set("cond","1")
         st.setState(STARTED)
         st.playSound("ItemSound.quest_accept")
       else :
         htmltext = "31539-02.htm"
         st.exitQuest(1)
     elif event == "31539-05.htm" and torches >= 1000 :
       htmltext = "31539-07.htm"
       st.takeItems(TORCH,1000)
       st.giveItems(REWARDS[st.getRandom(len(REWARDS))],1)
     elif event == "31539-08.htm" :
       st.takeItems(TORCH,-1)
       st.exitQuest(1)
     elif event.isdigit() and int(event) in REWARDS :
       if torches >= 1200 :
          st.takeItems(TORCH,1200)
          st.giveItems(int(event),1)
          htmltext = None
       else :
          htmltext = "Incorrect item count"
     return htmltext    

 def onTalk (self,npc,st):
     htmltext = "<html><head><body>I have nothing to say to you.</body></html>"
     id = st.getState()
     cond = st.getInt("cond")
     torches = st.getQuestItemsCount(TORCH)
     npcId = npc.getNpcId()
     if npcId == VULCAN :
       if id == CREATED :
         if st.getPlayer().getLevel() < 74 :
            st.exitQuest(1)
            htmltext = "31539-02.htm"
         else :
            htmltext = "31539-01.htm"
       elif torches < 1000 :
         htmltext = "31539-05.htm"
       else :
         htmltext = "31539-04.htm"
#     elif npcId == ROONEY and id == STARTED :
#       if torches >= 1200 :
#          htmltext = "32049-01.htm"
#       else :
#          htmltext = "32049-02.htm"
     return htmltext

 def onKill (self,npc,st):
     party = st.getPlayer().getParty()
     if party:
      for partyMember in party.getPartyMembers().toArray() :
       pst = partyMember.getQuestState(qn)
       if pst :
        torches = st.getQuestItemsCount(TORCH)
        chance = DROPLIST[npc.getNpcId()]
        drop = st.getRandom(100)
        qty,chance = divmod(chance*Config.RATE_DROP_QUEST,100)
        if drop < chance : qty += 1
        qty = int(qty)
        if qty :
         pst.giveItems(TORCH,qty)
         if divmod(torches+1,1000)[1] == 0  or divmod(torches+1,1200)[1] == 0 :
          pst.playSound("ItemSound.quest_middle")
         else :
          spst.playSound("ItemSound.quest_itemget")
     else:
      torches = st.getQuestItemsCount(TORCH)
      chance = DROPLIST[npc.getNpcId()]
      drop = st.getRandom(100)
      qty,chance = divmod(chance*Config.RATE_DROP_QUEST,100)
      if drop < chance : qty += 1
      qty = int(qty)
      if qty :
         st.giveItems(TORCH,qty)
         if divmod(torches+1,1000)[1] == 0  or divmod(torches+1,1200)[1] == 0 :
           st.playSound("ItemSound.quest_middle")
         else :
           st.playSound("ItemSound.quest_itemget")
     return

QUEST       = Quest(617, qn, "Gather The Flames")
CREATED     = State('Start', QUEST)
STARTED     = State('Started', QUEST)

QUEST.setInitialState(CREATED)
QUEST.addStartNpc(VULCAN)

QUEST.addTalkId(VULCAN)
STARTED.addTalkId(VULCAN)
CREATED.addTalkId(VULCAN)

#QUEST.addTalkId(ROONEY)

for mob in DROPLIST.keys() :
  STARTED.addKillId(mob)

STARTED.addQuestDrop(VULCAN,TORCH,1)
