Moved the parts of network out of server.
Partly added extensions to tic-tac-toe py.
This commit is contained in:
parent
f59d59814a
commit
71fa6f30ba
10 changed files with 237 additions and 74 deletions
15
games/tic-tac-toe/data.py
Normal file
15
games/tic-tac-toe/data.py
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
def greatest_sequence(match, pattern):
|
||||||
|
m = match
|
||||||
|
p = pattern
|
||||||
|
size = 0
|
||||||
|
max_size = 0
|
||||||
|
|
||||||
|
for p in pattern:
|
||||||
|
if m == p:
|
||||||
|
size += 1
|
||||||
|
else:
|
||||||
|
if size > max_size:
|
||||||
|
max_size = size
|
||||||
|
size = 0
|
||||||
|
|
||||||
|
return max_size
|
BIN
games/tic-tac-toe/data.pyc
Normal file
BIN
games/tic-tac-toe/data.pyc
Normal file
Binary file not shown.
15
games/tic-tac-toe/player.py
Normal file
15
games/tic-tac-toe/player.py
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
from pygame.mouse import get_pos
|
||||||
|
from point import Point
|
||||||
|
|
||||||
|
class Player(object):
|
||||||
|
def __init__(self, id, shape, board):
|
||||||
|
self.shape = shape
|
||||||
|
self.board = board
|
||||||
|
self.id = id
|
||||||
|
|
||||||
|
|
||||||
|
def turn(self):
|
||||||
|
#Ask mouse for position
|
||||||
|
board.make_turn(Point(get_pos()[0],get_pos()[1]))
|
||||||
|
|
||||||
|
|
23
games/tic-tac-toe/server.py
Normal file
23
games/tic-tac-toe/server.py
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#server.py
|
||||||
|
import json
|
||||||
|
from socket import socket, AF_INET, SOCK_STREAM
|
||||||
|
|
||||||
|
|
||||||
|
class server(object):
|
||||||
|
def __init__(self, port=None):
|
||||||
|
self.port = port
|
||||||
|
self.world = GGS.init()
|
||||||
|
self.socket = socket(AF_INET, SOCK_STREAM)
|
||||||
|
self.socket.connect(("www.???.com", 80))
|
||||||
|
|
||||||
|
def turn(self, id, index):
|
||||||
|
rows = sqrt(board.nr_of_rectangles)
|
||||||
|
x = int(index / rows)
|
||||||
|
y = int(index % rows)
|
||||||
|
|
||||||
|
json.dumps({"x": x, "y": y}
|
||||||
|
world.callCommand("tictactoe", "set", json.dumps({"x": x, "y": y}))
|
||||||
|
|
||||||
|
sent = 0
|
||||||
|
length = len(
|
||||||
|
while sent
|
14
games/tic-tac-toe/test_data.py
Normal file
14
games/tic-tac-toe/test_data.py
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import unittest
|
||||||
|
import data
|
||||||
|
|
||||||
|
|
||||||
|
class TestData(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
array = [0,1,1,1,3,4,5,2,2,3,3,3,3,3,33,4,2,2]
|
||||||
|
|
||||||
|
self.assertTrue(data.greatest_sequence(array, 3) == 5)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
|
|
||||||
|
|
BIN
src/.ggs_connection.erl.swp
Normal file
BIN
src/.ggs_connection.erl.swp
Normal file
Binary file not shown.
BIN
src/ggs_network.beam
Normal file
BIN
src/ggs_network.beam
Normal file
Binary file not shown.
160
src/ggs_network.erl
Normal file
160
src/ggs_network.erl
Normal file
|
@ -0,0 +1,160 @@
|
||||||
|
%%%----------------------------------------------------
|
||||||
|
%%% @author Jonatan Pålsson <Jonatan.p@gmail.com>
|
||||||
|
%%% @copyright 2010 Jonatan Pålsson
|
||||||
|
%%% @doc RPC over TCP server
|
||||||
|
%%% @end
|
||||||
|
%%%----------------------------------------------------
|
||||||
|
%%% @author Mattias Pettersson <mattiaspgames@gmail.com>
|
||||||
|
%%% @doc Socket module for GGS
|
||||||
|
%%% @end
|
||||||
|
%%%----------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
-module(ggs_network).
|
||||||
|
|
||||||
|
-behaviour(gen_server).
|
||||||
|
|
||||||
|
%define
|
||||||
|
-define(SERVER, ?MODULE).
|
||||||
|
-define(DEFAULT_PORT, 1055).
|
||||||
|
|
||||||
|
|
||||||
|
% export
|
||||||
|
-export([start_link/0,start_link/1]).
|
||||||
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2]).
|
||||||
|
-export([get_count/1, send/3, send/4]).
|
||||||
|
-export([stop/0]).
|
||||||
|
|
||||||
|
%% gen_server callbacks
|
||||||
|
-export([terminate/2, code_change/3]).
|
||||||
|
|
||||||
|
%state
|
||||||
|
-record(state, {port, lsock, client_vm_map = []}).
|
||||||
|
|
||||||
|
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
%% @doc Starts gen_server
|
||||||
|
%% @end
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
start_link() ->
|
||||||
|
start_link(?DEFAULT_PORT).
|
||||||
|
|
||||||
|
start_link(Port) ->
|
||||||
|
process_flag(trap_exit, true),
|
||||||
|
gen_server:start_link({local, ?SERVER}, ?MODULE, [Port], []).
|
||||||
|
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
%% Creation
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
init([Port]) ->
|
||||||
|
{ok, LSock} = gen_tcp:listen(Port, [{active, true},
|
||||||
|
{reuseaddr, true}]),
|
||||||
|
{ok, #state{port = Port, lsock = LSock}, 0}.
|
||||||
|
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
%% @doc Fetches the number of requests made to this server
|
||||||
|
%% @spec get_count() -> {ok, Count}
|
||||||
|
%% where
|
||||||
|
%% Count = integer()
|
||||||
|
%% @end
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
get_count(get_count) ->
|
||||||
|
gen_server:call(?SERVER, get_count).
|
||||||
|
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
%% @doc Stops the server.
|
||||||
|
%% @spec stop() -> ok
|
||||||
|
%% @end
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
stop() ->
|
||||||
|
gen_server:cast(?SERVER, stop).
|
||||||
|
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
%% Handlers
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
handle_call(get_count, _From, State) ->
|
||||||
|
{reply, {ok, State#state.client_vm_map}, State}.
|
||||||
|
|
||||||
|
handle_cast(stop, State) ->
|
||||||
|
{stop, normal, State}.
|
||||||
|
|
||||||
|
handle_info({tcp, Socket, RawData}, State) -> %parameters coming from gen_server
|
||||||
|
NewState = do_JSCall(Socket, RawData, State), %TODO
|
||||||
|
OldMap = State#state.client_vm_map,
|
||||||
|
io:format("Old map: ~p NewState: ~p~n", [OldMap, NewState]),
|
||||||
|
{noreply, State#state{client_vm_map = OldMap ++ [NewState]}};
|
||||||
|
|
||||||
|
handle_info(timeout, #state{lsock = LSock} = State) ->
|
||||||
|
{ok, _Sock} = gen_tcp:accept(LSock),
|
||||||
|
{noreply, State}.
|
||||||
|
|
||||||
|
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
%% TCP Calls
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
send(Socket, RefID, String) ->
|
||||||
|
gen_tcp:send(Socket, io_lib:fwrite("~p ~p~n", [RefID,String])).
|
||||||
|
|
||||||
|
send(Socket, RefID, String1, String2) ->
|
||||||
|
gen_tcp:send(Socket, io_lib:fwrite("~p ~p ~p~n", [RefID, String1, String2])).
|
||||||
|
|
||||||
|
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
%% gen_server callbacks
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
terminate(_Reason, _State) ->
|
||||||
|
ok.
|
||||||
|
|
||||||
|
code_change(_OldVsn, State, _Extra) ->
|
||||||
|
{ok, State}.
|
||||||
|
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
%% Internal functions
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
do_JSCall(Socket, Data, State) ->
|
||||||
|
JSVM = js_runner:boot(),
|
||||||
|
js_runner:define(JSVM, "function userCommand(cmd, par) {return cmd+' '+ par}"),
|
||||||
|
Parsed = ggs_protocol:parse(Data),
|
||||||
|
NewState = case Parsed of
|
||||||
|
{cmd, Command, Parameter} ->
|
||||||
|
% Set the new state to []
|
||||||
|
Ret = js_runner:call(JSVM, "userCommand",
|
||||||
|
[list_to_binary(Command),
|
||||||
|
list_to_binary(Parameter)]),
|
||||||
|
connection:send(Socket, "RefID", "JS says: ", Ret),
|
||||||
|
[];
|
||||||
|
% Set the new state to the reference generated, and JSVM associated
|
||||||
|
{hello} ->
|
||||||
|
Client = getRef(),
|
||||||
|
connection:send(Socket, Client, "__ok_hello"),
|
||||||
|
{Client, JSVM};
|
||||||
|
{echo, RefID, _, MSG} ->
|
||||||
|
connection:send(Socket, RefID, "Your VM is ", getJSVM(RefID, State)),
|
||||||
|
[];
|
||||||
|
{crash, Zero} ->
|
||||||
|
10/Zero;
|
||||||
|
{vms} ->
|
||||||
|
connection:send(Socket, "RefID", State);
|
||||||
|
% Set the new state to []
|
||||||
|
Other ->
|
||||||
|
ggs_connection:send(Socket, "RefID", "__error"),
|
||||||
|
[]
|
||||||
|
end,
|
||||||
|
% Return the new state
|
||||||
|
NewState.
|
||||||
|
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
%% Helpers
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
getRef() ->
|
||||||
|
{A1,A2,A3} = now(),
|
||||||
|
random:seed(A1, A2, A3),
|
||||||
|
random:uniform(1000).
|
||||||
|
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
%% Helpers
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
getJSVM(RefID, State) ->
|
||||||
|
VMs = State#state.client_vm_map,
|
||||||
|
{value, {_,VM}} = lists:keysearch(RefID, 1, VMs),
|
||||||
|
VM.
|
BIN
src/ggs_server.beam
Normal file
BIN
src/ggs_server.beam
Normal file
Binary file not shown.
|
@ -5,27 +5,19 @@
|
||||||
%%%----------------------------------------------------
|
%%%----------------------------------------------------
|
||||||
|
|
||||||
-module(ggs_server).
|
-module(ggs_server).
|
||||||
-behaviour(gen_server).
|
|
||||||
|
|
||||||
% import
|
% import
|
||||||
-import(ggs_connection).
|
-import(ggs_network).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([start_link/1,
|
-export([start/0,
|
||||||
start_link/0,
|
start/1,
|
||||||
get_count/0,
|
get_count/0,
|
||||||
stop/0
|
stop/0
|
||||||
]).
|
]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
|
||||||
-export([terminate/2, code_change/3]).
|
|
||||||
|
|
||||||
|
|
||||||
-define(SERVER, ?MODULE).
|
|
||||||
-define(DEFAULT_PORT, 1055).
|
|
||||||
|
|
||||||
-record(state, {port, lsock, client_vm_map = []}).
|
|
||||||
|
|
||||||
%%%====================================================
|
%%%====================================================
|
||||||
%%% API
|
%%% API
|
||||||
%%%====================================================
|
%%%====================================================
|
||||||
|
@ -34,12 +26,12 @@
|
||||||
%% @doc Starts the server
|
%% @doc Starts the server
|
||||||
%% @end
|
%% @end
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
start_link(Port) ->
|
start() ->
|
||||||
process_flag(trap_exit, true),
|
ggs_network:start_link().
|
||||||
gen_server:start_link({local, ?SERVER}, ?MODULE, [Port], []).
|
|
||||||
|
start(Port) ->
|
||||||
|
ggs_network:start_link(Port).
|
||||||
|
|
||||||
start_link() ->
|
|
||||||
start_link(?DEFAULT_PORT).
|
|
||||||
|
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
%% @doc Fetches the number of requests made to this server
|
%% @doc Fetches the number of requests made to this server
|
||||||
|
@ -49,7 +41,7 @@ start_link() ->
|
||||||
%% @end
|
%% @end
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
get_count() ->
|
get_count() ->
|
||||||
gen_server:call(?SERVER, get_count).
|
ggs_network:get_count(get_count).
|
||||||
|
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
%% @doc Stops the server.
|
%% @doc Stops the server.
|
||||||
|
@ -57,60 +49,4 @@ get_count() ->
|
||||||
%% @end
|
%% @end
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
stop() ->
|
stop() ->
|
||||||
gen_server:cast(?SERVER, stop).
|
ggs_network:stop().
|
||||||
|
|
||||||
%%-----------------------------------------------------
|
|
||||||
%% gen_server callbacks
|
|
||||||
%%-----------------------------------------------------
|
|
||||||
terminate(_Reason, _State) ->
|
|
||||||
ok.
|
|
||||||
|
|
||||||
code_change(_OldVsn, State, _Extra) ->
|
|
||||||
{ok, State}.
|
|
||||||
|
|
||||||
%%-----------------------------------------------------
|
|
||||||
%% Internal functions
|
|
||||||
%%-----------------------------------------------------
|
|
||||||
do_JSCall(Socket, Data, State) ->
|
|
||||||
JSVM = js_runner:boot(),
|
|
||||||
js_runner:define(JSVM, "function userCommand(cmd, par) {return cmd+' '+ par}"),
|
|
||||||
Parsed = ggs_protocol:parse(Data),
|
|
||||||
NewState = case Parsed of
|
|
||||||
{cmd, Command, Parameter} ->
|
|
||||||
% Set the new state to []
|
|
||||||
Ret = js_runner:call(JSVM, "userCommand",
|
|
||||||
[list_to_binary(Command),
|
|
||||||
list_to_binary(Parameter)]),
|
|
||||||
connection:send(Socket, "RefID", "JS says: ", Ret),
|
|
||||||
[];
|
|
||||||
% Set the new state to the reference generated, and JSVM associated
|
|
||||||
{hello} ->
|
|
||||||
Client = getRef(),
|
|
||||||
connection:send(Socket, Client, "__ok_hello"),
|
|
||||||
{Client, JSVM};
|
|
||||||
{echo, RefID, _, MSG} ->
|
|
||||||
connection:send(Socket, RefID, "Your VM is ", getJSVM(RefID, State)),
|
|
||||||
[];
|
|
||||||
{crash, Zero} ->
|
|
||||||
10/Zero;
|
|
||||||
{vms} ->
|
|
||||||
connection:send(Socket, "RefID", State);
|
|
||||||
% Set the new state to []
|
|
||||||
Other ->
|
|
||||||
ggs_connection:send(Socket, "RefID", "__error"),
|
|
||||||
[]
|
|
||||||
end,
|
|
||||||
% Return the new state
|
|
||||||
NewState.
|
|
||||||
%%-----------------------------------------------------
|
|
||||||
%% Helpers
|
|
||||||
%%-----------------------------------------------------
|
|
||||||
getRef() ->
|
|
||||||
{A1,A2,A3} = now(),
|
|
||||||
random:seed(A1, A2, A3),
|
|
||||||
random:uniform(1000).
|
|
||||||
|
|
||||||
getJSVM(RefID, State) ->
|
|
||||||
VMs = State#state.client_vm_map,
|
|
||||||
{value, {_,VM}} = lists:keysearch(RefID, 1, VMs),
|
|
||||||
VM.
|
|
||||||
|
|
Reference in a new issue