walletSvc.py (7323B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\shared\neocom\wallet\walletSvc.py 2 import logging 3 import blue 4 from carbon.common.script.sys.service import Service 5 from carbon.common.script.util.logUtil import LogInfo 6 from eve.client.script.ui.shared.neocom.wallet import walletConst, walletSignals 7 from eve.client.script.ui.shared.neocom.wallet.noneNPCAccountOwnerDialog import NoneNPCAccountOwnerDialog 8 from eve.client.script.ui.shared.neocom.wallet.transferMoneyWnd import TransferMoneyWnd 9 from eve.client.script.ui.shared.neocom.wallet.walletUtil import GetSettingValue, HaveAccessToCorpWallet, HaveReadAccessToCorpWalletDivision 10 from eve.client.script.ui.shared.neocom.wallet.walletWnd import WalletWindow 11 from eve.common.lib import appConst 12 from eveaccounting import get_accounting_key_for_name 13 from eveexceptions import UserError 14 from localization import GetByLabel 15 from plex.client.api.signal import on_balance_changed 16 logger = logging.getLogger(__name__) 17 18 class WalletSvc(Service): 19 __guid__ = 'svc.wallet' 20 __notifyevents__ = ['OnBillReceived', 21 'OnAccountChange', 22 'OnShareChange', 23 'ProcessSessionReset'] 24 __servicename__ = 'wallet' 25 __displayname__ = 'Wallet Client Service' 26 __dependencies__ = ['window'] 27 __update_on_reload__ = 0 28 29 def __init__(self): 30 super(WalletSvc, self).__init__() 31 self._wealth = None 32 self.corpWealthByAccountKey = {} 33 34 def Run(self, memStream = None): 35 self.LogInfo('Starting Wallet') 36 self._wealth = sm.RemoteSvc('account').GetCashBalance(0) 37 on_balance_changed.connect(self._OnPLEXBalanceChange) 38 39 @property 40 def wealth(self): 41 if self._wealth is None: 42 self._wealth = sm.RemoteSvc('account').GetCashBalance(0) 43 return self._wealth 44 45 def ProcessSessionReset(self): 46 self._wealth = None 47 self.corpWealthByAccountKey = {} 48 49 def OnBillReceived(self, *args): 50 if not HaveAccessToCorpWallet(): 51 return 52 if GetSettingValue('notifyBillChange'): 53 sm.GetService('neocom').Blink('wallet', GetByLabel('UI/Neocom/Blink/NewBillReceived')) 54 self.TriggerOnCorpBillsChanged() 55 56 def OnShareChange(self, shareholderID, corporationID, change): 57 if session.charid == shareholderID: 58 self._OnShareChangeChar() 59 elif session.corpid in (shareholderID, corporationID) and HaveAccessToCorpWallet(): 60 self._OnShareChangeCorp() 61 62 def _OnShareChangeCorp(self): 63 if GetSettingValue('notifyShareChangeCorp'): 64 sm.GetService('neocom').Blink('wallet', GetByLabel('UI/Neocom/Blink/CorpSharesChanged')) 65 66 def _OnShareChangeChar(self): 67 if GetSettingValue('notifyShareChange'): 68 sm.GetService('neocom').Blink('wallet', GetByLabel('UI/Neocom/Blink/SharesChanged')) 69 70 def OnAccountChange(self, accountKey, ownerID, balance): 71 LogInfo('Wallet::OnAccountChange', accountKey, ownerID, balance) 72 if balance is not None: 73 if accountKey == 'cash' and ownerID == session.charid: 74 self._OnCharAccountChange(balance) 75 elif accountKey in ('cash', 'cash2', 'cash3', 'cash4', 'cash5', 'cash6', 'cash7') and ownerID == session.corpid: 76 self._OnCorpAccountChange(accountKey, balance) 77 sm.ScatterEvent('OnAccountChange_Local', accountKey, ownerID, balance) 78 79 def _OnCharAccountChange(self, newBalance): 80 if GetSettingValue('notifyAccountChange'): 81 sm.GetService('neocom').Blink('wallet', GetByLabel('UI/Neocom/Blink/ISKBalanceChanged')) 82 self.BlinkPersonalWallet(walletConst.PANEL_TRANSACTIONS) 83 difference = newBalance - self.wealth 84 sm.ScatterEvent('OnPersonalAccountChangedClient', newBalance, difference) 85 self._wealth = newBalance 86 walletSignals.on_personal_isk_balance_changed(newBalance, difference) 87 88 def _OnCorpAccountChange(self, accountKey, balance): 89 if not HaveAccessToCorpWallet(): 90 return 91 corpAccountKey = self._GetCorpAccountKey(accountKey) 92 if not HaveReadAccessToCorpWalletDivision(corpAccountKey): 93 return 94 if GetSettingValue('notifyAccountChangeCorp'): 95 sm.GetService('neocom').Blink('wallet', GetByLabel('UI/Neocom/Blink/CorpISKBalanceChanged')) 96 self.BlinkCorpWallet() 97 difference = balance - self.GetCorpWealthCached1Min(corpAccountKey) 98 self.UpdateCachedBalance(corpAccountKey, balance) 99 sm.ScatterEvent('OnCorpAccountChangedClient', balance, corpAccountKey, difference) 100 walletSignals.on_corp_isk_balance_changed(balance, corpAccountKey, difference) 101 102 def _OnPLEXBalanceChange(self, *args): 103 if GetSettingValue('notifyPlexBalanceChange'): 104 sm.GetService('neocom').Blink('wallet', GetByLabel('UI/Neocom/Blink/PlexBalanceChanged')) 105 106 def _GetCorpAccountKey(self, accountKey): 107 return get_accounting_key_for_name(accountKey, 1000) 108 109 def PayBill(self, bill): 110 if bill.debtorID == session.charid: 111 sm.RemoteSvc('billMgr').CharPayBill(bill.billID) 112 elif bill.debtorID == session.corpid: 113 if appConst.corpRoleAccountant & session.corprole == appConst.corpRoleAccountant: 114 sm.RemoteSvc('billMgr').PayCorporationBill(bill.billID, fromAccountKey=session.corpAccountKey) 115 self.TriggerOnCorpBillsChanged() 116 elif bill.debtorID == session.allianceid: 117 if session.allianceid and appConst.corpRoleAccountant & session.corprole == appConst.corpRoleAccountant: 118 sm.GetService('alliance').PayBill(bill.billID, fromAccountKey=session.corpAccountKey) 119 self.TriggerOnCorpBillsChanged() 120 121 def TriggerOnCorpBillsChanged(self): 122 sm.ScatterEvent('OnCorpWalletBillsChanged') 123 124 def TransferMoney(self, fromID, fromAccountKey, toID, toAccountKey): 125 if toID is None: 126 dlg = NoneNPCAccountOwnerDialog.Open() 127 dlg.ShowModal() 128 if not dlg.ownerID: 129 return 130 toID = dlg.ownerID 131 TransferMoneyWnd.CloseIfOpen() 132 TransferMoneyWnd.Open(fromID=fromID, fromAccountKey=fromAccountKey, toID=toID, toAccountKey=toAccountKey) 133 134 def GetWealth(self): 135 return self.wealth 136 137 def GetCorpWealth(self, accountKey = None): 138 try: 139 return sm.RemoteSvc('account').GetCashBalance(1, accountKey=accountKey) 140 except UserError: 141 logger.exception('Unable to fetch corp wealth') 142 return 0 143 144 def GetCorpWealthCached1Min(self, accountKey): 145 balance, cacheTime = self.corpWealthByAccountKey.get(accountKey, (None, None)) 146 if balance is None or blue.os.GetTime() - cacheTime > appConst.MIN: 147 self.corpWealthByAccountKey[accountKey] = (self.GetCorpWealth(accountKey), blue.os.GetTime()) 148 return self.corpWealthByAccountKey[accountKey][0] 149 150 def UpdateCachedBalance(self, accountKey, balance): 151 self.corpWealthByAccountKey[accountKey] = (balance, blue.os.GetTime()) 152 153 def BlinkPersonalWallet(self, panelID = None): 154 WalletWindow.BlinkPersonalWallet(panelID) 155 156 def BlinkCorpWallet(self, subtabname = None, subsubtabname = None): 157 WalletWindow.BlinkCorpWallet(subtabname)