summaryrefslogtreecommitdiff |
diff options
Diffstat (limited to 'src/roster/struct')
-rw-r--r-- | src/roster/struct/rst_character.erl | 238 | ||||
-rw-r--r-- | src/roster/struct/rst_roster.erl | 127 |
2 files changed, 365 insertions, 0 deletions
diff --git a/src/roster/struct/rst_character.erl b/src/roster/struct/rst_character.erl new file mode 100644 index 0000000..e3663cc --- /dev/null +++ b/src/roster/struct/rst_character.erl @@ -0,0 +1,238 @@ +-module(rst_character). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-record +( + character, + { + name :: binary(), + portrait :: binary(), + weapon_ids :: {shr_weapon:id(), shr_weapon:id()}, + armor_id :: shr_armor:id(), + glyph_ids :: array:array(shr_glyph:id()), + glyph_board_id :: shr_glyph_board:id() + } +). + +-opaque type() :: #character{}. + +-export_type([type/0]). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% Accessors +-export +( + [ + get_name/1, + get_portrait/1, + get_weapon_ids/1, + get_armor_id/1, + get_glyph_ids/1, + get_glyph_board_id/1, + + set_name/2, + set_portrait/2, + set_weapon_ids/2, + set_armor_id/2, + set_glyph_ids/2, + set_glyph_board_id/2, + + get_name_field/0, + get_portrait_field/0, + get_weapon_ids_field/0, + get_armor_id_field/0, + get_glyph_ids_field/0, + get_glyph_board_id_field/0 + ] +). + +-export +( + [ + decode/1, + random/0 + ] +). + +-export +( + [ + validate/2 + ] +). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec validate_name (binary()) -> ok. +validate_name (_Name) -> + % TODO [SECURITY][LOW]: unimplemented + ok. + +-spec validate_portrait (shr_inventory:type(), binary()) -> ok. +validate_portrait (_Inventory, _Portrait) -> + % TODO [SECURITY][LOW]: unimplemented + ok. + +-spec validate_weapons + ( + shr_inventory:type(), + {shr_weapon:id(), shr_weapon:id()} + ) + -> ok. +validate_weapons (_Inventory, {_ActiveWeapon, _SecondaryWeapon}) -> + % TODO [SECURITY][LOW]: unimplemented + ok. + +-spec validate_armor (shr_inventory:type(), shr_armor:id()) -> ok. +validate_armor (_Inventory, _Armor) -> + % TODO [SECURITY][LOW]: unimplemented + ok. + +-spec validate_glyphs (shr_inventory:type(), array:array(shr_glyph:id())) -> ok. +validate_glyphs (_Inventory, _Glyphs) -> + % TODO [SECURITY][LOW]: unimplemented + ok. + +-spec validate_glyph_board (shr_inventory:type(), shr_glyph_board:id()) -> ok. +validate_glyph_board (_Inventory, _GlyphBoard) -> + % TODO [SECURITY][LOW]: unimplemented + ok. + +-spec validate_glyphs_on_board + ( + array:array(shr_glyph:id()), + shr_glyph_board:id() + ) + -> ok. +validate_glyphs_on_board (_Glyphs, _GlyphBoard) -> + % TODO [SECURITY][LOW]: unimplemented + ok. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% Accessors +-spec get_name (type()) -> binary(). +get_name (Char) -> Char#character.name. + +-spec get_portrait (type()) -> binary(). +get_portrait (Char) -> Char#character.portrait. + +-spec get_weapon_ids (type()) -> {shr_weapon:id(), shr_weapon:id()}. +get_weapon_ids (Char) -> Char#character.weapon_ids. + +-spec get_armor_id (type()) -> shr_armor:id(). +get_armor_id (Char) -> Char#character.armor_id. + +-spec get_glyph_ids (type()) -> array:array(shr_glyph:id()). +get_glyph_ids (Char) -> Char#character.glyph_ids. + +-spec get_glyph_board_id (type()) -> shr_glyph_board:id(). +get_glyph_board_id (Char) -> Char#character.glyph_board_id. + + +-spec set_name (binary(), type()) -> type(). +set_name (Name, Char) -> + Char#character + { + name = Name + }. + +-spec set_portrait (binary(), type()) -> type(). +set_portrait (PortraitID, Char) -> + Char#character + { + portrait = PortraitID + }. + +-spec set_armor_id (shr_armor:id(), type()) -> type(). +set_armor_id (ArmorID, Char) -> + Char#character + { + armor_id = ArmorID + }. + +-spec set_weapon_ids ({shr_weapon:id(), shr_weapon:id()}, type()) -> type(). +set_weapon_ids (WeaponIDs, Char) -> + Char#character + { + weapon_ids = WeaponIDs + }. + +-spec set_glyph_ids (array:array(shr_glyph:id()), type()) -> type(). +set_glyph_ids (GlyphIDs, Char) -> + Char#character + { + glyph_ids = GlyphIDs + }. + +-spec set_glyph_board_id (shr_glyph_board:id(), type()) -> type(). +set_glyph_board_id (GlyphBoardID, Char) -> + Char#character + { + glyph_board_id = GlyphBoardID + }. + +-spec random () -> type(). +random () -> + #character + { + name = <<"">>, + portrait = <<"0">>, + weapon_ids = {0, 0}, + armor_id = 0, + glyph_ids = array:new(), + glyph_board_id = <<"0">> + }. + +-spec get_name_field () -> non_neg_integer(). +get_name_field () -> #character.name. +-spec get_portrait_field () -> non_neg_integer(). +get_portrait_field () -> #character.portrait. +-spec get_armor_id_field () -> non_neg_integer(). +get_armor_id_field () -> #character.armor_id. +-spec get_weapon_ids_field () -> non_neg_integer(). +get_weapon_ids_field () -> #character.weapon_ids. +-spec get_glyph_ids_field () -> non_neg_integer(). +get_glyph_ids_field () -> #character.glyph_ids. +-spec get_glyph_board_id_field () -> non_neg_integer(). +get_glyph_board_id_field () -> #character.glyph_board_id. + +-spec decode (map()) -> type(). +decode (JSONReqMap) -> + Name = maps:get(<<"nam">>, JSONReqMap), + Portrait = maps:get(<<"prt">>, JSONReqMap), + ActiveWeapon = maps:get(<<"awp">>, JSONReqMap), + SecondaryWeapon = maps:get(<<"swp">>, JSONReqMap), + Armor = maps:get(<<"ar">>, JSONReqMap), + GlyphsList = maps:get(<<"gls">>, JSONReqMap), + GlyphBoard = maps:get(<<"gb">>, JSONReqMap), + + #character + { + name = Name, + portrait = Portrait, + weapon_ids = {ActiveWeapon, SecondaryWeapon}, + armor_id = Armor, + glyph_ids = array:from_list(GlyphsList), + glyph_board_id = GlyphBoard + }. + +-spec validate (shr_inventory:type(), type()) -> ok. +validate (Inventory, Character) -> + Glyphs = Character#character.glyph_ids, + GlyphBoard = Character#character.glyph_board_id, + + validate_name(Character#character.name), + validate_portrait(Inventory, Character#character.portrait), + validate_weapons(Inventory, Character#character.weapon_ids), + validate_armor(Inventory, Character#character.armor_id), + validate_glyphs(Inventory, Glyphs), + validate_glyph_board(Inventory, GlyphBoard), + validate_glyphs_on_board(Glyphs, GlyphBoard), + + ok. diff --git a/src/roster/struct/rst_roster.erl b/src/roster/struct/rst_roster.erl new file mode 100644 index 0000000..a362619 --- /dev/null +++ b/src/roster/struct/rst_roster.erl @@ -0,0 +1,127 @@ +-module(rst_roster). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-type id() :: binary(). + +-record +( + roster, + { + id :: id(), + owner :: binary(), + characters :: array:array(rst_character:type()) + } +). + +-opaque type() :: #roster{}. + +-export_type([type/0, id/0]). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% Accessors +-export +( + [ + get_id/1, + 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/2 + ] +). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% Accessors +-spec get_id (type()) -> id(). +get_id (Roster) -> Roster#roster.id. + +-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(), binary()) -> type(). +new (ID, Owner) -> + #roster + { + id = ID, + owner = Owner, + characters = array:new() + }. |