Skip to content
Open
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
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# syntax=docker/dockerfile:1
FROM erlang:25.0.4

LABEL author="Fabien Lamarche-Filion"

EXPOSE 8080

ENV ERL_AFLAGS="-enable-feature all"

RUN useradd -ms /bin/bash warp
WORKDIR /warp
USER warp
CMD bash
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
warp
=====

`warp` is a real time economic simulator that represents an arbitrary large 3d galaxy.
`warp` (working title) is a real time economic simulator that represents an arbitrary large 3d galaxy.

Very WIP.

Expand All @@ -10,14 +10,23 @@ Dependencies: erlang 25.0.4 or docker.
Build
-----
Erlang:
$ rebar3 compile
$ rebar3 as dev compile

Docker:
Build docker image:
$ docker image build --tag warp:dev .

Run docker with a bind on your local directory:
$ docker container run -it -v $(pwd):/warp:rw warp:dev
(or run `./dev.sh)

Compile from within the container:
$ rebar3 compile

Run
-----
running `./dev.sh` will start the development Docker image
and expose port 8080.

Start the application with
$ rebar3 shell
2 changes: 2 additions & 0 deletions dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
docker container run -it --publish 8080:8080 -v $(pwd):/warp:rw warp:dev
6 changes: 6 additions & 0 deletions include/warp.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-ifndef(WARP_HEADER).
-define(WARP_HEADER, defined).

-type coord() :: {integer(), integer(), integer()}.

-endif.
42 changes: 42 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{erl_opts, [debug_info]}.
{deps, [
observer_cli,
{cowboy, ".*",
{git, "https://github.com/extend/cowboy.git", {tag, "2.9.0"}}},
{jiffy, ".*",
{git, "https://github.com/davisp/jiffy.git", {tag, "1.1.1"}}}
]}.

{plugins, [
{gradualizer,
{git, "https://github.com/josefs/Gradualizer.git", {tag, "0.1.3"}}},
erlfmt
]}.

{erlfmt, [
write,
{files, "{src,include,test}/**/*.{hrl,erl}"}
]}.

{gradualizer_opts, [
{i, "./include"},
{print_file, true}
]}.

{profiles, [
{dev, [
{provider_hooks, [
{pre, [
{compile, fmt}
]},
{post, [
{compile, {default, gradualizer}}
]}
]}
]}
]}.

{shell, [
% {config, "config/sys.config"},
{apps, [warp]}
]}.
22 changes: 22 additions & 0 deletions src/http_character_lookup_handler.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-module(http_character_lookup_handler).

-behaviour(cowboy_handler).

-export([init/2]).

init(Req0, State) ->
Req =
case cowboy_req:binding(character_id, Req0, undefined) of
undefined ->
cowboy_req:reply(400, #{}, <<>>, Req0);
Id ->
case warp_character_server:lookup(Id) of
{error, _} ->
cowboy_req:reply(204, #{}, <<>>, Req0);
{ok, CharacterPid} ->
{ok, CharacterState} = warp_character:get_state(CharacterPid),
StateJson = utils:ensure_binary(jiffy:encode(CharacterState)),
cowboy_req:reply(200, #{}, <<StateJson/binary, "\n">>, Req0)
end
end,
{ok, Req, State}.
21 changes: 21 additions & 0 deletions src/http_character_spawn_handler.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-module(http_character_spawn_handler).

-behaviour(cowboy_handler).

-export([init/2]).

init(Req0, State) ->
Req =
case cowboy_req:binding(character_id, Req0, undefined) of
undefined ->
cowboy_req:reply(400, #{}, <<>>, Req0);
ID ->
case warp_character_server:lookup(ID) of
{error, character_not_found} ->
warp_character_server:spawn_character(ID),
cowboy_req:reply(200, #{}, <<>>, Req0);
{ok, _CharacterPid} ->
cowboy_req:reply(304, #{}, <<>>, Req0)
end
end,
{ok, Req, State}.
26 changes: 26 additions & 0 deletions src/http_ship_move_handler.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-module(http_ship_move_handler).
-include("warp.hrl").
-include("ship.hrl").

-behaviour(cowboy_handler).

-export([init/2]).

init(Req0, State) ->
% EEEEWWW. TODO: anything but that, really. This needs to be properly validated
MoveCoord = {
binary_to_integer(cowboy_req:binding(x, Req0, undefined)),
binary_to_integer(cowboy_req:binding(y, Req0, undefined)),
binary_to_integer(cowboy_req:binding(z, Req0, undefined))
},
ShipId = cowboy_req:binding(ship_id, Req0, undefined),

{ok, ShipPid} = warp_ship_server:lookup(ShipId),
#ship_state{position = StartCoord} = warp_ship:get_state(ShipPid),
{ok, Distance} = warp_ship:move_to(ShipPid, MoveCoord),
Message = io_lib:format("You moved ~p space units from ~p to ~p\n", [
Distance, StartCoord, MoveCoord
]),
MessageBin = list_to_binary(lists:flatten(Message)),
Req = cowboy_req:reply(200, #{}, <<MessageBin/binary>>, Req0),
{ok, Req, State}.
10 changes: 10 additions & 0 deletions src/http_ship_spawn_handler.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-module(http_ship_spawn_handler).

-behaviour(cowboy_handler).

-export([init/2]).

init(Req, State) ->
Id = warp_ship_server:spawn_ship(),
cowboy_req:reply(200, #{}, <<Id/binary>>, Req),
{ok, Req, State}.
23 changes: 23 additions & 0 deletions src/http_space_object_lookup_handler.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-module(http_space_object_lookup_handler).

-behaviour(cowboy_handler).

-export([init/2]).

init(Req0, State) ->
% EEEEWWW. TODO: anything but that, really. This needs to be properly validated
Coords = {
binary_to_integer(cowboy_req:binding(x, Req0, undefined)),
binary_to_integer(cowboy_req:binding(y, Req0, undefined)),
binary_to_integer(cowboy_req:binding(z, Req0, undefined))
},
Req =
case warp_space_object_server:lookup(Coords) of
{error, _} ->
cowboy_req:reply(204, #{}, <<>>, Req0);
{ok, SpaceObjectPid} ->
{ok, SpaceObjectState} = warp_space_object:get_state(SpaceObjectPid),
StateJson = utils:ensure_binary(jiffy:encode(SpaceObjectState)),
cowboy_req:reply(200, #{}, <<StateJson/binary, "\n">>, Req0)
end,
{ok, Req, State}.
44 changes: 44 additions & 0 deletions src/http_space_object_scan_handler.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-module(http_space_object_scan_handler).

-behaviour(cowboy_handler).

-export([init/2]).

init(Req0, State) ->
% EEEEWWW. TODO: anything but that, really. This needs to be properly validated
Coord = {
binary_to_integer(cowboy_req:binding(x, Req0, undefined)),
binary_to_integer(cowboy_req:binding(y, Req0, undefined)),
binary_to_integer(cowboy_req:binding(z, Req0, undefined))
},
Radius = binary_to_integer(cowboy_req:binding(r, Req0, undefined)),

SpaceObjects = scan(Coord, Radius),
BinaryCoordsSpaceObjects = [
<<
(integer_to_binary(X))/binary,
"/",
(integer_to_binary(Y))/binary,
"/",
(integer_to_binary(Z))/binary,
"\n"
>>
|| {{X, Y, Z}, _Pid} <- SpaceObjects
],
Req = cowboy_req:reply(200, #{}, BinaryCoordsSpaceObjects, Req0),
{ok, Req, State}.

% private functions
% TODO: obviously, this needs to be a sphere, not a cube
scan(Coord, Radius) ->
%% Xs = lists:seq(X - Radius, X + Radius),
%% Ys = lists:seq(Y - Radius, Y + Radius),
%% Zs = lists:seq(Z - Radius, Z + Radius),
% Look at me I erlang good herp derp
%% AllCoords = [{X1, Y1, Z1} || X1 <- Xs, Y1 <- Ys, Z1 <- Zs],
%% Results = [{Coord, warp_space_object_server:lookup(Coord)} || Coord <- AllCoords],
%% FilteredResults = [{Coord, Pid} || {Coord, {ok, Pid}} <- Results].
{ok, Results} = warp_space_object_server:get_sphere(Coord, Radius),
Results.

%[warp_space_object:get_state(Pid) || {_, Pid} <- Results].
Loading