⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mytype.pas

📁 用于开发税务票据管理的软件
💻 PAS
字号:
unit mytype;

interface
 uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, IdBaseComponent, IdComponent, IdTCPServer,
  DB, ADODB;

type
{
定义命令型变量用于与客户端传送命令。
command是发送的命令,p1...p3是命令的参数(选用);
}
Rcommandtext=record
command:integer;
param1:string[15];
param2:string[15];
param3:string[15];
num:integer;
end;

{用户信息类型}
Ruserdata=record
username:string[20];
passwd:string[20];
email:string[30];
face:integer;
fivejifeng:int64;
fivewin:int64;
fivelose:int64;
fiveescape:integer;
fivetotal:int64;
IP:string[15];
post:integer;
lastsendtime:Tdatetime;
whatdoing:integer;
AThread: TIdPeerThread
end;

{聊天信息数据类型}
Rtextdata=record
fromwho:string[20];
towho:string[20];
text:string[100];
size:integer;
color:Tcolor;
end;


{
管理服务端的类,大部分本地的数据片理都由它完成。
}
Tctrl=class
private

procedure moditext(var textdata:rtextdata);
public
CS: TRTLCriticalSection;
onlineview:Tlistview;
online:array of Ruserdata;
idtcpserver:Tidtcpserver;
adoquery:Tadoquery;

function reg(user_data:Ruserdata):string;  //注册功能
function login(var user_data:ruserdata):string;   //登陆功能
procedure addonline(user_data:Ruserdata);  //增加在线人数功能
procedure deleonline(user_data:Ruserdata);overload; //离线功能
procedure deleonline(athread:TIdPeerThread);overload;
procedure sendallonline(athread:TIdPeerThread); //发送所以在线的用户
procedure sendtext(ctext:Rcommandtext;textdata:Rtextdata);
procedure modiuserstate(userdata:Ruserdata);
procedure modifeng(ctext:Rcommandtext;userdata:Ruserdata);
procedure modiwhatdoing(ctext:Rcommandtext;userdata:Ruserdata);
end;


implementation

uses myconst;

{ Tctrl }

function Tctrl.login(var user_data: ruserdata): string;
begin
EnterCriticalSection(CS);
adoquery.Close;
adoquery.SQL.Text:='select * from userdata where username="'+user_data.username+'" and passwd="'+user_data.passwd+'"';
adoquery.Open;
if not adoquery.Eof then
  begin
  user_data.face:=adoquery.fieldbyname('face').AsInteger;
  user_data.fivejifeng:=adoquery.fieldbyname('fivejifeng').AsInteger;
  user_data.fivewin:=adoquery.fieldbyname('fivewin').AsInteger;
  user_data.fiveescape:=adoquery.fieldbyname('fiveescape').AsInteger;
  user_data.fivelose:=adoquery.fieldbyname('fivelose').AsInteger;
  user_data.fivetotal:=adoquery.fieldbyname('fivetotal').AsInteger;
  user_data.passwd:='';
  user_data.whatdoing:=0;
  user_data.lastsendtime:=now;
  result:='success';
  end
else result:='用户名或密码不符。';
adoquery.Close;
Leavecriticalsection(CS);
end;

function Tctrl.reg(user_data: Ruserdata): string;
begin
EnterCriticalSection(CS);
adoquery.Close;
adoquery.SQL.Text:='select * from userdata where username="'+user_data.username+'"';
adoquery.Open;
if adoquery.Eof then
  begin
  adoquery.Append;
  adoquery.FieldByName('username').AsString:=user_data.username;
  adoquery.FieldByName('passwd').AsString:=user_data.passwd;
  adoquery.FieldByName('email').AsString:=user_data.email;
  adoquery.Post;
  adoquery.close;
  result:='注册成功';
  end
else
  begin
  result:='用户名已存在。';
  end;
leaveCriticalsection(cs);
end;

procedure Tctrl.addonline(user_data: Ruserdata);
var
i:integer;
ctext:Rcommandtext;
begin
entercriticalsection(cs);
//先向已经登陆的人发送增加的在线用户的命令和信息
ctext.command:=Caddonline;
for i:=0 to high(online) do
  begin
  online[i].AThread.Connection.WriteBuffer(ctext,sizeof(ctext));
  online[i].AThread.Connection.WriteBuffer(user_data,sizeof(user_data));
  end;
//然后才增加节点
setlength(online,high(online)+2);//增加一个节点。
online[high(online)]:=user_data;
with onlineview.Items.add do
begin
  caption:=user_data.username;
  subitems.Add(user_data.ip);
  SubItems.add(datetimetostr(user_data.lastsendtime));
  subitems.Add(inttostr(user_data.post));
end;
leavecriticalsection(cs);
end;

procedure Tctrl.deleonline(user_data: Ruserdata);
var
i:integer;
ctext:Rcommandtext;
begin
ctext.command:=Cdeleonline;
entercriticalsection(cs);
for i:=0 to high(online) do
  begin
  //删除联机节点
  if online[i].username=user_data.username then
    begin
    online[i]:=online[high(online)];
    setlength(online,high(online));
    end
  else
    begin
    online[i].AThread.Connection.WriteBuffer(ctext,sizeof(ctext));
    online[i].AThread.Connection.WriteBuffer(user_data,sizeof(user_data));
    end;
  //listview中显示的节点
  if onlineview.Items.Item[i].caption=user_data.username then
  onlineview.Items.Item[i].Delete;
  end;
