From 71fa6f30ba69d1502f57df24d80ba77849aa2604 Mon Sep 17 00:00:00 2001 From: Kallfaktorn Date: Fri, 4 Feb 2011 14:10:23 +0100 Subject: [PATCH] Moved the parts of network out of server. Partly added extensions to tic-tac-toe py. --- games/tic-tac-toe/data.py | 15 ++++ games/tic-tac-toe/data.pyc | Bin 0 -> 531 bytes games/tic-tac-toe/player.py | 15 ++++ games/tic-tac-toe/server.py | 23 +++++ games/tic-tac-toe/test_data.py | 14 +++ src/.ggs_connection.erl.swp | Bin 0 -> 12288 bytes src/ggs_network.beam | Bin 0 -> 2820 bytes src/ggs_network.erl | 160 +++++++++++++++++++++++++++++++++ src/ggs_server.beam | Bin 0 -> 616 bytes src/ggs_server.erl | 84 +++-------------- 10 files changed, 237 insertions(+), 74 deletions(-) create mode 100644 games/tic-tac-toe/data.py create mode 100644 games/tic-tac-toe/data.pyc create mode 100644 games/tic-tac-toe/player.py create mode 100644 games/tic-tac-toe/server.py create mode 100644 games/tic-tac-toe/test_data.py create mode 100644 src/.ggs_connection.erl.swp create mode 100644 src/ggs_network.beam create mode 100644 src/ggs_network.erl create mode 100644 src/ggs_server.beam diff --git a/games/tic-tac-toe/data.py b/games/tic-tac-toe/data.py new file mode 100644 index 0000000..b83c3ad --- /dev/null +++ b/games/tic-tac-toe/data.py @@ -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 diff --git a/games/tic-tac-toe/data.pyc b/games/tic-tac-toe/data.pyc new file mode 100644 index 0000000000000000000000000000000000000000..682dfaaa9031d0195431a988ddaa757e082356a8 GIT binary patch literal 531 zcmb_YO-sW-5S?tBR-`$2^X4@dyGK!^hziAn?V$%jkg_C88rn_L{eUXT%^&WYRM3M* zcQbF_%)FhvWI0JrucuGX8U0!j_#MI}fSl-)=oKWQRVq-)7?6bgqkOgrW3WbfuWc1rWO{7E5Zsj?I-K;YD_JO`o%CgP=@p{ZGFM(roA+>rIfd1@>-^g3eb7~=*@!y_ qP&7JjF{V*8i4rkEo`^-tlD{winC#iVIprU5YwA4g^sG0?{=NWR({FA7 literal 0 HcmV?d00001 diff --git a/games/tic-tac-toe/player.py b/games/tic-tac-toe/player.py new file mode 100644 index 0000000..081112c --- /dev/null +++ b/games/tic-tac-toe/player.py @@ -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])) + + diff --git a/games/tic-tac-toe/server.py b/games/tic-tac-toe/server.py new file mode 100644 index 0000000..91e32b5 --- /dev/null +++ b/games/tic-tac-toe/server.py @@ -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 diff --git a/games/tic-tac-toe/test_data.py b/games/tic-tac-toe/test_data.py new file mode 100644 index 0000000..38282f0 --- /dev/null +++ b/games/tic-tac-toe/test_data.py @@ -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() + + diff --git a/src/.ggs_connection.erl.swp b/src/.ggs_connection.erl.swp new file mode 100644 index 0000000000000000000000000000000000000000..0c009f8287377d2e3763a3fb7610231c3237d3fb GIT binary patch literal 12288 zcmeI2O>Z1E7{^^+M0qKoqMo?8TdkN?XJ3-EAceGwl!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network.erl @@ -0,0 +1,160 @@ +%%%---------------------------------------------------- +%%% @author Jonatan Pålsson +%%% @copyright 2010 Jonatan Pålsson +%%% @doc RPC over TCP server +%%% @end +%%%---------------------------------------------------- +%%% @author Mattias Pettersson +%%% @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. diff --git a/src/ggs_server.beam b/src/ggs_server.beam new file mode 100644 index 0000000000000000000000000000000000000000..8818249268afb82e14ee2ed7b316d9b5a9b4d5c5 GIT binary patch literal 616 zcmY+Cv2GJV5Qb;>&KGPGaXMbWiV_JLUqDDSDb83Sie#L~MwB?~^X7cEZ}+sjHZ+kU zAt4HiJVKiEl$2EIQ`7StnK^6`M*435`DZjc(%##X!3zL>_U5qjuH$q803VThBg^c_ zO0$xtW}PsuNrsZ{lQ#24<%|}&n%6VwMyXyZ=Ub=CW}zp`g&gJTTnExDgvy>!#{(On zt0xlgNRbes=p)-mz(OAF0BfPa_ECl`>4zaZ<kpXs{ zL|91J56nXLEAgI^(K_{D=*)YZ3&%xCXAcPw5AhL-jb>|I!*iplm*NppN66o3e)##n znOp1nV?0SyG?0fgXf_3T9HDu%2Wbl4(<$v)?bdP5pnmpcfnPS|6Yy46Rf!5g_o6?~ zFK+I>erw+T-2K32v+l7v$IkCnzfb*Jx^%f#c2*_$mKL)))Lyk;w_lRyyS$LoV2m68 z7F_sZThzoJRRXJ*CY5tIh^M-cF}|*nlQK8L$+$E+GomPRmBoKwIX*fX#@3|qzyGF< Lm04$O9qt78!%2Cj literal 0 HcmV?d00001 diff --git a/src/ggs_server.erl b/src/ggs_server.erl index f440cde..c3514e7 100644 --- a/src/ggs_server.erl +++ b/src/ggs_server.erl @@ -5,27 +5,19 @@ %%%---------------------------------------------------- -module(ggs_server). --behaviour(gen_server). % import --import(ggs_connection). +-import(ggs_network). %% API --export([start_link/1, - start_link/0, +-export([start/0, + start/1, get_count/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 %%%==================================================== @@ -34,12 +26,12 @@ %% @doc Starts the server %% @end %%----------------------------------------------------- -start_link(Port) -> - process_flag(trap_exit, true), - gen_server:start_link({local, ?SERVER}, ?MODULE, [Port], []). +start() -> + ggs_network:start_link(). + +start(Port) -> + ggs_network:start_link(Port). -start_link() -> - start_link(?DEFAULT_PORT). %%----------------------------------------------------- %% @doc Fetches the number of requests made to this server @@ -49,7 +41,7 @@ start_link() -> %% @end %%----------------------------------------------------- get_count() -> - gen_server:call(?SERVER, get_count). + ggs_network:get_count(get_count). %%----------------------------------------------------- %% @doc Stops the server. @@ -57,60 +49,4 @@ get_count() -> %% @end %%----------------------------------------------------- stop() -> - gen_server:cast(?SERVER, 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. + ggs_network:stop().