From 53935637008ea2a0d04e9ff6db9ff4f643e3d63e Mon Sep 17 00:00:00 2001 From: KonoTyran Date: Tue, 24 Jan 2023 21:14:46 -0800 Subject: [PATCH] MultiServer: Data Storage Additions #1411 adds 3 new operations to datastorage that allows adding and removing of elements from list and dicts. --- MultiServer.py | 29 ++++++++++++++++++++++++++++- docs/network protocol.md | 3 +++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/MultiServer.py b/MultiServer.py index 32c8c1a5..1df09362 100644 --- a/MultiServer.py +++ b/MultiServer.py @@ -2,6 +2,7 @@ from __future__ import annotations import argparse import asyncio +import copy import functools import logging import zlib @@ -43,6 +44,28 @@ min_client_version = Version(0, 1, 6) print_command_compatability_threshold = Version(0, 3, 5) # Remove backwards compatibility around 0.3.7 colorama.init() + +def remove_from_list(container, value): + try: + container.remove(value) + except ValueError: + pass + return container + + +def pop_from_container(container, value): + try: + container.pop(value) + except ValueError: + pass + return container + + +def update_dict(dictionary, entries): + dictionary.update(entries) + return dictionary + + # functions callable on storable data on the server by clients modify_functions = { "add": operator.add, # add together two objects, using python's "+" operator (works on strings and lists as append) @@ -59,6 +82,10 @@ modify_functions = { "and": operator.and_, "left_shift": operator.lshift, "right_shift": operator.rshift, + # lists/dicts + "remove": remove_from_list, + "pop": pop_from_container, + "update": update_dict, } @@ -1739,7 +1766,7 @@ async def process_client_cmd(ctx: Context, client: Client, args: dict): return args["cmd"] = "SetReply" value = ctx.stored_data.get(args["key"], args.get("default", 0)) - args["original_value"] = value + args["original_value"] = copy.copy(value) for operation in args["operations"]: func = modify_functions[operation["operation"]] value = func(value, operation["value"]) diff --git a/docs/network protocol.md b/docs/network protocol.md index 46780a44..ff503515 100644 --- a/docs/network protocol.md +++ b/docs/network protocol.md @@ -411,6 +411,9 @@ The following operations can be applied to a datastorage key | xor | Applies a bitwise Exclusive OR to the current value of the key with `value`. | | left_shift | Applies a bitwise left-shift to the current value of the key by `value`. | | right_shift | Applies a bitwise right-shift to the current value of the key by `value`. | +| remove | List only: removes the first instance of `value` found in the list. | +| pop | List or Dict: for lists it will remove the index of the `value` given. for dicts it removes the element with the specified key of `value`. | +| update | Dict only: Updates the dictionary with the specified elements given in `value` creating new keys, or updating old ones if they previously existed. | ### SetNotify Used to register your current session for receiving all [SetReply](#SetReply) packages of certain keys to allow your client to keep track of changes.