Skip to content

Commit

Permalink
Avoid github dependency + add elixir syntax sweet
Browse files Browse the repository at this point in the history
  • Loading branch information
netoctone committed Jan 25, 2014
1 parent 05a50b5 commit c0f969e
Show file tree
Hide file tree
Showing 13 changed files with 849 additions and 9 deletions.
18 changes: 10 additions & 8 deletions tetris-servers/erlang/Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
.PHONY: all clean

all:
mkdir -p lib
cd lib && git clone https://github.com/jeremyong/websocket_client
cd ..
mkdir -p ebin
erlc -o ebin -pa ebin lib/websocket_client/src/*.erl
erlc -o ebin -pa ebin utilities.erl
erlc -o ebin -pa ebin player.erl
erlc -o ebin -pa ebin ws_handler.erl
erlc -o ebin -pa ebin erlang_lib/*.erl
erlc -o ebin -pa ebin websocket_client/src/*.erl
erlc -o ebin -pa ebin erlang_lib/ws_handler.erl

elixir:
mkdir -p ebin
elixirc -o ebin -pa ebin elixir_lib/*.ex
erlc -o ebin -pa ebin websocket_client/src/*.erl
erlc -o ebin -pa ebin elixir_lib/ws_handler.erl

clean:
rm -rf ebin lib
rm -rf ebin
Binary file not shown.
28 changes: 28 additions & 0 deletions tetris-servers/erlang/elixir_lib/glass.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Glass do
defp parse_glass(binary) do
Enum.map Enum.chunk(String.to_char_list!(binary), 10), fn(chunk) ->
Enum.map String.codepoints(iolist_to_binary(chunk)), fn(token) ->
case binary_to_atom(token) do
:' ' -> :blank
:'*' -> :box
end
end
end
end

def parse(binary) do
Enum.map String.split(binary, "&"), fn(pair) ->
[name, value_raw] = String.split(pair, "=")
name_atom = binary_to_atom(name)
value = case name_atom do
:figure -> binary_to_atom(value_raw) # :O, :I, :J, :L, :S, :Z, :T
:x -> binary_to_integer(value_raw)
:y -> binary_to_integer(value_raw)
:glass -> parse_glass(value_raw)
:next -> Enum.map String.codepoints(value_raw), &binary_to_atom/1
_ -> nil
end
{name_atom, value}
end
end
end
17 changes: 17 additions & 0 deletions tetris-servers/erlang/elixir_lib/player.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Player do
import Keyword, only: [get: 2]

def username do 'netoctone' end
def hostname do '192.168.0.100:8080' end

def step(msg) do
keys = Glass.parse(msg)
IO.puts """
x = #{get(keys, :x)}
y = #{get(keys, :y)}
figure = #{get(keys, :figure)}
"""
get(keys, :next)
'left=1'
end
end
33 changes: 33 additions & 0 deletions tetris-servers/erlang/elixir_lib/ws_handler.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-module(ws_handler).

-behaviour(websocket_client_handler).
-export([
start/0,
init/2,
websocket_info/3,
websocket_handle/3,
websocket_terminate/3
]).

-define(Host,'Elixir.Player':hostname()).
-define(User,'Elixir.Player':username()).

start() ->
websocket_client:start_link("ws://"++ ?Host ++ "/tetris-contest/ws?user=" ++ ?User, ?MODULE, []).

init([], _ConnState) ->
websocket_client:cast(self(), {text, <<"message 1">>}),
{ok, 2}.

websocket_handle({text, Msg}, _ConnState, State) ->
io:format("Received msg ~p~n", [Msg]),
Step = list_to_binary('Elixir.Player':step(Msg)),
{reply, {text, Step}, State}.

websocket_info(start, _ConnState, State) ->
{reply, {text, <<"erlang message received">>}, State}.

websocket_terminate(Reason, _ConnState, State) ->
io:format("Websocket closed in state ~p wih reason ~p~n",
[State, Reason]),
ok.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
username() ->
"anatoli".
hostname() ->
"localhost:8080".
"192.168.0.100:8080".

step(Msg) ->
Arr = utilities:process_msg(Msg),
Expand Down
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions tetris-servers/erlang/websocket_client/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# The MIT License

Copyright (C) 2012-2013 Jeremy Ong

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{application, websocket_client,
[
{description, "Erlang websocket client"},
{vsn, "0.5.4"},
{registered, []},
{applications, [
ssl,
crypto
]},
{env, []}
]
}.
Loading

0 comments on commit c0f969e

Please sign in to comment.