From adf43a8fbe23ecbf926580c3c8f218bbd0aef546 Mon Sep 17 00:00:00 2001 From: Someone Date: Sat, 1 Apr 2023 23:42:01 +0200 Subject: [PATCH] modules/DialogManagedGuess.py --- modules/DialogManagedGuess.py | 136 ++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 modules/DialogManagedGuess.py diff --git a/modules/DialogManagedGuess.py b/modules/DialogManagedGuess.py new file mode 100644 index 0000000..73d3177 --- /dev/null +++ b/modules/DialogManagedGuess.py @@ -0,0 +1,136 @@ +# Mattermost Bot module. +# Copyright (c) 2016-2022 by Someone (aka. Jan Vales ) +# published under MIT-License + +from inspect import cleandoc +import glob +import os + + +# pylint: disable=wrong-import-position +from AbstractCommand import AbstractCommand +class DialogManagedGuess(AbstractCommand): + TRIGGER = "guess" + CONFIG = {"display_name": "somebot-command", "auto_complete": True, + "auto_complete_hint": "", + } + CONFIG["auto_complete_desc"] = CONFIG["description"] = AbstractCommand.ICON_PRIVATE+"Opens dialog to edit/delete your guess." + + + def __init__(self, team_id, datadir): + super().__init__(team_id) + + self.datadir = datadir + + + def on_POST(self, request, data): + #self._open_dialog(request, data) + #request.respond_cmd_temp("## :white_check_mark: Success! :)\n#### Opened dialog.") + request.respond_cmd_err("No guessing game running.") + + +# def dis_on_POST_interactive(self, request, data): +# import pprint +# print("on_POST_interactive") +# pprint.pprint(data) + + #self._open_dialog(request, data) + #request.respond() + + + def dis_on_POST_dialog(self, request, data): +# import pprint +# print("on_POST_dialog") +# pprint.pprint(data) + + errors = {} + if data["submission"]["guess_count"] and int(data["submission"]["guess_count"]) < 0: + errors["guess_count"] = "Count must be positive." + + if data["submission"]["guess_value"] and int(data["submission"]["guess_value"]) < 0: + errors["guess_value"] = "Value must be positive." + + counts = [] + for f in glob.glob("data/guess/*-guess_count.txt"): + if data["callback_id"] in f: + continue + with open(f, "r") as fd: + counts.append(int(fd.read())) + + if int(data["submission"]["guess_count"]) in counts: + errors["guess_count"] = "Count must be unique. Somebody else already logged this guess before you." + + values = [] + for f in glob.glob("data/guess/*-guess_value.txt"): + if data["callback_id"] in f: + continue + with open(f, "r") as fd: + values.append(int(fd.read())) + + if int(data["submission"]["guess_value"]) in values: + errors["guess_value"] = "Value must be unique. Somebody else already logged this guess before you." + + if errors: + request.respond(200, {"errors": errors}) + return + + + user = self.bot.api.get_user(data['user_id']) + self._response_store(self.datadir+data["callback_id"], "guess_count.txt", str(int(data["submission"]["guess_count"])), user["username"]) + self._response_store(self.datadir+data["callback_id"], "guess_value.txt", str(int(data["submission"]["guess_value"])), user["username"]) + + request.respond(200, {}) + + + def _open_dialog(self, request, data): + callback_id = "res-"+data["user_id"]+"-" + + guess_count = self._response_load(self.datadir+callback_id, "guess_count.txt") + guess_value = self._response_load(self.datadir+callback_id, "guess_value.txt") + + dialog = { + "callback_id": callback_id, + "title": "Guess the coins", + "submit_label":"Submit", + "elements":[{ + "display_name": "Count of all coins?", + "placeholder": "Count. Positive integer.", + "name": "guess_count", + "type": "text", + "subtype": "number", + "optional": True, + "default": guess_count + },{ + "display_name": "Value of all coins?", + "placeholder": "EUR Value. Positive integer.", + "name": "guess_value", + "type": "text", + "subtype": "number", + "optional": True, + "default": guess_value + }] + } + + self.bot.api.open_dialog(data["trigger_id"], self.URL+"/dialog", dialog) + + + def _response_load(self, path, filename, default=""): + path = path+filename + ret = default + if os.path.isfile(path): + with open(path, "r") as f: + ret = f.read() + return ret + + + def _response_store(self, path, filename, data, username=""): + path = path+filename + data = str(data).strip() + if data == "" or data == "None" or data == "0": + if os.path.isfile(path): + os.remove(path) + self.bot.debug_chan("``guess::"+filename+"`` (**``@"+username+"``**) result deleted.") + else: + with open(path, "w") as f: + f.write(data) + self.bot.debug_chan("``guess::"+filename+"`` by (**``@"+username+"``**) result stored.") -- 2.43.0