-
Notifications
You must be signed in to change notification settings - Fork 5
/
create.pas
228 lines (192 loc) · 6.36 KB
/
create.pas
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
unit create;
interface
uses
Windows, SysUtils, Classes, Controls, Forms, StdCtrls, ExtCtrls;
type
TFormNew = class(TForm)
LabelGame: TLabel;
BevelGame: TBevel;
LabelName: TLabel;
EditName: TEdit;
LabelKind: TLabel;
cbKind: TComboBox;
BevelClient: TBevel;
LabelClient: TLabel;
LabelIP: TLabel;
EditIP: TEdit;
ButtonClient: TButton;
BevelServer: TBevel;
LabelServer: TLabel;
LabelPlayer2: TLabel;
cbPlayer2: TComboBox;
LabelPlayer3: TLabel;
cbPlayer3: TComboBox;
LabelPlayer4: TLabel;
cbPlayer4: TComboBox;
ButtonServer: TButton;
BevelMsg: TBevel;
LabelMsg: TLabel;
MemoMsg: TMemo;
EditMsg: TEdit;
ButtonMsg: TButton;
BevelButton: TBevel;
ButtonStart: TButton;
ButtonCancel: TButton;
procedure FormCreate(Sender: TObject);
procedure cbKindSelect(Sender: TObject);
procedure EditMsgEnter(Sender: TObject);
procedure EditMsgExit(Sender: TObject);
procedure ButtonMsgClick(Sender: TObject);
procedure ButtonClientClick(Sender: TObject);
procedure ButtonServerClick(Sender: TObject);
procedure ButtonStartClick(Sender: TObject);
procedure ButtonCancelClick(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FormNew: TFormNew;
implementation
uses
main;
{$R *.dfm}
procedure TFormNew.FormCreate(Sender: TObject);
begin
MemoMsg.Lines.Clear;
EditName.Text := FormGaple.ConfigFile.ReadString('PLAYERS','1','Player 1');
EditIP.Text := FormGaple.ConfigFile.ReadString('NETWORK','SERVER','172.17.3.145');
end;
procedure TFormNew.FormKeyPress(Sender: TObject; var Key: Char);
begin
if Key = char(VK_ESCAPE) then
ModalResult := mrCancel
else
ModalResult := mrNone;
end;
procedure TFormNew.cbKindSelect(Sender: TObject);
begin
EditIP.Enabled := (cbKind.ItemIndex = 2);
ButtonClient.Enabled := (cbKind.ItemIndex = 2);
cbPlayer2.Enabled := (cbKind.ItemIndex = 1);
cbPlayer3.Enabled := (cbKind.ItemIndex = 1);
cbPlayer4.Enabled := (cbKind.ItemIndex = 1);
ButtonServer.Enabled := (cbKind.ItemIndex = 1);
MemoMsg.Enabled := (cbKind.ItemIndex = 1) or (cbKind.ItemIndex = 2);
ButtonStart.Enabled := (cbKind.ItemIndex = 0);
if cbKind.ItemIndex = 0 then FormGaple.GameKind := gkAlone
else if cbKind.ItemIndex = 1 then FormGaple.GameKind := gkServer
else if cbKind.ItemIndex = 2 then FormGaple.GameKind := gkClient;
end;
procedure TFormNew.EditMsgEnter(Sender: TObject);
begin
ButtonMsg.Default := true;
ButtonStart.Default := false;
end;
procedure TFormNew.EditMsgExit(Sender: TObject);
begin
ButtonMsg.Default := false;
ButtonStart.Default := true;
end;
procedure TFormNew.ButtonMsgClick(Sender: TObject);
var
i: integer;
begin
if EditMsg.Text <> '' then
begin
if FormGaple.ServerSocket.Active then
begin
for i := 0 to FormGaple.ServerSocket.Socket.ActiveConnections-1 do
FormGaple.ServerSocket.Socket.Connections[i].SendText(EditName.Text+' > '+EditMsg.Text);
MemoMsg.Lines.Add(EditName.Text+' > '+EditMsg.Text);
EditMsg.Text := '';
EditMsg.SetFocus;
end
else if FormGaple.ClientSocket.Active then
begin
FormGaple.ClientSocket.Socket.SendText(EditName.Text+' > '+EditMsg.Text);
EditMsg.Text := '';
EditMsg.SetFocus;
end;
end;
end;
procedure TFormNew.ButtonStartClick(Sender: TObject);
begin
FormGaple.ConfigFile.WriteString('PLAYERS','1',EditName.Text);
FormGaple.ConfigFile.WriteString('NETWORK','SERVER',EditIP.Text);
end;
procedure TFormNew.ButtonCancelClick(Sender: TObject);
begin
FormGaple.ServerSocket.Active := false;
FormGaple.ClientSocket.Active := false;
end;
procedure TFormNew.ButtonClientClick(Sender: TObject);
begin
if EditIP.Text <> '' then
begin
FormGaple.ClientSocket.Address := EditIP.Text;
if FormGaple.ClientSocket.Active then
FormGaple.ClientSocket.Close
else
FormGaple.ClientSocket.Open;
end
else
FormGaple.NoServerError;
end;
procedure TFormNew.ButtonServerClick(Sender: TObject);
var
np2,np3,np4: boolean;
begin
// open server
if not FormGaple.ServerSocket.Active then
begin
np2 := (cbPlayer2.ItemIndex = 0);
np3 := (cbPlayer3.ItemIndex = 0);
np4 := (cbPlayer4.ItemIndex = 0);
// determine number of network players
FormGaple.NetworkPlayers := 0;
if np2 then Inc(FormGaple.NetworkPlayers);
if np3 then Inc(FormGaple.NetworkPlayers);
if np4 then Inc(FormGaple.NetworkPlayers);
// ascociate players kind
if np2 then FormGaple.Players[2].Kind := plNetwork else FormGaple.Players[2].Kind := plComputer;
if np3 then FormGaple.Players[3].Kind := plNetwork else FormGaple.Players[3].Kind := plComputer;
if np4 then FormGaple.Players[4].Kind := plNetwork else FormGaple.Players[4].Kind := plComputer;
// start game server
if FormGaple.NetworkPlayers > 0 then
FormGaple.ServerSocket.Open
else
begin
FormGaple.NoNetPlayerError;
Exit;
end;
// enable components
cbKind.Enabled := not FormGaple.ServerSocket.Active;
EditName.Enabled := not FormGaple.ServerSocket.Active;
cbPlayer2.Enabled := not FormGaple.ServerSocket.Active;
cbPlayer3.Enabled := not FormGaple.ServerSocket.Active;
cbPlayer4.Enabled := not FormGaple.ServerSocket.Active;
EditMsg.Enabled := FormGaple.ServerSocket.Active;
ButtonMsg.Enabled := FormGaple.ServerSocket.Active;
ButtonServer.Caption := '&Close';
MemoMsg.Lines.Add('Waiting for '+IntToStr(FormGaple.NetworkPlayers)+' network players...');
end
// closing server
else
begin
FormGaple.ServerSocket.Close;
// disable components
cbKind.Enabled := not FormGaple.ServerSocket.Active;
EditName.Enabled := not FormGaple.ServerSocket.Active;
cbPlayer2.Enabled := not FormGaple.ServerSocket.Active;
cbPlayer3.Enabled := not FormGaple.ServerSocket.Active;
cbPlayer4.Enabled := not FormGaple.ServerSocket.Active;
EditMsg.Enabled := FormGaple.ServerSocket.Active;
ButtonMsg.Enabled := FormGaple.ServerSocket.Active;
ButtonServer.Caption := '&Create';
MemoMsg.Lines.Add('Game server has been closed.');
end;
end;
end.