section.begin
   Name = OnLanEventSession
   Code : struct.begin
      [*] = ;const cAllowDebug = False;
      [*] = ;
      [*] = ;procedure LanGameReady();
      [*] = ;begin
      [*] = ;   var idFrom : Integer = GetIntValueByName('Tag');
      [*] = ;   if (idFrom<=0) then ErrorLog('OnLanEventSession : idFrom<=0');
      [*] = ;   var i, loaded : Integer;
      [*] = ;   for i:=0 to gc_MaxPlayerCount-1 do
      [*] = ;   begin
      [*] = ;      if (gMap.players[i].lanid<>0) then
      [*] = ;      begin
      [*] = ;         if (gMap.players[i].lanid=idFrom) then
      [*] = ;         gMap.players[i].bloaded := True;
      [*] = ;         if (gMap.players[i].lanid=LanMyInfoID) then
      [*] = ;         gMap.players[i].bloaded := True;
      [*] = ;         if (gMap.players[i].bloaded) then
      [*] = ;         loaded := loaded+1;
      [*] = ;         ExecuteState('ShowPause');
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;end;
      [*] = ;
      [*] = ;//------------------------------------------------------------------------------
      [*] = ;if (cAllowDebug) and ((gLanEvent<>leShellMessage) or (LanPublicServerGetRegIDFrom<>LanPublicServerGetRegIDTo)) then
      [*] = ;begin
      [*] = ;   Log('Session:gLanEvent='+IntToStr(gLanEvent)+' RegIDFrom = '+IntToStr(LanPublicServerGetRegIDFrom)+' RegIDTo = '+ IntToStr(LanPublicServerGetRegIDTo)+' tag='+IntToStr(GetIntValueByName('Tag'))+' index='+IntToStr(LanGetClientIndexByID(GetIntValueByName('Tag'))));
      [*] = ;end;
      [*] = ;
      [*] = ;gfloat_gui_lanlasteventreceived := _misc_GetRealTime;
      [*] = ;
      [*] = ;//------------------------------------------------------------------------------
      [*] = ;case gLanEvent of
      [*] = ;   leSessionParser : begin
      [*] = ;      if (GetLanMode<>0) then
      [*] = ;      begin
      [*] = ;         //Log('leSessionParser, RegIDFrom='+IntToStr(LanPublicServerGetRegIDFrom)+' : tag='+IntToStr(GetIntValueByName('Tag')));
      [*] = ;         var idFrom : Integer = LanPublicServerGetRegIDFrom;
      [*] = ;         if (idFrom<>LanMyInfoID) then
      [*] = ;         begin
      [*] = ;            var pLan : Integer = LanSelectParser;
      [*] = ;            case (LanGetParserID) of
      [*] = ;               gc_LAN_GAME_SYNC_REQUEST : begin
      [*] = ;                  if (_net_IsServer) then
      [*] = ;                  _misc_ReadSyncServer(idFrom, pLan);
      [*] = ;               end;
      [*] = ;               gc_LAN_GAME_SYNC_DATA : begin
      [*] = ;                  if (_net_IsClient) then
      [*] = ;                  _misc_ReadSyncClient(pLan);
      [*] = ;               end;
      [*] = ;            end;
      [*] = ;         end;
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;   leParser : begin
      [*] = ;      if (GetLanMode<>0) then
      [*] = ;      begin
      [*] = ;         //Log('leParser, RegIDFrom='+IntToStr(LanPublicServerGetRegIDFrom)+' : tag='+IntToStr(GetIntValueByName('Tag')));
      [*] = ;         var idFrom : Integer = LanPublicServerGetRegIDFrom;
      [*] = ;         if (idFrom<>LanMyInfoID) then
      [*] = ;         begin
      [*] = ;            var pLan : Integer = LanSelectParser;
      [*] = ;            if (cAllowDebug) then
      [*] = ;            Log('Session:LanGetParserID = '+IntToStr(LanGetParserID));
      [*] = ;            case (LanGetParserID) of
      [*] = ;               gc_LAN_GAME_SURRENDER : begin
      [*] = ;                  if (cAllowDebug) then
      [*] = ;                  Log('Session:Receaved:gc_LAN_GAME_SURRENDER : idFrom='+IntToStr(idFrom)+' IsServer='+BoolToStr(_net_IsServer));
      [*] = ;                  if (_net_IsServer) then
      [*] = ;                  begin
      [*] = ;                     var pParser : Integer = _parser_ParserTemporary(True);
      [*] = ;                     var indFrom : Integer = _misc_GetMapPlayerIndexByLanID(idFrom);
      [*] = ;                     if (indFrom>=0) then
      [*] = ;                     begin
      [*] = ;                        gMap.players[indFrom].bleave := True;
      [*] = ;                        gPlayer[indFrom].victorystate := gc_player_victorystate_lose;
      [*] = ;                        if (gPlayer[indFrom].victorystategametime=0) then
      [*] = ;                        gPlayer[indFrom].victorystategametime := GetGameTime;
      [*] = ;                        _misc_LanServerSendResults;
      [*] = ;                        var s1, s2 : String;
      [*] = ;                        _misc_GetNewTextLocale('player.leave', s1);
      [*] = ;                        s1 := StrReplace(s1, '%name%', gMap.players[indFrom].name);
      [*] = ;                        _misc_ShowLogMessage(gc_logmessagesid_custom, s1, s2, False);
      [*] = ;                     end;
      [*] = ;                     ParserSetIntValueByKeyByHandle(pParser, 'id', idFrom);
      [*] = ;                     ParserSetIntValueByKeyByHandle(pParser, 'ind', indFrom);
      [*] = ;                     LanSendParser(gc_LAN_GAME_SURRENDER_CONFIRM, pParser);
      [*] = ;                  end;
      [*] = ;               end;
      [*] = ;               gc_LAN_GAME_SURRENDER_CONFIRM : begin
      [*] = ;                  if (cAllowDebug) then
      [*] = ;                  Log('Session:Receaved:gc_LAN_GAME_SURRENDER_CONFIRM : idFrom='+IntToStr(idFrom)+' IsServer='+BoolToStr(_net_IsServer));
      [*] = ;                  if (_net_IsClient) then
      [*] = ;                  begin
      [*] = ;                     var id : Integer = ParserGetIntValueByKeyByHandle(pLan, 'id');
      [*] = ;                     var ind : Integer = ParserGetIntValueByKeyByHandle(pLan, 'ind');
      [*] = ;                     if (ind>=0) and (ind<gc_MaxPlayerCount) then
      [*] = ;                     begin
      [*] = ;                        gMap.players[ind].bleave := True;
      [*] = ;                        gPlayer[ind].victorystate := gc_player_victorystate_lose;
      [*] = ;                        if (gPlayer[ind].victorystategametime=0) then
      [*] = ;                        gPlayer[ind].victorystategametime := GetGameTime;
      [*] = ;                        var s1, s2 : String;
      [*] = ;                        _misc_GetNewTextLocale('player.leave', s1);
      [*] = ;                        s1 := StrReplace(s1, '%name%', gMap.players[ind].name);
      [*] = ;                        _misc_ShowLogMessage(gc_logmessagesid_custom, s1, s2, False);
      [*] = ;                     end
      [*] = ;                     else
      [*] = ;                     ErrorLog('OnLanEvent:gc_LAN_GAME_SURRENDER_CONFIRM:out of bounds');
      [*] = ;                     // test valid
      [*] = ;                     var indtmp : Integer = _misc_GetMapPlayerIndexByLanID(id);
      [*] = ;                     if (indtmp<>ind) then
      [*] = ;                     ErrorLog('indtmp='+IntToStr(indtmp)+'<>ind='+IntToStr(ind));
      [*] = ;                  end;
      [*] = ;               end;
      [*] = ;               gc_LAN_GAME_SERVER_LEAVE : begin
      [*] = ;                  if (cAllowDebug) then
      [*] = ;                  Log('Session:Receaved:gc_LAN_GAME_SERVER_LEAVE : idFrom='+IntToStr(idFrom)+' IsServer='+BoolToStr(_net_IsServer));
      [*] = ;                  if (_net_IsClient) then
      [*] = ;                  begin
      [*] = ;                     var indFrom : Integer = _misc_GetMapPlayerIndexByLanID(idFrom);
      [*] = ;                     if (indFrom>=0) then
      [*] = ;                     begin
      [*] = ;                        gPlayer[indFrom].victorystate := gc_player_victorystate_lose;
      [*] = ;                        if (gPlayer[indFrom].victorystategametime=0) then
      [*] = ;                        gPlayer[indFrom].victorystategametime := GetGameTime;
      [*] = ;                        gMap.players[indFrom].bleave := True;
      [*] = ;                        var s1, s2 : String;
      [*] = ;                        _misc_GetNewTextLocale('host.leave.continue', s1);
      [*] = ;                        s1 := StrReplace(s1, '%name%', gMap.players[indFrom].name);
      [*] = ;                        _misc_ShowLogMessage(gc_logmessagesid_custom, s1, s2, False);
      [*] = ;                     end;
      [*] = ;                  end;
      [*] = ;               end;
      [*] = ;               gc_LAN_GAME_SESSION_RESULTS : begin
      [*] = ;                  if (cAllowDebug) then
      [*] = ;                  Log('Session:Receaved:gc_LAN_GAME_SESSION_RESULTS : idFrom='+IntToStr(idFrom)+' IsServer='+BoolToStr(_net_IsServer));
      [*] = ;                  if (_net_IsClient) then
      [*] = ;                  begin
      [*] = ;                     var i : Integer;
      [*] = ;                     for i:=0 to ParserGetCountByHandle(pLan)-1 do
      [*] = ;                     begin
      [*] = ;                        var pLanResult : Integer = ParserSelectByHandleByIndex(pLan, i);
      [*] = ;                        if (pLanResult<>0) then
      [*] = ;                        begin
      [*] = ;                           var id : Integer = ParserGetIntValueByKeyByHandle(pLanResult, 'id');
      [*] = ;                           var ind : Integer = ParserGetIntValueByKeyByHandle(pLanResult, 'ind');
      [*] = ;                           var res : Integer = ParserGetIntValueByKeyByHandle(pLanResult, 'res');
      [*] = ;                           if {(id>0) and} (ind>=0) and (ind<gc_MaxPlayerCount) then
      [*] = ;                           begin
      [*] = ;                              if (res<>gPlayer[ind].victorystate) then
      [*] = ;                              begin
      [*] = ;                                 gPlayer[ind].victorystate := res;
      [*] = ;                                 if (gPlayer[ind].victorystategametime=0) then
      [*] = ;                                 gPlayer[ind].victorystategametime := GetGameTime;
      [*] = ;                              end;
      [*] = ;                           end;
      [*] = ;                        end;
      [*] = ;                     end;
      [*] = ;                  end;
      [*] = ;               end;
      [*] = ;               gc_LAN_GAME_ANSWER_READY : begin
      [*] = ;                  if (cAllowDebug) then
      [*] = ;                  Log('Session:Receaved:gc_LAN_GAME_ANSWER_READY : idFrom='+IntToStr(idFrom)+' IsServer='+BoolToStr(_net_IsServer));
      [*] = ;                  LanGameReady;
      [*] = ;                  if (_net_IsClient) then
      [*] = ;                  begin
      [*] = ;                     var i : Integer;
      [*] = ;                     for i:=0 to gc_MaxPlayerCount-1 do
      [*] = ;                     gMap.players[i].bloaded := ParserGetBoolValueByKeyByHandle(pLan, IntToStr(i));
      [*] = ;                     ExecuteState('ShowPause');
      [*] = ;                  end;
      [*] = ;               end;
      [*] = ;               gc_LAN_GAME_READY : begin // visualization update
      [*] = ;                  if (cAllowDebug) then
      [*] = ;                  Log('Session:Receaved:gc_LAN_GAME_READY : idFrom='+IntToStr(idFrom)+' IsServer='+BoolToStr(_net_IsServer));
      [*] = ;                  LanGameReady;
      [*] = ;                  var pParser : Integer = _parser_ParserTemporary(True);
      [*] = ;                  var i : Integer;
      [*] = ;                  for i:=0 to gc_MaxPlayerCount-1 do
      [*] = ;                  ParserSetBoolValueByKeyByHandle(pParser, IntToStr(i), gMap.players[i].bloaded);
      [*] = ;                  LanSendParser(gc_LAN_GAME_ANSWER_READY, pParser);
      [*] = ;                  if (cAllowDebug) then
      [*] = ;                  Log('Session:LanSendParser:gc_LAN_GAME_ANSWER_READY');
      [*] = ;               end;
      [*] = ;               gc_LAN_GAME_START : begin
      [*] = ;               end;
      [*] = ;               {gc_LAN_GAME_SYNC_ALIVE : begin
      [*] = ;                  if (_net_IsServer) then
      [*] = ;                  begin
      [*] = ;                     if (GetGameTime>1) then // game actually started
      [*] = ;                     begin
      [*] = ;                        var lanid : Integer = ParserGetIntValueByKeyByHandle(pLan, 'a');
      [*] = ;                        var ind : Integer = _misc_GetMapPlayerIndexByLanID(lanid);
      [*] = ;                        if (ind>=0) and (ind<gc_MaxPlayerCount-1) then
      [*] = ;                        gfloat_gui_lanlastaliveeventreceived[ind] := _misc_GetRealTime;
      [*] = ;                     end;
      [*] = ;                  end;
      [*] = ;               end;}
      [*] = ;               gc_LAN_GAME_SYNC_GAMETIME : begin
      [*] = ;                  if (_net_IsClient) then
      [*] = ;                  begin
      [*] = ;                     if (GetGameTime>1) then // game actually started
      [*] = ;                     begin
      [*] = ;                        var gametime : Float = ParserGetFloatValueByKeyByHandle(pLan, 't');
      [*] = ;                        var gamespeed : Float = ParserGetFloatValueByKeyByHandle(pLan, 's');
      [*] = ;                        gfloat_gui_lanlastgamespeedsyncreceived := gamespeed;
      [*] = ;
      [*] = ;                        if (gfloat_gui_lanlastgametimesyncreceived=0) then // first time just sync time
      [*] = ;                        begin
      [*] = ;                           SetGameTime(gametime);
      [*] = ;                           SetTimeSpeedFactor(gamespeed);
      [*] = ;                        end
      [*] = ;                        else
      [*] = ;                        begin
      [*] = ;                           const constt = 5;
      [*] = ;                           var deltat : Float = gametime-GetGameTime;
      [*] = ;                           var deltas : Float = gamespeed*deltat;
      [*] = ;                           var delta : Float = (deltas-gamespeed*constt)/constt;
      [*] = ;                           var desirablespeed : Float = gamespeed+delta;
      [*] = ;                           var percent : Float = 1+desirablespeed/gamespeed;
      [*] = ;                           if (percent>0.975) and (percent<1.025) then
      [*] = ;                           percent := 1;
      [*] = ;                           const debug = False;
      [*] = ;                           var newspeed : Float = gamespeed*Clamp(percent, 0.875, 1.125);
      [*] = ;                           if (newspeed<>GetTimeSpeedFactor) then
      [*] = ;                           begin
      [*] = ;                              if debug then
      [*] = ;                              TimeLog('deltat='+FloatToStr(deltat)+' gamespeed='+FloatToStr(gamespeed)+' newspeed='+FloatToStr(newspeed));
      [*] = ;                              SetTimeSpeedFactor(newspeed);
      [*] = ;                           end
      [*] = ;                           else
      [*] = ;                           if debug then
      [*] = ;                           TimeLog('deltat='+FloatToStr(deltat)+' gamespeed='+FloatToStr(gamespeed)+' newspeed='+FloatToStr(newspeed));
      [*] = ;                        end;
      [*] = ;
      [*] = ;                        gfloat_gui_lanlastgametimesyncreceived := _misc_GetRealTime;
      [*] = ;                     end;
      [*] = ;                     //SetGameTime(gametime);
      [*] = ;                  end;
      [*] = ;               end;
      [*] = ;               gc_LAN_ADVISER_CLIENT_DATACHANGE : begin
      [*] = ;                  if (_net_IsServer) then
      [*] = ;                  begin
      [*] = ;                     //ParserSaveToFileByHandle(pLan, 'pLan');
      [*] = ;                     var pl : Integer = ParserGetIntValueByKeyByHandle(pLan, 'pl');
      [*] = ;                     //gPlayer[pl].playeradviser.adviser.bvisible := ParserGetBoolValueByKeyByHandle(pLan, 'v');
      [*] = ;                     gPlayer[pl].playeradviser.adviser.benable := ParserGetBoolValueByKeyByHandle(pLan, 'ea');
      [*] = ;                     //gPlayer[pl].playeradviser.adviser.bavailable := ParserGetBoolValueByKeyByHandle(pLan, 'a');
      [*] = ;                     //gPlayer[pl].playeradviser.adviser.bconstruct := ParserGetBoolValueByKeyByHandle(pLan, 'c');
      [*] = ;                     gPlayer[pl].playeradviser.adviser.beconomy := ParserGetBoolValueByKeyByHandle(pLan, 'ec');
      [*] = ;                     gPlayer[pl].playeradviser.adviser.bcontrolpeasants := ParserGetBoolValueByKeyByHandle(pLan, 'cp');
      [*] = ;                     gPlayer[pl].playeradviser.adviser.bproducepeasants := ParserGetBoolValueByKeyByHandle(pLan, 'pp');
      [*] = ;                     gPlayer[pl].playeradviser.adviser.brepairbuildings := ParserGetBoolValueByKeyByHandle(pLan, 'rb');
      [*] = ;                     gPlayer[pl].playeradviser.adviser.bfillmines := ParserGetBoolValueByKeyByHandle(pLan, 'fm');
      [*] = ;                     gPlayer[pl].playeradviser.adviser.bmarketadvises := ParserGetBoolValueByKeyByHandle(pLan, 'ma');
      [*] = ;                     //gPlayer[pl].playeradviser.adviser.bmanualresource := ParserGetBoolValueByKeyByHandle(pLan, 'mr');
      [*] = ;                     //gPlayer[pl].playeradviser.adviser.resonfood := ParserGetFloatValueByKeyByHandle(pLan, 'rf');
      [*] = ;                     //gPlayer[pl].playeradviser.adviser.resonwood := ParserGetFloatValueByKeyByHandle(pLan, 'rw');
      [*] = ;                     //gPlayer[pl].playeradviser.adviser.resonstone := ParserGetFloatValueByKeyByHandle(pLan, 'rs');
      [*] = ;                     //gPlayer[pl].playeradviser.adviser.bupgrades := ParserGetBoolValueByKeyByHandle(pLan, 'u');
      [*] = ;                     //gPlayer[pl].playeradviser.adviser.bupgradesunavailable := ParserGetBoolValueByKeyByHandle(pLan, 'uu');
      [*] = ;                     gPlayer[pl].playeradviser.adviser.bquartermaster := ParserGetBoolValueByKeyByHandle(pLan, 'q');
      [*] = ;                     gPlayer[pl].playeradviser.adviser.bsetupguards := ParserGetBoolValueByKeyByHandle(pLan, 'sg');
      [*] = ;                     gPlayer[pl].playeradviser.adviser.bproduceofficers := ParserGetBoolValueByKeyByHandle(pLan, 'po');
      [*] = ;                     //gPlayer[pl].playeradviser.adviser.bshowsquads := ParserGetBoolValueByKeyByHandle(pLan, 'ss');
      [*] = ;                  end;
      [*] = ;               end;
      [*] = ;               else
      [*] = ;               ErrorLog('Session:leParser unknown ID = '+IntToStr(LanGetParserID));
      [*] = ;            end;
      [*] = ;         end;
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;   leDisconnect : begin
      [*] = ;   end;
      [*] = ;   leShutdown : begin
      [*] = ;      ErrorLog('Session:leShutDown');
      [*] = ;      if (gInterface.gamemode=gc_gamemode_game) or (gInterface.gamemode=gc_gamemode_spectator) then
      [*] = ;      begin
      [*] = ;         var plIOInd : Integer = GetPlayerIndexByHandle(GetPlayerHandleInterfaceIO);
      [*] = ;         if (gPlayer[plIOInd].victorystate=gc_player_victorystate_none) then
      [*] = ;         _gui_ShowConnectStateMessage(gc_ConnectState_Disconnect);
      [*] = ;      end;
      [*] = ;      //gInternetShell.bauth := False; // after bad game log off public server
      [*] = ;
      [*] = ;      SetGUIEventStateOnLanChange('OnLanEvent');
      [*] = ;   end;
      [*] = ;   leTerminated : begin
      [*] = ;      ErrorLog('Session:leTerminated');
      [*] = ;
      [*] = ;      //_misc_RepairSession(True, True, True, True);
      [*] = ;      //gLanHostID := 0; // needed to support reconnect
      [*] = ;   end;
      [*] = ;   leReady : begin
      [*] = ;      if (cAllowDebug) then
      [*] = ;      Log('Session:leReady'); // do everything on DoProgress if IsLanReady once
      [*] = ;   end;
      [*] = ;   leShellMessage, leShellSessionMessage : begin
      [*] = ;      if (cAllowDebug) then
      [*] = ;      begin
      [*] = ;         if (gLanEvent=leShellMessage) then
      [*] = ;         Log('Session:leShellMessage')
      [*] = ;         else
      [*] = ;         Log('Session:leShellSessionMessage');
      [*] = ;      end;
      [*] = ;      var idFrom : Integer = LanPublicServerGetRegIDFrom;
      [*] = ;      var idTo : Integer = LanPublicServerGetRegIDTo;
      [*] = ;      var msg : String = LanPublicServerGetRegMessage;
      [*] = ;      if {(idFrom<>LanMyInfoID) and} (msg<>'') then
      [*] = ;      begin
      [*] = ;         if IsDelimiterCharExists(msg, gc_gui_delimiterchar) and (GetDelimiterStringCount(msg, gc_gui_delimiterchar, gc_gui_quotechar)>1) then
      [*] = ;         begin
      [*] = ;            var pos : Integer = StrPos(gc_gui_delimitercharstr, msg);
      [*] = ;            var chatmessagemode : Integer = StrToInt(GetDelimiterStringByIndex(msg, gc_gui_delimiterchar, gc_gui_quotechar, 0));
      [*] = ;            var bcanshow : Boolean;
      [*] = ;            if (chatmessagemode<>gc_chatmessage_mode_ally) then
      [*] = ;            bcanshow := True
      [*] = ;            else
      [*] = ;            begin
      [*] = ;               var indMy : Integer = _misc_GetMapPlayerIndexByLanID(LanMyInfoID);
      [*] = ;               var indFrom : Integer = _misc_GetMapPlayerIndexByLanID(idFrom);
      [*] = ;               if (indMy>=0) and (indFrom>=0) then
      [*] = ;               bcanshow := (gMap.players[indMy].team<>0) and (gMap.players[indFrom].team<>0) and (gMap.players[indMy].team=gMap.players[indFrom].team);
      [*] = ;            end;
      [*] = ;            if (bcanshow) then
      [*] = ;            begin
      [*] = ;               msg := SubStr(msg, pos+1, StrLength(msg)-pos);
      [*] = ;               var sTo, sFrom : String;
      [*] = ;               if (idTo<>0) then
      [*] = ;               begin
      [*] = ;                  var pclient : Pointer = gInternetShell.clients.GetByLanID(idTo);
      [*] = ;                  if (pclient<>nil) then
      [*] = ;                  sTo := TInternetShellClient(pclient).nick;
      [*] = ;               end;
      [*] = ;               if (idFrom<>0) then
      [*] = ;               begin
      [*] = ;                  var pclient : Pointer = gInternetShell.clients.GetByLanID(idFrom);
      [*] = ;                  if (pclient<>nil) then
      [*] = ;                  sFrom := TInternetShellClient(pclient).nick;
      [*] = ;               end;
      [*] = ;               gChatMessages.Add(chatmessagemode, sFrom, sTo, msg);
      [*] = ;            end;
      [*] = ;         end
      [*] = ;         else
      [*] = ;         ErrorLog('Session:Message, no delimiter. msg='+msg);
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;   // ignore events
      [*] = ;   leShellClientDisconnected : begin
      [*] = ;      if (cAllowDebug) then
      [*] = ;      Log('Session:leDisconnect');
      [*] = ;      var id : Integer = LanPublicServerGetRegIDFrom;
      [*] = ;      if id <> LanMyInfoID then
      [*] = ;      begin
      [*] = ;         var ind : Integer = _misc_GetMapPlayerIndexByLanID(id);
      [*] = ;         if (ind>=0) and (not gMap.players[ind].bleave) then
      [*] = ;         begin
      [*] = ;            var s1, s2 : String;
      [*] = ;            if (ind=0) then
      [*] = ;            _misc_GetNewTextLocale('host.disconnected.continue', s1)
      [*] = ;            else
      [*] = ;            _misc_GetNewTextLocale('player.disconnected', s1);
      [*] = ;            s1 := StrReplace(s1, '%name%', gMap.players[ind].name);
      [*] = ;            _misc_ShowLogMessage(gc_logmessagesid_custom, s1, s2, False);
      [*] = ;
      [*] = ;            //if (_net_IsServer) then // 17.12.27 experiment to fix, nick is not greyed after host disconnect on client pcs
      [*] = ;            gMap.players[ind].bleave := True;
      [*] = ;            gPlayer[ind].victorystate := gc_player_victorystate_lose;
      [*] = ;            if (gPlayer[ind].victorystategametime=0) then
      [*] = ;            gPlayer[ind].victorystategametime := GetGameTime;
      [*] = ;         end;
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;   leConnect : begin
      [*] = ;      if (gLanRecreate.brecreate) then
      [*] = ;      begin
      [*] = ;         var idTo : Integer = LanPublicServerGetRegIDTo;
      [*] = ;         var idFrom : Integer = LanPublicServerGetRegIDFrom;
      [*] = ;         var tag : Integer = GetIntValueByName('Tag');
      [*] = ;         Log('Session:leConnect idTo='+IntToStr(idTo)+' idFrom='+IntToStr(idFrom)+' myID='+IntToStr(LanMyInfoID)+' tag='+IntToStr(tag)+' clientscount='+IntToStr(LanGetClientsCount));
      [*] = ;
      [*] = ;         // dublicate same command after Create and Join. check if we need them there.
      [*] = ;         if (tag=LanMyInfoID) then
      [*] = ;         begin
      [*] = ;            var team : Integer = _misc_GetClLanTeamByLanID(LanMyInfoID);
      [*] = ;            LanClSetMyTeam(team);
      [*] = ;            Log('Session : OnConnect (ReHost) : LanClSetMyTeam('+IntToStr(team)+')');
      [*] = ;         end;
      [*] = ;
      [*] = ;         if (_net_IsServer) then
      [*] = ;         begin
      [*] = ;            Log('Session:leConnect : gLanHostID := '+IntToStr(gLanHostID)+' LanMyInfoID='+IntToStr(LanMyInfoID));
      [*] = ;            if (LanGetClientsCount>=gLanRecreate.clientcount) then
      [*] = ;            begin
      [*] = ;               LanLockServer;
      [*] = ;               LanDoStart;
      [*] = ;            end;
      [*] = ;         end;
      [*] = ;      end
      [*] = ;      else
      [*] = ;      Log('Session:leConnect : gLanRecreate.brecreate=False : nothing happens in this ca.se');
      [*] = ;   end;
      [*] = ;   leShellSessionCreate : begin
      [*] = ;      if (gLanRecreate.brecreate) then
      [*] = ;      begin
      [*] = ;         var idFrom : Integer = LanPublicServerGetRegIDFrom;
      [*] = ;         if (idFrom=LanMyInfoID) then
      [*] = ;         begin
      [*] = ;            gLanHostID := LanMyInfoID;
      [*] = ;            gLanRecreate.createdtime := _misc_GetRealTime;
      [*] = ;         end
      [*] = ;         else
      [*] = ;         if (idFrom=gLanRecreate.hostid) then
      [*] = ;         begin
      [*] = ;            Log('Session:leShellSessionCreate idFrom=gLanRecreate.hostid='+IntToStr(idFrom));
      [*] = ;            gLanHostID := gLanRecreate.hostid;
      [*] = ;            LanJoinGame('', IntToStr(gLanRecreate.hostid), False);
      [*] = ;            var team : Integer = _misc_GetClLanTeamByLanID(LanMyInfoID);
      [*] = ;            LanClSetMyTeam(team);
      [*] = ;            Log('Session : leShellSessionCreate (ReHost) : LanClSetMyTeam('+IntToStr(team)+')');
      [*] = ;         end;
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;   leShellSessionLock : begin
      [*] = ;      if (cAllowDebug) then
      [*] = ;      Log('Session:leShellSessionLock');
      [*] = ;
      [*] = ;      LanDoStart;
      [*] = ;      gConnectTimer := _misc_GetRealTime;
      [*] = ;   end;
      [*] = ;   leShellClientConnected, leShellClientUpdateInfo, leShellClientInfo : ;
      [*] = ;   leShellSessionJoin, leShellSessionInfo : ;
      [*] = ;   leShellSessionClose, leShellSessionKick, leShellSessionLeave : ;
      [*] = ;   leShellSessionLeave : begin
      [*] = ;      if (cAllowDebug) then
      [*] = ;      Log('Session:leShellSessionLeave');
      [*] = ;
      [*] = ;      var id : Integer = LanPublicServerGetRegIDFrom;
      [*] = ;      if id <> LanMyInfoID then
      [*] = ;      begin
      [*] = ;         var ind : Integer = _misc_GetMapPlayerIndexByLanID(id);
      [*] = ;         if (ind>=0) then
      [*] = ;         begin
      [*] = ;            if (_net_IsServer) then
      [*] = ;            begin
      [*] = ;               gMap.players[ind].bleave := True;
      [*] = ;               gPlayer[ind].victorystate := gc_player_victorystate_lose;
      [*] = ;               if (gPlayer[ind].victorystategametime=0) then
      [*] = ;               gPlayer[ind].victorystategametime := GetGameTime;
      [*] = ;            end;
      [*] = ;         end;
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;   leGenerate : begin
      [*] = ;      if (cAllowDebug) then
      [*] = ;      Log('Session:leGenerate:LanDoReady');
      [*] = ;
      [*] = ;      //gbool_gui_landoreadysent := False;
      [*] = ;      if (_net_IsServer) then
      [*] = ;      _net_lanmode_last := gc_lanmode_server;
      [*] = ;      LanDoReady;
      [*] = ;   end;
      [*] = ;   leShellUpdateTopList : ;
      [*] = ;   leServerInfo : begin
      [*] = ;      if (cAllowDebug) then
      [*] = ;      Log('Session:leServerInfo');
      [*] = ;   end;
      [*] = ;   leError : begin
      [*] = ;      if (LanGetCustErrHandler) then
      [*] = ;      _misc_LanCustErrCodeHandler()
      [*] = ;      else
      [*] = ;      LanShutdown;
      [*] = ;      ErrorLog('Session:leError');
      [*] = ;   end;
      [*] = ;   leInvalidSynch : begin
      [*] = ;      var envplhnd : Integer = GetPlayerHandleByIndex(gc_playerind_env);
      [*] = ;      var envcount : Integer = GetPlayerGameObjectsCountByHandle(envplhnd);
      [*] = ;      ErrorLog('Session:leInvalidSynch : gLanUID = '+IntToStr(gLanUID)+' : GetMapNextUniqId = '+IntToStr(GetMapNextUniqId)+' : envcount = '+IntToStr(envcount));
      [*] = ;      _misc_WriteSyncClient(gLanUID, GetMapNextUniqId, envcount);
      [*] = ;      {if (gLanUID>0) then
      [*] = ;      begin
      [*] = ;         var parg: Pointer = StateMachineGetArgDataByInd(GetPlayerStateMachineHandle(GetPlayerHandleInterfaceIO), gc_argplayer_plr);
      [*] = ;         if parg<>nil then begin
      [*] = ;            TPlayerArgs(parg).fid:=gLanUID;
      [*] = ;            PlayerExecuteStateByHandle(GetPlayerHandleInterfaceIO, 'WriteSync');
      [*] = ;         end else _misc_ErrorLog('parg = nil');
      [*] = ;      end;}
      [*] = ;   end;
      [*] = ;   leSessionRecreate : begin
      [*] = ;      var bCustomParser : Boolean;
      [*] = ;      var pLan : Integer = _parser_ParserCustomSessionRehost(False);
      [*] = ;      if (pLan=0) or (ParserGetCountByHandle(pLan)=0) then
      [*] = ;      pLan := LanSelectParser
      [*] = ;      else
      [*] = ;      bCustomParser := True;
      [*] = ;
      [*] = ;      var indFrom : Integer = _misc_GetMapPlayerIndexByLanID(gLanHostID); // gLanHostID - prev host id
      [*] = ;      if (not bCustomParser) and (indFrom>=0) then
      [*] = ;      begin
      [*] = ;         gMap.players[indFrom].bleave := True;
      [*] = ;         gPlayer[indFrom].victorystate := gc_player_victorystate_lose;
      [*] = ;         if (gPlayer[indFrom].victorystategametime=0) then
      [*] = ;         gPlayer[indFrom].victorystategametime := GetGameTime;
      [*] = ;      end;
      [*] = ;
      [*] = ;      if (bCustomParser) or (not _misc_CheckEndGame) then
      [*] = ;      begin
      [*] = ;         gMap.gamestage := gc_map_gamestage_waitingloading;
      [*] = ;         SetProgressingPauseMode(True);
      [*] = ;         ExecuteState('ShowPause');
      [*] = ;
      [*] = ;         var gamename : String = ParserGetValueByKeyByHandle(pLan, 'gamename');
      [*] = ;         var mapname : String = ParserGetValueByKeyByHandle(pLan, 'mapname');
      [*] = ;         var master : Integer = ParserGetIntValueByKeyByHandle(pLan, 'master');
      [*] = ;         var clients : Integer = ParserGetIntValueByKeyByHandle(pLan, 'clients');
      [*] = ;         var sessionid : Integer = ParserGetIntValueByKeyByHandle(pLan, 'session');
      [*] = ;         //ParserSaveToFileByHandle(pLan, 'pLan');
      [*] = ;         Log('sessionid is = '+IntToStr(sessionid));
      [*] = ;         //if clients>1 then // not last host
      [*] = ;         begin
      [*] = ;            gLanRecreate.clientcount := clients;
      [*] = ;            gLanRecreate.brecreate := True;
      [*] = ;            gLanRecreate.brecreatestarted := True;
      [*] = ;            gLanRecreate.brecreatefinished := False;
      [*] = ;
      [*] = ;            _misc_RepairSession(True, True, True, True);
      [*] = ;
      [*] = ;            gLanRecreate.hostid := 0;
      [*] = ;            Log('leSessionRecreate clientcount='+IntToStr(clients));
      [*] = ;            if StrExists(gamename, 'qp=') then
      [*] = ;            begin
      [*] = ;               Log('leSessionRecreate game name was qp=...');
      [*] = ;               if StrExists(gamename, 'qp=') then
      [*] = ;               gamename := StrReplace(gamename, 'qp=', 'qq=')
      [*] = ;               else
      [*] = ;               gamename := StrReplace(gamename, 'qt=', 'qqt=');
      [*] = ;            end;
      [*] = ;            if (sessionid<>0) then
      [*] = ;            LanRecreateGame('', gamename, mapname, gc_MaxPlayerCount-1, sessionid)
      [*] = ;            else
      [*] = ;            LanCreateGame('', gamename, mapname, gc_MaxPlayerCount-1);
      [*] = ;
      [*] = ;            gfloat_gui_lanlastgametimesyncreceived := 0;
      [*] = ;
      [*] = ;            var team : Integer = _misc_GetClLanTeamByLanID(LanMyInfoID);
      [*] = ;            LanClSetMyTeam(team);
      [*] = ;            Log('Session : leSessionRecreate (ReHost) : LanClSetMyTeam('+IntToStr(team)+')');
      [*] = ;         end;
      [*] = ;      end
      [*] = ;      else
      [*] = ;      if gInterface.gamemode<>gc_gamemode_endgamestatistics then
      [*] = ;      begin
      [*] = ;         var plIOInd : Integer = GetPlayerIndexByHandle(GetPlayerHandleInterfaceIO);
      [*] = ;         if (gPlayer[plIOInd].victorystate=gc_player_victorystate_none) then
      [*] = ;         begin
      [*] = ;            gPlayer[plIOInd].victorystate := gc_player_victorystate_win;
      [*] = ;            if (gPlayer[plIOInd].victorystategametime=0) then
      [*] = ;            gPlayer[plIOInd].victorystategametime := GetGameTime;
      [*] = ;            ErrorLog('OnLanEventSession : victorystate=none');
      [*] = ;         end;
      [*] = ;         //ErrorLog('OnLanEventSession : gInterface.gamemode<>gc_gamemode_endgamestatistics');
      [*] = ;         //gInterface.gamemode := gc_gamemode_endgamestatistics;
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;   leSessionRejoin : begin
      [*] = ;      gMap.gamestage := gc_map_gamestage_waitingloading;
      [*] = ;      gfloat_gui_lanlastgametimesyncreceived := 0;
      [*] = ;      SetProgressingPauseMode(True);
      [*] = ;      ExecuteState('ShowPause');
      [*] = ;      var newHostID : Integer = LanPublicServerGetRegIDFrom;
      [*] = ;      gLanRecreate.hostid := newHostID;
      [*] = ;      gLanRecreate.brecreate := True;
      [*] = ;      gLanRecreate.brecreatestarted := True;
      [*] = ;      gLanRecreate.brecreatefinished := False;
      [*] = ;
      [*] = ;      _misc_RepairSession(True, True, True, False);
      [*] = ;
      [*] = ;      Log('leSessionRejoin newHostID='+IntToStr(newHostID));
      [*] = ;   end;
      [*] = ;   leClientInfo : Log('leClientInfo');
      [*] = ;   leLoaded : begin
      [*] = ;      if (_net_IsServer) then
      [*] = ;      begin
      [*] = ;         if (cAllowDebug) then
      [*] = ;         Log('Session:leLoaded IsServer lanID = '+IntToStr(gLanUID));
      [*] = ;      end
      [*] = ;      else
      [*] = ;      if (_net_IsClient) and (not gbool_gui_landoreadysent) then
      [*] = ;      begin
      [*] = ;         if (cAllowDebug) then
      [*] = ;         Log('Session:leLoaded IsClient lanID = '+IntToStr(gLanUID));
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;   else
      [*] = ;   begin
      [*] = ;      ErrorLog('Session:Unknown event! gLanEvent='+IntToStr(gLanEvent)+' RegIDFrom = '+IntToStr(LanPublicServerGetRegIDFrom)+' RegIDTo = '+ IntToStr(LanPublicServerGetRegIDTo)+' tag='+IntToStr(GetIntValueByName('Tag'))+' index='+IntToStr(LanGetClientIndexByID(GetIntValueByName('Tag'))));
      [*] = ;      if (gInterface.gamemode=gc_gamemode_mainmenu) then
      [*] = ;      begin
      [*] = ;         Log('Session:Unknown event forwarded to OnLanEvent');
      [*] = ;         ExecuteState('OnLanEvent');
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;end;
      [*] = ;
      [*] = ;if (not _net_IsOnline) then
      [*] = ;begin
      [*] = ;   Log('OnLanEventSession : IsOnline=False, switch to OnLanEvent');
      [*] = ;   SetGUIEventStateOnLanChange('OnLanEvent');
      [*] = ;end;
   struct.end
section.end

