From 335185ded4c2fdb1ad3e53e0435404a02de559a3 Mon Sep 17 00:00:00 2001 From: KuMiShi Date: Fri, 23 Jan 2026 09:47:06 +0100 Subject: [PATCH] Game Utils v1.0 --- .gitignore | 3 +++ __init__.py | 2 ++ dice.py | 17 +++++++++++++ entity.py | 0 game.py | 0 main.py | 6 +++++ npc.py | 0 player.py | 0 pyproject.toml | 7 ++++++ serializable.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 99 insertions(+) create mode 100644 .gitignore create mode 100644 __init__.py create mode 100644 dice.py create mode 100644 entity.py create mode 100644 game.py create mode 100644 main.py create mode 100644 npc.py create mode 100644 player.py create mode 100644 pyproject.toml create mode 100644 serializable.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..207a3c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# UV properties +.venv/ +.python-version \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..5a19714 --- /dev/null +++ b/__init__.py @@ -0,0 +1,2 @@ +__name__ = 'NLP_MCP' +__all__ = ['Game'] \ No newline at end of file diff --git a/dice.py b/dice.py new file mode 100644 index 0000000..74a3f22 --- /dev/null +++ b/dice.py @@ -0,0 +1,17 @@ +import random as rd +from . import serializable + +class Dice(serializable.Serializable): + def __init__(self, seed=42): + self.seed = seed + rd.seed(self.seed) + + def roll(self, num_faces=20): + return rd.randrange(start=1, stop=num_faces+1, step=1) + + def head_or_tails(): + result = rd.randint(0,1) + if result: # true + return "head" # face + else: + return "tails" # pile \ No newline at end of file diff --git a/entity.py b/entity.py new file mode 100644 index 0000000..e69de29 diff --git a/game.py b/game.py new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py new file mode 100644 index 0000000..5c5d443 --- /dev/null +++ b/main.py @@ -0,0 +1,6 @@ +def main(): + print("Hello from mcp-nlp!") + + +if __name__ == "__main__": + main() diff --git a/npc.py b/npc.py new file mode 100644 index 0000000..e69de29 diff --git a/player.py b/player.py new file mode 100644 index 0000000..e69de29 diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..425d1a8 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,7 @@ +[project] +name = "mcp-nlp" +version = "0.1.0" +description = "Add your description here" +readme = "README.md" +requires-python = ">=3.11" +dependencies = [] diff --git a/serializable.py b/serializable.py new file mode 100644 index 0000000..d6d1ca7 --- /dev/null +++ b/serializable.py @@ -0,0 +1,64 @@ +import json +from typing import Any, Dict, List, Type, TypeVar + +T = TypeVar('T', bound='Serializable') + +class Serializable: + @classmethod + def from_dict(cls: Type[T], data: Dict[str, Any]) -> T: + """Creates an instance of the class from a dictionary.""" + instance = cls.__new__(cls) + instance.deserialize_dict(data) + return instance + + def serialize(self, file) -> str: + """Serializes the object and all nested Serializable objects to JSON.""" + def serialize_value(value: Any) -> Any: + if isinstance(value, Serializable): + return value.serialize_dict() + elif isinstance(value, list): + return [serialize_value(v) for v in value] + elif isinstance(value, dict): + return {k: serialize_value(v) for k, v in value.items()} + else: + return value + + attrs = {k: serialize_value(v) for k, v in self.__dict__.items() if not k.startswith('_')} + return json.dumps(attrs, file, ensure_ascii=False, indent=2) + + def serialize_dict(self) -> Dict[str, Any]: + """Serializes the object to a dictionary (for nested serialization).""" + def serialize_value(value: Any) -> Any: + if isinstance(value, Serializable): + return value.serialize_dict() + elif isinstance(value, list): + return [serialize_value(v) for v in value] + elif isinstance(value, dict): + return {k: serialize_value(v) for k, v in value.items()} + else: + return value + + return {k: serialize_value(v) for k, v in self.__dict__.items() if not k.startswith('_')} + + def deserialize(self, json_str: str): + """Deserializes JSON and recursively reconstructs Serializable objects.""" + data = json.loads(json_str) + self.deserialize_dict(data) + + def deserialize_dict(self, data: Dict[str, Any]): + """Deserializes a dictionary and recursively reconstructs Serializable objects.""" + for key, value in data.items(): + if isinstance(value, dict) and 'py/class' in value: + # Handle nested Serializable objects + class_name = value['py/class'] + class_ = globals()[class_name] + setattr(self, key, class_.from_dict(value)) + elif isinstance(value, list): + # Handle lists of Serializable objects + setattr(self, key, [ + item if not isinstance(item, dict) or 'py/class' not in item + else globals()[item['py/class']].from_dict(item) + for item in value + ]) + else: + setattr(self, key, value) \ No newline at end of file