//============================================================================
// control/client/misc/serverscreen.cs
//
// Server query code module for 3DGPAI1 Koob23 sample game
//
// Copyright (c) 2003, 2006 by Kenneth C.  Finney.
//============================================================================
function ServerScreen::onWake()
{
   JoinServer.SetActive(ServerList.rowCount() > 0);
   ServerScreen.queryLan();
}


function ServerScreen::QueryLan(%this)
{
   QueryLANServers(
      28000,      // lanPort for local queries
      0,          // Query flags
      $Client::GameTypeQuery,       // gameTypes
      $Client::MissionTypeQuery,    // missionType
      0,          // minPlayers
      100,        // maxPlayers
      0,          // maxBots
      2,          // regionMask
      0,          // maxPing
      100,        // minCPU
      0           // filterFlags
      );
}

function ServerScreen::Cancel(%this)
{
   CancelServerQuery();
}

function ServerScreen::Close(%this)
{
   CancelServerQuery();
   Canvas.SetContent(MenuScreen);
}

function ServerScreen::Update(%this)
{
   QueryStatus.SetVisible(false);
   ServerList.Clear();
   %sc = GetServerCount();
   for (%i = 0; %i < %sc; %i++)
   {
      SetServerInfo(%i);
      ServerList.AddRow(%i,
         ($ServerInfo::Password? "Yes": "No") TAB
         $ServerInfo::Name TAB
         $ServerInfo::Ping TAB
         $ServerInfo::PlayerCount @ "/" @ $ServerInfo::MaxPlayers TAB
         $ServerInfo::Version TAB
         $ServerInfo::GameType TAB
         %i);
   }
   ServerList.Sort(0);
   ServerList.SetSelectedRow(0);
   ServerList.ScrollVisible(0);
   JoinServer.SetActive(ServerList.RowCount() > 0);
}

function ServerScreen::Join(%this)
{
   CancelServerQuery();
   %id = ServerList.GetSelectedId();
   %index = GetField(ServerList.GetRowTextById(%id),6);
   if (SetServerInfo(%index)) {
      %conn = new GameConnection(ServerConnection);
      %conn.SetConnectArgs($pref::Player::Name);
      %conn.SetJoinPassword($Client::Password);
      %conn.Connect($ServerInfo::Address);
   }
}

function onServerQueryStatus(%status, %msg, %value)
{
   if (!QueryStatus.IsVisible())
      QueryStatus.SetVisible(true);

   switch$ (%status) {
      case "start":

      case "ping":
         StatusText.SetText("Ping Servers");
         StatusBar.SetValue(%value);

      case "query":

      case "done":
         QueryStatus.SetVisible(false);
         ServerScreen.Update();
   }
}
