Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.14 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. import time
  18. g_isBuildingPrivateShop = False
  19. g_itemPriceDict = {}
  20. g_privateShopAdvertisementBoardDict = {}
  21. g_privateShopAdvertisementLastViewed = {}
  22. g_displayedBoardVIDList = []
  23.  
  24. ##### DISABLE/ENABLE SHOP VISIT COLOR ###
  25. SHOP_VISIT=True
  26. ##### SHOP VISIT COLOR #####
  27. SHOP_VISIT_COLOR=0xFFff0000
  28. SHOP_NORMAL_COLOR=0xFFFFFFFF
  29. ###########################
  30.  
  31.  
  32. def Clear():
  33.     global g_itemPriceDict
  34.     global g_isBuildingPrivateShop
  35.     g_itemPriceDict={}
  36.     if app.ENABLE_CHEQUE_SYSTEM:
  37.         g_itemChequeDict={}
  38.     g_isBuildingPrivateShop = False
  39.  
  40. def IsPrivateShopItemPriceList():
  41.     return g_itemPriceDict
  42.  
  43. if app.ENABLE_CHEQUE_SYSTEM:
  44.     def IsPrivateShopItemChequeList():
  45.         global g_itemChequeDict
  46.         if g_itemChequeDict:
  47.             return True
  48.         else:
  49.             return False
  50.  
  51. def IsBuildingPrivateShop():
  52.     global g_isBuildingPrivateShop
  53.     return player.IsOpenPrivateShop() or g_isBuildingPrivateShop
  54.  
  55. def SetPrivateShopItemPrice(itemVNum, itemPrice):
  56.     global g_itemPriceDict
  57.     if app.ENABLE_CHEQUE_SYSTEM:
  58.         global g_itemChequeDict
  59.     g_itemPriceDict[int(itemVNum)]=itemPrice
  60.    
  61. def GetPrivateShopItemPrice(itemVNum):
  62.     try:
  63.         return g_itemPriceDict[itemVNum]
  64.     except KeyError:
  65.         return 0
  66.  
  67. if app.ENABLE_CHEQUE_SYSTEM:
  68.     def SetPrivateShopItemCheque(itemVNum, itemPrice):
  69.         global g_itemChequeDict
  70.         g_itemChequeDict[int(itemVNum)]=itemPrice
  71.  
  72.     def GetPrivateShopItemCheque(itemVNum):
  73.         try:
  74.             global g_itemChequeDict
  75.             return g_itemChequeDict[itemVNum]
  76.         except KeyError:
  77.             return 0
  78.  
  79. def DeleteADBoard(vid):
  80.     if not g_privateShopAdvertisementBoardDict.has_key(vid):
  81.         return
  82.    
  83.     global g_privateShopAdvertisementLastViewed
  84.     board = g_privateShopAdvertisementBoardDict[vid]
  85.     # If we had offline time, and it should persist, save it so that it does
  86.     if board.lastSeen > 0 and board.lastSeen + PrivateShopAdvertisementBoard.viewedPersistMinutes*60 > app.GetTime():
  87.         g_privateShopAdvertisementLastViewed[vid] = board.lastSeen
  88.  
  89.     del g_privateShopAdvertisementBoardDict[vid]
  90.  
  91. def ClearSign():
  92.     global g_privateShopAdvertisementBoardDict
  93.     global g_displayedBoardVIDList
  94.     global g_privateShopAdvertisementLastViewed
  95.     if g_privateShopAdvertisementBoardDict and  type(g_privateShopAdvertisementBoardDict) is dict:
  96.         for key,item in g_privateShopAdvertisementBoardDict:
  97.             DeleteADBoard(key)
  98.     g_privateShopAdvertisementBoardDict = {}
  99.     g_displayedBoardVIDList = {}
  100.     g_privateShopAdvertisementLastViewed = {}
  101.  
  102. def RefreshShopBoards():
  103.     global g_displayedBoardVIDList
  104.  
  105.     # Initialization
  106.     showSalesText = systemSetting.IsShowSalesText()
  107.     allBoardVIDList = g_privateShopAdvertisementBoardDict.keys()
  108.     dictKeys = []
  109.  
  110.     # Limit the display of shops to those around us if we've chosen to display all
  111.     if showSalesText:
  112.         boardsDist = {}
  113.         for vid in allBoardVIDList:
  114.             dist = player.GetCharacterDistance(vid)
  115.             if dist > 3000: # Too far!
  116.                 continue
  117.  
  118.             boardsDist[vid] = dist
  119.  
  120.         # This will get us a new keys tuple with the keys sorted
  121.         dictKeys = sorted(boardsDist, key=boardsDist.get)
  122.  
  123.         # And then we'll pick the first few (i.e those closest to us)
  124.         # ...as well as the board we are hovering / selected
  125.         # dictKeys = dictKeys[0:systemSetting.GetNearbyShopsDisplayed()]
  126.  
  127.     # We'll always display the selected target and the hovered target's names.
  128.     dictKeys = [player.GetTargetVID(), chr.Pick()] + dictKeys
  129.  
  130.     for vid in dictKeys:
  131.         if not vid in allBoardVIDList:
  132.             continue
  133.  
  134.         board = g_privateShopAdvertisementBoardDict[vid]
  135.         board.Show()
  136.         x, y = chr.GetProjectPosition(vid, 220)
  137.         board.SetPosition(x - board.GetWidth()/2, y - board.GetHeight()/2)
  138.  
  139.         # Return back to the color if it's required
  140.         if board.bg == "purple" and board.lastSeen + board.viewedPersistMinutes * 60 < app.GetTime():
  141.             board.MarkUnseen()
  142.    
  143.     # Hide the ones that were visible before but are not now
  144.     for vid in g_displayedBoardVIDList:
  145.         if vid not in dictKeys and vid in allBoardVIDList:
  146.             g_privateShopAdvertisementBoardDict[vid].Hide()
  147.  
  148.     # Update the visible board tuple
  149.     g_displayedBoardVIDList = dictKeys
  150.    
  151.  
  152. class PrivateShopAdvertisementBoard(ui.ThinBoard):
  153.     # These are the minutes for which the shop will display its different "viewed" color.
  154.     viewedPersistMinutes = 30
  155.  
  156.     def __init__(self):
  157.         ui.ThinBoard.__init__(self, "UI_BOTTOM")
  158.         self.vid = None
  159.         self.title = ""
  160.         self.bg = "new"
  161.         self.lastSeen = 0
  162.         self.__MakeTextLine()
  163.  
  164.     def __del__(self):
  165.         ui.ThinBoard.__del__(self)
  166.  
  167.     def __MakeTextLine(self):
  168.         self.textLine = ui.TextLine()
  169.         self.textLine.SetParent(self)
  170.         self.textLine.SetWindowHorizontalAlignCenter()
  171.         self.textLine.SetWindowVerticalAlignCenter()
  172.         self.textLine.SetHorizontalAlignCenter()
  173.         self.textLine.SetVerticalAlignCenter()
  174.         self.textLine.Show()
  175.  
  176.     def Open(self, vid, text):
  177.         self.vid = vid
  178.         self.title = text
  179.  
  180.         self.textLine.SetText(text)
  181.         self.textLine.UpdateRect()
  182.         self.SetSize(len(text)*6 + 10*2, 10)
  183.        
  184.            
  185.         if vid in g_privateShopAdvertisementLastViewed:
  186.             self.lastSeen = g_privateShopAdvertisementLastViewed[vid]
  187.             #self.UpdatePattern("purple")
  188.         g_privateShopAdvertisementBoardDict[vid] = self
  189.    
  190.     def UpdateTitle(self, text):
  191.         self.title = text
  192.  
  193.     def OnMouseLeftButtonUp(self):
  194.         if not self.vid:
  195.             return
  196.         self.MarkSeen()
  197.         net.SendOnClickPacket(self.vid)
  198.        
  199.         return True
  200.  
  201.     def UpdatePattern(self, name):
  202.         if self.bg == name:
  203.             return
  204.  
  205.         self.bg = name
  206.         self.SetPattern(name)
  207.         self.SetSize(len(self.title)*6 + 10*2, 10) # This is required because load image screws up the resulting image 
  208.  
  209.     def MarkUnseen(self):
  210.         #self.UpdatePattern("new")
  211.         self.textLine.SetPackedFontColor(SHOP_NORMAL_COLOR)
  212.  
  213.     def MarkSeen(self):
  214.         self.lastSeen = app.GetTime()
  215.         #self.UpdatePattern("purple")
  216.         self.textLine.SetPackedFontColor(SHOP_VISIT_COLOR)
  217.  
  218.     def Destroy(self):
  219.         DeleteADBoard(self.vid)
  220.     lastUpdateTime = 0 
  221.     def OnRender(self):
  222.         if time.time() - self.lastUpdateTime > 10000:
  223.             self.lastUpdateTime = time.time()
  224.             if not chr.HasInstance(self.vid):
  225.                 self.Hide()
  226.                 self.Destroy()
  227. class PrivateShopBuilder(ui.ScriptWindow):
  228.     def __init__(self):
  229.         ui.ScriptWindow.__init__(self)
  230.  
  231.         self.__LoadWindow()
  232.         self.itemStock = {}
  233.         self.tooltipItem = None
  234.         self.priceInputBoard = None
  235.         self.title = ""
  236.         self.days = 0
  237.  
  238.     def __del__(self):
  239.         ui.ScriptWindow.__del__(self)
  240.  
  241.     def __LoadWindow(self):
  242.         try:
  243.             pyScrLoader = ui.PythonScriptLoader()
  244.             pyScrLoader.LoadScriptFile(self, "UIScript/PrivateShopBuilder.py")
  245.         except:
  246.             import exception
  247.             exception.Abort("PrivateShopBuilderWindow.LoadWindow.LoadObject")
  248.  
  249.         try:
  250.             GetObject = self.GetChild
  251.             self.nameLine = GetObject("NameLine")
  252.             self.itemSlot = GetObject("ItemSlot")
  253.             self.btnOk = GetObject("OkButton")
  254.             self.btnClose = GetObject("CloseButton")
  255.             self.titleBar = GetObject("TitleBar")
  256.         except:
  257.             import exception
  258.             exception.Abort("PrivateShopBuilderWindow.LoadWindow.BindObject")
  259.  
  260.         self.btnOk.SetEvent(ui.__mem_func__(self.OnOk))
  261.         self.btnClose.SetEvent(ui.__mem_func__(self.OnClose))
  262.         self.titleBar.SetCloseEvent(ui.__mem_func__(self.OnClose))
  263.  
  264.         self.itemSlot.SetSelectEmptySlotEvent(ui.__mem_func__(self.OnSelectEmptySlot))
  265.         self.itemSlot.SetSelectItemSlotEvent(ui.__mem_func__(self.OnSelectItemSlot))
  266.         self.itemSlot.SetOverInItemEvent(ui.__mem_func__(self.OnOverInItem))
  267.         self.itemSlot.SetOverOutItemEvent(ui.__mem_func__(self.OnOverOutItem))
  268.  
  269.     def Destroy(self):
  270.         self.ClearDictionary()
  271.  
  272.         self.days = 0
  273.         self.nameLine = None
  274.         self.itemSlot = None
  275.         self.btnOk = None
  276.         self.btnClose = None
  277.         self.titleBar = None
  278.         self.priceInputBoard = None
  279.  
  280.     def Open(self, title, days):
  281.  
  282.         self.days = days
  283.         self.title = title
  284.  
  285.         if len(title) > 25:
  286.             title = title[:22] + "..."
  287.  
  288.         self.itemStock = {}
  289.         shop.ClearPrivateShopStock()
  290.         self.nameLine.SetText(title)
  291.         self.SetCenterPosition()
  292.         self.Refresh()
  293.         self.Show()
  294.  
  295.         global g_isBuildingPrivateShop
  296.         g_isBuildingPrivateShop = True
  297.  
  298.     def Close(self):
  299.         global g_isBuildingPrivateShop
  300.         g_isBuildingPrivateShop = False
  301.  
  302.         self.title = ""
  303.         self.days = 0
  304.         self.itemStock = {}
  305.         shop.ClearPrivateShopStock()
  306.         self.Hide()
  307.         if self.priceInputBoard:
  308.             self.priceInputBoard.Close()
  309.             self.priceInputBoard = None
  310.  
  311.     def SetItemToolTip(self, tooltipItem):
  312.         self.tooltipItem = tooltipItem
  313.  
  314.     def Refresh(self):
  315.         getitemVNum=player.GetItemIndex
  316.         getItemCount=player.GetItemCount
  317.         setitemVNum=self.itemSlot.SetItemSlot
  318.         delItem=self.itemSlot.ClearSlot
  319.  
  320.         for i in xrange(shop.SHOP_SLOT_COUNT):
  321.  
  322.             if not self.itemStock.has_key(i):
  323.                 delItem(i)
  324.                 continue
  325.  
  326.             pos = self.itemStock[i]
  327.  
  328.             itemCount = getItemCount(*pos)
  329.             if itemCount <= 1:
  330.                 itemCount = 0
  331.             setitemVNum(i, getitemVNum(*pos), itemCount)
  332.  
  333.         self.itemSlot.RefreshSlot()
  334.  
  335.     def ReadFilePrice(self,vnum,count):
  336.         d = "shops"
  337.         if not os.path.exists(d):
  338.             os.makedirs(d)
  339.         oldPrice=0
  340.         n=d+"/"+str(vnum)+"_"+str(count)+".txt"
  341.         if os.path.exists(n):
  342.             fd = open( n,'r')
  343.             oldPrice=int(fd.readlines()[0])
  344.            
  345.         return oldPrice
  346.     def SaveFilePrice(self,vnum,count,price):
  347.         d = "shops"
  348.         if not os.path.exists(d):
  349.             os.makedirs(d)
  350.         n=d+"/"+str(vnum)+"_"+str(count)+".txt"
  351.         f = file(n, "w+")
  352.         f.write(str(price))
  353.         f.close()
  354.  
  355.     def OnSelectEmptySlot(self, selectedSlotPos):
  356.  
  357.         isAttached = mouseModule.mouseController.isAttached()
  358.         if isAttached:
  359.             attachedSlotType = mouseModule.mouseController.GetAttachedType()
  360.             attachedSlotPos = mouseModule.mouseController.GetAttachedSlotNumber()
  361.             mouseModule.mouseController.DeattachObject()
  362.  
  363.             if player.SLOT_TYPE_INVENTORY != attachedSlotType and player.SLOT_TYPE_DRAGON_SOUL_INVENTORY != attachedSlotType:
  364.                 return
  365.             attachedInvenType = player.SlotTypeToInvenType(attachedSlotType)
  366.  
  367.             itemVNum = player.GetItemIndex(attachedInvenType, attachedSlotPos)
  368.             count = player.GetItemCount(attachedInvenType, attachedSlotPos)
  369.             item.SelectItem(itemVNum)
  370.  
  371.             if item.IsAntiFlag(item.ANTIFLAG_GIVE) or item.IsAntiFlag(item.ANTIFLAG_MYSHOP):
  372.                 chat.AppendChat(chat.CHAT_TYPE_INFO, localeInfo.PRIVATE_SHOP_CANNOT_SELL_ITEM)
  373.                 return
  374.             priceInputBoard = uiCommon.MoneyInputDialog()
  375.             priceInputBoard.SetTitle(localeInfo.PRIVATE_SHOP_INPUT_PRICE_DIALOG_TITLE)
  376.             priceInputBoard.SetAcceptEvent(ui.__mem_func__(self.AcceptInputPrice))
  377.             priceInputBoard.SetCancelEvent(ui.__mem_func__(self.CancelInputPrice))
  378.             priceInputBoard.Open()
  379.  
  380.             itemPrice=self.ReadFilePrice(itemVNum,count)
  381.  
  382.             if itemPrice>0:
  383.                 priceInputBoard.SetValue(itemPrice)
  384.  
  385.             if app.ENABLE_CHEQUE_SYSTEM:
  386.                 itemCheque=GetPrivateShopItemCheque(itemVNum)
  387.                 if itemCheque>0:
  388.                     priceInputBoard.SetValueCheque(itemCheque)
  389.  
  390.             self.priceInputBoard = priceInputBoard
  391.             self.priceInputBoard.itemVNum = itemVNum
  392.             self.priceInputBoard.sourceWindowType = attachedInvenType
  393.             self.priceInputBoard.sourceSlotPos = attachedSlotPos
  394.             self.priceInputBoard.targetSlotPos = selectedSlotPos
  395.  
  396.     def OnSelectItemSlot(self, selectedSlotPos):
  397.  
  398.         isAttached = mouseModule.mouseController.isAttached()
  399.         if isAttached:
  400.             snd.PlaySound("sound/ui/loginfail.wav")
  401.             mouseModule.mouseController.DeattachObject()
  402.  
  403.         else:
  404.             if not selectedSlotPos in self.itemStock:
  405.                 return
  406.  
  407.             invenType, invenPos = self.itemStock[selectedSlotPos]
  408.             shop.DelPrivateShopItemStock(invenType, invenPos)
  409.             snd.PlaySound("sound/ui/drop.wav")
  410.             del self.itemStock[selectedSlotPos]
  411.  
  412.             self.Refresh()
  413.  
  414.     def AcceptInputPrice(self):
  415.  
  416.         if not self.priceInputBoard:
  417.             return True
  418.  
  419.         if app.ENABLE_CHEQUE_SYSTEM:
  420.             text = self.priceInputBoard.GetText()
  421.             cheque = self.priceInputBoard.GetTextCheque()
  422.             if not text:
  423.                 return
  424.             if not text.isdigit():
  425.                 return
  426.  
  427.             if not cheque:
  428.                 return
  429.             if not cheque.isdigit():
  430.                 return
  431.  
  432.             if int(cheque) <=0 and int(text)<=0:
  433.                 chat.AppendChat(chat.CHAT_TYPE_INFO, localeInfo.CHEQUE_NO_ADD_SALE_PRICE)
  434.                 return
  435.         else:
  436.             text = self.priceInputBoard.GetText()
  437.  
  438.             if not text:
  439.                 return True
  440.  
  441.             if not text.isdigit():
  442.                 return True
  443.  
  444.             if int(text) <= 0:
  445.                 return True
  446.  
  447.         attachedInvenType = self.priceInputBoard.sourceWindowType
  448.         sourceSlotPos = self.priceInputBoard.sourceSlotPos
  449.         targetSlotPos = self.priceInputBoard.targetSlotPos
  450.  
  451.         for privatePos, (itemWindowType, itemSlotIndex) in self.itemStock.items():
  452.             if itemWindowType == attachedInvenType and itemSlotIndex == sourceSlotPos:
  453.                 shop.DelPrivateShopItemStock(itemWindowType, itemSlotIndex)
  454.                 del self.itemStock[privatePos]
  455.  
  456.         price = int(self.priceInputBoard.GetText())
  457.  
  458.         if IsPrivateShopItemPriceList():
  459.             SetPrivateShopItemPrice(self.priceInputBoard.itemVNum, price)
  460.  
  461.         if app.ENABLE_CHEQUE_SYSTEM:
  462.             chequep = int(self.priceInputBoard.GetTextCheque())
  463.             if IsPrivateShopItemChequeList():
  464.                 SetPrivateShopItemCheque(self.priceInputBoard.itemVNum, chequep)
  465.             shop.AddPrivateShopItemStock(attachedInvenType, sourceSlotPos, targetSlotPos, price, chequep)
  466.         else:
  467.             shop.AddPrivateShopItemStock(attachedInvenType, sourceSlotPos, targetSlotPos, price)
  468.         count = player.GetItemCount(attachedInvenType, sourceSlotPos)
  469.         vnum = player.GetItemIndex(attachedInvenType, sourceSlotPos)
  470.         self.SaveFilePrice(vnum,count,price)
  471.         self.itemStock[targetSlotPos] = (attachedInvenType, sourceSlotPos)
  472.         snd.PlaySound("sound/ui/drop.wav")
  473.  
  474.         self.Refresh()
  475.         self.priceInputBoard = None
  476.         return True
  477.  
  478.     def CancelInputPrice(self):
  479.         if self.priceInputBoard:
  480.             self.priceInputBoard.Close()
  481.         self.priceInputBoard = None
  482.         return 1
  483.  
  484.     def OnOk(self):
  485.  
  486.         if not self.title:
  487.             return
  488.  
  489.         if 0 == len(self.itemStock):
  490.             return
  491.  
  492.         shop.BuildPrivateShop(self.title,self.days)
  493.         self.Close()
  494.  
  495.     def OnClose(self):
  496.         self.Close()
  497.  
  498.     def OnPressEscapeKey(self):
  499.         self.Close()
  500.         return True
  501.  
  502.     def OnOverInItem(self, slotIndex):
  503.         if self.tooltipItem:
  504.             if self.itemStock.has_key(slotIndex):
  505.                 self.tooltipItem.SetPrivateShopBuilderItem(*self.itemStock[slotIndex] + (slotIndex,))
  506.    
  507.     def OnOverOutItem(self):
  508.  
  509.         if self.tooltipItem:
  510.             self.tooltipItem.HideToolTip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement