Skip to content

Commit

Permalink
Merge pull request #129 from oestrich/target-drop-tuple
Browse files Browse the repository at this point in the history
Start to remove the tuple wrapping characters
  • Loading branch information
oestrich committed Mar 9, 2019
2 parents 2b838f3 + 3dd9810 commit 0416d36
Show file tree
Hide file tree
Showing 80 changed files with 656 additions and 564 deletions.
2 changes: 2 additions & 0 deletions lib/data/character.ex
Expand Up @@ -14,6 +14,8 @@ defmodule Data.Character do
alias Data.User

schema "characters" do
field(:type, :string, virtual: true, default: "player")

field(:name, :string)
field(:save, Save)
field(:flags, {:array, :string})
Expand Down
2 changes: 2 additions & 0 deletions lib/data/npc.ex
Expand Up @@ -28,6 +28,8 @@ defmodule Data.NPC do
]

schema "npcs" do
field(:type, :string, virtual: true, default: "npc")

field(:original_id, :integer, virtual: true)
field(:name, :string)
field(:level, :integer, default: 1)
Expand Down
4 changes: 2 additions & 2 deletions lib/game/channel.ex
Expand Up @@ -119,8 +119,8 @@ defmodule Game.Channel do
{:noreply, state}
end

def handle_cast({:join_tell, pid, {type, who}}, state = %{tells: tells}) do
tells = Map.put(tells, "tells:#{type}:#{who.id}", pid)
def handle_cast({:join_tell, pid, %{type: type, id: id}}, state = %{tells: tells}) do
tells = Map.put(tells, "tells:#{type}:#{id}", pid)
{:noreply, Map.put(state, :tells, tells)}
end

Expand Down
4 changes: 2 additions & 2 deletions lib/game/channel/server.ex
Expand Up @@ -153,8 +153,8 @@ defmodule Game.Channel.Server do
A message will be sent to the player's session in the form of `{:channel, {:tell, from, message}}`.
"""
@spec tell(Channel.state(), Character.t(), Character.t(), Message.t()) :: :ok
def tell(%{tells: tells}, {type, who}, from, message) do
case tells |> Map.get("tells:#{type}:#{who.id}", nil) do
def tell(%{tells: tells}, %{type: type, id: id}, from, message) do
case tells |> Map.get("tells:#{type}:#{id}", nil) do
nil ->
nil

Expand Down
44 changes: 29 additions & 15 deletions lib/game/character.ex
Expand Up @@ -9,25 +9,28 @@ defmodule Game.Character do
- `{:apply_effects, effects, player}`
"""

alias Data.NPC
alias Data.User
alias Game.Character.Simple
alias Game.Character.Via

@typedoc """
Tagged tuple of a player or npc struct
Valid options:
- `{:player, player}`
- `{:npc, npc}`
A simple character struct
"""
@type t :: tuple()
@type t :: %Simple{}

@doc """
Convert a character into a stripped down version
"""
def to_simple(character = %Simple{}), do: character

def to_simple(character), do: Simple.from_character(character)

def simple_gossip(player_name) do
%Simple{
type: "gossip",
name: player_name
}
end

@doc """
Let the target know they are being targeted
"""
Expand Down Expand Up @@ -71,14 +74,25 @@ defmodule Game.Character do
GenServer.cast({:via, Via, who(target)}, {:notify, event})
end

@doc """
Check if a character equals another character, generaly the simple version
"""
def equal?(nil, _target), do: false

def equal?(_character, nil), do: false

def equal?({_, character}, {_, target}), do: equal?(character, target)

def equal?({_, character}, target), do: equal?(character, target)

def equal?(character, {_, target}), do: equal?(character, target)

def equal?(character, target) do
character.type == target.type && character.id == target.id
end

