unit u_invitation;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, u_paths, IniFiles, JPeg, Sockets, Contnrs,
u_Board;
type
TInvitationForm =
class(TForm)
ImgBG: TImage;
LabelText: TLabel;
LabelStatus: TLabel;
BtnAccept: TImage;
BtnRefuse: TImage;
BtnCancel: TImage;
TcpClient: TTcpClient;
TcpServer: TTcpServer;
procedure FormCreate(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure ImgBGMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure BtnCancelClick(Sender: TObject);
procedure BtnAcceptClick(Sender: TObject);
procedure BtnRefuseClick(Sender: TObject);
procedure BtnCancelMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure BtnAcceptMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure BtnRefuseMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure BtnCancelMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure BtnAcceptMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure BtnRefuseMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure TcpServerAccept(Sender: TObject;
ClientSocket: TCustomIpClient);
private
{ Private-Deklarationen }
Paths: RPaths;
{ enthält alle Pfade }
OppNick:
String;
{ Nickname des Gegners }
Language:
String;
{ Sprache }
ABoards: TObjectList;
{ Referenz auf alle offenen Schachbretter }
AIFiles: RInvitation;
{ Statuswert (Rückgabe) }
{ Images & Index der Buttons }
BtnCancelImgs: RImgBtn;
BtnAcceptImgs: RImgBtn;
BtnRefuseImgs: RImgBtn;
procedure LoadImages;
procedure FillImgs(
var FImgs: RImgBtn; FTempPath:
String);
procedure SetForm;
procedure SetButtons;
procedure SetButton(
var FBtn: TImage;
var FBtnImgs: RImgBtn);
procedure CenterLabels;
procedure SetStatus(FStatus:
String);
procedure Send(FText:
String; FPort: Integer);
function SearchHighestPort(FBoards: TObjectList; FPort: Integer): Integer;
public
{ Public-Deklarationen }
function SetFiles(FStatus, FOppNick, FLanguage:
String; FPaths: RPaths;
FOppIP:
String; FPort: Integer; FBoards: TObjectList): RInvitation;
end;
var
InvitationForm: TInvitationForm;
implementation
uses u_main;
{$R *.dfm}
//----Hauptroutinen-(MainForm)--------------------------------------------------
procedure TInvitationForm.FormCreate(Sender: TObject);
begin
BorderIcons:=[];
FirstStart:=True;
BorderStyle:=bsNone;
AlphaBlend:=True;
AlphaBlendValue:=200;
end;
procedure TInvitationForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action:=caFree;
end;
//------------------------------------------------------------------------------
//----Einrichtungs-Routinen-----------------------------------------------------
function TInvitationForm.SetFiles(FStatus, FOppNick, FLanguage:
String; FPaths:
RPaths; FOppIP:
String; FPort: Integer; FBoards: TObjectList): RInvitation;
begin
OppNick:=FOppNick;
Language:=FLanguage;
Paths:=FPaths;
LoadImages;
SetButtons;
SetForm;
ABoards:=FBoards;
TcpServer.LocalPort:=IntToStr(FPort);
TcpServer.Active:=True;
TcpClient.RemotePort:=IntToStr(FPort);
TcpClient.RemoteHost:=FOppIP;
SetStatus(FStatus);
ShowModal;
result:=AIFiles;
end;
//------------------------------------------------------------------------------
//----on-Klick-Routinen---------------------------------------------------------
procedure TInvitationForm.BtnCancelClick(Sender: TObject);
begin
Send('
Cancel',0);
AIFiles.Status:='
Cancel';
Close;
end;
procedure TInvitationForm.BtnAcceptClick(Sender: TObject);
var
AHighPort: Integer;
begin
AHighPort:=SearchHighestPort(ABoards,5800);
inc(AHighPort);
Send('
Port',AHighPort);
end;
procedure TInvitationForm.BtnRefuseClick(Sender: TObject);
begin
Send('
Refused',0);
AIFiles.Status:='
Refused';
Close;
end;
//------------------------------------------------------------------------------
//----Netzwerk-Routinen---------------------------------------------------------
procedure TInvitationForm.Send(FText:
String; FPort: Integer);
var
AInfo: RInvitation;
begin
AInfo.Status:=FText;
AInfo.UsePort:=FPort;
try
if TcpClient.Connect
then
begin
TcpClient.SendBuf(AInfo,SizeOf(AInfo));
end;
finally
TcpClient.Disconnect;
end;
end;
procedure TInvitationForm.TcpServerAccept(Sender: TObject;
ClientSocket: TCustomIpClient);
var
AInfo: RInvitation;
APort: Integer;
begin
ClientSocket.ReceiveBuf(AInfo,SizeOf(AInfo));
AIFiles.Status:=AInfo.Status;
AIFiles.UsePort:=AInfo.UsePort;
if (AInfo.Status='
Port')
then
begin
APort:=SearchHighestPort(ABoards,AInfo.UsePort);
inc(APort);
Send('
StartGame',APort);
AIFiles.Status:='
StartGame';
AIFiles.UsePort:=APort;
end;
Close;
Refresh;
end;
//------------------------------------------------------------------------------
//----Such-Routinen-------------------------------------------------------------
function TInvitationForm.SearchHighestPort(FBoards: TObjectList; FPort:
Integer): Integer;
var
i: Integer;
ActPort: Integer;
APort: Integer;
begin
APort:=FPort;
for i:=0
to FBoards.Count-1
do
begin
ActPort:=TBoardForm(FBoards.Items[i]).GlobalFiles.UsedPort;
if ActPort > APort
then APort:=ActPort;
end;
result:=APort;
end;
//------------------------------------------------------------------------------
end.