voting.py (11049B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\services\corporation\voting.py 2 from carbon.common.script.sys.crowset import CRowset 3 from carbon.common.script.sys.service import Service 4 from corporation.voting import * 5 from eve.client.script.ui.shared.neocom.corporation import corpUISignals 6 from locks import RLock 7 from itertools import chain 8 from caching.memoize import Memoize 9 from collections import defaultdict 10 from gametime import GetWallclockTime 11 from eve.common.lib import appConst as const 12 from eve.common.script.net.eveMoniker import GetCorpRegistry 13 14 class CorporationVotes(Service): 15 __servicename__ = 'corpvotes' 16 __displayname__ = 'Corporation Voting Service' 17 __guid__ = 'svc.corpvotes' 18 __notifyevents__ = ['OnVoteCaseChanged', 19 'OnVoteCast', 20 'DoSessionChanging', 21 'OnShareChange', 22 'OnCorporationChanged', 23 'OnSanctionedActionChanged'] 24 25 def __init__(self): 26 Service.__init__(self) 27 self._voteCases = None 28 self.voteCaseOptions = {} 29 self.votesByVoteCaseID = {} 30 self.sanctionedActionsByState = defaultdict(list) 31 self.canRunForCEO = None 32 self.canViewVotes = None 33 self.canVote = None 34 self.remoteSvc = None 35 self.readlock = None 36 self._voteCaseHeader = None 37 38 def Run(self, *args): 39 Service.Run(self, args) 40 self.readlock = RLock('corpvotes') 41 self.remoteSvc = sm.RemoteSvc('voteManager') 42 43 def FlushAll(self): 44 self._voteCases = None 45 self.voteCaseOptions.clear() 46 self.votesByVoteCaseID.clear() 47 self.canRunForCEO = None 48 self.canViewVotes = None 49 self.canVote = None 50 self.sanctionedActionsByState.clear() 51 self._GetShares.clear_memoized() 52 53 @property 54 def voteCases(self): 55 with self.readlock: 56 if self._voteCases is None: 57 self._voteCases = self.remoteSvc.GetVoteCasesByCorporation(session.corpid) 58 return self._voteCases 59 60 @property 61 def voteCaseHeader(self): 62 if self._voteCaseHeader is None: 63 for vc in chain(self.voteCases.itervalues()): 64 self._voteCaseHeader = vc.header 65 break 66 67 return self._voteCaseHeader 68 69 def OnVoteCaseChanged(self, corporationID, voteCaseID): 70 if session.corpid == corporationID: 71 self._voteCases = None 72 self.voteCaseOptions.pop(voteCaseID, None) 73 self.votesByVoteCaseID.pop(voteCaseID, None) 74 corpUISignals.on_vote_case_changed(corporationID, voteCaseID) 75 76 def OnVoteCast(self, corporationID, voteCaseID): 77 if corporationID == session.corpid: 78 self.votesByVoteCaseID.pop(voteCaseID, None) 79 corpUISignals.on_vote_cast(corporationID, voteCaseID) 80 81 def OnShareChange(self, shareholderID, corporationID, _): 82 if shareholderID == session.charid or corporationID == session.corpid: 83 self.FlushAll() 84 85 def DoSessionChanging(self, _, __, change): 86 if 'corpid' in change: 87 self.FlushAll() 88 89 def OnCorporationChanged(self, corpID, change): 90 if corpID == session.corpid: 91 self.FlushAll() 92 93 def OnSanctionedActionChanged(self, corporationID, voteCaseID): 94 self.sanctionedActionsByState.clear() 95 corpUISignals.on_sanctioned_action_changed(corporationID, voteCaseID) 96 97 def GetVoteCasesByCorporation(self, corporationID, status = VOTECASE_STATUS_ALL): 98 if corporationID == session.corpid: 99 voteCases = self.voteCases 100 else: 101 voteCases = self.remoteSvc.GetVoteCasesByCorporation(corporationID, VOTECASE_STATUS_ALL) 102 try: 103 return voteCases[status] 104 except KeyError: 105 return CRowset(self.voteCaseHeader, chain(voteCases[VOTECASE_STATUS_OPEN], voteCases[VOTECASE_STATUS_CLOSED])) 106 107 def GetVoteCase(self, corporationID, voteCaseID): 108 for vc in chain(self.voteCases[VOTECASE_STATUS_OPEN], self.voteCases[VOTECASE_STATUS_CLOSED]): 109 if vc.voteCaseID == voteCaseID: 110 return vc 111 112 vc = self.remoteSvc.GetVoteCase(corporationID, voteCaseID) 113 status = VOTECASE_STATUS_OPEN if vc.endDateTime > GetWallclockTime() else VOTECASE_STATUS_CLOSED 114 self._voteCases[status].append(vc) 115 return vc 116 117 def GetVoteCaseOptions(self, voteCaseID, corporationID = None): 118 if corporationID is None: 119 corporationID = session.corpid 120 if corporationID == session.corpid: 121 with self.readlock: 122 if voteCaseID not in self.voteCaseOptions: 123 self.voteCaseOptions[voteCaseID] = self.remoteSvc.GetVoteCaseOptions(corporationID, voteCaseID) 124 return self.voteCaseOptions[voteCaseID] 125 return self.remoteSvc.GetVoteCaseOptions(corporationID, voteCaseID) 126 127 def GetVotes(self, corporationID, voteCaseID): 128 if corporationID == session.corpid: 129 with self.readlock: 130 if voteCaseID not in self.votesByVoteCaseID: 131 self.votesByVoteCaseID[voteCaseID] = self.remoteSvc.GetVotes(voteCaseID) 132 return self.votesByVoteCaseID[voteCaseID] 133 return self.remoteSvc.GetVotes(voteCaseID) 134 135 def CanViewVotes(self, corporationID): 136 if corporationID == session.corpid: 137 with self.readlock: 138 if self.canViewVotes is None: 139 self.canViewVotes = self.remoteSvc.CanViewVotes(session.corpid) 140 return self.canViewVotes 141 return self.remoteSvc.CanViewVotes(corporationID or session.corpid) 142 143 def CanVote(self, corporationID): 144 if corporationID == session.corpid: 145 with self.readlock: 146 if self.canVote is None: 147 self.canVote = self._CanVote(corporationID) 148 return self.canVote 149 return self._CanVote(corporationID) 150 151 def _CanVote(self, corporationID): 152 isCEOTypePerson = sm.GetService('corp').GetCorporation().ceoID == session.charid 153 if session.corpid == corporationID and isCEOTypePerson: 154 return True 155 personalShares, _ = self._GetShares() 156 if corporationID in personalShares: 157 return True 158 if isCEOTypePerson and session.corpid != corporationID and session.corpid in GetCorpRegistry().GetShareholders(corporationID): 159 return True 160 return False 161 162 @Memoize 163 def _GetShares(self): 164 corpShares = [] 165 personalShares = sm.GetService('corp').GetSharesByShareholder() 166 if session.corprole & (const.corpRoleAccountant | const.corpRoleJuniorAccountant): 167 corpShares = sm.GetService('corp').GetSharesByShareholder(1) 168 return (personalShares, corpShares) 169 170 def CanRunForCEO(self): 171 if not sm.GetService('skills').GetSkill(const.typeCorporationManagement): 172 return 0 173 if self.canRunForCEO is None: 174 self.canRunForCEO = self.remoteSvc.CanRunForCEO() 175 return self.canRunForCEO 176 177 def CreateVote(self, voteData): 178 voteCaseText = voteData['title'] 179 description = voteData['description'] 180 voteType = voteData['votetype'] 181 duration = voteData['time'] 182 if voteType == voteShares: 183 options = (('UI/Corporations/CorporationWindow/Politics/CreateShares', {'shares': voteData['shares']}), 184 ('UI/Corporations/CorporationWindow/Politics/DoNotCreateShares',), 185 voteData['shares'], 186 None, 187 None) 188 elif voteType == voteItemLockdown: 189 options = (('UI/Corporations/CorporationWindow/Politics/LockdownItem', {'item': voteData['typeID']}), 190 ('UI/Corporations/CorporationWindow/Politics/DontLockdownItem', {'item': voteData['typeID']}), 191 voteData['itemID'], 192 voteData['typeID'], 193 voteData['locationID']) 194 elif voteType == voteItemUnlock: 195 options = (('UI/Corporations/CorporationWindow/Politics/UnlockItem', {'item': voteData['typeID']}), 196 ('UI/Corporations/CorporationWindow/Politics/DontUnlockItem', {'item': voteData['typeID']}), 197 voteData['itemID'], 198 voteData['typeID'], 199 voteData['locationID']) 200 elif voteType == voteKickMember: 201 options = (('UI/Corporations/CorporationWindow/Politics/ExpelFromCorporation', {'char': voteData['kickmember']}), 202 ('UI/Corporations/CorporationWindow/Politics/DontExpelFromCorporation', {'memberName': voteData['memberName']}), 203 voteData['kickmember'], 204 None, 205 None) 206 elif voteType == voteCEO: 207 options = (('UI/Corporations/CorporationWindow/Politics/SomeoneForCEO', {'char': session.charid}), 208 ('UI/Corporations/CorporationWindow/Politics/DontChangeCEO',), 209 session.charid, 210 None, 211 None) 212 elif voteType == voteGeneral: 213 options = [ option for option in voteData['options'] ] 214 else: 215 self.LogError('Unknown Vote type %d' % voteType) 216 return 217 self.remoteSvc.InsertVoteCase(voteCaseText, description, voteType, options, duration) 218 219 def InsertVote(self, corpID, voteCaseID, voteValue): 220 self.remoteSvc.InsertVote(corpID, voteCaseID, voteValue) 221 222 def GetSanctionedActions(self, state): 223 with self.readlock: 224 if state not in self.sanctionedActionsByState: 225 now = GetWallclockTime() 226 for action in self.remoteSvc.GetCorpSanctionedActions().itervalues(): 227 if action.inEffect == 0 and action.expires > now: 228 self.sanctionedActionsByState[SANCTIONED_ACTION_STATUS_NOT_IN_EFFECT].append(action) 229 elif action.inEffect: 230 self.sanctionedActionsByState[SANCTIONED_ACTION_STATUS_IN_EFFECT].append(action) 231 elif action.expires < now: 232 self.sanctionedActionsByState[SANCTIONED_ACTION_STATUS_EXPIRED].append(action) 233 234 try: 235 return self.sanctionedActionsByState[state] 236 except KeyError: 237 return [] 238 239 def ApplySanctionedAction(self, voteCaseID): 240 self.remoteSvc.ActivateSanctionedAction(voteCaseID) 241 242 def IsBlueprintSubjectOfActiveVoteCase(self, blueprintID): 243 now = GetWallclockTime() 244 for voteCase in self.GetVoteCasesByCorporation(session.corpid, voting.VOTECASE_STATUS_OPEN): 245 if voteCase.endDateTime > now: 246 for option in self.GetVoteCaseOptions(voteCase.voteCaseID, voteCase.corporationID): 247 if option.parameter == blueprintID: 248 return True 249 250 return False 251 252 def IsBlueprintSubjectOfPendingUnlockAction(self, blueprintID): 253 return blueprintID in {sa.parameter for sa in self.GetSanctionedActions(voting.SANCTIONED_ACTION_STATUS_NOT_IN_EFFECT) if sa.voteType == voting.voteItemUnlock and sa.parameter and not sa.inEffect}