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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
-module(rst_roster).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-type id() :: binary().
-record
(
roster,
{
owner :: binary(),
characters :: array:array(rst_character:type())
}
).
-opaque type() :: #roster{}.
-export_type([type/0, id/0]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Accessors
-export
(
[
get_owner/1,
get_characters/1,
get_character/2,
set_characters/2,
set_character/3,
add_character/2,
remove_character/2
]
).
-export
(
[
get_characters_field/0
]
).
-export
(
[
new/1
]
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Accessors
-spec get_owner (type()) -> binary().
get_owner (Roster) -> Roster#roster.owner.
-spec get_characters (type()) -> array:array(rst_character:type()).
get_characters (Roster) -> Roster#roster.characters.
-spec get_character (non_neg_integer(), type()) -> rst_character:type().
get_character (IX, Roster) -> array:get(IX, Roster#roster.characters).
-spec set_characters (array:array(rst_character:type()), type()) -> type().
set_characters (Characters, Roster) -> Roster#roster{ characters = Characters }.
-spec set_character
(
non_neg_integer(),
rst_character:type(),
type()
)
-> type().
set_character (IX, Character, Roster) ->
Roster#roster
{
characters = array:set(IX, Character, Roster#roster.characters)
}.
-spec add_character (rst_character:type(), type()) -> type().
add_character (Character, Roster) ->
CurrentCharacters = Roster#roster.characters,
CurrentSize = array:size(CurrentCharacters),
Roster#roster
{
characters = array:set(CurrentSize, Character, CurrentCharacters)
}.
-spec remove_character (non_neg_integer(), type()) -> type().
remove_character (IX, Roster) ->
CurrentCharacters = Roster#roster.characters,
CurrentSize = array:size(CurrentCharacters),
NewSize = (CurrentSize - 1),
LastCharacter = array:get(NewSize, CurrentCharacters),
S0Characters = array:set(IX, LastCharacter, CurrentCharacters),
S1Characters = array:resize(NewSize, S0Characters),
Roster#roster
{
characters = S1Characters
}.
-spec get_characters_field () -> non_neg_integer().
get_characters_field () -> #roster.characters.
-spec new (binary()) -> type().
new (Owner) ->
NewChar = rst_character:new(),
#roster
{
owner = Owner,
characters =
array:from_list
(
[
NewChar,
NewChar,
NewChar,
NewChar,
NewChar,
NewChar,
NewChar,
NewChar
]
)
}.
|