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

📄 frnengn.pas

📁 FIR引擎最新源码+注册
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit FrnEngn;

interface

uses
  Windows, Classes, SysUtils, Grobal2, SDK;
type
  TFrontEngine = class(TThread)
    m_UserCriticalSection: TRTLCriticalSection;
    m_LoadRcdList: TList;
    m_SaveRcdList: TList;
    m_ChangeGoldList: TList;
  private
    m_LoadRcdTempList: TList;
    m_SaveRcdTempList: TList;
    procedure GetGameTime();
    procedure ProcessGameDate();
    function LoadHumFromDB(LoadUser: pTLoadDBInfo; var boReTry: Boolean): Boolean;
    function ChangeUserGoldInDB(GoldChangeInfo: pTGoldChangeInfo): Boolean;
    procedure Run();
    { Private declarations }
  protected
    procedure Execute; override;
  public
    constructor Create(CreateSuspended: Boolean);
    destructor Destroy; override;
    function SaveListCount(): Integer;
    function IsIdle(): Boolean;
    function IsFull(): Boolean;
    procedure DeleteHuman(nGateIndex, nSocket: Integer);
    function InSaveRcdList(sAccount, sChrName: string): Boolean;
    procedure AddChangeGoldList(sGameMasterName, sGetGoldUserName: string; nGold: Integer);
    procedure AddToLoadRcdList(sAccount, sChrName, sIPaddr: string; boFlag: Boolean; nSessionID: Integer; nPayMent, nPayMode, nSoftVersionDate, nSocket, nGSocketIdx, nGateIdx: Integer);
    procedure AddToLoadHeroRcdList(sCharName, sMsg: string; PlayObject: TObject; btLoadType: Byte);
    procedure AddToSaveRcdList(SaveRcd: pTSaveRcd);
    function UpDataSaveRcdList(SaveRcd: pTSaveRcd): Boolean;
    function GetSaveRcd(sAccount, sCharName: string): pTSaveRcd;
  end;

implementation
uses
  M2Share, RunDB, ObjBase, HUtil32;
{ TFrontEngine }

constructor TFrontEngine.Create(CreateSuspended: Boolean);
begin
  inherited;
  InitializeCriticalSection(m_UserCriticalSection);
  m_LoadRcdList := TList.Create;
  m_SaveRcdList := TList.Create;
  m_ChangeGoldList := TList.Create;
  m_LoadRcdTempList := TList.Create;
  m_SaveRcdTempList := TList.Create;
  //  FreeOnTerminate:=True;
  //AddToProcTable(@TFrontEngine.ProcessGameDate, 'TFrontEngine.ProcessGameDatea');
end;

destructor TFrontEngine.Destroy;
begin
  m_LoadRcdList.Free;
  m_SaveRcdList.Free;
  m_ChangeGoldList.Free;
  m_LoadRcdTempList.Free;
  m_SaveRcdTempList.Free;
  DeleteCriticalSection(m_UserCriticalSection);
  inherited;
end;

procedure TFrontEngine.Execute;
resourcestring
  sExceptionMsg = '[Exception] TFrontEngine::Execute';
begin
  while not Terminated do begin
    try
      Run();
    except
      MainOutMessage(sExceptionMsg);
    end;
    Sleep(1);
  end;
end;

procedure TFrontEngine.GetGameTime;
var
  Hour, Min, Sec, MSec: Word;
begin
  DecodeTime(Time, Hour, Min, Sec, MSec);
  case Hour of
    5, 6, 7, 8, 9, 10, 16, 17, 18, 19, 20, 21, 22: g_nGameTime := 1;
    11, 23: g_nGameTime := 2;
    4, 15: g_nGameTime := 0;
    0, 1, 2, 3, 12, 13, 14: g_nGameTime := 3;
  end;
end;

function TFrontEngine.IsIdle: Boolean;
begin
  Result := False;
  EnterCriticalSection(m_UserCriticalSection);
  try
    if m_SaveRcdList.Count = 0 then Result := True;
  finally
    LeaveCriticalSection(m_UserCriticalSection);
  end;
end;

function TFrontEngine.SaveListCount: Integer;
begin
  Result := 0;
  EnterCriticalSection(m_UserCriticalSection);
  try
    Result := m_SaveRcdList.Count;
  finally
    LeaveCriticalSection(m_UserCriticalSection);
  end;
end;

procedure TFrontEngine.ProcessGameDate;
var
  I: Integer;
  II: Integer;
  TempList: TList;
  ChangeGoldList: TList;
  LoadDBInfo, LoadDB: pTLoadDBInfo;
  SaveRcd: pTSaveRcd;
  GoldChangeInfo: pTGoldChangeInfo;
  boReTryLoadDB: Boolean;
  boSaveRcd: Boolean;
  nCode: Integer;
resourcestring
  sExceptionMsg = '[Exception] TFrontEngine::ProcessGameDate Code:%d';
  sSaveExceptionMsg = '数据库服务器出现异常,请重新启动数据库服务器!!!';
