-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
378 additions
and
239 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
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
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
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
51 changes: 51 additions & 0 deletions
51
services/app/apps/codebattle/lib/codebattle/tournament/matches.ex
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,51 @@ | ||
defmodule Codebattle.Tournament.Matches do | ||
def create_table do | ||
:ets.new(:t_matches, [:set, :public, {:write_concurrency, true}, {:read_concurrency, true}]) | ||
end | ||
|
||
def put_match(tournament, match) do | ||
:ets.insert(tournament.matches_table, {match.id, match.state, match}) | ||
end | ||
|
||
def get_match(tournament, match_id) do | ||
:ets.lookup_element(tournament.matches_table, match_id, 3) | ||
rescue | ||
_e -> | ||
nil | ||
end | ||
|
||
def get_matches(tournament) do | ||
:ets.select(tournament.matches_table, [{{:"$1", :"$2", :"$3"}, [], [:"$3"]}]) | ||
end | ||
|
||
def get_matches(tournament, matches_ids) when is_list(matches_ids) do | ||
Enum.map(matches_ids, fn match_id -> | ||
get_match(tournament, match_id) | ||
end) | ||
end | ||
|
||
def get_matches(tournament, state) when is_binary(state) do | ||
:ets.select(tournament.matches_table, [{{:"$1", state, :"$3"}, [], [:"$3"]}]) | ||
end | ||
|
||
def count(tournament) do | ||
:ets.select_count(tournament.matches_table, [{:_, [], [true]}]) | ||
end | ||
end | ||
|
||
alias Codebattle.Tournament.Matches, as: M | ||
table = M.create_table() | ||
t = %{matches_table: table} | ||
|
||
M.put_match(t, %{id: 1, game_id: 1, state: "canceled", player_ids: [1, 2]}) | ||
M.put_match(t, %{id: 2, game_id: 2, state: "canceled", player_ids: [3, 4]}) | ||
M.put_match(t, %{id: 3, game_id: 3, state: "game_over", player_ids: [1, 2]}) | ||
M.put_match(t, %{id: 4, game_id: 4, state: "timeout", player_ids: [3, 4]}) | ||
M.put_match(t, %{id: 5, game_id: 5, state: "playing", player_ids: [1, 2]}) | ||
M.put_match(t, %{id: 6, game_id: 6, state: "playing", player_ids: [3, 4]}) | ||
|
||
M.get_match(t, 1) | ||
M.get_matches(t) | ||
M.get_matches(t, [2, 3]) | ||
M.get_matches(t, "playing") | ||
M.get_matches(t, "canceled") |
30 changes: 30 additions & 0 deletions
30
services/app/apps/codebattle/lib/codebattle/tournament/players.ex
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,30 @@ | ||
defmodule Codebattle.Tournament.Players do | ||
def create_table do | ||
:ets.new(:t_players, [:set, :public, {:write_concurrency, true}, {:read_concurrency, true}]) | ||
end | ||
|
||
def count(tournament) do | ||
:ets.select_count(tournament.players_table, [{:_, [], [true]}]) | ||
end | ||
|
||
def put_player(tournament, player) do | ||
:ets.insert(tournament.players_table, {player.id, player}) | ||
end | ||
|
||
def get_player(tournament, player_id) do | ||
:ets.lookup_element(tournament.players_table, player_id, 2) | ||
rescue | ||
_e -> | ||
nil | ||
end | ||
|
||
def get_players(tournament) do | ||
:ets.select(tournament.players_table, [{{:"$1", :"$2"}, [], [:"$2"]}]) | ||
end | ||
|
||
def get_players(tournament, player_ids) do | ||
Enum.map(player_ids, fn player_id -> | ||
get_player(tournament, player_id) | ||
end) | ||
end | ||
end |
Oops, something went wrong.