IpDrv
Class RegisterServerToUbiCom

source: C:\XIII\IpDrv\Classes\REGISTERSERVERTOUBICOM.UC
Core.Object
   |
   +--Engine.Actor
      |
      +--Engine.Info
         |
         +--IpDrv.RegisterServerToUbiCom
Direct Known Subclasses:None

class RegisterServerToUbiCom
extends Engine.Info

//============================================================================= // RegisterServerToUbiCom: To register a dedicated server to UBI.COM //=============================================================================
Variables
 int ExitMessageReminderCounter
 GSAlias, GSPassword
 string Info4GS
 ServerName, IsPrivate
 string LastUpdateValue
 int ListeningPort
 int MaxPlayer
 PlayerReplicationInfo Ordered[32]
 int PlayerCount
 int ResultCode
 byte Status
 MatchMakingManager myMMManager

States
KeepGSposted, STA_RegisterGameServer

Function Summary
 void BeginPlay()
 bool BuildPlayerListWithScore()
 void CheckIfConnectionLostWithUbiCom()
 string LevelTimeInReadableFormat()
 void SortScores(int N)
 void UpdatePlayerList()


State KeepGSposted Function Summary


State STA_RegisterGameServer Function Summary



Source Code


00001	//=============================================================================
00002	// RegisterServerToUbiCom: To register a dedicated server to UBI.COM
00003	//=============================================================================
00004	class RegisterServerToUbiCom extends Info;
00005	
00006	
00007	var MatchMakingManager myMMManager;
00008	var int ResultCode;
00009	
00010	
00011	var string GSAlias, GSPassword;
00012	var string ServerName, IsPrivate;
00013	var int MaxPlayer;
00014	var string Info4GS;
00015	var int ListeningPort;
00016	
00017	var byte Status;
00018	var string LastUpdateValue;
00019	var int ExitMessageReminderCounter;
00020	
00021	var int PlayerCount;
00022	var PlayerReplicationInfo Ordered[32];
00023	
00024	
00025	
00026	function BeginPlay()
00027	{
00028	    local string URLOptions, MapName;
00029	    local bool bGSServer;
00030	    local int GameIdx, FragLimit, TimeLimit;
00031	
00032	    URLOptions = Level.getLocalURL();
00033	    URLOptions = Mid(URLOptions, InStr( URLOptions, "?" ));
00034	
00035	    //log("In RegisterServerToUbiCom, URLOptions = "$URLOptions);
00036	    bGSServer = (Level.Game.ParseOption ( URLOptions, "GS" ) != "");
00037	
00038	    if ((Level.NetMode != NM_DedicatedServer) || !bGSServer)
00039	    {
00040	        Destroy();
00041	    }
00042	    else
00043	    {
00044	        GSAlias = Level.Game.ParseOption ( URLOptions, "GSAlias" );
00045	        GSPassword = Level.Game.ParseOption ( URLOptions, "GSPassword" );
00046	        ServerName = Level.Game.ParseOption ( URLOptions, "ServerName" );
00047	        IsPrivate = Level.Game.ParseOption ( URLOptions, "IsPrivate" );
00048	        MaxPlayer = int(Level.Game.ParseOption ( URLOptions, "NP" ));
00049	        
00050	        GameIdx = int(Level.Game.ParseOption ( URLOptions, "GameIdx" ));
00051	        MapName = Level.Game.ParseOption ( URLOptions, "MapName" );
00052	        FragLimit = int(Level.Game.ParseOption ( URLOptions, "FR" ));
00053	        TimeLimit = int(Level.Game.ParseOption ( URLOptions, "TI" ));
00054	        Info4GS = GameIdx$"?"$MapName$"?"$FragLimit$"?"$TimeLimit$"?"$IsPrivate;
00055	
00056	        ListeningPort = int(ConsoleCommand( "ListeningPort" ));
00057	        if ((ListeningPort <= 0) || (ListeningPort >65535))
00058	            ListeningPort = 7777;
00059	
00060	        Status = 0;
00061	
00062	
00063	        //log("GSAlias="$GSAlias$", GSPassword="$GSPassword$", ServerName="$ServerName$", IsPrivate="$IsPrivate$", Info4GS="$Info4GS);
00064	        //log("MaxPlayer="$MaxPlayer$", GameIdx="$GameIdx$", MapIdx="$MapIdx$", FragLimit="$FragLimit$", TimeLimit="$TimeLimit$", ListeningPort="$ListeningPort);
00065	
00066	    }
00067	}
00068	
00069	
00070	event Destroyed()
00071	{
00072	    // Unregister the server  (but don't seem to come here...)
00073	    if (myMMManager != none)
00074	    {
00075	        myMMManager.UnregisterMyGameServer();
00076	    }
00077	}
00078	
00079	
00080	event Timer()
00081	{
00082	    switch (Status)
00083	    {
00084	    case 0:
00085	        GotoState('STA_RegisterGameServer');
00086	        break;
00087	
00088	    case 1:
00089	        GotoState('KeepGSposted');
00090	        break;
00091	    }
00092	}
00093	
00094	
00095	
00096	
00097	function SortScores(int N)
00098	{
00099	    local int I, J, Max;
00100	    local PlayerReplicationInfo TempPRI;
00101	
00102	    for ( I=0; I<N-1; I++ )
00103	    {
00104	      Max = I;
00105	      for ( J=I+1; J<N; J++ )
00106	      {
00107	        if ( Ordered[J].Score > Ordered[Max].Score )
00108	          Max = J;
00109	        else if ((Ordered[J].Score == Ordered[Max].Score) && (Ordered[J].Deaths < Ordered[Max].Deaths))
00110	          Max = J;
00111	        else if ((Ordered[J].Score == Ordered[Max].Score) && (Ordered[J].Deaths == Ordered[Max].Deaths) &&
00112	          (Ordered[J].PlayerID < Ordered[Max].Score))
00113	          Max = J;
00114	      }
00115	
00116	      TempPRI = Ordered[Max];
00117	      Ordered[Max] = Ordered[I];
00118	      Ordered[I] = TempPRI;
00119	    }
00120	}
00121	
00122	function UpdatePlayerList()
00123	{
00124	    local int i;
00125	    local PlayerReplicationInfo PRI;
00126	
00127	    // Wipe everything.
00128	    for ( i=0; i<ArrayCount(Ordered); i++ )
00129	      Ordered[i] = None;
00130	    PlayerCount = 0;
00131	
00132	    foreach AllActors(class'PlayerReplicationInfo', PRI)
00133	    {
00134	      if ( !PRI.bIsSpectator || PRI.bWaitingPlayer )
00135	      {
00136	        Ordered[PlayerCount] = PRI;
00137	        PlayerCount++;
00138	        if ( PlayerCount == ArrayCount(Ordered) )
00139	          break;
00140	      }
00141	    }
00142	
00143	    SortScores(PlayerCount);
00144	}
00145	
00146	
00147	function bool BuildPlayerListWithScore()
00148	{
00149	    local string Result;
00150	    local int Loop;
00151	
00152	    UpdatePlayerList();
00153	
00154	    for ( Loop=0; Loop<PlayerCount; Loop++ )
00155	    {
00156	        if (Loop > 0)
00157	            Result = Result$"?";
00158	        Result = Result$Ordered[Loop].PlayerName$"="$int(Ordered[Loop].Score);
00159	    }
00160	
00161	    if (LastUpdateValue == Result)
00162	    {
00163	        return false;
00164	    } 
00165	    else
00166	    {
00167	        LastUpdateValue = Result;
00168	        return true;
00169	    }
00170	}
00171	
00172	
00173	
00174	function CheckIfConnectionLostWithUbiCom()
00175	{
00176	    local bool ConnectionLost, GameServerStillResgistered;
00177	    ConnectionLost = false; //myMMManager.IsConnectionLostWithGS();
00178	    GameServerStillResgistered = myMMManager.IsMyGameServerStillRegistered();
00179	    //if (ConnectionLost)
00180	    //    log("----> Connection lost with ubi.com !!");
00181	    if (!GameServerStillResgistered)
00182	        log("----> My game server is no longer registered !!");
00183	
00184	    if (myMMManager!=none)
00185	    {
00186	        if (ConnectionLost || !GameServerStillResgistered)
00187	        {
00188	            myMMManager.LogOut();
00189	            log("**********************************************************************");
00190	            log(" At "$LevelTimeInReadableFormat()$" since server launch");
00191	            log(" Disconnected from Ubi.com. Will register the server again in 30 sec. ");
00192	            log("**********************************************************************");
00193	            GotoState('');
00194	            Status = 0;
00195	            LastUpdateValue="...";
00196	        }
00197	    }
00198	    else
00199	    {
00200	        // no myMMManager ? Force the creation of one
00201	        GotoState('');
00202	        Status = 0;
00203	        LastUpdateValue="...";
00204	    }
00205	}
00206	
00207	
00208	
00209	function string LevelTimeInReadableFormat()
00210	{
00211	    local float LevelTime;
00212	    local int Day, Hour, Min, Sec;
00213	
00214	    LevelTime = Level.TimeSeconds;
00215	    Day = int(LevelTime/86400);         // 86400 = 24 hours * 3600 secs
00216	    LevelTime -= Day*86400;
00217	    Hour = int(LevelTime / 3600);
00218	    LevelTime -= Hour*3600;
00219	    Min = int(LevelTime / 60);
00220	    LevelTime -= Min * 60;
00221	    Sec = int(LevelTime);
00222	
00223	    return Day$"d "$Hour$"h "$Min$"m "$Sec$"s";
00224	}
00225	
00226	
00227	
00228	auto State STA_RegisterGameServer
00229	{
00230	
00231	begin:
00232	    if (myMMManager == none)
00233	        myMMManager = new(none) class'MatchMakingManager';
00234	
00235	    //log("Log into GS with "$myMMManager);
00236	    myMMManager.Login(GSAlias, GSPassword);
00237	
00238	    //log("Getting server list");
00239	    /*
00240	    myMMManager.RequestGameServerList();
00241	    while ( !myMMManager.IsGameServerListComplete(ResultCode) )
00242	    {
00243	        Sleep(0.1);
00244	    }
00245	
00246	    if (ResultCode != 0) 
00247	    {
00248	        // will retry later
00249	        myMMManager.LogOut();
00250	        SetTimer(30, true);
00251	        log("*****************************************************************");
00252	        log("*****************************************************************");
00253	        log(" At "$LevelTimeInReadableFormat()$" since server launch");
00254	        log(" Failed to register the game server (Phase 1). Will retry in 30 seconds...");
00255	        log("*****************************************************************");
00256	        log("*****************************************************************");
00257	        GotoState('');
00258	    }
00259	    */
00260	
00261	    //log("Register server to GS...");
00262		myMMManager.RegisterMyGameServer(ServerName, MaxPlayer, 0, "", Info4GS, "", ListeningPort, true);
00263		while (!myMMManager.IsMyGameServerRegistered(ResultCode))
00264		{
00265			Sleep(0.1);
00266		}
00267		
00268		if (ResultCode == 0) 
00269		{
00270	        log("***********************************");
00271	        log("***********************************");
00272	        log(" Game server registered to Ubi.com ");
00273	        log(" at "$LevelTimeInReadableFormat()$" since server launch");
00274	        log("***********************************");
00275	        log("***********************************");
00276	        log(" Press 'q' to terminate the server");
00277	        log("***********************************");
00278	        //myMMManager.IStartMatch();      // the master joins its room...
00279	
00280	        Status = 1;
00281		}
00282		else 
00283		{
00284	        log("*****************************************************************");
00285	        log("*****************************************************************");
00286	        log(" At "$LevelTimeInReadableFormat()$" since server launch");
00287	        log(" Failed to register the game server (Phase 2). Will retry in 30 seconds...");
00288	        log("*****************************************************************");
00289	        log("*****************************************************************");
00290	        myMMManager.LogOut();
00291		}
00292	    
00293	    SetTimer(30, true);
00294	    GotoState('');
00295	}
00296	
00297	
00298	
00299	State KeepGSposted
00300	{
00301	begin:
00302	    CheckIfConnectionLostWithUbiCom();
00303	    //log("Maybe I will update GS...");
00304	    if (BuildPlayerListWithScore())
00305	    {
00306	        CheckIfConnectionLostWithUbiCom();
00307	        log("Sending "$LastUpdateValue$" to GS");
00308	        myMMManager.UpdateMyGameServer(-1, -1, "", ""/*info*/, LastUpdateValue/*AdditionalInfo*/, -1);
00309	        while ( !myMMManager.IsMyGameServerUpdated(ResultCode) )
00310	        {
00311	            CheckIfConnectionLostWithUbiCom();
00312	            Sleep(0.1);
00313	        }
00314	        /*  Whatever the ResultCode, there is nothing we can do...
00315	        if (ResultCode == 0)
00316	        {
00317	            log("Server updated on GS");
00318	        }
00319	        else
00320	        {
00321	            log("Error while updating the server on GS");
00322	        }
00323	        */
00324	    }
00325	
00326	    ExitMessageReminderCounter++;
00327	    if (ExitMessageReminderCounter > 10)
00328	    {
00329	        log("----- Press 'q' to terminate the server ("$LevelTimeInReadableFormat()$" since server launch)");
00330	        ExitMessageReminderCounter = 0;
00331	    }
00332	
00333	    GotoState('');
00334	}
00335	
00336	
00337	
00338	
00339	defaultproperties
00340	{
00341	     LastUpdateValue="..."
00342	}

End Source Code