Add simple whiteboard
This commit is contained in:
1
server/app/items/types/whiteboard/__init__.py
Normal file
1
server/app/items/types/whiteboard/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Whiteboard item type package."""
|
||||
23
server/app/items/types/whiteboard/actions.py
Normal file
23
server/app/items/types/whiteboard/actions.py
Normal file
@@ -0,0 +1,23 @@
|
||||
"""Whiteboard item use actions."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Callable
|
||||
|
||||
from ....item_types import ItemUseResult
|
||||
from ....models import WorldItem
|
||||
|
||||
|
||||
def use_item(item: WorldItem, nickname: str, _clock_formatter: Callable[[dict], str]) -> ItemUseResult:
|
||||
"""Report whiteboard contents to the user who used it."""
|
||||
|
||||
lines = item.params.get("lines", [])
|
||||
if not isinstance(lines, list):
|
||||
lines = []
|
||||
n = len(lines)
|
||||
line_text = f"{n} line{'s' if n != 1 else ''}"
|
||||
|
||||
return ItemUseResult(
|
||||
self_message=f"You open {item.title}. {line_text}.",
|
||||
others_message=f"{nickname} opens {item.title}.",
|
||||
)
|
||||
19
server/app/items/types/whiteboard/definition.py
Normal file
19
server/app/items/types/whiteboard/definition.py
Normal file
@@ -0,0 +1,19 @@
|
||||
"""Whiteboard item static metadata and defaults."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
LABEL = "whiteboard"
|
||||
TOOLTIP = "A shared text board. Use to read and edit lines."
|
||||
EDITABLE_PROPERTIES: tuple[str, ...] = ("title",)
|
||||
CAPABILITIES: tuple[str, ...] = ("editable", "carryable", "deletable", "usable")
|
||||
USE_SOUND: str | None = None
|
||||
EMIT_SOUND: str | None = None
|
||||
USE_COOLDOWN_MS = 500
|
||||
EMIT_RANGE = 15
|
||||
DIRECTIONAL = False
|
||||
DEFAULT_TITLE = "whiteboard"
|
||||
DEFAULT_PARAMS: dict = {"lines": []}
|
||||
PARAM_KEYS: tuple[str, ...] = ("lines",)
|
||||
PROPERTY_METADATA: dict[str, dict[str, object]] = {
|
||||
"title": {"valueType": "text", "tooltip": "Display name.", "maxLength": 80},
|
||||
}
|
||||
16
server/app/items/types/whiteboard/plugin.py
Normal file
16
server/app/items/types/whiteboard/plugin.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""Plugin registration for whiteboard item type."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from ..plugin_helpers import build_item_module
|
||||
from . import actions, definition, validator
|
||||
|
||||
ITEM_TYPE_PLUGIN = {
|
||||
"type": "whiteboard",
|
||||
"order": 70,
|
||||
"module": build_item_module(
|
||||
definition,
|
||||
validate_update=validator.validate_update,
|
||||
use_item=actions.use_item,
|
||||
),
|
||||
}
|
||||
32
server/app/items/types/whiteboard/validator.py
Normal file
32
server/app/items/types/whiteboard/validator.py
Normal file
@@ -0,0 +1,32 @@
|
||||
"""Whiteboard item validation/normalization."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from ....models import WorldItem
|
||||
from ...helpers import keep_only_known_params
|
||||
from .definition import PARAM_KEYS
|
||||
|
||||
_MAX_LINES = 20
|
||||
_MAX_LINE_LENGTH = 200
|
||||
|
||||
|
||||
def validate_update(_item: WorldItem, next_params: dict) -> dict:
|
||||
"""Validate and normalize whiteboard params."""
|
||||
|
||||
lines = next_params.get("lines", [])
|
||||
if not isinstance(lines, list):
|
||||
raise ValueError("lines must be a list.")
|
||||
if len(lines) > _MAX_LINES:
|
||||
raise ValueError(f"A whiteboard can have at most {_MAX_LINES} lines.")
|
||||
|
||||
cleaned: list[str] = []
|
||||
for line in lines:
|
||||
if not isinstance(line, str):
|
||||
raise ValueError("Each line must be a string.")
|
||||
stripped = line.strip()
|
||||
if len(stripped) > _MAX_LINE_LENGTH:
|
||||
raise ValueError(f"Each line must be at most {_MAX_LINE_LENGTH} characters.")
|
||||
cleaned.append(stripped)
|
||||
|
||||
next_params["lines"] = cleaned
|
||||
return keep_only_known_params(next_params, PARAM_KEYS)
|
||||
Reference in New Issue
Block a user