begin
  try
    nCode := 0;
    ChangeGoldList := nil;
    if g_nSaveRcdErrorCount >= 10 then begin
      if GetTickCount - g_dwShowSaveRcdErrorTick > 1000 then begin
        g_dwShowSaveRcdErrorTick := GetTickCount;
        MainOutMessage(sSaveExceptionMsg);
      end;
    end;
    EnterCriticalSection(m_UserCriticalSection);
    try
      for I := 0 to m_SaveRcdList.Count - 1 do begin
        m_SaveRcdTempList.Add(m_SaveRcdList.Items[I]);
      end;
      nCode := 1;
      TempList := m_LoadRcdTempList;
      nCode := 2;
      m_LoadRcdTempList := m_LoadRcdList;
      nCode := 3;
      m_LoadRcdList := TempList;
      nCode := 4;
      if m_ChangeGoldList.Count > 0 then begin
        ChangeGoldList := TList.Create;
        for I := 0 to m_ChangeGoldList.Count - 1 do begin
          ChangeGoldList.Add(m_ChangeGoldList.Items[I]);
        end;
      end;
      m_ChangeGoldList.Clear;
    finally
      LeaveCriticalSection(m_UserCriticalSection);
    end;

    for I := 0 to m_SaveRcdTempList.Count - 1 do begin
      SaveRcd := m_SaveRcdTempList.Items[I];
      if (not DBSocketConnected) or (g_nSaveRcdErrorCount >= 10) then begin //DBS关闭 不保存
        if (SaveRcd.PlayObject <> nil) and (not SaveRcd.boIsHero) then begin
          TPlayObject(SaveRcd.PlayObject).m_boRcdSaved := True;
        end;
        EnterCriticalSection(m_UserCriticalSection);
        try
          for II := m_SaveRcdList.Count - 1 downto 0 do begin
            if m_SaveRcdList.Items[II] = SaveRcd then begin
              m_SaveRcdList.Delete(II);
              nCode := 5;
              DisPoseAndNil(SaveRcd);
              nCode := 6;
              Break;
            end;
          end;
        finally
          LeaveCriticalSection(m_UserCriticalSection);
        end;
      end else begin
        boSaveRcd := False;
        if SaveRcd.nReTryCount = 0 then begin
          boSaveRcd := True;
        end else
          if (SaveRcd.nReTryCount < 50) and (GetTickCount - SaveRcd.dwSaveTick > 5000) then begin //保存错误等待5秒后在保存
          boSaveRcd := True;
        end else
          if SaveRcd.nReTryCount >= 50 then begin //失败50次后不在保存
          if (SaveRcd.PlayObject <> nil) and (not SaveRcd.boIsHero) then begin
            TPlayObject(SaveRcd.PlayObject).m_boRcdSaved := True;
          end;
          EnterCriticalSection(m_UserCriticalSection);
          try
            for II := m_SaveRcdList.Count - 1 downto 0 do begin
              if m_SaveRcdList.Items[II] = SaveRcd then begin
                m_SaveRcdList.Delete(II);
                nCode := 7;
                DisPoseAndNil(SaveRcd);
                nCode := 8;
                Break;
              end;
            end;
          finally
            LeaveCriticalSection(m_UserCriticalSection);
          end;
        end;

        if boSaveRcd then begin
          if SaveHumRcdToDB(SaveRcd.sAccount, SaveRcd.sChrName, SaveRcd.nSessionID, SaveRcd.boIsHero, SaveRcd.HumanRcd) then begin
            if (SaveRcd.PlayObject <> nil) and (not SaveRcd.boIsHero) then begin
              TPlayObject(SaveRcd.PlayObject).m_boRcdSaved := True;
            end;
            EnterCriticalSection(m_UserCriticalSection);
            try
              for II := m_SaveRcdList.Count - 1 downto 0 do begin
                if m_SaveRcdList.Items[II] = SaveRcd then begin
                  m_SaveRcdList.Delete(II);
                  nCode := 9;
                  DisPoseAndNil(SaveRcd);
                  nCode := 10;
                  Break;
                end;
              end;
            finally
              LeaveCriticalSection(m_UserCriticalSection);
            end;
          end else begin //保存失败
            Inc(SaveRcd.nReTryCount);
            SaveRcd.dwSaveTick := GetTickCount;
          end;
        end;
      end;
    end;
    m_SaveRcdTempList.Clear;
    nCode := 11;
    for I := 0 to m_LoadRcdTempList.Count - 1 do begin
      LoadDBInfo := m_LoadRcdTempList.Items[I];
      if (not LoadHumFromDB(LoadDBInfo, boReTryLoadDB)) and (not LoadDBInfo.boIsHero) then
        RunSocket.CloseUser(LoadDBInfo.nGateIdx, LoadDBInfo.nSocket);
      if not boReTryLoadDB then begin
        DisPoseAndNil(LoadDBInfo);
      end else begin //如果读取人物数据失败(数据还没有保存),则重新加入队列
        EnterCriticalSection(m_UserCriticalSection);
        try
          m_LoadRcdList.Add(LoadDBInfo);
        finally
          LeaveCriticalSection(m_UserCriticalSection);
        end;
      end;
    end;
    m_LoadRcdTempList.Clear;
    nCode := 12;
    if ChangeGoldList <> nil then begin
      for I := 0 to ChangeGoldList.Count - 1 do begin
        GoldChangeInfo := ChangeGoldList.Items[I];
        ChangeUserGoldInDB(GoldChangeInfo);
        nCode := 13;
        Dispose(GoldChangeInfo);
        nCode := 14;
      end;
      nCode := 15;
      ChangeGoldList.Free;

⌨️ 快捷键说明

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