Skip to content

Commit

Permalink
Contracts
Browse files Browse the repository at this point in the history
See issue #266
  • Loading branch information
lekeno committed Dec 19, 2019
1 parent 926388d commit 764b0cc
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 69 deletions.
70 changes: 48 additions & 22 deletions edr/edrclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -1361,32 +1361,53 @@ def where_ship(self, name_or_type):
else:
self.__notify(_(u"Ship locator"), [_(u"Couldn't find anything")], clear_before = True)

def contract(self, target_cmdr, reward):
def contracts(self):
if self.is_anonymous():
EDRLOG.log(u"Skipping contracts since the user is anonymous.", "INFO")
self.advertise_full_account(_(u"Sorry, this feature only works with an EDR account."), passive=False)
return False

contracts = self.edrcmdrs.contracts()
if contracts:
self.__notify(_(u"Kill Rewards"),[_(u"{} {} on Cmdr {}").format(contracts[c]["reward"], contracts[c]["unit"], contracts[c]["cname"]) for c in contracts], clear_before = True)
else:
instructions = random.choice([_(u"Send '!contract example $$$ 10 VO$' in chat to set a reward of 10 void opals on Cmdr 'example'"), _(u"Send '!contract example $$$ 10 P$' in chat to set a reward of 10 painite on Cmdr 'example'")])
self.__notify(_(u"Kill Rewards"),[_(u"You haven't set any contract yet"), instructions], clear_before = True)
return True

def contract(self, cmdr_name):
if self.is_anonymous():
EDRLOG.log(u"Skipping contract since the user is anonymous.", "INFO")
self.advertise_full_account(_(u"Sorry, this feature only works with an EDR account."), passive=False)
return False

if target_cmdr is None:
contracts = self.player.contracts()
self.__notify(_(u"Kill Rewards"),[_(u"Reward of {} void opals for a kill on Cmdr {}").format(c["cmdr_name"],c["reward"]) for c in contracts], clear_before = True)
return True
c = self.edrcmdrs.contract_for(cmdr_name)
self.__notify(_(u"Kill Rewards"),[_(u"Reward of {} {} for a kill on Cmdr {}").format(c["reward"], c["unit"], cmdr_name)], clear_before = True)
return True

def contract_on(self, cmdr_name, reward, unit):
if self.is_anonymous():
EDRLOG.log(u"Skipping contract since the user is anonymous.", "INFO")
self.advertise_full_account(_(u"Sorry, this feature only works with an EDR account."), passive=False)
return False

if reward is None:
contract = self.player.contract(target_cmdr)
if not contract:
self.__notify(_(u"Kill Rewards"),[_(u"You haven't set any reward for a kill on Cmdr {}").format(target_cmdr), _(u"Send '!contract {} 10' in chat to set a reward of 10 void opals").format(target_cmdr)], clear_before = True)
if reward > 0:
reward = min(reward, 750)
success = self.edrcmdrs.place_contract(cmdr_name, reward, unit)
if success:
self.__notify(_(u"Kill Rewards"),[_(u"Reward of {} {} for a kill on Cmdr {}").format(reward, unit, cmdr_name), _(u"Send '!contract {} $$$ 0' in chat to remove the kill reward").format(cmdr_name)], clear_before = True)
return True
self.__notify(_(u"Kill Rewards"),[_(u"Reward of {} void opals for a kill on Cmdr {}").format(target_cmdr,contract["reward"]), _(u"Send '!contract {} 0' in chat to remove the kill reward").format(target_cmdr)], clear_before = True)
self.__notify(_(u"Kill Rewards"),[_(u"Failed to place a reward for a kill on Cmdr {}").format(cmdr_name), _(u"You may have too many active contracts."), _(u"Send '!contracts' to see all your contracts.").format(cmdr_name)], clear_before = True)
return False