@doc """
Converts a tuple with a struct to a tuple with an id
"""
@spec who({:npc, integer()} | {:npc, NPC.t()}) :: {:npc, integer()}
@spec who({:player, integer()} | {:player, User.t()}) :: {:player, integer()}
def who(target)
def who({:npc, id}) when is_integer(id), do: {:npc, id}
def who({:npc, npc}), do: {:npc, npc.id}
def who({:player, id}) when is_integer(id), do: {:player, id}
def who({:player, player}), do: {:player, player.id}
def who(character = %Simple{}), do: character
end
36 changes: 21 additions & 15 deletions lib/game/character/simple.ex
Expand Up @@ -5,22 +5,24 @@ defmodule Game.Character.Simple do
Minimal data for accessing the full character
"""

defstruct [:type, :id, :name, extra: %{}]
defstruct [:type, :id, :name, :level, extra: %{}]

@doc """
Convert a character into their simple version
"""
def from_character({:npc, npc}), do: {:npc, from_npc(npc)}
def from_character({:player, player}), do: {:player, from_player(player)}
def from_character(npc = %{type: "npc"}), do: from_npc(npc)

def from_character(player = %{type: "player"}), do: from_player(player)

@doc """
Convert a player to the simple version
"""
def from_player(player) do
%__MODULE__{
type: :player,
type: player.type,
id: player.id,
name: player.name,
level: player.save.level,
extra: %{
room_id: player.save.room_id,
flags: player.flags,
Expand All @@ -35,20 +37,24 @@ defmodule Game.Character.Simple do
Convert an NPC to the simple version
"""
def from_npc(npc) do
extra =
Map.take(npc, [
:original_id,
:status_line,
:status_listen,
:description,
:experience_points,
:is_quest_giver,
:is_trainer,
:trainable_skills
])

%__MODULE__{
type: :npc,
type: npc.type,
id: npc.id,
name: npc.name,
extra:
Map.take(npc, [
:original_id,
:status_line,
:status_listen,
:description,
:is_quest_giver,
:is_trainer,
:trainable_skills
])
level: npc.level,
extra: extra
}
end
end
14 changes: 7 additions & 7 deletions lib/game/character/via.ex
Expand Up @@ -13,12 +13,12 @@ defmodule Game.Character.Via do
@spec whereis_name(any) :: pid
def whereis_name(who)

def whereis_name({:npc, id}) do
def whereis_name(%{type: "npc", id: id}) do
:global.whereis_name({Game.NPC, id})
end

def whereis_name({:player, player_id}) do
case Session.Registry.find_connected_player(player_id) do
def whereis_name(%{type: "player", id: id}) do
case Session.Registry.find_connected_player(id) do
%{pid: pid} ->
pid

Expand All @@ -33,14 +33,14 @@ defmodule Game.Character.Via do
@spec send(any, any) :: :ok
def send(who, message)

def send({:npc, id}, message) do
def send(%{type: "npc", id: id}, message) do
:global.send({Game.NPC, id}, message)
end

def send({:player, id}, message) do
case whereis_name({:player, id}) do
def send(character = %{type: "player"}, message) do
case whereis_name(character) do
:undefined ->
{:badarg, {{:player, id}, message}}
{:badarg, {character, message}}

pid ->
Kernel.send(pid, message)
Expand Down
5 changes: 3 additions & 2 deletions lib/game/command/drop.ex
Expand Up @@ -6,6 +6,7 @@ defmodule Game.Command.Drop do
use Game.Command
use Game.Currency

alias Game.Character
alias Game.Environment
alias Game.Item
alias Game.Items
Expand Down Expand Up @@ -94,7 +95,7 @@ defmodule Game.Command.Drop do
message = gettext("You dropped %{amount} %{currency}.", amount: amount, currency: currency())
state |> Socket.echo(message)

Environment.drop_currency(save.room_id, {:player, state.character}, amount)
Environment.drop_currency(save.room_id, Character.to_simple(state.character), amount)

{:update, state}
end
Expand All @@ -116,7 +117,7 @@ defmodule Game.Command.Drop do
{instance, items} = Item.remove(save.items, item)
state = Player.update_save(state, %{save | items: items})

Environment.drop(save.room_id, {:player, state.character}, instance)
Environment.drop(save.room_id, Character.to_simple(state.character), instance)

message = gettext("You dropped %{name}.", name: Format.item_name(item))
state |> Socket.echo(message)
Expand Down
5 changes: 3 additions & 2 deletions lib/game/command/emote.ex
Expand Up @@ -5,6 +5,7 @@ defmodule Game.Command.Emote do

use Game.Command

alias Game.Character
alias Game.Events.RoomHeard
alias Game.Format.Channels, as: FormatChannels

Expand Down Expand Up @@ -58,10 +59,10 @@ defmodule Game.Command.Emote do
def run(command, state)

