standingsvc.py (12399B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\services\standingsvc.py 2 import types 3 import blue 4 import uthread 5 from carbon.common.script.sys.service import Service 6 from eve.client.script.ui.shared.standings import standingsUIUtil 7 from eve.common.lib import appConst 8 from eve.common.script.sys import eveCfg, idCheckers 9 from eve.common.script.util.standingUtil import ApplyBonusToStanding, CalculateNewStandings, CalculateStandingsByRawChange 10 from localization import GetByLabel 11 12 class Standing(Service): 13 __notifyevents__ = ['OnStandingSet', 'OnStandingsModified', 'ProcessSessionChange'] 14 __guid__ = 'svc.standing' 15 __servicename__ = 'standing' 16 __displayname__ = 'Standing Service' 17 18 def Run(self, memStream = None): 19 self.npccorpstandings = {} 20 self.npccharstandings = {} 21 self.npcnpcstandingsto = {} 22 self.npcnpcstandingsfrom = {} 23 self.unreadStandingEntries = {} 24 self.standingTransactionCache = {} 25 self.fromStandingHeader = blue.DBRowDescriptor((('fromID', appConst.DBTYPE_I4), ('standing', appConst.DBTYPE_R5))) 26 self.__RefreshStandings() 27 28 def Stop(self, memStream = None): 29 pass 30 31 def OnStandingSet(self, fromID, toID, standing): 32 self.CheckInvalidateTransactionCache(fromID, toID) 33 if idCheckers.IsNPC(fromID): 34 if toID == session.charid: 35 if not standing: 36 if fromID in self.npccharstandings: 37 if session.warfactionid: 38 sm.ScatterEvent('OnNPCStandingChange', fromID, 0.0, self.npccharstandings[fromID].standing) 39 del self.npccharstandings[fromID] 40 else: 41 oldStanding = 0.0 42 if fromID in self.npccharstandings: 43 oldStanding = self.npccharstandings[fromID].standing 44 if session.warfactionid: 45 sm.ScatterEvent('OnNPCStandingChange', fromID, standing, oldStanding) 46 self._AppendNPCCharStanding(fromID, standing) 47 elif toID == session.corpid: 48 if not standing: 49 if fromID in self.npccorpstandings: 50 del self.npccorpstandings[fromID] 51 else: 52 self.npccorpstandings[fromID] = blue.DBRow(self.fromStandingHeader, [fromID, standing]) 53 54 def OnStandingsModified(self, modifications): 55 for modification in modifications: 56 fromID, toID, rawChange, minAbs, maxAbs = modification 57 self.CheckInvalidateTransactionCache(fromID, toID) 58 if toID == session.charid: 59 self._ModifyCharacterStandings(fromID, rawChange) 60 self.AddToUnreadEntries(fromID, rawChange) 61 elif toID == session.corpid: 62 self._ModifyCorporationStandings(fromID, rawChange) 63 64 def _ModifyCharacterStandings(self, fromID, rawChange): 65 if fromID in self.npccharstandings: 66 new_standings_value = CalculateStandingsByRawChange(currentStanding=self.npccharstandings[fromID].standing, rawChange=rawChange) 67 sm.ScatterEvent('OnNPCStandingChange', fromID, new_standings_value, self.npccharstandings[fromID].standing) 68 self.npccharstandings[fromID].standing = new_standings_value 69 else: 70 new_standings_value = CalculateNewStandings(rawChange) 71 self._AppendNPCCharStanding(fromID, new_standings_value) 72 73 def _AppendNPCCharStanding(self, fromID, standing): 74 self.npccharstandings[fromID] = blue.DBRow(self.fromStandingHeader, [fromID, standing]) 75 76 def _ModifyCorporationStandings(self, fromID, rawChange): 77 if fromID in self.npccorpstandings: 78 new_standings_value = CalculateStandingsByRawChange(currentStanding=self.npccharstandings[fromID].standing, rawChange=rawChange) 79 self.npccorpstandings[fromID].standing = new_standings_value 80 else: 81 new_standings_value = CalculateNewStandings(rawChange) 82 self.npccorpstandings[fromID] = blue.DBRow(self.fromStandingHeader, [fromID, new_standings_value]) 83 84 def CanUseAgent(self, factionID, corporationID, agentID, level, agentTypeID): 85 faction_standing = self.npccharstandings[factionID].standing if factionID in self.npccharstandings else 0.0 86 corporation_standing = self.npccharstandings[corporationID].standing if corporationID in self.npccharstandings else 0.0 87 character_standing = self.npccharstandings[agentID].standing if agentID in self.npccharstandings else 0.0 88 return eveCfg.CanUseAgent(level, agentTypeID, faction_standing, corporation_standing, character_standing, corporationID, factionID, {}) 89 90 def ProcessSessionChange(self, isRemote, session, change): 91 if 'charid' in change and change['charid'][1] or 'corpid' in change and change['corpid'][1]: 92 self.__RefreshStandings() 93 94 def __RefreshStandings(self): 95 if not session.charid: 96 return 97 standingsMgr = sm.RemoteSvc('standingMgr') 98 tmp = standingsMgr.GetNPCNPCStandings() 99 self.npcnpcstandingsto = tmp.Filter('toID') 100 self.npcnpcstandingsfrom = tmp.Filter('fromID') 101 if idCheckers.IsNPC(session.corpid): 102 self.npccharstandings = standingsMgr.GetCharStandings() 103 self.npccorpstandings = {} 104 else: 105 ret = uthread.parallel([(standingsMgr.GetCharStandings, ()), (standingsMgr.GetCorpStandings, ())]) 106 self.npccharstandings = ret[0] 107 self.npccorpstandings = ret[1] 108 if not isinstance(self.npccorpstandings, types.DictType): 109 self.npccorpstandings = self.npccorpstandings.Index('fromID') 110 if not isinstance(self.npccharstandings, types.DictType): 111 self.npccharstandings = self.npccharstandings.Index('fromID') 112 for factionID in appConst.factions: 113 if factionID not in self.npccharstandings and factionID == appConst.factionByRace[session.raceID]: 114 self._AppendNPCCharStanding(factionID, 0.0) 115 116 def AppendZeroStandingIfNeeded(self, ownerID): 117 if ownerID not in self.npccharstandings: 118 self._AppendNPCCharStanding(ownerID, 0.0) 119 return True 120 return False 121 122 def GetStanding(self, fromID, toID): 123 relationship = None 124 standing = None 125 if fromID == session.charid: 126 relationship = sm.GetService('addressbook').GetContacts().contacts.get(toID, None) 127 elif fromID == session.corpid and not idCheckers.IsNPC(session.corpid): 128 relationship = sm.GetService('addressbook').GetContacts().corpContacts.get(toID, None) 129 elif fromID == session.allianceid: 130 relationship = sm.GetService('addressbook').GetContacts().allianceContacts.get(toID, None) 131 elif toID == session.charid: 132 if idCheckers.IsNPC(fromID) and fromID in self.npccharstandings: 133 standing = self.npccharstandings[fromID] 134 elif toID == session.corpid and not idCheckers.IsNPC(session.corpid): 135 if idCheckers.IsNPC(fromID) and fromID in self.npccorpstandings: 136 standing = self.npccorpstandings[fromID] 137 elif idCheckers.IsNPC(fromID) and idCheckers.IsNPC(toID) and fromID in self.npcnpcstandingsfrom: 138 for each in self.npcnpcstandingsfrom[fromID]: 139 if each.toID == toID: 140 standing = each 141 break 142 143 if standing is not None: 144 ret = standing.standing 145 elif relationship is not None: 146 ret = relationship.relationshipID 147 else: 148 ret = appConst.contactNeutralStanding 149 return ret 150 151 def GetStandingTransactions(self, fromID, toID): 152 key = (fromID, toID) 153 if key not in self.standingTransactionCache: 154 transactions = sm.RemoteSvc('standingMgr').GetStandingTransactions(fromID, toID) 155 for each in transactions: 156 each.modification = float(each.modification * 10.0) 157 158 self.standingTransactionCache[key] = transactions 159 return self.standingTransactionCache[key][:] 160 161 def CheckInvalidateTransactionCache(self, fromID, toID): 162 if (fromID, toID) in self.standingTransactionCache: 163 self.standingTransactionCache.pop((fromID, toID)) 164 165 def GetStandingWithSkillBonus(self, fromID, toID): 166 standing = self.GetStanding(fromID, toID) or 0.0 167 bonus = standingsUIUtil.GetStandingSkillBonus(standing, fromID, toID) 168 return ApplyBonusToStanding(bonus, standing) 169 170 def GetStandingWithSkillBonusFromValue(self, standingValue, fromID, toID): 171 bonus = standingsUIUtil.GetStandingSkillBonus(standingValue, fromID, toID) 172 return ApplyBonusToStanding(bonus, standingValue) 173 174 def GetEffectiveStandingWithAgent(self, agentID, getLabel = True): 175 characterID = session.charid 176 agentInfo = sm.GetService('agents').GetAgentByID(agentID) 177 standings = [self.GetStandingWithSkillBonus(agentInfo.factionID, characterID), self.GetStandingWithSkillBonus(agentInfo.corporationID, characterID), self.GetStandingWithSkillBonus(agentInfo.agentID, characterID)] 178 if min(*standings) <= -2.0: 179 value = min(*standings) 180 label_path = 'UI/Agents/Dialogue/EffectiveStandingLow' 181 else: 182 value = max(*standings) or 0.0 183 label_path = 'UI/Agents/Dialogue/EffectiveStanding' 184 label = GetByLabel(label_path, effectiveStanding=value) 185 return (float(value), label) 186 187 def AddToEffectiveStandingWithAgent(self, standingGains, agentID): 188 newStandingsWithBonus, _ = self.GetNewAndCurrentStandings(standingGains, agentID) 189 standing_values = newStandingsWithBonus.values() 190 min_standing = min(standing_values) 191 if min_standing <= -2.0: 192 return min_standing 193 else: 194 max_standing = max(standing_values) 195 return max_standing or 0.0 196 197 def GetNewAndCurrentStandings(self, standingGains, agentID): 198 characterID = session.charid 199 agentInfo = sm.GetService('agents').GetAgentByID(agentID) 200 factionID = agentInfo.factionID 201 corporationID = agentInfo.corporationID 202 currentStandings = {factionID: self.GetStanding(factionID, characterID) or 0.0, 203 corporationID: self.GetStanding(corporationID, characterID) or 0.0, 204 agentID: self.GetStanding(agentID, characterID) or 0.0} 205 rawStandings = currentStandings.copy() 206 for fromID, standingGain in standingGains.iteritems(): 207 rawStandings[fromID] += standingGain 208 209 standingsWithBonus = {factionID: self.GetStandingWithSkillBonusFromValue(rawStandings[factionID], factionID, characterID), 210 corporationID: self.GetStandingWithSkillBonusFromValue(rawStandings[corporationID], corporationID, characterID), 211 agentID: self.GetStandingWithSkillBonusFromValue(rawStandings[agentID], agentID, characterID)} 212 return (standingsWithBonus, currentStandings) 213 214 def GetStandingsDataNPCsToMyCharacter(self): 215 standings = [] 216 for npcID in self.npccharstandings: 217 standings.append((npcID, self.npccharstandings[npcID].standing)) 218 219 cfg.eveowners.Prime([ standing[0] for standing in standings ]) 220 return standings 221 222 def GetStandingsDataNPCsToMyCorp(self): 223 standings = [] 224 corpIDs = [] 225 for npcID in self.npccorpstandings: 226 if idCheckers.IsCorporation(npcID): 227 corpIDs.append(npcID) 228 standings.append((npcID, self.npccorpstandings[npcID].standing)) 229 230 cfg.corptickernames.Prime(corpIDs) 231 cfg.eveowners.Prime([ standing[0] for standing in standings ]) 232 return standings 233 234 def AddToUnreadEntries(self, ownerID, standingsChange): 235 self.unreadStandingEntries[ownerID] = standingsChange 236 237 def RemoveFromUnreadEntries(self, ownerID): 238 if ownerID in self.unreadStandingEntries: 239 del self.unreadStandingEntries[ownerID] 240 241 def FlushUnreadEntries(self): 242 self.unreadStandingEntries.clear() 243 244 def GetUnreadEntries(self): 245 return self.unreadStandingEntries 246 247 def GetStandingCompositions(self, fromID, toID): 248 return sm.RemoteSvc('standingMgr').GetStandingCompositions(fromID, toID)