Game Utils v1.0

This commit is contained in:
KuMiShi
2026-01-23 09:47:06 +01:00
parent 2027c3906a
commit 335185ded4
10 changed files with 99 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
# UV properties
.venv/
.python-version

2
__init__.py Normal file
View File

@@ -0,0 +1,2 @@
__name__ = 'NLP_MCP'
__all__ = ['Game']

17
dice.py Normal file
View File

@@ -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

0
entity.py Normal file
View File

0
game.py Normal file
View File

6
main.py Normal file
View File

@@ -0,0 +1,6 @@
def main():
print("Hello from mcp-nlp!")
if __name__ == "__main__":
main()

0
npc.py Normal file
View File

0
player.py Normal file
View File

7
pyproject.toml Normal file
View File

@@ -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 = []

64
serializable.py Normal file
View File

@@ -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)