Statistics
| Branch: | Tag: | Revision:

root / Server / UAccount.pas @ 13:c78b5eafa10e

History | View | Annotate | Download (6.5 kB)

1
(*
2
 * CDDL HEADER START
3
 *
4
 * The contents of this file are subject to the terms of the
5
 * Common Development and Distribution License, Version 1.0 only
6
 * (the "License").  You may not use this file except in compliance
7
 * with the License.
8
 *
9
 * You can obtain a copy of the license at
10
 * http://www.opensource.org/licenses/cddl1.php.
11
 * See the License for the specific language governing permissions
12
 * and limitations under the License.
13
 *
14
 * When distributing Covered Code, include this CDDL HEADER in each
15
 * file and include the License file at
16
 * http://www.opensource.org/licenses/cddl1.php.  If applicable,
17
 * add the following below this CDDL HEADER, with the fields enclosed
18
 * by brackets "[]" replaced with your own identifying * information:
19
 *      Portions Copyright [yyyy] [name of copyright owner]
20
 *
21
 * CDDL HEADER END
22
 *
23
 *
24
 *      Portions Copyright 2008 Andreas Schneider
25
 *)
26
unit UAccount;
27
28
{$mode objfpc}{$H+}
29
30
interface
31
32
uses
33
  Classes, SysUtils, contnrs, math, DOM, UXmlHelper, UInterfaces, UEnums;
34
35
type
36
37
  { TAccount }
38
39
  TAccount = class(TObject, ISerializable, IInvalidate)
40
    constructor Create(AOwner: IInvalidate; AName, APasswordHash: string;
41
      AAccessLevel: TAccessLevel; ARegions: TStringList);
42
    constructor Deserialize(AOwner: IInvalidate; AElement: TDOMElement);
43
    destructor Destroy; override;
44
    procedure Serialize(AElement: TDOMElement);
45
  protected
46
    FOwner: IInvalidate;
47
    FName: string;
48
    FAccessLevel: TAccessLevel;
49
    FPasswordHash: string;
50
    FLastPos: TPoint;
51
    FRegions: TStringList;
52
    procedure SetAccessLevel(const AValue: TAccessLevel);
53
    procedure SetPasswordHash(const AValue: string);
54
    procedure SetLastPos(const AValue: TPoint);
55
  public
56
    property Name: string read FName;
57
    property AccessLevel: TAccessLevel read FAccessLevel write SetAccessLevel;
58
    property PasswordHash: string read FPasswordHash write SetPasswordHash;
59
    property LastPos: TPoint read FLastPos write SetLastPos;
60
    property Regions: TStringList read FRegions;
61
    procedure Invalidate;
62
  end;
63
64
  { TAccountList }
65
66
  TAccountList = class(TObjectList, ISerializable, IInvalidate)
67
    constructor Create(AOwner: IInvalidate); reintroduce;
68
    constructor Deserialize(AOwner: IInvalidate; AElement: TDOMElement);
69
    procedure Serialize(AElement: TDOMElement);
70
  protected
71
    FOwner: IInvalidate;
72
  public
73
    function IndexOf(AName: string): Integer;
74
    function Find(AName: string): TAccount;
75
    procedure Delete(AName: string);
76
    procedure Invalidate;
77
  end;
78
79
implementation
80
81
uses
82
  UCEDServer, UConfig;
83
84
{ TAccount }
85
86
constructor TAccount.Create(AOwner: IInvalidate; AName, APasswordHash: string;
87
  AAccessLevel: TAccessLevel; ARegions: TStringList);
88
begin
89
  inherited Create;
90
  FOwner := AOwner;
91
  FName := AName;
92
  FPasswordHash := APasswordHash;
93
  FAccessLevel := AAccessLevel;
94
  if ARegions <> nil then
95
    FRegions := ARegions
96
  else
97
    FRegions := TStringList.Create;
98
end;
99
100
constructor TAccount.Deserialize(AOwner: IInvalidate; AElement: TDOMElement);
101
var
102
  xmlElement, xmlRegion: TDOMElement;
103
  nodelist: TDOMNodeList;
104
  i: Integer;
105
begin
106
  inherited Create;
107
  FOwner := AOwner;
108
  FName := TXmlHelper.ReadString(AElement, 'Name', '');
109
  FAccessLevel := TAccessLevel(TXmlHelper.ReadInteger(AElement, 'AccessLevel', 0));
110
  FPasswordHash := TXmlHelper.ReadString(AElement, 'PasswordHash', '');
111
  FLastPos := Point(0, 0);
112
  TXmlHelper.ReadCoords(AElement, 'LastPos', FLastPos.X, FLastPos.Y);
113
  FRegions := TStringList.Create;
