{ *********************************************************************** }
{                                                                         }
{ Copular Chat Server v3.0 Source Code                                    }
{ Main Form Unit                                                          }
{                                                                         }
{ Copyright (c) 1998-2002 SAF Studio                                      }
{                                                                         }
{ Author  : Niu Yu Ping                                                   }
{ Nickname: DecimalOX                                                     }
{ Address : Jilin City China                                              }
{                                                                         }
{ QICQ    : 103106262                                                     }
{ Homepage: www.safree.com                                                }
{ EMail   : decimalox@sohu.com                                            }
{                                                                         }
{ *********************************************************************** }

unit Server_Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, AppEvnts, Core, XPMan, RXShell, KsHooks, KsForms,
  KsSkinForms, KsSkinEngine;

const
  LOG_FILE: string='server.log';

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Label1: TLabel;
    ApplicationEvents1: TApplicationEvents;
    CastMemo: TMemo;
    RxTrayIcon1: TRxTrayIcon;
    SeSkinEngine1: TSeSkinEngine;
    SeSkinForm1: TSeSkinForm;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure ApplicationEvents1Exception(Sender: TObject; E: Exception);
    procedure RxTrayIcon1DblClick(Sender: TObject);
    procedure ApplicationEvents1Minimize(Sender: TObject);
    procedure ApplicationEvents1Restore(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }

    LogFile:TextFile;
  public
    { Public declarations }

    //TRoom Class in Core.pass
    Room: TRoom;

    //Ad Message Index
    AdverNo:Integer;

    //Update Server Log File
    procedure LogIt(LogText:string);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  //Create a room on Port=MAIN_ROOM_SOCKET
  Room:=TRoom.Create(MAIN_ROOM_SOCKET);

  AdverNo:=0;
  AssignFile(LogFile,LOG_FILE);
  ShowWindow(Application.Handle, SW_HIDE);

  //Send Ad Message Timer
  Timer1.Enabled:=True;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Room.Free;
end;

procedure TForm1.LogIt(LogText: string);
begin
  Append(LogFile);
  Writeln(LogFile,LogText);
  CloseFile(LogFile);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Room.OnlineList.AddMarksToUsers(1);

  //Send Ad Messages in the CastMemo
  if CastMemo.Lines.Count<>0 then
  begin
    Room.SendTextToAll(CreateCommandLine(ADVER_TEXT,CastMemo.Lines[AdverNo]));
    Inc(AdverNo);
    AdverNo:=AdverNo mod CastMemo.Lines.Count;
  end;
end;

procedure TForm1.ApplicationEvents1Exception(Sender: TObject;
  E: Exception);
begin
  LogIt(E.Message);
end;

procedure TForm1.RxTrayIcon1DblClick(Sender: TObject);
begin
  Application.Restore;
  ShowWindow(Application.Handle, SW_Normal);
  SetForegroundWindow(Handle);
end;

procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
  ShowWindow(Application.Handle, SW_HIDE);
  RxTrayIcon1.Active:=True;
end;

procedure TForm1.ApplicationEvents1Restore(Sender: TObject);
begin
  RxTrayIcon1.Active:=False;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose:=MessageBox(Handle, '真的要结束服务程序吗？', '确认', MB_ICONQUESTION or MB_YESNO)=idYes;
end;

end.
