Advertisement
Guest User

Untitled

a guest
Sep 9th, 2017
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.29 KB | None | 0 0
  1. import ui
  2. import snd
  3. import shop
  4. import mouseModule
  5. import player
  6. import chr
  7. import net
  8. import uiCommon
  9. import localeInfo
  10. import chat
  11. import item
  12. import systemSetting
  13. import player
  14. #ifdef OFFLINE_SHOP
  15. import constInfo
  16. import app
  17. import os
  18.  
  19. ##### DISABLE/ENABLE SHOP VISIT COLOR ###
  20. SHOP_VISIT=True
  21. ##### SHOP VISIT COLOR #####
  22. SHOP_VISIT_COLOR=0xFF00C8FF
  23. ###########################
  24. #endif
  25.  
  26. g_isBuildingPrivateShop = False
  27.  
  28. g_itemPriceDict={}
  29.  
  30. g_privateShopAdvertisementBoardDict={}
  31.  
  32. #ifdef OFFLINE_SHOP
  33. #def GetShopNamesRange():
  34. #   val=1.000
  35. #   try:
  36. #       f=old_open("shop.cfg", 'r')
  37. #       val=float(f.read().replace('\n', ''))
  38. #   except IOError:
  39. #       pass
  40. #   return float(val)*1000.0
  41. #
  42. #def SetShopNamesRange(pos):
  43. #   with old_open("shop.cfg", 'w+') as f:
  44. #       f.write(str(pos))
  45. #       f.close()
  46. #endif
  47.  
  48. def Clear():
  49.     global g_itemPriceDict
  50.     global g_isBuildingPrivateShop
  51.     g_itemPriceDict={}
  52.     g_isBuildingPrivateShop = False
  53.  
  54. def IsPrivateShopItemPriceList():
  55.     global g_itemPriceDict
  56.     if g_itemPriceDict:
  57.         return True
  58.     else:
  59.         return False
  60.  
  61. def IsBuildingPrivateShop():
  62.     global g_isBuildingPrivateShop
  63.     if player.IsOpenPrivateShop() or g_isBuildingPrivateShop:
  64.         return True
  65.     else:
  66.         return False
  67.  
  68. def SetPrivateShopItemPrice(itemVNum, itemPrice):
  69.     global g_itemPriceDict
  70.     g_itemPriceDict[int(itemVNum)]=itemPrice
  71.        
  72. def GetPrivateShopItemPrice(itemVNum):
  73.     try:
  74.         global g_itemPriceDict
  75.         return g_itemPriceDict[itemVNum]
  76.     except KeyError:
  77.         return 0
  78.        
  79. def UpdateADBoard():
  80.     for key in g_privateShopAdvertisementBoardDict.keys():
  81.         g_privateShopAdvertisementBoardDict[key].Show()
  82.  
  83. def DeleteADBoard(vid):
  84.     if not g_privateShopAdvertisementBoardDict.has_key(vid):
  85.         return
  86.  
  87.     del g_privateShopAdvertisementBoardDict[vid]
  88.        
  89.  
  90. class PrivateShopAdvertisementBoard(ui.ThinBoard):
  91.     def __init__(self):
  92.         ui.ThinBoard.__init__(self, "UI_BOTTOM")
  93.     #ifdef OFFLINE_SHOP
  94.         self.shopAdvertismentBoardSeen = []
  95.     #endif
  96.         self.vid = None
  97.         self.__MakeTextLine()
  98.  
  99.     def __del__(self):
  100.         ui.ThinBoard.__del__(self)
  101.  
  102.     def __MakeTextLine(self):
  103.         self.textLine = ui.TextLine()
  104.         self.textLine.SetParent(self)
  105.         self.textLine.SetWindowHorizontalAlignCenter()
  106.         self.textLine.SetWindowVerticalAlignCenter()
  107.         self.textLine.SetHorizontalAlignCenter()
  108.         self.textLine.SetVerticalAlignCenter()
  109.         self.textLine.Show()
  110.  
  111.     def Open(self, vid, text):
  112.         self.vid = vid
  113.  
  114.         self.textLine.SetText(text)
  115.         self.textLine.UpdateRect()
  116.         self.SetSize(len(text)*6 + 10*2, 20)
  117.         self.Show()
  118.                
  119.         g_privateShopAdvertisementBoardDict[vid] = self
  120.  
  121.     def OnMouseLeftButtonUp(self):
  122.         if not self.vid:
  123.             return
  124.         net.SendOnClickPacket(self.vid)
  125.  
  126.     #ifdef OFFLINE_SHOP
  127.         if self.vid != player.GetMainCharacterIndex():
  128.             self.textLine.SetPackedFontColor(SHOP_VISIT_COLOR)
  129.             self.shopAdvertismentBoardSeen.append(self.vid)
  130.     #endif
  131.  
  132.         return True
  133.  
  134.     def OnUpdate(self):
  135.         if not self.vid:
  136.             return
  137.  
  138.         if systemSetting.IsShowSalesText():
  139.             self.Show()
  140.             x, y = chr.GetProjectPosition(self.vid, 220)
  141.             self.SetPosition(x - self.GetWidth()/2, y - self.GetHeight()/2)
  142.  
  143.         else:
  144.             for key in g_privateShopAdvertisementBoardDict.keys():
  145.                 if  player.GetMainCharacterIndex() == key:  #»óÁˇÇłĽ±Ŕ» ľČş¸ŔĚ°Ô °¨Ăß´Â °ćżěżˇµµ, ÇĂ·ąŔĚľî ŔÚ˝ĹŔÇ »óÁˇ ÇłĽ±Ŕş ş¸Ŕ̵µ·Ď ÇÔ. by ±čÁŘČŁ
  146.                     g_privateShopAdvertisementBoardDict[key].Show()
  147.                     x, y = chr.GetProjectPosition(player.GetMainCharacterIndex(), 220)
  148.                     g_privateShopAdvertisementBoardDict[key].SetPosition(x - self.GetWidth()/2, y - self.GetHeight()/2)
  149.                 else:
  150.                     g_privateShopAdvertisementBoardDict[key].Hide()
  151.  
  152. class PrivateShopBuilder(ui.ScriptWindow):
  153.  
  154.     def __init__(self):
  155.         #print "NEW MAKE_PRIVATE_SHOP_WINDOW ----------------------------------------------------------------"
  156.         ui.ScriptWindow.__init__(self)
  157.  
  158.         self.__LoadWindow()
  159.         self.itemStock = {}
  160.         self.tooltipItem = None
  161.         self.priceInputBoard = None
  162.     #ifdef OFFLINE_SHOP
  163.         self.days = 0
  164.     #endif
  165.         self.title = ""
  166.  
  167.     def __del__(self):
  168.         #print "------------------------------------------------------------- DELETE MAKE_PRIVATE_SHOP_WINDOW"
  169.         ui.ScriptWindow.__del__(self)
  170.  
  171.     def __LoadWindow(self):
  172.         try:
  173.             pyScrLoader = ui.PythonScriptLoader()
  174.             pyScrLoader.LoadScriptFile(self, "UIScript/PrivateShopBuilder.py")
  175.         except:
  176.             import exception
  177.             exception.Abort("PrivateShopBuilderWindow.LoadWindow.LoadObject")
  178.  
  179.         try:
  180.             GetObject = self.GetChild
  181.             self.nameLine = GetObject("NameLine")
  182.             self.itemSlot = GetObject("ItemSlot")
  183.             self.btnOk = GetObject("OkButton")
  184.             self.btnClose = GetObject("CloseButton")
  185.             self.titleBar = GetObject("TitleBar")
  186.         except:
  187.             import exception
  188.             exception.Abort("PrivateShopBuilderWindow.LoadWindow.BindObject")
  189.  
  190.         self.btnOk.SetEvent(ui.__mem_func__(self.OnOk))
  191.         self.btnClose.SetEvent(ui.__mem_func__(self.OnClose))
  192.         self.titleBar.SetCloseEvent(ui.__mem_func__(self.OnClose))
  193.  
  194.         self.itemSlot.SetSelectEmptySlotEvent(ui.__mem_func__(self.OnSelectEmptySlot))
  195.         self.itemSlot.SetSelectItemSlotEvent(ui.__mem_func__(self.OnSelectItemSlot))
  196.         self.itemSlot.SetOverInItemEvent(ui.__mem_func__(self.OnOverInItem))
  197.         self.itemSlot.SetOverOutItemEvent(ui.__mem_func__(self.OnOverOutItem))
  198.  
  199.     def Destroy(self):
  200.         self.ClearDictionary()
  201.  
  202.         self.nameLine = None
  203.         self.itemSlot = None
  204.         self.btnOk = None
  205.         self.btnClose = None
  206.         self.titleBar = None
  207.         self.priceInputBoard = None
  208.  
  209. #ifdef OFFLINE_SHOP
  210.     def Open(self, title, days):
  211.  
  212.         self.days = days
  213. #else
  214.     #def Open(self, title):
  215. #endif
  216.  
  217.         self.title = title
  218.  
  219.         if len(title) > 25:
  220.             title = title[:22] + "..."
  221.  
  222.         self.itemStock = {}
  223.         shop.ClearPrivateShopStock()
  224.         self.nameLine.SetText(title)
  225.         self.SetCenterPosition()
  226.         self.Refresh()
  227.         self.Show()
  228.  
  229.         global g_isBuildingPrivateShop
  230.         g_isBuildingPrivateShop = True
  231.         # shop.RefreshInventorySafa()
  232.  
  233.     def Close(self):
  234.         global g_isBuildingPrivateShop
  235.         g_isBuildingPrivateShop = False
  236.  
  237.         self.title = ""
  238.         self.itemStock = {}
  239.         shop.ClearPrivateShopStock()
  240.         self.Hide()
  241.  
  242.     def SetItemToolTip(self, tooltipItem):
  243.         self.tooltipItem = tooltipItem
  244.  
  245.     def Refresh(self):
  246.         getitemVNum=player.GetItemIndex
  247.         getItemCount=player.GetItemCount
  248.         setitemVNum=self.itemSlot.SetItemSlot
  249.         delItem=self.itemSlot.ClearSlot
  250.  
  251.         for i in xrange(shop.SHOP_SLOT_COUNT):
  252.  
  253.             if not self.itemStock.has_key(i):
  254.                 delItem(i)
  255.                 continue
  256.  
  257.             pos = self.itemStock[i]
  258.  
  259.             itemCount = getItemCount(*pos)
  260.             if itemCount <= 1:
  261.                 itemCount = 0
  262.             setitemVNum(i, getitemVNum(*pos), itemCount)
  263.  
  264.         self.itemSlot.RefreshSlot()
  265.  
  266. #ifdef OFFLINE_SHOP
  267.     def ReadFilePrice(self,vnum,count):
  268.         d = "shops"
  269.         if not os.path.exists(d):
  270.             os.makedirs(d)
  271.         oldPrice=0
  272.         n=d+"/"+str(vnum)+"_"+str(count)+".txt"
  273.         if os.path.exists(n):
  274.             fd = open( n,'r')
  275.             oldPrice=int(fd.readlines()[0])
  276.            
  277.         return oldPrice
  278.     def SaveFilePrice(self,vnum,count,price):
  279.         d = "shops"
  280.         if not os.path.exists(d):
  281.             os.makedirs(d)
  282.         n=d+"/"+str(vnum)+"_"+str(count)+".txt"
  283.         f = file(n, "w+")
  284.         f.write(str(price))
  285.         f.close()
  286. #endif
  287.  
  288.     def OnSelectEmptySlot(self, selectedSlotPos):
  289.  
  290.         isAttached = mouseModule.mouseController.isAttached()
  291.         if isAttached:
  292.             attachedSlotType = mouseModule.mouseController.GetAttachedType()
  293.             attachedSlotPos = mouseModule.mouseController.GetAttachedSlotNumber()
  294.             mouseModule.mouseController.DeattachObject()
  295.  
  296.             if player.SLOT_TYPE_INVENTORY != attachedSlotType and player.SLOT_TYPE_DRAGON_SOUL_INVENTORY != attachedSlotType:
  297.                 return
  298.             attachedInvenType = player.SlotTypeToInvenType(attachedSlotType)
  299.         #ifdef OFFLINE_SHOP
  300.             count = player.GetItemCount(attachedInvenType, attachedSlotPos)
  301.         #endif
  302.                
  303.             itemVNum = player.GetItemIndex(attachedInvenType, attachedSlotPos)
  304.             item.SelectItem(itemVNum)
  305.  
  306.             if item.IsAntiFlag(item.ANTIFLAG_GIVE) or item.IsAntiFlag(item.ANTIFLAG_MYSHOP):
  307.                 chat.AppendChat(chat.CHAT_TYPE_INFO, localeInfo.PRIVATE_SHOP_CANNOT_SELL_ITEM)
  308.                 return
  309.  
  310.             priceInputBoard = uiCommon.MoneyInputDialog()
  311.             priceInputBoard.SetTitle(localeInfo.PRIVATE_SHOP_INPUT_PRICE_DIALOG_TITLE)
  312.             priceInputBoard.SetAcceptEvent(ui.__mem_func__(self.AcceptInputPrice))
  313.             priceInputBoard.SetCancelEvent(ui.__mem_func__(self.CancelInputPrice))
  314.         #ifdef OFFLINE_SHOP
  315.             priceInputBoard.SetMaxLength(16)
  316.         #endif
  317.             priceInputBoard.Open()
  318.  
  319.         #ifdef OFFLINE_SHOP
  320.             itemPrice=self.ReadFilePrice(itemVNum,count)
  321.         #else
  322.             #itemPrice=GetPrivateShopItemPrice(itemVNum)
  323.         #endif
  324.  
  325.             if itemPrice>0:
  326.                 priceInputBoard.SetValue(itemPrice)
  327.  
  328.             self.priceInputBoard = priceInputBoard
  329.             self.priceInputBoard.itemVNum = itemVNum
  330.             self.priceInputBoard.sourceWindowType = attachedInvenType
  331.             self.priceInputBoard.sourceSlotPos = attachedSlotPos
  332.             self.priceInputBoard.targetSlotPos = selectedSlotPos
  333.  
  334.     def OnSelectItemSlot(self, selectedSlotPos):
  335.  
  336.         isAttached = mouseModule.mouseController.isAttached()
  337.         if isAttached:
  338.             snd.PlaySound("sound/ui/loginfail.wav")
  339.             mouseModule.mouseController.DeattachObject()
  340.  
  341.         else:
  342.             if not selectedSlotPos in self.itemStock:
  343.                 return
  344.  
  345.             invenType, invenPos = self.itemStock[selectedSlotPos]
  346.             shop.DelPrivateShopItemStock(invenType, invenPos)
  347.             snd.PlaySound("sound/ui/drop.wav")
  348.  
  349.             del self.itemStock[selectedSlotPos]
  350.  
  351.             self.Refresh()
  352.  
  353.     def AcceptInputPrice(self):
  354.  
  355.         if not self.priceInputBoard:
  356.             return True
  357.  
  358.         text = self.priceInputBoard.GetText()
  359.  
  360.         if not text:
  361.             return True
  362.  
  363.         if not text.isdigit():
  364.             return True
  365.  
  366.         if int(text) <= 0:
  367.             return True
  368.  
  369.         attachedInvenType = self.priceInputBoard.sourceWindowType
  370.         sourceSlotPos = self.priceInputBoard.sourceSlotPos
  371.         targetSlotPos = self.priceInputBoard.targetSlotPos
  372.  
  373.         for privatePos, (itemWindowType, itemSlotIndex) in self.itemStock.items():
  374.             if itemWindowType == attachedInvenType and itemSlotIndex == sourceSlotPos:
  375.                 shop.DelPrivateShopItemStock(itemWindowType, itemSlotIndex)
  376.                 del self.itemStock[privatePos]
  377.  
  378.         price = int(self.priceInputBoard.GetText())
  379.  
  380.         if IsPrivateShopItemPriceList():
  381.             SetPrivateShopItemPrice(self.priceInputBoard.itemVNum, price)
  382.  
  383.         shop.AddPrivateShopItemStock(attachedInvenType, sourceSlotPos, targetSlotPos, price)
  384.  
  385.     #ifdef OFFLINE_SHOP
  386.         count = player.GetItemCount(attachedInvenType, sourceSlotPos)
  387.         vnum = player.GetItemIndex(attachedInvenType, sourceSlotPos)
  388.         self.SaveFilePrice(vnum,count,price)
  389.     #end
  390.  
  391.         self.itemStock[targetSlotPos] = (attachedInvenType, sourceSlotPos)
  392.         snd.PlaySound("sound/ui/drop.wav")
  393.  
  394.         self.Refresh()     
  395.  
  396.         #####
  397.  
  398.         self.priceInputBoard = None
  399.         return True
  400.  
  401.     def CancelInputPrice(self):
  402.         self.priceInputBoard = None
  403.         return True
  404.  
  405.     def OnOk(self):
  406.  
  407.         if not self.title:
  408.             return
  409.  
  410.         if 0 == len(self.itemStock):
  411.             return
  412.  
  413.     #ifdef OFFLINE_SHOP
  414.         shop.BuildPrivateShop(self.title,self.days)
  415.     #else
  416.         #shop.BuildPrivateShop(self.title)
  417.     #endif
  418.         self.Close()
  419.  
  420.     def OnClose(self):
  421.         # shop.RefreshInventorySafa()
  422.         self.Close()
  423.  
  424.     def OnPressEscapeKey(self):
  425.         self.Close()
  426.         return True
  427.  
  428.     def OnOverInItem(self, slotIndex):
  429.  
  430.         if self.tooltipItem:
  431.             if self.itemStock.has_key(slotIndex):
  432.                 self.tooltipItem.SetPrivateShopBuilderItem(*self.itemStock[slotIndex] + (slotIndex,))
  433.  
  434.     def OnOverOutItem(self):
  435.  
  436.         if self.tooltipItem:
  437.             self.tooltipItem.HideToolTip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement