From ee694e9e7e65877403d1df1f2e47118c388bd634 Mon Sep 17 00:00:00 2001 From: makefu Date: Sat, 29 Aug 2020 11:39:43 +0200 Subject: add probabilities to !choose and !list --- kalauerbot/bot.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/kalauerbot/bot.py b/kalauerbot/bot.py index 912584a..de95ade 100644 --- a/kalauerbot/bot.py +++ b/kalauerbot/bot.py @@ -104,8 +104,15 @@ class Kalauerbot(MatrixBot): weights.append(entity['weight']) log.debug(names) log.debug(weights) - db["__chosen__"] = chosen = random.choices(names,weights=weights)[0] - say(f"{chosen} was chosen to be the next kalauer könig, congratulations!") + chosen = random.choices(names,weights=weights)[0] + chosen_weight = db[chosen]['weight'] + all_weight = sum(weights) + prob = round(chosen_weight / all_weight * 100,1) + crosscheck = random.choices(names,weights=weights,k=100000) + say(f"{chosen} was chosen with a probability of {prob}% to be the next kalauer könig, congratulations!\n" + + f"cross-check: { crosscheck.count(chosen):,} out of 100,000 rolls were {chosen}") + + db["__chosen__"] = chosen self.save_db() elif command in ["!who","!wer", "!current"]: @@ -147,10 +154,11 @@ class Kalauerbot(MatrixBot): say(f"the name '{name}' does not exist in the database, add the user before demanding a kalauer from him") self.save_db() elif command == "!list": + all_weight = sum([db[a]["weight"] for a in db if a != "__chosen__"]) txt = "Kalauer roster:\n\n" for name,entry in db.items(): if name == '__chosen__': continue - txt += f"* {name} - {entry['weight']} weight - {entry['total']} total {'(active)' if entry['active'] else '(inactive)'}\n" + txt += f"* {name} - {entry['weight']} weight ({round(entry['weight']/all_weight * 100,1)}%) - {entry['total']} total {'(active)' if entry['active'] else '(inactive)'}\n" say(txt) elif command == "!kill": name = args.strip() -- cgit v1.2.3