public void RunCurrentselected() { switch (currentIndex) { case 0: { // Create new lobby DisableKeyboard = !DisableKeyboard; loader = new Loader <List <LobbyInfo> >("Creating lobby", () => { ClientProtocol.CreateLobby(); return(null); }, (success) => ParentState._context.TransitionTo(new LobbyState(Game)) ); } break; case 1: { // Join existing lobby loader = new Loader <List <LobbyInfo> >("Loading lobbies", () => ClientProtocol.GetLobbies(), (lobbies) => { if (lobbies.Count != 0) { DisableKeyboard = !DisableKeyboard; ParentState._context.TransitionTo(new SelectLobbyState(Game)); } else { var info = clientInformation.GetComponent <Text>(); info.Message = "No existing lobbies"; loader = null; } } ); } break; default: break; } }
// Task which repeatedly fetches the current lobbies, // and signal for an update of the UI in case they've changed private async void UpdateLobbies() { updateLobbies = true; await Task.Run(() => { while (updateLobbies) { var lobbies = ClientProtocol.GetLobbies(); lock (currentLobies) { // Check if lobbies requires update var matched = lobbies.Count == currentLobies.Count; if (matched) { foreach (var lobby in lobbies) { if (!currentLobies.ContainsKey(lobby.Id)) { matched = false; break; } } } // Update lobbies if (!matched) { currentLobies.Clear(); foreach (var lobby in lobbies) { currentLobies[lobby.Id] = lobby; } lobbiesWereUpdated = true; } // Since the GetLobbies() function creates a new connection to the server for // each lobby that exists, it may overload the server. Reducing the frequency // of the connections fixes the problem as of now. Thread.Sleep(500); } } }); }