success = self.edrcmdrs.remove_contract(cmdr_name)
instructions = random.choice([_(u"Send '!contract {} $$$ 10 VO$' in chat to set a reward of 10 void opals"), _(u"Send '!contract {} $$$ 10 P$' in chat to set a reward of 10 painite")])
if success:
self.__notify(_(u"Kill Rewards"),[_(u"Removed reward for a kill on Cmdr {}").format(cmdr_name), intrusctions.format(cmdr_name)], clear_before = True)
return True

outcome = self.player.place_contract(cmdr_name, reward)
if outcome["success"]:
self.__notify(_(u"Kill Rewards"),[_(u"Successfully set a reward of {} void opals for a kill on {}").format(target_cmdr,reward), _(u"Send '!contract {} 0' in chat to remove the kill reward").format(target_cmdr)], clear_before = True)
else:
self.__notify(_(u"Kill Rewards"),[_(u"Failed to set a kill reward on {}").format(target_cmdr), _(u"Reason: {}").format(outcome["comment"])], clear_before = True)
return outcome["success"]


self.__notify(_(u"Kill Rewards"),[_(u"Failed to remove reward for a kill on Cmdr {} (not even set?)").format(cmdr_name), instructions.format(cmdr_name)], clear_before = True)
return False

def outlaws(self):
try:
Expand All @@ -1403,10 +1424,15 @@ def enemies(self):
return False

def _opponents(self, kind):
if kind is EDROpponents.ENEMIES and not self.player.power:
EDRLOG.log(u"Not pledged to any power, can't have enemies.", "INFO")
self.__notify(_(u"Recently Sighted {kind}").format(kind=_(kind)), [_(u"You need to be pledged to a power.")], clear_before = True)
return False
if kind is EDROpponents.ENEMIES:
if self.is_anonymous():
EDRLOG.log(u"Skipping enemies since the user is anonymous.", "INFO")
self.advertise_full_account(_(u"Sorry, this feature only works with an EDR account."), passive=False)
return False
elif not self.player.power:
EDRLOG.log(u"Not pledged to any power, can't have enemies.", "INFO")
self.__notify(_(u"Recently Sighted {kind}").format(kind=_(kind)), [_(u"You need to be pledged to a power.")], clear_before = True)
return False
opponents_report = self.edropponents[kind].recent_sightings()
if not opponents_report:
EDRLOG.log(u"No recently sighted {}".format(kind), "INFO")
Expand Down
36 changes: 36 additions & 0 deletions edr/edrcmdrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,42 @@ def tag_cmdr(self, cmdr_name, tag):
else:
return self.__tag_cmdr(cmdr_name, tag)

def contracts(self):
return self.server.contracts()

def contract_for(self, cmdr_name):
if not cmdr_name:
return False

profile = self.cmdr(cmdr_name)
if not profile:
return False

return self.server.contract_for(profile.cid)

def place_contract(self, cmdr_name, reward, unit):
if not cmdr_name:
return False

if reward <= 0:
return self.remove_contract(cmdr_name)

profile = self.cmdr(cmdr_name)
if not profile:
return False

return self.server.place_contract(profile.cid, {"cname": cmdr_name.lower(), "reward": reward, "unit": unit})

def remove_contract(self, cmdr_name):
if not cmdr_name:
return False

profile = self.cmdr(cmdr_name)
if not profile:
return False

return self.server.remove_contract(profile.cid)

def __tag_cmdr(self, cmdr_name, tag):
EDRLOG.log(u"Tagging {} with {}".format(cmdr_name, tag), "DEBUG")
profile = self.__edr_cmdr(cmdr_name, False)
Expand Down
21 changes: 0 additions & 21 deletions edr/edrcontracts.py

This file was deleted.

47 changes: 33 additions & 14 deletions edr/edrserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,23 @@ def __dex(self, dex_path, cmdr_id):
else:
return None