leavecriticalsection(cs);
end;

procedure Tctrl.deleonline(athread: TIdPeerThread);
var
userdata:Ruserdata;
ctext:Rcommandtext;
i:integer;
begin
ctext.command:=Cdeleonline;
entercriticalsection(cs);
for i:=0 to high(online) do
  begin
  if online[i].athread=AThread then
    begin
    userdata:=online[i];
    online[i]:=online[high(online)];
    setlength(online,high(online));
    break;
    end;
  end;
for i:=0 to high(online) do
  begin
  online[i].AThread.Connection.WriteBuffer(ctext,sizeof(ctext));
  online[i].AThread.Connection.WriteBuffer(userdata,sizeof(userdata));
  if onlineview.Items.Item[i].caption=userdata.username then
  onlineview.Items.Item[i].Delete;
  end;
if (i=onlineview.Items.Count-1) and (onlineview.Items.Item[i].caption=userdata.username) then
onlineview.Items.Item[i].Delete;
leavecriticalsection(cs);
end;


procedure Tctrl.sendallonline(athread: TIdPeerThread);
var
i:integer;
ctext:Rcommandtext;
begin
ctext.command:=Caddonline;
entercriticalsection(cs);
for i:=0 to high(online) do
  begin
  athread.Connection.WriteBuffer(ctext,sizeof(ctext));
  athread.connection.WriteBuffer(online[i],sizeof(online[i]));
  end;
leavecriticalsection(cs);
end;

procedure Tctrl.moditext(var textdata: rtextdata);
begin
if textdata.text='/:)' then
begin
textdata.text:=textdata.fromwho+'微笑';
textdata.fromwho:='';
end;
end;

procedure Tctrl.sendtext(ctext:Rcommandtext;textdata: Rtextdata);
var
i:integer;
begin
entercriticalsection(cs);
for i:=0 to high(online) do
begin
online[i].AThread.Connection.WriteBuffer(ctext,sizeof(ctext));
online[i].AThread.Connection.WriteBuffer(textdata,sizeof(textdata));
end;
leavecriticalsection(cs);
end;



procedure Tctrl.modiuserstate(userdata: Ruserdata);
var
i:integer;
ctext:Rcommandtext;
begin
ctext.command:=Cmodiuserstate;
 for i:=0 to high(online) do
 begin
 online[i].AThread.Connection.WriteBuffer(ctext,sizeof(ctext));
 online[i].AThread.Connection.WriteBuffer(userdata,sizeof(userdata));
 end;
end;

procedure Tctrl.modifeng(ctext: Rcommandtext; userdata: Ruserdata);
var
i:integer;
begin
entercriticalsection(cs);
adoquery.Close;
adoquery.SQL.Text:='select * from userdata where username="'+userdata.username+'"';
adoquery.Open;
adoquery.Edit;
case ctext.num of
Cwin:adoquery.FieldByName('fivewin').AsInteger:=adoquery.FieldByName('fivewin').AsInteger+1;
Close1:adoquery.FieldByName('fivelose').AsInteger:=adoquery.FieldByName('fivelose').AsInteger+1;
Cescape:adoquery.fieldbyname('fiveescape').AsInteger:=adoquery.fieldbyname('fiveescape').AsInteger+1;
end;//case
adoquery.fieldbyname('fivefend').AsInteger:=adoquery.FieldByName('fivewin').AsInteger*5-
adoquery.FieldByName('fiveflose').AsInteger*3-adoquery.FieldByName('fiveescape').AsInteger*10;
adoquery.post;
userdata.face:=adoquery.fieldbyname('face').AsInteger;
userdata.fivejifeng:=adoquery.fieldbyname('fivejifen').AsInteger;
userdata.fivewin:=adoquery.fieldbyname('fivewin').AsInteger;
userdata.fiveescape:=adoquery.fieldbyname('fiveescape').AsInteger;
userdata.fivelose:=adoquery.fieldbyname('fivelose').AsInteger;
//修改online里的分值
for i:=0 to high(online) do
  begin
  if online[i].username=adoquery.fieldbyname('username').asstring then
    begin
    online[i].fivejifeng:=adoquery.fieldbyname('jifeng').asinteger;
    online[i].fivewin:=adoquery.fieldbyname('fivewin').asinteger;
    online[i].fivelose:=adoquery.fieldbyname('fivelose').asinteger;
    online[i].fiveescape:=adoquery.fieldbyname('fiveescape').asinteger;
    online[i].fivetotal:=adoquery.fieldbyname('fivetotal').asinteger;
    self.modiuserstate(online[i]);
    break;
    end;
  end;
adoquery.close;
leavecriticalsection(cs);
end;



procedure Tctrl.modiwhatdoing(ctext: Rcommandtext; userdata: Ruserdata);
var
i:integer;
begin
entercriticalsection(cs);
for i:=0 to high(online) do
  begin
  if online[i].username=userdata.username then
    begin
    online[i].whatdoing:=ctext.num;
    modiuserstate(online[i]);
    break;
    end;
  end;

leavecriticalsection(cs);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -