Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
6243448
Room.Context.put_data() added so contexts can update the context.data…
i-am-tanni Dec 16, 2022
1ed7473
Added get_meta() for pulling meta data out of conn.character
i-am-tanni Feb 18, 2023
3bd207e
Added event hooks for zones
i-am-tanni Feb 19, 2023
a12ab16
Added callback for initializing the zone
i-am-tanni Feb 19, 2023
b15e61e
Zone @behavior no longer necessary now that callbacks are introduced
i-am-tanni Feb 19, 2023
635da47
Added initalized protocol to zone
i-am-tanni Feb 19, 2023
e4cd61e
Zones can now render output back to users
i-am-tanni Feb 20, 2023
8ab0a47
Fixed handle_continue() so state would be updated
i-am-tanni Feb 21, 2023
847599f
handle_continue() now updates state
i-am-tanni Feb 21, 2023
af49564
Added delay_event()
i-am-tanni Feb 23, 2023
6521f02
Removed delay_event() from room to zone
i-am-tanni Feb 23, 2023
b04a8d3
Zone can now handle delayed events
i-am-tanni Feb 23, 2023
a1aca73
Clarity
i-am-tanni Feb 24, 2023
428b7c0
Removed delay from rooms for now
i-am-tanni Mar 16, 2023
7727ffa
Movement now passes a room an event with data that it can route
i-am-tanni Jun 10, 2023
1694b8e
move() data and assigns are now separate concepts
i-am-tanni Jun 13, 2023
1c877c4
Fixed bug where communication module returns :error instead of {:erro…
i-am-tanni Jun 17, 2023
064b8e0
Exposed items to be changed outside add/drop. This is important for g…
i-am-tanni Jul 31, 2023
318fa6d
Context.prompt() default for assigns parameter
i-am-tanni Aug 6, 2023
129a055
Added delete(cache, value) function for caches
i-am-tanni Aug 20, 2023
1a79688
Added broadcast function
i-am-tanni Dec 4, 2023
3834b31
Revised broadcast function
i-am-tanni Dec 4, 2023
8fcb48e
Documentation of options
i-am-tanni Dec 18, 2023
6424508
Fixed callback typespec
i-am-tanni Jan 4, 2024
99483e5
Remove broken feature
i-am-tanni Jan 14, 2024
a0c1cea
revert change
i-am-tanni Jan 16, 2024
f6aca19
Movement structures now optionally carry the entrance name
i-am-tanni Jan 18, 2024
84e1cd2
Allow data to be passed to Movement events
i-am-tanni Feb 17, 2024
a5a6362
Allow data to be passed to Movement events
i-am-tanni Feb 17, 2024
b598687
Attempt #3 at passing data to Movement.Notice
i-am-tanni Feb 17, 2024
3af0e26
Make the notice data leaner
i-am-tanni Feb 17, 2024
6a5913a
Expose controller in conn
i-am-tanni Feb 18, 2024
d876c6d
Consider the case data could be nil
i-am-tanni Feb 25, 2024
84d35a6
Assigns should override flash in merge_assigns, not vice versa
i-am-tanni Feb 26, 2024
b3206c4
Restricting move so that it does not have access to flash or session …
i-am-tanni Feb 27, 2024
25f9a2a
Prevent flash and session data from leaking to assigns
i-am-tanni Feb 28, 2024
8edd522
Fix assigns on move
i-am-tanni Feb 28, 2024
8925141
Rooms now allow character data updates
i-am-tanni Mar 1, 2024
38981c6
Fix typo
i-am-tanni Mar 1, 2024
d66f3b3
Fixed error when there is no extra event data to pass
i-am-tanni Jul 8, 2024
7edacbd
Revert "Fixed error when there is no extra event data to pass"
i-am-tanni Jul 8, 2024
449d367
Fixed issue when extra_data on Movement.Notice is nil
i-am-tanni Jul 8, 2024
8051d1a
Added default assing of ':self' for prompt instead of ':character' to…
i-am-tanni Oct 18, 2024
687fcdd
Cleanup and add handle request to dump state
i-am-tanni Oct 27, 2024
1a59efa
Only send what is necessary of the room state in a dump
i-am-tanni Oct 27, 2024
65dc3d6
Fix bug preventing the split to match
i-am-tanni Oct 29, 2024
fb86523
Added assigns to Zone context
i-am-tanni Oct 30, 2024
b7ab995
Should make the server feel snappier
i-am-tanni Nov 29, 2024
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
14 changes: 14 additions & 0 deletions lib/kalevala/cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ defmodule Kalevala.Cache do
Kalevala.Cache.get(__MODULE__, key)
end

@doc """
Delete a value from the cache
"""
def delete(key) do
Kalevala.Cache.delete(__MODULE__, key)
end

@doc """
Get a value from the cache

Expand Down Expand Up @@ -118,6 +125,13 @@ defmodule Kalevala.Cache do
end
end

@doc """
Delete a value from the cache
"""
def delete(name, key) do
:ets.delete(name, key)
end

@doc """
Get a list of all keys in a table
"""
Expand Down
2 changes: 1 addition & 1 deletion lib/kalevala/character/action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Kalevala.Character.Action do
flee in a random direction.
"""

@callback run(Conn.t(), map()) :: :ok
@callback run(Conn.t(), map()) :: Conn.t()

defstruct [:request_id, :type, delay: 0, params: %{}]

Expand Down
29 changes: 20 additions & 9 deletions lib/kalevala/character/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ defmodule Kalevala.Character.Conn.Text do
new text.
"""

defstruct [:data, newline: false, go_ahead: false]
defstruct [:data, newline: false, go_ahead: true]
end

defmodule Kalevala.Character.Conn.Option do
Expand All @@ -116,6 +116,7 @@ defmodule Kalevala.Character.Conn do
alias Kalevala.Meta

defstruct [
:controller,
:character,
:params,
assigns: %{},
Expand Down Expand Up @@ -159,10 +160,9 @@ defmodule Kalevala.Character.Conn do
end

defp merge_assigns(conn, assigns) do
conn.session
|> Map.put(:character, Private.character(conn))
Map.new()
|> Map.put(:self, Private.character(conn))
|> Map.merge(conn.assigns)
|> Map.merge(conn.flash)
|> Map.merge(assigns)
end

Expand Down Expand Up @@ -307,9 +307,11 @@ defmodule Kalevala.Character.Conn do
@doc """
Creates an even to move from one room to another
"""
def move(conn, direction, room_id, view, template, assigns) do
assigns = merge_assigns(conn, assigns)
data = view.render(template, assigns)
def move(conn, direction, room_id, view, template, data \\ %{}) do
data = Map.put(data, :character, Private.character(conn))
assigns = merge_assigns(conn, data)

reason = view.render(template, assigns)

event = %Kalevala.Event{
acting_character: Private.character(conn),
Expand All @@ -318,8 +320,9 @@ defmodule Kalevala.Character.Conn do
data: %Kalevala.Event.Movement{
character: Private.character(conn),
direction: direction,
reason: data,
room_id: room_id
reason: reason,
room_id: room_id,
data: data
}
}

Expand Down Expand Up @@ -449,6 +452,14 @@ defmodule Kalevala.Character.Conn do
put_character(conn, character)
end

@doc """
Get values in a character's meta map
"""
def get_meta(conn, key) do
character = character(conn)
Meta.get(character.meta, key)
end

defp put_private(conn, key, value) do
private = Map.put(conn.private, key, value)
Map.put(conn, :private, private)
Expand Down
1 change: 1 addition & 0 deletions lib/kalevala/character/foreman.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ defmodule Kalevala.Character.Foreman do
@doc false
def new_conn(state) do
%Conn{
controller: state.controller,
character: state.character,
session: state.session,
flash: state.flash,
Expand Down
6 changes: 3 additions & 3 deletions lib/kalevala/communication.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ defmodule Kalevala.Communication do
Channel.subscribe(pid, channel_name, subscriber_pid, options)

_ ->
:error
{:error, :channel_not_found}
end
end

Expand All @@ -119,7 +119,7 @@ defmodule Kalevala.Communication do
Channel.unsubscribe(pid, channel_name, subscriber_pid, options)

_ ->
:error
{:error, :channel_not_found}
end
end

Expand All @@ -142,6 +142,6 @@ defmodule Kalevala.Communication do
Only events with a topic of #{__MODULE__} allowed.
""")