def contract(self, cmdr_id):
def contracts(self):
if self.is_anonymous():
return None
contracts_path = "/v1/contracts/{}".format(self.uid())
EDRLOG.log(u"Contracts request", "DEBUG")
params = { "auth" : self.auth_token()}
endpoint = "{server}{con}.json".format(server=self.EDR_SERVER, con=contracts_path)
EDRLOG.log(u"Endpoint: {}".format(endpoint), "DEBUG")
resp = self.__get(endpoint, "EDR", params)
EDRLOG.log(u"resp= {}".format(resp.status_code), "DEBUG")

if self.__check_response(resp, "EDR", "Contracts"):
return json.loads(resp.content)
else:
return None

def contract_for(self, cmdr_id):
if self.is_anonymous():
return None
contracts_path = "/v1/contracts/{}/".format(self.uid())
Expand All @@ -554,28 +570,32 @@ def contract(self, cmdr_id):
resp = self.__get(endpoint, "EDR", params)
EDRLOG.log(u"resp= {}".format(resp.status_code), "DEBUG")

if self.__check_response(resp, "EDR", "Contracts"):
if self.__check_response(resp, "EDR", "Contract_for"):
return json.loads(resp.content)
else:
return None

def reward(self, cmdr_id, reward_entry):
if self.is_anonymous():
def place_contract(self, cmdr_id, contract_entry):
if self.is_anonymous() or contract_entry is None:
return False
contract_path = "/v1/cmdrsdex/{}/".format(self.uid())
if rewward_entry is None:
return self.__remove_contract(contract_path, cmdr_id)
contract_path = "/v1/contracts/{}/".format(self.uid())

return self.__update_contract(contract_path, cmdr_id, reward_entry)
return self.__update_contract(contract_path, cmdr_id, contract_entry)

def remove_contract(self, cmdr_id):
if self.is_anonymous():
return False
contract_path = "/v1/contracts/{}/".format(self.uid())
return self.__remove_contract(contract_path, cmdr_id)

def __update_contract(self, contract_path, cmdr_id, contract_entry):
params = { "auth" : self.auth_token()}
EDRLOG.log(u"Contract entry for cmdr {cid} with json:{json}".format(cid=cmdr_id, json=reward_entry), "INFO")
EDRLOG.log(u"Contract entry for cmdr {cid} with json:{json}".format(cid=cmdr_id, json=contract_entry), "INFO")
endpoint = "{server}{contract}{cid}/.json".format(server=self.EDR_SERVER, contract=contract_path, cid=cmdr_id)
EDRLOG.log(u"Endpoint: {} with {}".format(endpoint, reward_entry), "DEBUG")
resp = self.__put(endpoint, "EDR", json=reward_entry, params=params)
EDRLOG.log(u"Endpoint: {} with {}".format(endpoint, contract_entry), "DEBUG")
resp = self.__put(endpoint, "EDR", json=contract_entry, params=params)
EDRLOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
return self.__check_response(resp, "EDR")
return self.__check_response(resp, "EDR", "Update_contract")

def __remove_contract(self, contract_path, cmdr_id):
params = { "auth" : self.auth_token()}
Expand All @@ -584,8 +604,7 @@ def __remove_contract(self, contract_path, cmdr_id):
EDRLOG.log(u"Endpoint: {}".format(endpoint), "DEBUG")
resp = self.__delete(endpoint, "EDR", params=params)
EDRLOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
return self.__check_response(resp, "EDR")

return self.__check_response(resp, "EDR", "Remove_contract")

def preflight_realtime(self, kind):
api_name = u"realtime_{}".format(kind.lower())
Expand Down
32 changes: 20 additions & 12 deletions edr/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,6 @@ def edr_submit_crime_self(criminal_cmdr, offence, victim, timestamp):
}

report["criminals"][0]["power"] = criminal_cmdr.powerplay.canonicalize() if criminal_cmdr.powerplay else u""

