Packages
game_server_sdk
1.0.1039
1.0.1082
1.0.1079
1.0.1078
1.0.1077
1.0.1076
1.0.1075
1.0.1074
1.0.1073
1.0.1070
1.0.1068
1.0.1067
1.0.1063
1.0.1059
1.0.1058
1.0.1057
1.0.1056
1.0.1055
1.0.1050
1.0.1049
1.0.1048
1.0.1047
1.0.1046
1.0.1044
1.0.1043
1.0.1042
1.0.1041
1.0.1040
1.0.1039
1.0.1038
1.0.1034
1.0.1033
1.0.1029
1.0.1028
1.0.1026
1.0.1025
1.0.1024
1.0.1023
1.0.1022
1.0.1021
1.0.1020
1.0.1019
1.0.1018
1.0.1017
1.0.1016
1.0.1015
1.0.1014
1.0.1013
1.0.1012
1.0.1011
1.0.1009
1.0.1008
1.0.1007
1.0.1006
1.0.1005
1.0.1004
1.0.1003
1.0.1001
1.0.999
1.0.998
1.0.997
1.0.996
1.0.995
1.0.994
1.0.993
1.0.992
1.0.991
1.0.990
1.0.989
1.0.988
1.0.987
1.0.986
1.0.985
1.0.984
1.0.983
1.0.982
1.0.981
1.0.980
1.0.979
1.0.978
1.0.977
1.0.976
1.0.975
1.0.974
1.0.973
1.0.972
1.0.971
1.0.970
1.0.969
1.0.968
1.0.967
1.0.966
1.0.965
1.0.964
1.0.963
1.0.962
1.0.961
1.0.959
1.0.958
1.0.956
1.0.951
1.0.950
1.0.943
1.0.942
1.0.941
1.0.940
1.0.938
1.0.936
1.0.935
1.0.931
1.0.929
1.0.928
1.0.927
1.0.926
1.0.925
1.0.924
1.0.923
1.0.921
1.0.920
1.0.919
1.0.918
1.0.917
1.0.916
1.0.911
1.0.910
1.0.902
1.0.899
1.0.898
1.0.897
1.0.896
1.0.894
1.0.893
1.0.891
1.0.890
1.0.889
1.0.888
1.0.887
1.0.886
1.0.885
1.0.884
1.0.883
1.0.882
1.0.881
1.0.880
1.0.879
1.0.878
1.0.877
1.0.26
1.0.25
1.0.22
1.0.21
1.0.20
1.0.19
1.0.15
1.0.14
1.0.13
1.0.12
1.0.10
1.0.9
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.1.0
SDK for GameServer hooks development. Provides type specs, documentation, and IDE autocomplete for GameServer modules without requiring the full server.
Current section
Files
Jump to
Current section
Files
lib/game_server/kv.ex
defmodule GameServer.KV do
@moduledoc ~S"""
Generic key/value storage.
This is intentionally minimal and un-opinionated.
If you want namespacing, encode it in `key` (e.g. `"polyglot_pirates:key1"`).
If you want per-user values, pass `user_id: ...` to `get/2`, `put/4`, and `delete/2`.
If you want per-lobby values, pass `lobby_id: ...` to the same functions.
You can also pass both to scope a key to a user within a lobby.
This module uses the app cache (`GameServer.Cache`) as a best-effort read cache.
Writes update the cache and deletes evict it.
**Note:** This is an SDK stub. Calling these functions will raise an error.
The actual implementation runs on the GameServer.
"""
@type list_opts() :: [
page: pos_integer(),
page_size: pos_integer(),
user_id: Ecto.UUID.t(),
lobby_id: Ecto.UUID.t(),
global_only: boolean(),
key: String.t()
]
@type attrs() :: %{
:key => String.t(),
optional(:user_id) => String.t(),
optional(:lobby_id) => String.t(),
:value => value(),
optional(:metadata) => metadata()
}
@type payload() :: %{value: value(), metadata: metadata()}
@type metadata() :: map()
@type value() :: map()
@doc ~S"""
Count the number of entries that match the optional filter.
Accepts the same options as `list_entries/1` (see `t:list_opts/0`). Returns a non-negative integer.
"""
@spec count_entries() :: non_neg_integer()
def count_entries() do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
0
_ ->
raise "GameServer.KV.count_entries/0 is a stub - only available at runtime on GameServer"
end
end
@doc ~S"""
Count the number of entries that match the optional filter.
Accepts the same options as `list_entries/1` (see `t:list_opts/0`). Returns a non-negative integer.
"""
@spec count_entries(list_opts()) :: non_neg_integer()
def count_entries(_opts) do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
0
_ ->
raise "GameServer.KV.count_entries/1 is a stub - only available at runtime on GameServer"
end
end
@doc ~S"""
Create a new `Entry` from `attrs` (expecting `key`, optional `user_id`/`lobby_id`,
`value`, `metadata`).
Returns `{:ok, entry}` or `{:error, changeset}`.
"""
@spec create_entry(attrs()) :: {:ok, GameServer.KV.Entry.t()} | {:error, Ecto.Changeset.t()}
def create_entry(_attrs) do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
{:ok, nil}
_ ->
raise "GameServer.KV.create_entry/1 is a stub - only available at runtime on GameServer"
end
end
@doc ~S"""
Delete the entry at `key`.
Pass `user_id: id` or `lobby_id: id` in `opts` to delete a scoped key. Returns `:ok`.
"""
@spec delete(String.t()) :: :ok
def delete(_key) do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
:ok
_ ->
raise "GameServer.KV.delete/1 is a stub - only available at runtime on GameServer"
end
end
@doc ~S"""
Delete the entry at `key`.
Pass `user_id: id` or `lobby_id: id` in `opts` to delete a scoped key. Returns `:ok`.
"""
@spec delete(
String.t(),
keyword()
) :: :ok
def delete(_key, _opts) do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
:ok
_ ->
raise "GameServer.KV.delete/2 is a stub - only available at runtime on GameServer"
end
end
@doc ~S"""
Delete an entry by its `id`.
Returns `:ok` whether or not the entry existed.
"""
@spec delete_entry(pos_integer()) :: :ok
def delete_entry(_id) do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
:ok
_ ->
raise "GameServer.KV.delete_entry/1 is a stub - only available at runtime on GameServer"
end
end
@doc ~S"""
Retrieve the value and metadata stored for `key`.
Pass `user_id: id` or `lobby_id: id` in `opts` to scope the lookup.
Returns `{:ok, %{value: map(), metadata: map()}}` when found, or `:error` when not present.
"""
@spec get(String.t()) :: {:ok, payload()} | :error
def get(_key) do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
if :erlang.phash2(make_ref(), 2) == 0, do: :error, else: {:ok, %{value: %{}, metadata: %{}}}
_ ->
raise "GameServer.KV.get/1 is a stub - only available at runtime on GameServer"
end
end
@doc ~S"""
Retrieve the value and metadata stored for `key`.
Pass `user_id: id` or `lobby_id: id` in `opts` to scope the lookup.
Returns `{:ok, %{value: map(), metadata: map()}}` when found, or `:error` when not present.
"""
@spec get(
String.t(),
keyword()
) :: {:ok, payload()} | :error
def get(_key, _opts) do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
if :erlang.phash2(make_ref(), 2) == 0, do: :error, else: {:ok, %{value: %{}, metadata: %{}}}
_ ->
raise "GameServer.KV.get/2 is a stub - only available at runtime on GameServer"
end
end
@doc ~S"""
Fetch an `Entry` by its `id`.
Returns the `Entry` struct or `nil` if not found.
"""
@spec get_entry(Ecto.UUID.t()) :: GameServer.KV.Entry.t() | nil
def get_entry(_id) do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
nil
_ ->
raise "GameServer.KV.get_entry/1 is a stub - only available at runtime on GameServer"
end
end
@doc ~S"""
List key/value entries with optional pagination and filtering.
Supported options: `:page`, `:page_size`, `:user_id`, `:lobby_id`, `:global_only`,
and `:key` (substring filter).
See `t:list_opts/0` for the expected option types.
Returns a list of `Entry` structs ordered by most recently updated.
"""
@spec list_entries() :: [GameServer.KV.Entry.t()]
def list_entries() do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
[]
_ ->
raise "GameServer.KV.list_entries/0 is a stub - only available at runtime on GameServer"
end
end
@doc ~S"""
List key/value entries with optional pagination and filtering.
Supported options: `:page`, `:page_size`, `:user_id`, `:lobby_id`, `:global_only`,
and `:key` (substring filter).
See `t:list_opts/0` for the expected option types.
Returns a list of `Entry` structs ordered by most recently updated.
"""
@spec list_entries(list_opts()) :: [GameServer.KV.Entry.t()]
def list_entries(_opts) do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
[]
_ ->
raise "GameServer.KV.list_entries/1 is a stub - only available at runtime on GameServer"
end
end
@doc ~S"""
Store `value` with optional `metadata` at `key`.
When using the 4-arity, supported options include `user_id: id` or `lobby_id: id` to scope
the entry.
Returns `{:ok, entry}` on success or `{:error, changeset}` on validation failure.
"""
@spec put(String.t(), value()) :: {:ok, GameServer.KV.Entry.t()} | {:error, Ecto.Changeset.t()}
def put(_key, _value) do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
{:ok, nil}
_ ->
raise "GameServer.KV.put/2 is a stub - only available at runtime on GameServer"
end
end
@doc false
@spec put(String.t(), value(), metadata()) ::
{:ok, GameServer.KV.Entry.t()} | {:error, Ecto.Changeset.t()}
def put(_key, _value, _metadata) do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
{:ok, nil}
_ ->
raise "GameServer.KV.put/3 is a stub - only available at runtime on GameServer"
end
end
@doc ~S"""
Store `value` with optional `metadata` at `key`.
When using the 4-arity, supported options include `user_id: id` or `lobby_id: id` to scope
the entry.
Returns `{:ok, entry}` on success or `{:error, changeset}` on validation failure.
"""
@spec put(String.t(), value(), metadata(), list_opts()) ::
{:ok, GameServer.KV.Entry.t()} | {:error, Ecto.Changeset.t()}
def put(_key, _value, _metadata, _opts) do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
{:ok, nil}
_ ->
raise "GameServer.KV.put/4 is a stub - only available at runtime on GameServer"
end
end
@doc ~S"""
Subscribe the current process to changes for a specific key/scope.
"""
@spec subscribe(
String.t(),
keyword()
) :: :ok | {:error, term()}
def subscribe(_key, _opts) do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
:ok
_ ->
raise "GameServer.KV.subscribe/2 is a stub - only available at runtime on GameServer"
end
end
@doc ~S"""
Unsubscribe the current process from changes for a specific key/scope.
"""
@spec unsubscribe(
String.t(),
keyword()
) :: :ok | {:error, term()}
def unsubscribe(_key, _opts) do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
:ok
_ ->
raise "GameServer.KV.unsubscribe/2 is a stub - only available at runtime on GameServer"
end
end
@doc ~S"""
Update an existing entry by `id` with `attrs`.
Returns `{:ok, entry}`, `{:error, :not_found}` if missing, or `{:error, changeset}` on validation error.
"""
@spec update_entry(pos_integer(), attrs()) ::
{:ok, GameServer.KV.Entry.t()} | {:error, :not_found} | {:error, Ecto.Changeset.t()}
def update_entry(_id, _attrs) do
case Application.get_env(:game_server_sdk, :stub_mode, :raise) do
:placeholder ->
{:ok, nil}
_ ->
raise "GameServer.KV.update_entry/2 is a stub - only available at runtime on GameServer"
end
end
end