summaryrefslogtreecommitdiff
blob: 4b9a80a8a70fe6d3bc6510d149072035d975e3c9 (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
-module(spe_player).

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

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec reserve_login (binary(), binary()) -> 'ok'.
reserve_login (UsernameLC, EmailLC) ->
   shr_janitor:new(login_db, UsernameLC),
   shr_janitor:new(login_db, EmailLC),

   ok = ataxia_client:reserve(login_db, ataxia_security:janitor(), UsernameLC),
   ok = ataxia_client:reserve(login_db, ataxia_security:janitor(), EmailLC),

   ok.

-spec finalize_login (binary(), binary(), binary()) -> 'ok'.
finalize_login (UsernameLC, EmailLC, PlayerID) ->
   LoginUpdateQueryOps =
      ataxic:sequence_meta
      (
         [
            ataxic:value(ataxic:constant(PlayerID)),
            ataxic:read_permission(ataxic:constant(ataxia_security:any())),
            ataxic:write_permission
            (
               ataxic:constant([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 (ataxia_id:type()) -> ataxia_id:type().
generate_inventory (PlayerID) ->
   Inventory = shr_inventory:new(PlayerID),

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

   InventoryID.

-spec generate_roster (ataxia_id:type()) -> ataxia_id:type().
generate_roster (PlayerID) ->
   Roster = rst_roster:new(PlayerID),
   {ok, RosterID} =
      ataxia_client:add
      (
         roster_db,
         ataxia_security:any(),
         [ataxia_security:user_from_id(PlayerID)],
         Roster
      ),

   RosterID.

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

   ok = reserve_login(UsernameLC, EmailLC),

   Player = shr_player:new(<<"">>, Username, Password, Email),

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

   shr_janitor:new(player_db, PlayerID),

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

   PlayerUpdateQueryOps =
      ataxic:sequence_meta
      (
         [
            ataxic:value
            (
               ataxic:sequence
               (
                  [
                     ataxic:update_field
                     (
                        shr_player:get_id_field(),
                        ataxic:constant(PlayerID)
                     ),
                     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:read_permission(ataxic:constant(ataxia_security:any())),
            ataxic:write_permission
            (
               ataxic:constant([ataxia_security:user_from_id(PlayerID)])
            )
         ]
      ),

   ok = finalize_login(UsernameLC, EmailLC, PlayerID),

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


   Result = shr_player:set_id(PlayerID, Player),

   Result.