Do a lot of things with the badge plugin

* Add list argument to badgeinfo (lists badge names)
* Prevent badgeinfo from crashing the bot on a non-existant badge
* Allow shorthand badge names in +transmute (b, sh, sp, etc.)
    * Throw new error when the resolved badge doesn't exist
    * Throw new error when the resolved badge is ambiguous
* Add +transmuteall command to transmute 3 most common badges until you run out of badges to transmute
    * Don't transmute Tildebadges automatically
* Add admin command to take a badge away (for cleaning up after a test)
This commit is contained in:
Robert Miles 2020-06-08 16:02:28 -04:00
parent d5cd6a17a3
commit bc6d472740
1 changed files with 87 additions and 5 deletions

View File

@ -91,16 +91,67 @@ def on_cmd_listbadges(event):
else:
respond(event,"You have: "+", ".join(ret))
def on_cmd_transmute(event):
class AmbiguousBadge(Exception):
pass
class NoSuchBadge(Exception):
pass
def resolve_badge(s,names=None):
if names is None: names = list(badge_weights.keys())
if len(s)==0: raise NoSuchBadge("No such badge `` (check for extra spaces in your command)")
elif len(s)==1: s=s.upper()
else: s=s[0].upper()+s[1:].lower() # len(s)>1
ret = [name for name in names if name.startswith(s)]
if len(ret)==0: raise NoSuchBadge(f"No such badge `{s}`.")
elif len(ret)>1:
r = ", or ".join(ret)
raise AmbiguousBadge(f"Ambiguous badge choice `{s}` (do you mean {r}?)")
else: # len(ret)==1
return ret[0]
def on_cmd_transmute(event,silent=False):
if silent: respond=lambda *x: None
else: respond=globals()["respond"]
if BOT is None: return
account = event.tags.get("account",None)
if account is None: return
if len(event.parts)<3:
parts = event.parts
# TODO: implement auto transmute of 3 worst badges
if len(parts)==1 and parts[0].lower() in 'a auto'.split():
rarities = badge.calculate_rarities(population.value.population)
badges = list(reversed(sorted(population.value.badges[account],key=lambda x: rarities[x.name][2])))
parts = [x.name for x in badges[:3]]
if "Tildebadge" in parts:
respond(event,"This would burn a Tildebadge. To proceed, type `+transmute {}` (or equivalent)".format(" ".join(parts)))
return
try:
badges_transmutable = [resolve_badge(x) for x in parts]
except NoSuchBadge as e:
respond(event,e.args[0])
return
except AmbiguousBadge as e:
respond(event,e.args[0])
return
except:
traceback.print_exc()
url = "Error saving traceback! Tell khuxkm to check error.log!"
err = traceback.format_exc()
try:
with open("error.log","a") as f:
f.write(err+"\n"+("-"*80)+"\n")
r = requests.post("https://ttm.sh",files=pack_file(err))
url = r.text.strip()
except: pass
respond(event,"Something went wrong! Error: "+url)
respond(event,"If you lost badges because of this error, please tell khuxkm which badges and he will refund you. (You shouldn't have in this case.)")
return
print(badges_transmutable)
if len(badges_transmutable)<3:
respond(event,"You must insert at least 3 badges for use in transmutation.")
return
badges_given = [(x[0].upper())+x[1:].lower() for x in event.parts]
try:
badge_result = population.value.transmute(account,*badges_given)
badge_result = population.value.transmute(account,*badges_transmutable)
except badge.UserDoesntHaveEnoughBadges:
respond(event,"You must have at least one (1) of each badge you wish to use in the transmutation.")
return
@ -116,18 +167,21 @@ def on_cmd_transmute(event):
respond(event,"Something went wrong! Error: "+url)
respond(event,"If you lost badges because of this error, please tell khuxkm which badges and he will refund you.")
return
respond(event,"You put in the {!s} badges above, and out pops a {}!".format(len(event.parts),badge_result))
respond(event,"You put in the {!s} badges above, and out pops a {}!".format(len(parts),badge_result))
population.value.give_badge(account,badge_result,False)
population.save("badges.json")
def on_cmd_badgeinfo(event):
bag = population.value.population
if "list" in event.parts:
respond(event,"The badges are: "+",".join(sorted(list(set(badge.name for badge in bag)),key=lambda x: -badge_weights[x])))
if "all" in event.parts:
event.data["parts"]=sorted(list(set(badge.name for badge in bag)),key=lambda x: -badge_weights[x])
for badge in event.parts:
badge = badge[0].upper()+badge[1:].lower()
b = [obadge for obadge in bag if obadge.name==badge]
count = len(b)
if count==0: continue
normal = len([x for x in b if x.normal])
rarity = population.value.rarity(badge)
respond(event,"There {} {} in existence, {!s} of them randomly generated (effective rarity of {:0.2%})".format(are(count),plural(count,badge),normal,rarity))
@ -152,6 +206,20 @@ def on_cmd_optin(event):
optouts.remove(account)
respond(event,"Alright! Let's play!")
def on_cmd_transmuteall(event):
if BOT is None: return
account = event.tags.get("account",None)
if account is None: return
while True:
rarities = badge.calculate_rarities(population.value.population)
badges = list(reversed(sorted(population.value.badges[account],key=lambda x: rarities[x.name][2])))
parts = [x.name for x in badges[:3]]
if "Tildebadge" in parts:
respond(event,"Finished!")
return
event.data["parts"]=parts
on_cmd_transmute(event,True)
def admin_givebadge(event):
print(event.name,event.data)
try:
@ -163,6 +231,19 @@ def admin_givebadge(event):
population.save("badges.json")
except: pass
def admin_takebadge(event):
print(event.name,event.data)
try:
account, badge = event.parts[:2]
badge = badge[0].upper()+badge[1:].lower()
i=len(population.value.badges[account])-1
while i>0:
if populations.value.badges[account][i].name==badge:
populations.value.badges[account].pop(i)
i=-1
population.save("badges.json")
except: pass
def admin_manualoptout(event):
for account in event.parts:
if account not in optouts:
@ -177,6 +258,7 @@ def register(bot):
bot.event_manager.on("command_help",on_cmd_help)
bot.event_manager.on("command_listbadges",on_cmd_listbadges)
bot.event_manager.on("command_transmute",on_cmd_transmute)
bot.event_manager.on("command_transmuteall",on_cmd_transmuteall)
bot.event_manager.on("command_badgeinfo",on_cmd_badgeinfo)
bot.event_manager.on("command_optout",on_cmd_optout)
bot.event_manager.on("command_optin",on_cmd_optin)