NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

copyContracts.py (6169B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\contracts\copyContracts.py
      2 from collections import defaultdict
      3 import evetypes
      4 from brennivin.itertoolsext import Bundle
      5 from inventorycommon.const import categoryBlueprint, flagHangar
      6 
      7 def GetByLabelX(messageLabel, *args, **kwargs):
      8     from localization import GetByLabel
      9     return GetByLabel(messageLabel, *args, **kwargs)
     10 
     11 
     12 class CopyContractObject(object):
     13 
     14     def __init__(self, startStation, forCorp, contractItems, getItemsInStationFunc, corpFlagID):
     15         self.startStation = startStation
     16         self.forCorp = forCorp
     17         self.contractItems = contractItems
     18         self.getItemsInStationFunc = getItemsInStationFunc
     19         self.corpFlagID = corpFlagID
     20 
     21     def FindItemToSellInHangar(self, itemsToSell, sellBlueprintCopies, sellBlueprintOriginals):
     22         itemsToSellCopy = itemsToSell.copy()
     23         itemsInStation = self.GetItemsForCorrectFlag()
     24         itemsInStationByTypeID = defaultdict(list)
     25         for eachItem in itemsInStation:
     26             itemsInStationByTypeID[eachItem.typeID].append(eachItem)
     27 
     28         foundItems = self._GetFoundItems(itemsInStationByTypeID, itemsToSellCopy)
     29         foundBPC = self._GetFoundBlueprints(sellBlueprintCopies, itemsInStationByTypeID, True)
     30         foundBPO = self._GetFoundBlueprints(sellBlueprintOriginals, itemsInStationByTypeID, False)
     31         return (foundItems, foundBPC, foundBPO)
     32 
     33     def _GetFoundItems(self, itemsInStationByTypeID, itemsToSellCopy):
     34         foundItems = defaultdict(list)
     35         for sellTypeID, sellQty in itemsToSellCopy.items():
     36             itemsByType = itemsInStationByTypeID.get(sellTypeID, [])
     37             foundItemsForSell = []
     38             for each in itemsByType:
     39                 if each.itemID == session.shipid:
     40                     continue
     41                 if sellQty == each.stacksize:
     42                     foundItemsForSell.append(each)
     43                     itemsToSellCopy.pop(sellTypeID)
     44                     break
     45 
     46             if foundItemsForSell:
     47                 foundItems[sellTypeID] = foundItemsForSell
     48 
     49         return foundItems
     50 
     51     def _GetFoundBlueprints(self, sellBlueprints, itemsInStationByTypeID, findingCopies):
     52         foundBluprints = defaultdict(list)
     53         for eachBPCTypeID, qty in sellBlueprints.iteritems():
     54             itemsByType = itemsInStationByTypeID.get(eachBPCTypeID, [])
     55             itemsByType.sort(key=lambda x: x.stacksize)
     56             itemQty = 0
     57             for each in itemsByType:
     58                 if eachBPCTypeID in foundBluprints and len(foundBluprints[eachBPCTypeID]) >= qty:
     59                     break
     60                 if IsRightBlueprintVersion(each, findingCopies):
     61                     if itemQty + each.stacksize > qty:
     62                         break
     63                     foundBluprints[eachBPCTypeID].append(each)
     64                     itemQty += each.stacksize
     65 
     66         return foundBluprints
     67 
     68     def GetItemsForCorrectFlag(self):
     69         correctFlagID = flagHangar if not self.forCorp else self.corpFlagID
     70         itemsInStation = self.getItemsInStationFunc(self.startStation, self.forCorp)
     71         itemsInFlag = {x for x in itemsInStation if x.flagID == correctFlagID}
     72         return itemsInFlag
     73 
     74     def GetSellAndRequestedItems(self):
     75         requestItems = defaultdict(int)
     76         sellItems = defaultdict(int)
     77         sellBlueprintCopies = defaultdict(int)
     78         sellBlueprintOriginals = defaultdict(int)
     79         for contractItem in self.contractItems:
     80             if contractItem.inCrate:
     81                 sellDictToUse = sellItems
     82                 if IsBlueprint(contractItem.itemTypeID):
     83                     if IsItemBlueprintCopy(contractItem):
     84                         sellDictToUse = sellBlueprintCopies
     85                     else:
     86                         sellDictToUse = sellBlueprintOriginals
     87                 sellDictToUse[contractItem.itemTypeID] += contractItem.quantity
     88             else:
     89                 requestItems[contractItem.itemTypeID] += contractItem.quantity
     90 
     91         return (sellItems,
     92          sellBlueprintCopies,
     93          sellBlueprintOriginals,
     94          requestItems)
     95 
     96     def FindItemsInCorrectFlag(self, foundItemsByFlagID):
     97         if not self.forCorp:
     98             if len(foundItemsByFlagID) > 1:
     99                 raise UserError('A character that has found more than one hangar to pick from? ')
    100             return foundItemsByFlagID[flagHangar]
    101         return foundItemsByFlagID.get(self.corpFlagID, [])
    102 
    103 
    104 def IsBlueprint(typeID):
    105     return evetypes.GetCategoryID(typeID) == categoryBlueprint
    106 
    107 
    108 def IsRightBlueprintVersion(item, findingCopies):
    109     if findingCopies:
    110         return IsItemBlueprintCopy(item)
    111     else:
    112         return not IsItemBlueprintCopy(item)
    113 
    114 
    115 def IsItemBlueprintCopy(item):
    116     typeID = getattr(item, 'itemTypeID', None) or getattr(item, 'typeID', None)
    117     if not IsBlueprint(typeID):
    118         return False
    119     isCopy = item.get('copy', None) if isinstance(item, Bundle) else getattr(item, 'copy', None)
    120     if isCopy is not None:
    121         return isCopy
    122     if item.quantity == -2:
    123         return True
    124     return False
    125 
    126 
    127 def FindMissingTypesAndQty(foundItems, sellItems):
    128     notFound = []
    129     for typeID, quantity in sellItems.iteritems():
    130         foundItemsByType = foundItems.get(typeID, [])
    131         for item in foundItemsByType:
    132             if item.typeID == typeID and item.stacksize == quantity:
    133                 break
    134         else:
    135             notFound.append((typeID, quantity))
    136 
    137     return notFound
    138 
    139 
    140 def FindMissingBlueprints(foundItems, sellItems):
    141     notFound = []
    142     for typeID, quantity in sellItems.iteritems():
    143         qtyFound = 0
    144         foundItemsByType = foundItems.get(typeID, [])
    145         for item in foundItemsByType:
    146             if item.typeID == typeID:
    147                 qtyFound += item.stacksize
    148 
    149         if qtyFound != quantity:
    150             notFound.append((typeID, quantity - qtyFound))
    151 
    152     return notFound
    153 
    154 
    155 def GetMissingTypesTxt(notFound):
    156     textList = []
    157     for t, q in notFound:
    158         t = u'  \u2022' + GetByLabelX('UI/Contracts/ContractsService/NameXQuantity', typeID=t, quantity=q)
    159         textList.append(t)
    160 
    161     return '<br>'.join(textList)