EDRLOG.log(u"Perpetrated crime: {}".format(report), "DEBUG")

if not EDR_CLIENT.crime(criminal_cmdr.star_system, report):
Expand Down Expand Up @@ -833,7 +832,7 @@ def report_crime(cmdr, entry):
edr_submit_crime_self(cmdr, "Murder", victim, entry["timestamp"])
player_one.destroy(victim)
elif entry["event"] == "CrimeVictim" and "Offender" in entry and player_one.name and (entry["Offender"].lower() != player_one.name.lower()):
irrelevant_pattern = re.compile("^(\$([A-Za-z0-9]+_)+[A-Za-z0-9]+;)$")
irrelevant_pattern = re.compile(r"^(\$([A-Za-z0-9]+_)+[A-Za-z0-9]+;)$")
if not irrelevant_pattern.match(entry["Offender"]) and player_one.is_instanced_with(entry["Offender"]):
offender = player_one.instanced(entry["Offender"])
if "Bounty" in entry:
Expand All @@ -844,7 +843,7 @@ def report_crime(cmdr, entry):
else:
EDRLOG.log(u"Ignoring 'CrimeVictim' event: offender={}; instanced_with={}".format(entry["Offender"], player_one.is_instanced_with(entry["Offender"])), "DEBUG")
elif entry["event"] == "CommitCrime" and "Victim" in entry and player_one.name and (entry["Victim"].lower() != player_one.name.lower()):
irrelevant_pattern = re.compile("^(\$([A-Za-z0-9]+_)+[A-Za-z0-9]+;)$")
irrelevant_pattern = re.compile(r"^(\$([A-Za-z0-9]+_)+[A-Za-z0-9]+;)$")
if not irrelevant_pattern.match(entry["Victim"]) and player_one.is_instanced_with(entry["Victim"]):
victim = player_one.instanced(entry["Victim"])
if "Bounty" in entry:
Expand Down Expand Up @@ -1309,18 +1308,27 @@ def handle_bang_commands(cmdr, command, command_parts):
EDR_CLIENT.eval_build(eval_type)
else:
EDR_CLIENT.notify_with_details(_(u"Loadout information is stale"), [_(u"Congrats, you've found a bug in Elite!"), _(u"The modules info isn't updated right away :("), _(u"Try again after moving around or relog and check your modules.")])
elif command == "!contracts" and len(command_parts) == 1:
EDRLOG.log(u"Contracts command", "INFO")
EDR_CLIENT.contracts()
elif command == "!contract":
target_cmdr = None
target_cmdr = cmdr.target
reward = None
unit = u"VO$"
print command_parts
if len(command_parts) >= 2:
m = re.match(r"((.*) )?([1-9][0-9]*)", join(command_parts[1:]))
if not m:
EDRLOG.log(u"Aborting contract command (no/invalid params).", "DEBUG")
return
target_cmdr = m.group(1) if m.group(1) == 2 else cmdr.target
reward = m.grouo(2) if m.group(2) else None
EDRLOG.log(u"Contract command on {} with reward of {}".format(target_cmdr, reward), "INFO")
EDR_CLIENT.contract(target_cmdr, reward)
parts = " ".join(command_parts[1:]).split(" $$$ ", 1)
target_cmdr = parts[0]
if len(parts)>=2:
(reward, unit) = parts[1].split(" ", 1)
reward = int(reward)
print target_cmdr
print reward
EDRLOG.log(u"Contract command on {} with reward of {} {}".format(target_cmdr, reward, unit), "INFO")
if reward is None:
EDR_CLIENT.contract(target_cmdr)
else:
EDR_CLIENT.contract_on(target_cmdr, reward, unit)
elif command == "!help":
EDRLOG.log(u"Help command", "INFO")
EDR_CLIENT.help("" if len(command_parts) == 1 else command_parts[1])
Expand Down

0 comments on commit 764b0cc

Please sign in to comment.