Advertisement
Heathcliff97

uiPrivateShopBuilder.py

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