def run({emote}, state = %{character: character, save: save}) do
state |> Socket.echo(FormatChannels.emote({:player, character}, emote))
state |> Socket.echo(FormatChannels.emote(character, emote))

message = Message.emote(character, emote)
event = %RoomHeard{character: {:player, character}, message: message}
event = %RoomHeard{character: Character.to_simple(character), message: message}
Environment.notify(save.room_id, event.character, event)

:ok
Expand Down
11 changes: 4 additions & 7 deletions lib/game/command/give.ex
Expand Up @@ -121,11 +121,8 @@ defmodule Game.Command.Give do
message = gettext("\"%{name}\" could not be found.", name: character_name)
state |> Socket.echo(message)

{:player, character} ->
send_item_to_character(state, instance, item, {:player, character})

{:npc, npc} ->
send_item_to_character(state, instance, item, {:npc, npc})
{:ok, character} ->
send_item_to_character(state, instance, item, character)
end
end

Expand All @@ -149,7 +146,7 @@ defmodule Game.Command.Give do

Socket.echo(state, message)

event = %CurrencyReceived{character: {:player, state.character}, amount: currency}
event = %CurrencyReceived{character: Character.to_simple(state.character), amount: currency}
Character.notify(character, event)

state = Player.update_save(state, %{save | currency: save.currency - currency})
Expand All @@ -167,7 +164,7 @@ defmodule Game.Command.Give do

state |> Socket.echo(message)

event = %ItemReceived{character: {:player, state.character}, instance: instance}
event = %ItemReceived{character: Character.to_simple(state.character), instance: instance}
Character.notify(character, event)

items = List.delete(save.items, instance)
Expand Down
4 changes: 2 additions & 2 deletions lib/game/command/move.ex
Expand Up @@ -248,7 +248,7 @@ defmodule Game.Command.Move do

CharacterInstrumenter.movement(:player, fn ->
Environment.unlink(save.room_id)
Environment.leave(save.room_id, {:player, character}, leave_reason)
Environment.leave(save.room_id, character, leave_reason)

clear_target(state)

Expand All @@ -263,7 +263,7 @@ defmodule Game.Command.Move do
|> Map.put(:is_targeting, MapSet.new())
|> Map.put(:is_afk, false)

Environment.enter(room_id, {:player, character}, enter_reason)
Environment.enter(room_id, character, enter_reason)
Environment.link(room_id)

Quest.track_progress(state.character, {:room, room_id})
Expand Down
10 changes: 6 additions & 4 deletions lib/game/command/say.ex
Expand Up @@ -7,6 +7,7 @@ defmodule Game.Command.Say do

import Game.Room.Helpers, only: [find_character: 3]

alias Game.Character
alias Game.Events.RoomHeard
alias Game.Format.Channels, as: FormatChannels
alias Game.Hint
Expand Down Expand Up @@ -145,7 +146,7 @@ defmodule Game.Command.Say do
state |> Socket.echo(FormatChannels.say(:you, parsed_message))

message = Message.new(character, parsed_message)
event = %RoomHeard{character: {:player, character}, message: message}
event = %RoomHeard{character: Character.to_simple(character), message: message}
Environment.notify(save.room_id, event.character, event)
end

Expand All @@ -156,8 +157,8 @@ defmodule Game.Command.Say do
{:error, :not_found} ->
state |> Socket.echo(gettext("No character could be found matching your text."))

directed_character ->
message = Utility.strip_name(elem(directed_character, 1), parsed_message.message)
{:ok, directed_character} ->
message = Utility.strip_name(directed_character, parsed_message.message)

parsed_message =
parsed_message
Expand All @@ -168,11 +169,12 @@ defmodule Game.Command.Say do
state |> Socket.echo(message)

message = Message.say_to(character, directed_character, parsed_message)
event = %RoomHeard{character: {:player, character}, message: message}
event = %RoomHeard{character: Character.to_simple(character), message: message}
Environment.notify(room.id, event.character, event)
end
end

defp maybe_hint_on_quotes(state, %{is_quoted: true}), do: Hint.gate(state, "say.quoted")

defp maybe_hint_on_quotes(_state, _message), do: :ok
end

0 comments on commit 0416d36

Please sign in to comment.