@@ -15,46 +15,91 @@ defmodule Codebattle.Tournament.Storage.Ranking do
1515 def put_ranking ( tournament , elements ) do
1616 elements
1717 |> Enum . map ( & { & 1 . place , & 1 . id , & 1 } )
18- |> then ( & :ets . insert ( tournament . ranking_table , & 1 ) )
18+ |> then ( fn records ->
19+ try do
20+ :ets . insert ( tournament . ranking_table , records )
21+ rescue
22+ _e ->
23+ IO . inspect ( records , label: "Error inserting ranking records" )
24+ [ ]
25+ end
26+ end )
1927 end
2028
2129 def put_single_record ( tournament , place , record ) do
22- :ets . insert ( tournament . ranking_table , { place , record . id , record } )
30+ try do
31+ :ets . insert ( tournament . ranking_table , { place , record . id , record } )
32+ rescue
33+ _e ->
34+ IO . inspect ( { place , record . id , record } , label: "Error inserting single ranking record" )
35+ end
2336 end
2437
2538 def drop_player ( tournament , player_id ) do
2639 match_spec = [ { { :"$1" , player_id , :"$2" } , [ ] , [ true ] } ]
2740
28- :ets . select_delete ( tournament . ranking_table , match_spec )
41+ try do
42+ :ets . select_delete ( tournament . ranking_table , match_spec )
43+ rescue
44+ _e ->
45+ IO . inspect ( { player_id , tournament } , label: "Error dropping player from ranking" )
46+ end
2947 end
3048
3149 def get_first ( tournament , limit ) do
32- :ets . select ( tournament . ranking_table , [
33- { { :"$1" , :_ , :"$3" } , [ { :>= , :"$1" , 1 } , { :"=<" , :"$1" , limit } ] , [ :"$3" ] }
34- ] )
50+ try do
51+ :ets . select ( tournament . ranking_table , [
52+ { { :"$1" , :_ , :"$3" } , [ { :>= , :"$1" , 1 } , { :"=<" , :"$1" , limit } ] , [ :"$3" ] }
53+ ] )
54+ rescue
55+ _e ->
56+ IO . inspect ( { tournament , limit } , label: "Error getting first ranking records" )
57+ [ ]
58+ end
3559 end
3660
3761 def get_all ( tournament ) do
38- :ets . select ( tournament . ranking_table , [ { { :_ , :_ , :"$3" } , [ ] , [ :"$3" ] } ] )
62+ try do
63+ :ets . select ( tournament . ranking_table , [ { { :_ , :_ , :"$3" } , [ ] , [ :"$3" ] } ] )
64+ rescue
65+ _e ->
66+ IO . inspect ( tournament , label: "Error getting all ranking records" )
67+ [ ]
68+ end
3969 end
4070
4171 def get_by_id ( tournament , id ) do
42- case :ets . select ( tournament . ranking_table , [ { { :_ , :"$2" , :"$3" } , [ { :== , :"$2" , id } ] , [ :"$3" ] } ] ) do
43- [ ranking_entity ] -> ranking_entity
44- [ ] -> nil
72+ try do
73+ case :ets . select ( tournament . ranking_table , [ { { :_ , :"$2" , :"$3" } , [ { :== , :"$2" , id } ] , [ :"$3" ] } ] ) do
74+ [ ranking_entity ] -> ranking_entity
75+ [ ] -> nil
76+ end
77+ rescue
78+ _e ->
79+ IO . inspect ( { tournament , id } , label: "Error getting ranking record by id" )
80+ nil
4581 end
46- rescue
47- _e ->
48- nil
4982 end
5083
5184 def count ( tournament ) do
52- :ets . select_count ( tournament . ranking_table , [ { :_ , [ ] , [ true ] } ] )
85+ try do
86+ :ets . select_count ( tournament . ranking_table , [ { :_ , [ ] , [ true ] } ] )
87+ rescue
88+ _e ->
89+ IO . inspect ( tournament , label: "Error counting ranking records" )
90+ 0
91+ end
5392 end
5493
5594 def get_slice ( tournament , start_place , end_place ) do
56- :ets . select ( tournament . ranking_table , [
57- { { :"$1" , :_ , :"$3" } , [ { :>= , :"$1" , start_place } , { :"=<" , :"$1" , end_place } ] , [ :"$3" ] }
58- ] )
95+ try do
96+ :ets . select ( tournament . ranking_table , [
97+ { { :"$1" , :_ , :"$3" } , [ { :>= , :"$1" , start_place } , { :"=<" , :"$1" , end_place } ] , [ :"$3" ] }
98+ ] )
99+ rescue
100+ _e ->
101+ IO . inspect ( { tournament , start_place , end_place } , label: "Error getting slice of ranking records" )
102+ [ ]
103+ end
59104 end
60105end
0 commit comments