source: trunk/ServerList.pas

Last change on this file was 317, checked in by chronos, 6 months ago
  • Modified: Remove U prefix from unit names.
  • Modified: Use TFormEx for all forms for code simplification.
File size: 3.0 KB
Line 
1unit ServerList;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections, XMLConf;
7
8type
9
10 { TServerInfo }
11
12 TServerInfo = class
13 Name: string;
14 Address: string;
15 Port: Word;
16 PlayersCountCurrent: Integer;
17 PlayersCountMax: Integer;
18 Password: string;
19 MapName: string;
20 Latency: TTime;
21 procedure Assign(Source: TServerInfo);
22 constructor Create;
23 end;
24
25 TServerInfos = class(TObjectList<TServerInfo>)
26 end;
27
28 { TServerList }
29
30 TServerList = class
31 Name: string;
32 Address: string;
33 Items: TServerInfos;
34 procedure Assign(Source: TServerList);
35 constructor Create;
36 destructor Destroy; override;
37 procedure LoadConfig(Config: TXmlConfig; Path: string);
38 procedure SaveConfig(Config: TXmlConfig; Path: string);
39 end;
40
41
42implementation
43
44uses
45 GameServer;
46
47{ TServerInfo }
48
49procedure TServerInfo.Assign(Source: TServerInfo);
50begin
51 Name := Source.Name;
52 Address := Source.Address;
53 Port := Source.Port;
54 PlayersCountCurrent := Source.PlayersCountCurrent;
55 PlayersCountMax := Source.PlayersCountMax;
56 Password := Source.Password;
57 MapName := Source.MapName;
58 Latency := Source.Latency;
59end;
60
61constructor TServerInfo.Create;
62begin
63 Port := DefaultServerPort;
64end;
65
66{ TServerList }
67
68procedure TServerList.Assign(Source: TServerList);
69var
70 I: Integer;
71begin
72 while Items.Count > Source.Items.Count do
73 Items.Delete(Items.Count - 1);
74 while Items.Count < Source.Items.Count do
75 Items.Add(TServerInfo.Create);
76 for I := 0 to Items.Count - 1 do begin
77 Items[I].Assign(Source.Items[I]);
78 end;
79end;
80
81constructor TServerList.Create;
82begin
83 Items := TServerInfos.Create;
84end;
85
86destructor TServerList.Destroy;
87begin
88 FreeAndNil(Items);
89 inherited;
90end;
91
92procedure TServerList.LoadConfig(Config: TXmlConfig; Path: string);
93var
94 I: Integer;
95 Server: TServerInfo;
96begin
97 Items.Clear;
98 Items.Count := Config.GetValue(UnicodeString(Path + '/Count'), 0);
99 for I := 0 to Items.Count - 1 do begin
100 Server := TServerInfo.Create;
101 Server.Name := string(Config.GetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Name'), ''));
102 Server.Address := string(Config.GetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Address'), ''));
103 Server.Port := Config.GetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Port'), 0);
104 Items[I] := Server;
105 end;
106end;
107
108procedure TServerList.SaveConfig(Config: TXmlConfig; Path: string);
109var
110 I: Integer;
111 Server: TServerInfo;
112begin
113 Config.SetValue(UnicodeString(Path + '/Count'), Items.Count);
114 for I := 0 to Items.Count - 1 do begin
115 Server := Items[I];
116 Config.SetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Name'), UnicodeString(Server.Name));
117 Config.SetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Address'), UnicodeString(Server.Address));
118 Config.SetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Port'), Server.Port);
119 end;
120end;
121
122end.
123
Note: See TracBrowser for help on using the repository browser.