114
115
  xmlElement := TDOMElement(AElement.FindNode('Regions'));
116
  if xmlElement <> nil then
117
  begin
118
    nodeList := xmlElement.GetChildNodes;
119
    for i := 0 to nodeList.Count - 1 do
120
    begin
121
      if nodeList.Item[i].NodeName = 'Region' then
122
      begin
123
        xmlRegion := TDOMElement(nodeList.Item[i]);
124
        if assigned(xmlRegion.FirstChild) then
125
          FRegions.Add(TDOMText(xmlRegion.FirstChild).Data);
126
      end;
127
    end;
128
    nodeList.Free;
129
  end;
130
end;
131
132
destructor TAccount.Destroy;
133
begin
134
  if FRegions <> nil then FreeAndNil(FRegions);
135
  inherited Destroy;
136
end;
137
138
procedure TAccount.SetAccessLevel(const AValue: TAccessLevel);
139
begin
140
  FAccessLevel := AValue;
141
  Invalidate;
142
end;
143
144
procedure TAccount.SetPasswordHash(const AValue: string);
145
begin
146
  FPasswordHash := AValue;
147
  Invalidate;
148
end;
149
150
procedure TAccount.SetLastPos(const AValue: TPoint);
151
begin
152
  FLastPos.x := EnsureRange(AValue.x, 0, CEDServerInstance.Landscape.CellWidth - 1);
153
  FLastPos.y := EnsureRange(AValue.y, 0, CEDServerInstance.Landscape.CellHeight - 1);
154
  Invalidate;
155
end;
156
157
procedure TAccount.Invalidate;
158
begin
159
  FOwner.Invalidate;
160
end;
161
162
procedure TAccount.Serialize(AElement: TDOMElement);
163
var
164
  i: Integer;
165
  child: TDOMElement;
166
begin
167
  TXmlHelper.WriteString(AElement, 'Name', FName);
168
  TXmlHelper.WriteString(AElement, 'PasswordHash', FPasswordHash);
169
  TXmlHelper.WriteInteger(AElement, 'AccessLevel', Integer(FAccessLevel));
170
  TXmlHelper.WriteCoords(AElement, 'LastPos', FLastPos.X, FLastPos.Y);
171
  child := TXmlHelper.AssureElement(AElement, 'Regions');
172
  for i := 0 to FRegions.Count -1 do
173
    if Config.Regions.Find(FRegions[i]) <> nil then //Validate if the region (still) exists
174
      TXmlHelper.WriteString(child, 'Region', FRegions[i]);
175
end;
176
177
{ TAccountList }
178
179
constructor TAccountList.Create(AOwner: IInvalidate);
180
begin
181
  inherited Create(True);
182
  FOwner := AOwner;
183
end;
184
185
constructor TAccountList.Deserialize(AOwner: IInvalidate; AElement: TDOMElement);
186
var
187
  nodelist: TDOMNodeList;
188
  i: Integer;
189
begin
190
  Create(AOwner);
191
  nodeList := AElement.GetChildNodes;
192
  for i := 0 to nodeList.Count - 1 do
193
  begin
194
    if nodeList.Item[i].NodeName = 'Account' then
195
      Add(TAccount.Deserialize(Self, TDOMElement(nodeList.Item[i])));
196
  end;
197
  nodeList.Free;
198
end;
199
200
function TAccountList.IndexOf(AName: string): Integer;
201
var
202
  i: Integer;
203
begin
204
  Result := -1;
205
  i := 0;
206
  while (i < Count) and (Result = -1) do
207
  begin
208
    if TAccount(Items[i]).Name = AName then
209
      Result := i;
210
    Inc(i);
211
  end;
212
end;
213
214
function TAccountList.Find(AName: string): TAccount;
215
var
216
  i: Integer;
217
begin
218
  i := IndexOf(AName);
219
  if i > -1 then
220
    Result := TAccount(Items[i])
221
  else
222
    Result := nil;
223
end;
224
225
procedure TAccountList.Delete(AName: string);
226
var
227
  i: Integer;
228
begin
229
  i := IndexOf(AName);
230
  if i > -1 then
231
    inherited Delete(i);
232
end;
233
234
procedure TAccountList.Invalidate;
235
begin
236
  FOwner.Invalidate;
237
end;
238
239
procedure TAccountList.Serialize(AElement: TDOMElement);
240
var
241
  i: Integer;
242
  xmlAccount: TDOMElement;
243
begin
244
  for i := 0 to Count - 1 do
245
  begin
246
    xmlAccount := AElement.OwnerDocument.CreateElement('Account');
247
    AElement.AppendChild(xmlAccount);
248
    TAccount(Items[i]).Serialize(xmlAccount);
249
  end;
250
end;
251
252
end.
253