home *** CD-ROM | disk | FTP | other *** search
/ Revista CD Expert 51 / PCGamer51_17Demos.iso / games / CM / CyclingManagerDemoCangas.exe / CyclingManagerDemo / scripts / net / uci_server.cnc < prev    next >
Text File  |  2001-07-02  |  6KB  |  261 lines

  1.  
  2.  
  3. // ******************************************************
  4. // Network Callbacks
  5. // *******************************************************
  6.  
  7. func void fnGameConnectCB(Net_Client pClient)
  8. {
  9. }
  10.  
  11.  
  12. func void fnGameDisconnectCB(Net_Client _pClient)
  13. {
  14.     NetClient_Destroy(_pClient);
  15. }
  16.  
  17. // *********************
  18. // incomming messages
  19. // *********************
  20.  
  21.  
  22. func void fnGameDistantLogin(szx szGameLogin,szx szGamePassword,szx szAdminLogin,szx szAdminPassword,i32x iGameMode)
  23. {
  24.     // get client
  25.     var Net_Client pClient;
  26.     pClient = GetCurrentClient();
  27.     g_oGateway_MenuServerClient.m_pClient = pClient;
  28.  
  29.     var i32x iGameId;
  30.     iGameId = 1;
  31.  
  32.     pClient << mGame_LoginOK();
  33.  
  34.     // init user
  35.     g_oUser.m_szGatewayAuthorization = "*OK*";
  36. }
  37.  
  38.  
  39. // *********************
  40. // incomming messages
  41. // *********************
  42.  
  43.  
  44.  
  45. func void fnGameReady(i32x _iPort)
  46. {
  47.     print("game ready at ");println(itoa(_iPort));
  48.  
  49.     // get client
  50.     var Net_Client pClient;
  51.     pClient = GetCurrentClient();
  52.  
  53.     // store menu server adress
  54.     g_oMenuServer.m_szIP = "127.0.0.1";
  55.     g_oMenuServer.m_iPort = _iPort;
  56.  
  57.     // If already connected
  58.     if(g_oLobby.m_oServer)
  59.     {
  60.         // disconnect from gateway to allow new login
  61.         NetClient_Disconnect(g_oLobby.m_oServer);
  62.     }
  63.  
  64.     // admin is automatically connected
  65.     g_oLobby.m_oServer = NetClient_Create(g_oLobby.m_szIP, g_oLobby.m_iGamePort, GatewayInterface, fnGatewaySimpleConnectCB, fnGatewayDisconnectCB);
  66. }
  67.  
  68.  
  69.  
  70.  
  71. // user login
  72. func void fnUserLogin( szx _szLogin )
  73. {
  74.     print(">> User trying to login : ");
  75.     println(_szLogin); 
  76.     
  77.     var i32x iResult;
  78.     
  79.  
  80.     var Net_Client pClient;
  81.     var Net_ClientList    pClientList;
  82.     var i32x iUserId;
  83.     
  84.     var i32x iNewRow, iUserTable;
  85.  
  86.     iResult = 1;
  87.  
  88.  
  89.     // get net client
  90.     pClient = GetCurrentClient();
  91.  
  92.     // new login ?
  93.     if (iResult>0)
  94.     {
  95.         iUserId = Join_FindKeyFromStringData( g_oGatewayJoins.iUserName, _szLogin );
  96.         if (iUserId==-1)
  97.         {
  98.             // add user in table
  99.             iUserTable = Table_GetIndexFromName(QUERY_GAME_SERVERS,"Users");
  100.             iNewRow = Table_AddRow(QUERY_GAME_SERVERS,iUserTable);
  101.             Join_SetStringFromIndex(g_oGatewayJoins.iUserName,iNewRow,_szLogin);
  102.             iUserId = Join_GetIntFromIndex(g_oGatewayJoins.iUserId,iNewRow);
  103.             Table_Update(QUERY_GAME_SERVERS,iUserTable);
  104.             print("new user id:");println(itoa(iUserId));
  105.         }
  106.         else
  107.         {
  108.             print("old user id:");println(itoa(iUserId));
  109.         }
  110.     }
  111.     
  112.     if (iResult)
  113.     {    
  114.         // set user id
  115.         NetClient_SetId(pClient,iUserId);
  116.             
  117.         // get client list
  118.         pClientList = NetServer_GetClientList(g_pGameServer,0);
  119.                 
  120.         // connected to gateway ?
  121.         if ((NetClientList_FindClient(pClientList,iUserId)!=g_poNullClient) || (Join_GetIntFromKey(g_oGatewayJoins.iUserGameId, iUserId)!=0))
  122.         {
  123.             // ** login duplicated
  124.             print("Already connected\n");
  125.             pClient << mUser_GatewayLoginDuplicated();
  126.             iResult = 0;
  127.         }
  128.         else
  129.         {    
  130.             // ** login accepted
  131.  
  132.             // add client in list
  133.             NetClientList_AddClient( pClientList, pClient);
  134.             
  135.             // authorize client in menu server
  136.             g_oGateway_MenuServerClient.m_pClient << mGame_UserAuthorization(_szLogin, g_oUser.m_szGatewayAuthorization);
  137.         }
  138.     }
  139.     
  140.     else
  141.     {
  142.         // ** login failed
  143.         print("Login Failed.\n");
  144.         pClient << mUser_GatewayLoginFailed();
  145.         NetClient_Disconnect(pClient);
  146.     }
  147. }
  148.  
  149. func void fnGateway_UserAuthorizationOk(szx _szLogin,szx _szAuthorization)
  150. {
  151.  
  152.     if (strcmp(g_oUser.m_szLogin,_szLogin)==0)
  153.     {
  154.         // admin is automatically conected to the menu server
  155.         g_oMenuServer.m_oServer = NetClient_Create(
  156.             g_oMenuServer.m_szIP,
  157.             g_oMenuServer.m_iPort, 
  158.             iClientLogin, 
  159.             MenuServerConnectCB, 
  160.             MenuServerDisconnectCB);
  161.     }
  162.     else
  163.     {
  164.         // get client list
  165.         var Net_ClientList    pClientList;
  166.         var Net_Client pClient;
  167.         var i32x iUserId;
  168.  
  169.         pClientList = NetServer_GetClientList(g_pGameServer,0);
  170.         
  171.         iUserId = Join_FindKeyFromStringData(g_oGatewayJoins.iUserName, _szLogin);
  172.         if (iUserId>0)
  173.         {    
  174.             // get net client
  175.                 pClient = NetClientList_FindClient(pClientList,iUserId);
  176.             
  177.             if (pClient)
  178.             {
  179.                 pClient << mUser_GatewayAuthorizationOK(_szAuthorization,g_oMenuServer.m_szIP,g_oMenuServer.m_iPort);
  180.             }
  181.             else
  182.             {
  183.                 print("## authorization ack but client not found ");
  184.                 println(_szLogin);
  185.             }
  186.         }
  187.  
  188.     }
  189. }
  190.  
  191.  
  192.  
  193. func void fnUserConnection( szx _szLogin )
  194. {
  195.     // activate connection
  196.     var i32x iUserId,iNumConnected;
  197.     iUserId = Join_FindKeyFromStringData(g_oGatewayJoins.iUserName, _szLogin);
  198.     if (iUserId>0)
  199.     {
  200.         Join_SetIntFromKey(    g_oGatewayJoins.iUserGameId, iUserId, 1);
  201.  
  202.         g_oGateway_MenuServerClient.m_iNumConnected = g_oGateway_MenuServerClient.m_iNumConnected + 1;
  203.     }
  204. }
  205.  
  206.  
  207. func void fnUserDisconnection( szx _szLogin )
  208. {
  209.     // reset connection
  210.     var i32x iUserId,iNumConnected;
  211.     iUserId = Join_FindKeyFromStringData(g_oGatewayJoins.iUserName, _szLogin);
  212.     if (iUserId>0)
  213.     {
  214.         Join_SetIntFromKey(    g_oGatewayJoins.iUserGameId, iUserId, 0);
  215.  
  216.         g_oGateway_MenuServerClient.m_iNumConnected = g_oGateway_MenuServerClient.m_iNumConnected - 1;
  217.         if(g_oGateway_MenuServerClient.m_iNumConnected<0)
  218.             g_oGateway_MenuServerClient.m_iNumConnected = 0;
  219.     }
  220. }
  221.  
  222.  
  223.  
  224.  
  225.  
  226. func void fnGateway_UserAuthorizationFailed_Null(szx _szLogin)
  227. {
  228. }
  229.  
  230.  
  231.  
  232. // *************************
  233. // * Functions
  234. // *************************
  235.  
  236. func void fnGameServer_Create()
  237. {
  238.     // Launch server
  239.     g_oLobby.m_szIP = "127.0.0.1";
  240.     g_pGameServer = NetServer_Create( g_oLobby.m_iGamePort, GameInterface, fnGameConnectCB, fnGameDisconnectCB, 1 );
  241.     if (g_pGameServer)
  242.     {
  243.         // Read the server port
  244.         g_oLobby.m_iGamePort = NetServer_GetPort(g_pGameServer);
  245.  
  246.         print(">> Game Server started on port ");
  247.         println(itoa(g_oLobby.m_iGamePort));        
  248.     }
  249. }
  250.  
  251. func void fnGameServer_Destroy()
  252. {
  253.     if (g_pGameServer)
  254.     {
  255.         NetServer_Destroy(g_pGameServer);
  256.         g_pGameServer = g_pNullServer;
  257.     }
  258. }
  259.  
  260.  
  261.