:error
{:error, :channel_not_found}
end
end
10 changes: 6 additions & 4 deletions lib/kalevala/event/movement.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Kalevala.Event.Movement do
An event to move from one room to another
"""

defstruct [:character, :direction, :reason, :room_id]
defstruct [:character, :direction, :reason, :room_id, :data]

@typedoc """
A movement event
Expand All @@ -22,15 +22,15 @@ defmodule Kalevala.Event.Movement.Notice do
Event to send a notice to other characters in the room
"""

defstruct [:character, :direction, :reason]
defstruct [:character, :direction, :reason, :data]
end

defmodule Kalevala.Event.Movement.Commit do
@moduledoc """
Struct for committing movement between two rooms
"""

defstruct [:character, :to, :from, :exit_name]
defstruct [:character, :to, :from, :exit_name, :entrance_name]
end

defmodule Kalevala.Event.Movement.Abort do
Expand All @@ -57,6 +57,7 @@ defmodule Kalevala.Event.Movement.Voting do
:to,
:from,
:exit_name,
:entrance_name,
:reason,
aborted: false
]
Expand Down Expand Up @@ -86,7 +87,8 @@ defmodule Kalevala.Event.Movement.Voting do
character: event.data.character,
to: event.data.to,
from: event.data.from,
exit_name: event.data.exit_name
exit_name: event.data.exit_name,
entrance_name: event.data.entrance_name
}
}
end
Expand Down
7 changes: 6 additions & 1 deletion lib/kalevala/world/room.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ defmodule Kalevala.World.Room do

@impl true
def handle_continue(:initialized, state) do
Callbacks.initialized(state.data)
data = Callbacks.initialized(state.data)
state = %{state | data: data}
{:noreply, state}
end

Expand All @@ -114,6 +115,10 @@ defmodule Kalevala.World.Room do
{:reply, :ok, state}
end

def handle_call(:dump, _from, state) do
{:reply, state.data, state}
end

@impl true
def handle_info(event = %Event{}, state) do
Events.handle_event(event, state)
Expand Down
41 changes: 40 additions & 1 deletion lib/kalevala/world/room/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule Kalevala.World.Room.Context do
@doc """
Render a prompt back to a pid
"""
def prompt(context, to_pid, view, template, assigns) do
def prompt(context, to_pid, view, template, assigns \\ %{}) do
assigns = Map.merge(context.assigns, assigns)
data = view.render(template, assigns)
push(context, to_pid, data, true)
Expand All @@ -94,6 +94,45 @@ defmodule Kalevala.World.Room.Context do
Map.put(context, :events, context.events ++ [{to_pid, event}])
end

@doc """
Update the context data
"""
def put_data(context, key, val) do
data = Map.put(context.data, key, val)
Map.put(context, :data, data)
end

def update_character(context, character) do
%Kalevala.Character{id: id} = character

characters =
Enum.map(context.characters, fn
%{id: ^id} -> character
no_change -> no_change
end)

%{context | characters: characters}
end

@doc """
Broadcast an event to multiple characters in the room
Options:
- `to`: pre-filtered list of characters
"""
def broadcast(context, topic, data, opts \\ []) do
recipients = Keyword.get(opts, :to, context.characters)

recipients =
case !is_list(recipients) do
true -> List.wrap(recipients)
false -> recipients
end

Enum.reduce(recipients, context, fn character, acc ->
event(acc, character.pid, self(), topic, data)
end)
end

@doc """
Handle context after processing an event
"""
Expand Down
31 changes: 28 additions & 3 deletions lib/kalevala/world/room/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,23 @@ defmodule Kalevala.World.Room.Events do
case event.data.room_id == state.data.id do
true ->
state = Movement.handle_event(state, event)
{:noreply, state}

extra_data = with nil <- event.data.data, do: %{}
extra_data = Map.delete(extra_data, :character)

event = %Kalevala.Event{
acting_character: event.data.character,
from_pid: event.from_pid,
topic: Kalevala.Event.Movement.Notice,
data: %Kalevala.Event.Movement.Notice{
character: event.data.character,
direction: event.data.direction,
data: extra_data,
reason: event.data.reason
}
}

handle_event(event, state)

false ->
Room.global_name(event.data.room_id)
Expand Down Expand Up @@ -76,7 +92,12 @@ defmodule Kalevala.World.Room.Events do
|> Handler.event(event)
|> Context.handle_context()

state = Map.put(state, :data, context.data)
private =
state.private
|> Map.put(:characters, context.characters)
|> Map.put(:item_instances, context.item_instances)

state = %{state | data: context.data, private: private}

{:noreply, state}
end
Expand Down Expand Up @@ -264,14 +285,18 @@ defmodule Kalevala.World.Room.Movement do
Broadcast the event to characters in the room
"""
def broadcast(state, event) do
data = event.data.data
data = if is_map(data), do: Map.delete(event.data.data, :character)

event = %Kalevala.Event{
acting_character: event.data.character,
from_pid: event.from_pid,
topic: Kalevala.Event.Movement.Notice,
data: %Kalevala.Event.Movement.Notice{
character: event.data.character,
direction: event.data.direction,
reason: event.data.reason
reason: event.data.reason,
data: data
}
}

Expand Down
Loading