forked from anatoliliotych/tetris-servers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid github dependency + add elixir syntax sweet
- Loading branch information
Showing
13 changed files
with
849 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
12 changes: 12 additions & 0 deletions
12
tetris-servers/erlang/websocket_client/src/websocket_client.app.src
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, []} | ||
] | ||
}. |
Oops, something went wrong.