Advertisement
Guest User

Network Manager

a guest
Oct 22nd, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.86 KB | None | 0 0
  1.     public override void Awake()
  2.     {
  3.         base.Awake();
  4.         lobbyIDs = new List<CSteamID>();
  5.         myListOfLobbies = new List<ButtonLobby>();
  6.         Callback_lobbyCreated = Callback<LobbyCreated_t>.Create(OnLobbyCreate);
  7.         Callback_lobbyList = Callback<LobbyMatchList_t>.Create(OnLobbyList);
  8.         Call_matchList_t = CallResult<LobbyMatchList_t>.Create(OnGetLobbiesList);
  9.         Callback_lobbyEnter = Callback<LobbyEnter_t>.Create(OnLobbyEnter);
  10.         Callback_lobbyInfo = Callback<LobbyDataUpdate_t>.Create(OnGetLobbyInfo);
  11.         stuffToDestroy = new List<GameObject>();
  12.         gameObject.AddComponent<NetworkMatch>();
  13.        
  14.  
  15.        
  16.     }
  17.     /// <summary>
  18.     /// This is only for reducing the number of words every time you would use NetworkController.singleton
  19.     /// </summary>
  20.     public static NetworkController instance
  21.     {
  22.         get { return ((NetworkController)NetworkController.singleton); }
  23.     }
  24.     public void CreateGame(string name, uint maxPlayers, string password, int mapId)
  25.     {
  26.         currentMapId = mapId;
  27.         StartMatchMaker();
  28.         StartHostAll(name, maxPlayers, true, password, 0, 0, myPort);
  29.     }
  30.     public override void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
  31.     {
  32.         LogGenerator.GenerateLog("Network Controller", "Match is created, creating lobby now");
  33.         if (usingSteamMatchmaking)
  34.         {
  35.             SteamMatchmaking.CreateLobby(ELobbyType.k_ELobbyTypePublic, (int)instance.matchSize);
  36.         }
  37.         base.OnMatchCreate(success, extendedInfo, matchInfo);
  38.         LogGenerator.GenerateLog("Network Controller", "Lobby Successfully created");
  39.  
  40.     }
  41.     public void OnLobbyCreate(LobbyCreated_t result)
  42.     {
  43.         if(result.m_eResult == EResult.k_EResultOK)
  44.         {
  45.             __externalIp = instance.hostExternalIP;
  46.             __externalIPV6 = instance.hostExternalIPv6;
  47.             __internalIp = instance.hostInternalIP;
  48.             __internalIPV6 = instance.hostInternalIP;
  49.             LogGenerator.GenerateLog("NetworkController", "Lobby created sucessfully. Setting Lobby data");
  50.             myLobby = (CSteamID)result.m_ulSteamIDLobby;
  51.             LogGenerator.GenerateLog("Network Controller", "Lobby identificator is " + result.m_ulSteamIDLobby.ToString());
  52.             SteamMatchmaking.SetLobbyData((CSteamID)result.m_ulSteamIDLobby, "name",            instance.matchName);
  53.             SteamMatchmaking.SetLobbyData((CSteamID)result.m_ulSteamIDLobby, "publicIP",        instance.hostExternalIP);
  54.             SteamMatchmaking.SetLobbyData((CSteamID)result.m_ulSteamIDLobby, "publicIPv6",      instance.hostExternalIPv6);
  55.             SteamMatchmaking.SetLobbyData((CSteamID)result.m_ulSteamIDLobby, "internalIP",      instance.hostInternalIP);
  56.             SteamMatchmaking.SetLobbyData((CSteamID)result.m_ulSteamIDLobby, "internalIPV6",    instance.hostInternalIPv6);
  57.             SteamMatchmaking.SetLobbyData((CSteamID)result.m_ulSteamIDLobby, "map",             instance.listOfMaps[currentMapId]);
  58.             SteamMatchmaking.SetLobbyData((CSteamID)result.m_ulSteamIDLobby, "matchID",         instance.matchID.ToString());
  59.             SteamMatchmaking.SetLobbyData((CSteamID)result.m_ulSteamIDLobby, "guid", GetComponent<NATHelper>().guid.ToString());
  60.             LogGenerator.GenerateLog("Network Controller", "Lobby data sucessfully stored.");
  61.         }
  62.     }
  63.     void OnLobbyList(LobbyMatchList_t result)
  64.     {
  65.  
  66.     }
  67.     public void GetListOfHostsWithoutSteam(int page, int pagesize)
  68.     {
  69.         LogGenerator.GenerateLog("NetworkController", "Getting list of matches without steam");
  70.         listGet = false;
  71.         NetworkController.instance.matchMaker.ListMatches(page, pagesize, "", false, 0, 0, OnMatchList);
  72.         LogGenerator.GenerateLog("NetworkController", "Ending Call List host without steam");
  73.     }
  74.     public void GetListOfLobbies(int page, int pagesize)
  75.     {
  76.         //if(callForListLobbies != null)
  77.         LogGenerator.GenerateLog("Network Controller", "Getting List of Steam Lobbies");
  78.         listGet = false;
  79.         SteamMatchmaking.AddRequestLobbyListResultCountFilter(pagesize);
  80.         callForListLobbies = SteamMatchmaking.RequestLobbyList();
  81.         Call_matchList_t.Set(callForListLobbies);
  82.     }
  83.     public void OnGetLobbyInfo(LobbyDataUpdate_t result)
  84.     {
  85.         if (((int)result.m_bSuccess)>0)
  86.             LogGenerator.GenerateLog("Network Controller", "Lobby " + result.m_ulSteamIDLobby + " successfully updated");
  87.     }
  88.     public override void OnStartHost()
  89.     {
  90.         LogGenerator.GenerateLog("Network Controller", " I started a host, setting controller flags ");
  91.         isHost = true;
  92.         if (usingSteamMatchmaking)
  93.         {
  94.             SteamMatchmaking.CreateLobby(ELobbyType.k_ELobbyTypePublic, (int)instance.matchSize);
  95.         }
  96.         base.OnStartHost();
  97.         ChangeScene(currentMapId);
  98.     }
  99.     public override void OnStartServer()
  100.     {
  101.         isServer = true;
  102.         if (usingSteamMatchmaking)
  103.         {
  104.             SteamMatchmaking.CreateLobby(ELobbyType.k_ELobbyTypePublic, (int)instance.matchSize);
  105.         }
  106.         base.OnStartServer();
  107.         ChangeScene(currentMapId);
  108.     }
  109.     public void JoinGame(int serverIndex, string password)
  110.     {
  111.         if(!usingSteamMatchmaking)
  112.         {
  113.             StartClientAll(myListOfLobbies[serverIndex].myMatch, null, password);
  114.         }
  115.         else
  116.         {
  117.             SteamAPICall_t tryLobby = SteamMatchmaking.JoinLobby(lobbyIDs[serverIndex]);
  118.         }
  119.     }
  120.     public override void OnStartClient(NetworkClient client)
  121.     {
  122.         base.OnStartClient(client);
  123.     }
  124.     void OnGetLobbiesList(LobbyMatchList_t result, bool failure)
  125.     {
  126.         LogGenerator.GenerateLog("Network Controller", "Lobby list returned");
  127.         LogGenerator.GenerateLog("Network Controller", "Total of " + result.m_nLobbiesMatching.ToString() + " lobby(s)");
  128.         //LogGenerator.GenerateLog("NetworkController", "found " + result.m_nLobbiesMatching + " Lobbies");
  129.         for(int i = 0; i < result.m_nLobbiesMatching; i++)
  130.         {
  131.             CSteamID tmp = SteamMatchmaking.GetLobbyByIndex(i);
  132.             lobbyIDs.Add(tmp);
  133.             LogGenerator.GenerateLog("NetworkController", "Lobby " + tmp.ToString() + " added to LobbyIDs");
  134.             ButtonLobby bl = new ButtonLobby();
  135.             bl.lobbySelectionIndex = i;
  136.             bl.steamLobby =             tmp;
  137.             bl.lobbyName =              SteamMatchmaking.GetLobbyData(tmp, "name");
  138.             bl.mapName =                SteamMatchmaking.GetLobbyData(tmp, "map");
  139.             bl.gameDescription =        SteamMatchmaking.GetLobbyData(tmp, "Description");
  140.             bl.currentNumberOfPlayers = SteamMatchmaking.GetNumLobbyMembers(tmp);
  141.             bl.maxNumberOfPlayers =     SteamMatchmaking.GetLobbyMemberLimit(tmp);
  142.             LogGenerator.GenerateLog("Network Controller", "button with index " + i + " sucessfully saved");
  143.         }
  144.         listGet = true;
  145.     }
  146.     public void OnLobbyEnter(LobbyEnter_t result)
  147.     {
  148.         LogGenerator.GenerateLog("Network Controller", "Lobby entered");
  149.         SteamMatchmaking.GetLobbyData((CSteamID)result.m_ulSteamIDLobby, "name");
  150.         string _publicIP   = SteamMatchmaking.GetLobbyData((CSteamID)result.m_ulSteamIDLobby, "publicIP");
  151.         string _pubIPV6    = SteamMatchmaking.GetLobbyData((CSteamID)result.m_ulSteamIDLobby, "publicIPv6");
  152.         string _internalIP = SteamMatchmaking.GetLobbyData((CSteamID)result.m_ulSteamIDLobby, "internalIP");
  153.         string _internaIPV6 = SteamMatchmaking.GetLobbyData((CSteamID)result.m_ulSteamIDLobby, "internalIPV6");
  154.         string _map        = SteamMatchmaking.GetLobbyData((CSteamID)result.m_ulSteamIDLobby, "map");
  155.         //ulong tmp = ulong.Parse(SteamMatchmaking.GetLobbyData((CSteamID)result.m_ulSteamIDLobby, "matchID"));
  156.         ulong guid = ulong.Parse(SteamMatchmaking.GetLobbyData((CSteamID)result.m_ulSteamIDLobby, "guid"));
  157.         LogGenerator.GenerateLog("Network Controller", "Starting Client");
  158.         StartClientAll(_publicIP, _internalIP, myPort, guid, NetworkID.Invalid , _pubIPV6, _internaIPV6, OnMatchJoined, myPassword);
  159.     }
  160.     public Soldier[] soldiersM;
  161.     public Soldier[] soldiersF;
  162.     public override void OnMatchJoined(bool success, string extendedInfo, MatchInfo info)
  163.     {
  164.         isClient = true;
  165.         base.OnMatchJoined(success, extendedInfo, info);
  166.     }
  167.     public override void OnMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matchList)
  168.     {
  169.         LogGenerator.GenerateLog("Network Controller", "Listing matchs without Steamworks ");
  170.         if(success)
  171.         {
  172.             myListOfLobbies.Clear();
  173.             LogGenerator.GenerateLog("Network Controller", "List of Matchs cleared");
  174.             ButtonLobby bl;
  175.             for(int i = 0; i < matchList.Count; i++)
  176.             {
  177.                 MatchInfoSnapshot mis = matchList[i];
  178.                 bl = new ButtonLobby();
  179.                 bl.myMatch = mis;
  180.                 bl.lobbyName = mis.name;
  181.                 bl.currentNumberOfPlayers = mis.currentSize;
  182.                 bl.maxNumberOfPlayers = mis.maxSize;
  183.                 bl.myPing = -1;
  184.                 bl.mapId = mis.averageEloScore;
  185.                 bl.lobbySelectionIndex = i;
  186.                 myListOfLobbies.Add(bl);
  187.                 LogGenerator.GenerateLog("Network Controller", "Lobby Generated");
  188.             }
  189.         }
  190.         listGet = true;
  191.         base.OnMatchList(success, extendedInfo, matchList);
  192.     }
  193.     public void ChangeScene(int id)
  194.     {
  195.         if (isServer || isHost)
  196.         {
  197.             if (id >= 0 && id < listOfMaps.Length)
  198.             {
  199.                 while (stuffToDestroy.Count > 0)
  200.                 {
  201.                     Destroy(stuffToDestroy[0]);
  202.                 }
  203.                 instance.ServerChangeScene(listOfMaps[id]);
  204.             }
  205.         }
  206.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement