-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.erl
100 lines (88 loc) · 2.87 KB
/
commands.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
-compile(export_all).
-include("game.erl").
-include_lib("stdlib/include/qlc.hrl").
%% CON
%% Hace un llamado a la base de datos para saber si ya existe
%% un usuario con su mismo nombre, en caso contrario lo registra.
cmd_con(UName, PSocket) ->
case user_exists(UName) of
false -> case psock_get_user(PSocket) of
[] -> user_add(UName, PSocket),
"valid " ++ UName;
_ -> "error"
end;
_ -> "error"
end.
%% LSG
%% Devuelve todas las partidas listadas en la base de datos
%% de la forma "GameId Usuario1 Usuario2"
cmd_lsg() ->
GamesList = game_list_all(),
lists:map(fun({ _, X, Y, Z, _, _}) -> erlang:integer_to_list(X) ++ " "
++ Y ++ " "
++ Z end, GamesList).
%% NEW
%% Dado un usuario, crea una partida a su nombre con un id
%% unico. Deja al jugador2 vacio, en espera a que se una.
cmd_new(UName) ->
F = fun() ->
Handle = qlc:q([P#game.gameid || P <- mnesia:table(game)]),
qlc:e(Handle)
end,
Ids = mnesia:activity(transaction, F),
case Ids of
[] ->
game_create(1, UName),
"1";
_ ->
ID = lists:max(Ids) + 1,
game_create(ID, UName),
integer_to_list(ID)
end.
%% ACC
%% Dados un juego y un usuario, intenta unirse en la posicion
%% del jugador2 si esta esta vacia.
cmd_acc(GameID, UName) ->
GID = element(1, string:to_integer(GameID)),
if
not(is_integer(GID)) -> "error_notint";
true ->
case game_fill_room(GID, UName) of
valid -> "success " ++ GameID;
error -> "error_gfr"
end
end.
%% PLA
%%
cmd_pla(GameID, UName, Play) ->
T = game_get_table(GameID),
case T of
nil -> io:format("Error en cmd_pla~n~n", []),
"error";
_ -> MakePlay = table_make_play(GameID, Play),
case MakePlay of
game_over -> "error"; %%FIXME
error -> "error";
NewTable -> SetTable = game_set_table(GameID, NewTable, UName),
if
SetTable -> "success " ++ integer_to_list(GameID) ++ game_status(GameID, UName);
true -> "error"
end
end
end.
%% OBS
%% Dado un juego y un usuario, agrega el usuario a
%% la lista de observadores del juego.
cmd_obs(GameID, UName) ->
game_add_observer(GameID, UName).
%% LEA
%%
cmd_lea(GameID, UName) ->
game_delete_observer(GameID, UName).
%% BYE
%%
cmd_bye(UName) ->
List = user_get_opponents_psock(UName) ++ user_get_observers_psock(UName),
user_delete_all(UName),
user_delete(UName),
List.