summaryrefslogtreecommitdiff
blob: 8612042c026370beb20a5cfbdf0d11a948c12862 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
-module(bnt_generate_player).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-export([attempt/3]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec reserve_login (binary(), binary()) -> 'ok'.
reserve_login (UsernameLC, EmailLC) ->
   ok = ataxia_client:reserve(login_db, UsernameLC),
   ok = ataxia_client:reserve(login_db, EmailLC),

   ok.

-spec finalize_login (binary(), binary(), binary()) -> 'ok'.
finalize_login (UsernameLC, EmailLC, PlayerID) ->
   LoginUpdateQueryOps =
      ataxic:sequence_meta
      (
         [
            ataxic:update_value(ataxic:constant(PlayerID)),
            ataxic:update_read_permission
            (
               ataxic:constant
               (
                  ataxia_security:allow_only
                  (
                     ataxia_security:any()
                  )
               )
            ),
            ataxic:update_write_permission
            (
               ataxic:constant
               (
                  ataxia_security:allow_only
                  (
                     ataxia_security:user_from_id(PlayerID)
                  )
               )
            )
         ]
      ),

   ok =
      ataxia_client:update
      (
         login_db,
         ataxia_security:janitor(),
         LoginUpdateQueryOps,
         UsernameLC
      ),

   ok =
      ataxia_client:update
      (
         login_db,
         ataxia_security:janitor(),
         LoginUpdateQueryOps,
         EmailLC
      ),

   'ok'.

-spec generate_inventory (shr_player:id()) -> shr_inventory:id().
generate_inventory (PlayerID) ->
   Inventory = shr_inventory:new(PlayerID),

   {ok, InventoryID} =
      ataxia_client:add
      (
         inventory_db,
         ataxia_security:allow_only(ataxia_security:any()),
         ataxia_security:allow_only(ataxia_security:user_from_id(PlayerID)),
         Inventory
      ),

   InventoryID.

-spec generate_roster (shr_player:id()) -> rst_roster:id().
generate_roster (PlayerID) ->
   Roster = rst_roster:new(PlayerID),
   {ok, RosterID} =
      ataxia_client:add
      (
         roster_db,
         ataxia_security:allow_only(ataxia_security:any()),
         ataxia_security:allow_only(ataxia_security:user_from_id(PlayerID)),
         Roster
      ),

   RosterID.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec attempt
   (
      binary(),
      binary(),
      binary()
   )
   -> {shr_player:id(), shr_player:type()}.
attempt (Username, Password, Email) ->
   UsernameLC = string:lowercase(Username),
   EmailLC = string:lowercase(Email),

   ok = reserve_login(UsernameLC, EmailLC),

   Player = shr_player:new(Username, Password, Email),

   JanitorOnlyPermission =
      ataxia_security:allow_only(ataxia_security:janitor()),

   {ok, PlayerID} =
      ataxia_client:add
      (
         player_db,
         JanitorOnlyPermission,
         JanitorOnlyPermission,
         Player
      ),

   shr_janitor:new(player_db, PlayerID),

   InvID = generate_inventory(PlayerID),
   RosterID = generate_roster(PlayerID),

   PlayerUpdateQueryOps =
      ataxic:sequence_meta
      (
         [
            ataxic:update_value
            (
               ataxic:sequence
               (
                  [
                     ataxic:update_field
                     (
                        shr_player:get_inventory_id_field(),
                        ataxic:constant(InvID)
                     ),
                     ataxic:update_field
                     (
                        shr_player:get_roster_id_field(),
                        ataxic:constant(RosterID)
                     )
                  ]
               )
            ),
            ataxic:update_read_permission
            (
               ataxic:constant
               (
                  ataxia_security:allow_only(ataxia_security:any())
               )
            ),
            ataxic:update_write_permission
            (
               ataxic:constant
               (
                  ataxia_security:allow_only
                  (
                     ataxia_security:user_from_id(PlayerID)
                  )
               )
            )
         ]
      ),

   ok = finalize_login(UsernameLC, EmailLC, PlayerID),

   ok =
      ataxia_client:update
      (
         player_db,
         ataxia_security:janitor(),
         PlayerUpdateQueryOps,
         PlayerID
      ),

   {PlayerID, Player}.