Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

More pre launch fixes #210

Merged
merged 4 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deploy/web/server/run_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def str2bool(v):
parser.add_argument(
"--cookie-secret",
type=str,
default="0123456789",
default="temp8000800080008000",
help="Cookie secret for issueing cookies (SECRET!!!)",
)
parser.add_argument(
Expand Down
25 changes: 24 additions & 1 deletion deploy/web/server/tornado_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@
import tornado.auth # noqa E402: gotta install ioloop first
import tornado.websocket # noqa E402: gotta install ioloop first
import tornado.escape # noqa E402: gotta install ioloop first
from light.graph.events.graph_events import SoulSpawnEvent
from light.graph.events.graph_events import (
SoulSpawnEvent,
SystemMessageEvent,
DeathEvent,
)

from typing import Dict, Optional, TYPE_CHECKING

Expand Down Expand Up @@ -626,6 +630,11 @@ def player_observe_event(self, soul: "PlayerSoul", event: "GraphEvent"):
self.socket.safe_write_message(
json.dumps({"command": "actions", "data": [dat]})
)
if (
isinstance(event, DeathEvent)
and event.actor.node_id == soul.target_node.node_id
):
self.purgatory.clear_soul(soul.target_node)

def act(self, action_data, event_id: Optional[str] = None):
if self.player_soul is not None and self.player_soul.is_reaped:
Expand Down Expand Up @@ -653,6 +662,20 @@ def is_alive(self):
return self.socket.alive

def on_reap_soul(self, soul):
action = SystemMessageEvent(
soul.target_node,
[],
text_content=(
"Your soul slips off into the ether, unbound by your previous character. "
'"Oh no... this won\'t do", says the Dungeon Master, before peering over '
'into the world to see what has happened. "I can try to find a new place '
"for your soul, if you'd like?\" Send anything to respawn."
),
)
dat = action.to_frontend_form(soul.target_node)
self.socket.safe_write_message(
json.dumps({"command": "actions", "data": [dat]})
)
if self.user is not None:
if self.db is not None:
with self.db as ldb:
Expand Down
5 changes: 5 additions & 0 deletions light/graph/events/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,13 @@ def from_json(input_json: str, world: "World") -> "GraphEvent":
event = class_(*arglist)
for k, v in attribute_dict.items():
event.__dict__[k] = v
event.post_json_load(world)
return event

def post_json_load(self, world: "World") -> None:
"""Rectify any state following a load from json."""
pass

def to_json(self, viewer: GraphAgent = None, indent: int = None) -> str:
"""
Convert the content of this action into a json format that can be
Expand Down
13 changes: 7 additions & 6 deletions light/graph/events/graph_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,8 +1134,7 @@ def execute(self, world: "World") -> List[GraphEvent]:

# Trigger the actual death
world.oo_graph.agent_die(self.actor)
# TODO any other world processing of a death event, perhaps to
# update the player
# world.purgatory.clear_soul(self.actor) todo - clear soul only after message queue consumed
return []

@proper_caps_wrapper
Expand Down Expand Up @@ -3150,10 +3149,12 @@ def _get_target_description(self, world: "World") -> str:
inv_text = world.view.get_inventory_text_for(object_id, give_empty=False)
if inv_text != "":
base_desc += f"\n{self.__target_name} is {inv_text} "
if hasattr(
self.target_nodes[0], "_last_action_time"
) and actor_has_no_recent_action(
self.target_nodes[0]._last_action_time, time.time()
if (
self.target_nodes[0].is_player
and hasattr(self.target_nodes[0], "_last_action_time")
and actor_has_no_recent_action(
self.target_nodes[0]._last_action_time, time.time()
)
):
base_desc += "\nThey appear to be dozing off right now."
elif isinstance(target, GraphObject) and target.container:
Expand Down
11 changes: 10 additions & 1 deletion light/graph/events/use_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
AttributeCompareValueConstraint,
)
from light.graph.elements.graph_nodes import GraphAgent, GraphNode, GraphObject
from typing import Union, List, Optional
from typing import Union, List, Optional, TYPE_CHECKING

if TYPE_CHECKING:
from light.world.world import World


class UseEvent(GraphEvent):
Expand Down Expand Up @@ -326,3 +329,9 @@ def get_valid_actions(cls, graph: "OOGraph", actor: GraphAgent) -> List[GraphEve
valid_actions.append(cls(actor, target_nodes=[obj, entity]))

return valid_actions

def post_json_load(self, world: "World") -> None:
"""Do a json load on the processed event array"""
self.events = [
GraphEvent.from_json(json_event, world) for json_event in self.events
]
9 changes: 6 additions & 3 deletions light/world/souls/base_soul.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from copy import deepcopy
import os
import asyncio
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Optional
from light.graph.events.graph_events import SystemMessageEvent

if TYPE_CHECKING:
Expand All @@ -32,10 +32,13 @@ def __init__(self, target_node: "GraphAgent", world: "World"):
self.target_node._last_interaction_partner_id = None
self.reset_interaction_history(self.target_node)

def get_last_interaction_partner(self, node=None):
def get_last_interaction_partner(self, node=None) -> Optional["GraphAgent"]:
if node == None:
node = self.target_node
return node._last_interaction_partner_id
partner_id = node._last_interaction_partner_id
if partner_id is None:
return None
return self.world.oo_graph.get_node(partner_id)

def log_interaction_from_event(self, event):
event_name = event.__class__.__name__
Expand Down
22 changes: 7 additions & 15 deletions light/world/souls/models/generative_heuristic_model_soul.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,17 +274,13 @@ def npc_action(self):

# Get agents
agent = self.target_node
partner_id = self.get_last_interaction_partner(agent)
if partner_id is None:
return
partner = self.world.oo_graph.get_node(partner_id)
agent_id = agent.node_id
partner_id = self.get_last_interaction_partner()
if partner_id != None:
partner = self.world.oo_graph.get_node(partner_id)
partner = self.get_last_interaction_partner()
if partner is None:
return
partner_name = None
if partner != None:
partner_name = partner.name
else:
partner_name = None

quest_txt = None
if not hasattr(agent, "quests") or agent.quests is None:
Expand Down Expand Up @@ -388,13 +384,9 @@ def npc_dialogue(self, obs=None):
agent_id = agent.node_id

if obs is None:
partner_id = self.get_last_interaction_partner(agent)
if partner_id is None:
return
partner = self.world.oo_graph.get_node(partner_id)
partner = self.get_last_interaction_partner(agent)
else:
partner = obs.actor
partner_id = partner.node_id
partner_interactor_id = partner._last_interaction_partner_id
if (
isinstance(obs, SayEvent)
Expand Down Expand Up @@ -474,7 +466,7 @@ async def _take_timestep(self) -> None:
partner = random.choice(agents)
partner_id = partner.node_id
if (
partner.node_id != agent_id
partner_id != agent_id
and partner.get_prop("speed", 0) > 0
and self.get_last_interaction_partner(partner) is None
):
Expand Down
4 changes: 2 additions & 2 deletions light/world/souls/on_event_soul.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def trade_event_heuristics(self, event) -> bool:
def tell_goal_heuristics(self, event) -> bool:
agent = self.target_node
event_name = event.__class__.__name__
partner = self.get_last_interaction_partner(agent)
# Tell Mission to Other Agent (or not).
if (
(event_name == "SayEvent" and event.actor != agent)
Expand All @@ -221,7 +222,7 @@ def tell_goal_heuristics(self, event) -> bool:
and event.actor != agent
and event.target_nodes[0] == agent
)
) and (self.get_last_interaction_partner(agent) == event.actor.node_id):
) and (partner is not None and partner.node_id == event.actor.node_id):
about_goals = False
for words in ["mission", "goal", "quest", "what you want"]:
if words in event.text_content:
Expand Down Expand Up @@ -420,7 +421,6 @@ def timestep_actions(self):
return True

# Random movement for NPCs..

if (
random.randint(0, 300) < agent.speed
and self.get_last_interaction_partner(agent) is None
Expand Down
4 changes: 4 additions & 0 deletions light/world/utils/json_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@

class GraphEncoder(json.JSONEncoder):
def default(self, o):
from light.graph.events.base import GraphEvent

if isinstance(o, set):
return sorted(list(o))
if isinstance(o, list):
return sorted(o)
if isinstance(o, GraphEdge):
return {k: v for k, v in o.__dict__.copy().items() if not k.startswith("_")}
if isinstance(o, GraphEvent):
return o.to_json()
if not isinstance(o, GraphNode):
return super().default(o)
use_dict = {k: v for k, v in o.__dict__.copy().items() if not k.startswith("_")}
Expand Down