Permalink
Browse files

Avoid github dependency + add elixir syntax sweet

  • Loading branch information...
1 parent 05a50b5 commit fc681ba638a3f7a4117a54a08095d67d6fea7e39 @netoctone committed Jan 25, 2014
@@ -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.
@@ -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
@@ -0,0 +1,17 @@
+defmodule Player do
+ import Keyword, only: [get: 2]
+
+ def username do 'anatoli' 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
@@ -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.
@@ -8,7 +8,7 @@
username() ->
"anatoli".
hostname() ->
- "localhost:8080".
+ "192.168.0.100:8080".
step(Msg) ->
Arr = utilities:process_msg(Msg),
Submodule websocket_client added at e2a9ea

0 comments on commit fc681ba

Please sign in to comment.