protected virtual void OnConnectionStateChanged(Error error, ValueChangedArgs <bool> args) { m_progress.IsVisible = false; if (args.NewValue) { if (m_remoteGameServer.HasError(error)) { m_notification.ShowError(error); } } else { if (m_remoteGameServer.HasError(error)) { m_notification.ShowError(error); } else { m_notification.ShowError("You are offline. Server is not connected.", m_connectionButton.gameObject); } } if (args.NewValue != args.OldValue) { Dependencies.RemoteGameServer.CancelRequests(); Dependencies.LocalGameServer.CancelRequests(); m_navigation.ClearHistory(); m_navigation.Navigate("LoginMenu4Players"); } }
private void OnConnectionStateChanged(Error error, ValueChangedArgs <bool> args) { m_progress.IsVisible = false; for (int i = 0; i < m_playerMenu.Length; ++i) { m_playerMenu[i].Player = null; } if (args.NewValue) { if (m_remoteGameServer.HasError(error)) { m_notification.ShowError(error); } else { GetPlayers(); } } else { if (m_remoteGameServer.HasError(error)) { m_notification.ShowError(error); } else { m_notification.ShowError("You are offline. Server is not connected.", m_connectButton.gameObject); GetPlayers(); } } }
private static void Launch(string mapName, Action callback, Action <Error> error, IProgressIndicator progress, IGameServer server, IGlobalSettings gSettings) { server.SetReadyToLaunch(gSettings.ClientId, true, (readyToLaunchError, room) => { if (server.HasError(readyToLaunchError)) { error(readyToLaunchError); //progress.IsVisible = false; return; } server.Launch(gSettings.ClientId, (e5, serverUrl) => { if (server.HasError(e5)) { error(e5); //progress.IsVisible = false; return; } //progress.IsVisible = false; gSettings.MatchServerUrl = serverUrl; IMatchServer remoteMatchServer = Dependencies.RemoteMatchServer; IMatchServer localMatchServer = Dependencies.LocalMatchServer; if (Dependencies.RemoteGameServer != null && Dependencies.RemoteGameServer.IsConnected) { remoteMatchServer.Activate(); } else { localMatchServer.Activate(); } IVoxelInputManager inputManager = Dependencies.InputManager; if (inputManager != null) { inputManager.ActivateAll(); } if (mapName != "Default") { PlayerPrefs.SetString("lastmap", mapName); } callback(); }); }); }
private void OnUploadMap(string mapName, int maxPlayers) { m_progress.IsVisible = true; m_gameServer.GetMaps(m_gSettings.ClientId, (error, mapInfos) => { if (m_gameServer.HasError(error)) { m_progress.IsVisible = false; OutputError(error); return; } m_voxelMap.Save(bytes => { MapInfo mapInfo = mapInfos.Where(m => m.Name == mapName).FirstOrDefault(); if (mapInfo == null) { Guid mapId = Guid.NewGuid(); mapInfo = new MapInfo { Id = mapId, Name = mapName, MaxPlayers = maxPlayers, SupportedModes = GameMode.All }; } else { mapInfo.MaxPlayers = maxPlayers; mapInfo.SupportedModes = GameMode.All; } MapData mapData = new MapData { Id = mapInfo.Id, Bytes = bytes, }; m_gameServer.UploadMap(m_gSettings.ClientId, mapInfo, mapData, uploadError => { m_progress.IsVisible = false; if (m_gameServer.HasError(uploadError)) { OutputError(uploadError); } }); }); }); }
protected override void OnAction(int index) { if (index == SaveIndex) { if (!m_gameServer.IsConnected) { m_notification.ShowError("Game Server disconnected. Unable to save replay."); return; } m_progress.IsVisible = true; m_gameServer.SaveReplay(m_gSettings.ClientId, m_inputField.text, error => { m_progress.IsVisible = false; if (m_gameServer.HasError(error)) { m_notification.ShowError(error); return; } RaiseAction(SaveIndex); }); } else { base.OnAction(index); } }
private void OnDeviceDisabled(int index) { m_connectButton.gameObject.SetActive(m_inputManager.DeviceCount > 0); Player player = m_playerMenu[index].Player; m_playerMenu[index].gameObject.SetActive(false); if (player != null) { IProgressIndicator progress = m_progress.GetChild(index); progress.IsVisible = true; IGameServer gameServer = Dependencies.GameServer; gameServer.Logoff(m_gSettings.ClientId, player.Id, (error, playerId) => { progress.IsVisible = false; if (gameServer.HasError(error)) { m_notification.ShowError(error); return; } SetDisabledAsLast(index); TryNavigateToMainMenu(); }); } else { SetDisabledAsLast(index); TryNavigateToMainMenu(); } }
protected override void OnUnregisterClientSafe(ILowProtocol protocol, Guid clientId) { base.OnUnregisterClientSafe(protocol, clientId); //Sync method call no fail m_gameServer.UnregisterClient(clientId, error => { if (m_gameServer.HasError(error)) { Log.Error("Failed to Unregister Client: " + error.ToString()); } }); }
private void OnChatMessage(Error error, ChatMessage payload) { if (m_gameServer.HasError(error)) { m_notification.ShowError(error); return; } int playerIndex = m_gameState.GetPlayerIndex(payload.SenderId); Player player = m_gameState.GetPlayer(playerIndex); m_chatControl.Echo(player.Name, payload.Message, false); }
private void ShowErrorAndTryToLeaveRoomAndNavigateToMenu(string errorMessage = "") { m_notification.ShowErrorWithAction("Disconnected from game server " + errorMessage, () => { if (m_gameServer.IsConnected) { m_progress.IsVisible = true; m_gameServer.LeaveRoom(m_gSettings.ClientId, error => { m_progress.IsVisible = false; if (m_gameServer.HasError(error)) { Debug.LogError("Unable to leave room " + error.ToString()); } Dependencies.Navigation.Navigate("Menu", "LoginMenu4Players", null); }); } else { Dependencies.Navigation.Navigate("Menu", "LoginMenu4Players", null); } }); }
private static void CreateRoom(string mapName, int playerIndex, int botIndex, int playersCount, int botsCount, Action callback, Action <Error> error, IProgressIndicator progress, IGameServer server, IGlobalSettings gSettings, MapInfo mapInfo) { server.CreateRoom(gSettings.ClientId, mapInfo.Id, GameMode.FreeForAll, (e3, room) => { if (server.HasError(e3)) { error(e3); //progress.IsVisible = false; return; } InitGame(mapName, playerIndex, botIndex, playersCount, botsCount, callback, error); }); }
private static void InitGameOnConnectionStateChanged(bool preferRemote, string mapName, int playersCount, int botsCount, Action callback, Action <Error> error, INotification notification, IGameServer remoteGameServer) { ServerEventHandler <ValueChangedArgs <bool> > connectionStateChanged = null; connectionStateChanged = (Error e, ValueChangedArgs <bool> payload) => { #warning CHECK if actually unsubscribed if (!remoteGameServer.HasError(e)) { if (preferRemote) { if (!remoteGameServer.IsConnected) { remoteGameServer.Connect(); return; } } else { if (remoteGameServer.IsConnected) { remoteGameServer.Disconnect(); return; } } LogoffIfNeeded(() => { InitGame(mapName, 0, 0, playersCount, botsCount, callback, error); }); } else { notification.ShowError(e); LogoffIfNeeded(() => { InitGame(mapName, 0, 0, playersCount, botsCount, callback, error); }); } remoteGameServer.ConnectionStateChanged -= connectionStateChanged; }; remoteGameServer.ConnectionStateChanged += connectionStateChanged; }
private void OnMenuPanelAction(ButtonsPanel sender, int code) { switch (code) { case 0: //console m_menuPanel.SetIsOpened(false); m_consolePanel.SetIsOpened(true); break; case 1: //close menu m_menuPanel.SetIsOpened(false); break; case 2: //pause m_gameState.IsPaused = !m_gameState.IsPaused; break; case 3: //help m_menuPanel.SetIsOpened(false); m_settingsPanel.SetIsOpened(true); break; case 4: //back to menu if (m_gameServer.IsConnected) { m_gameServer.LeaveRoom(m_gSettings.ClientId, error => { if (m_gameServer.HasError(error)) { m_notification.ShowErrorWithAction(error, () => { m_navigation.Navigate("Menu", "MainMenu", null); }); } else { m_navigation.Navigate("Menu", "MainMenu", null); } }); } else { m_navigation.Navigate("Menu", "LoginMenu4Players", null); } break; case 5: //quit m_console.GetChild(LocalPlayerIndex).Write("quit"); break; case 6: //map editor m_console.GetChild(LocalPlayerIndex).Write("mapeditor"); break; } }
private static void LoginOrSignupCompleted(string mapName, int playerIndex, int botIndex, int playersCount, int botsCount, Action callback, Action <Error> error) { //IProgressIndicator progress = Dependencies.Progress; IGameServer server = Dependencies.GameServer; IGlobalSettings gSettings = Dependencies.Settings; playerIndex++; if (playerIndex == playersCount) { server.GetMaps(gSettings.ClientId, (e2, maps) => { if (server.HasError(e2)) { error(e2); // progress.IsVisible = false; return; } MapInfo mapInfo; if (!string.IsNullOrEmpty(mapName)) { mapInfo = maps.Where(m => m.Name == mapName).FirstOrDefault(); if (mapInfo == null) { Debug.Log(mapName + " not found. Searching for default map.."); mapName = "Default"; mapInfo = maps.Where(m => m.Name == mapName).FirstOrDefault(); if (mapInfo != null) { Debug.Log("Default map found"); } } } else { mapInfo = PlayerPrefs.HasKey("lastmap") ? maps.Where(m => m.Name == PlayerPrefs.GetString("lastmap")).FirstOrDefault() : maps.FirstOrDefault(); if (mapInfo == null) { mapInfo = maps.FirstOrDefault(); } } if (mapInfo == null) { Debug.LogWarning("No maps. Creating Default map..."); CreateDefaultMap((createDefaultError, defaultMapInfo) => { if (server.HasError(createDefaultError)) { error(createDefaultError); //progress.IsVisible = false; return; } mapName = defaultMapInfo.Name; mapInfo = defaultMapInfo; CreateRoom(mapName, playerIndex, botIndex, playersCount, botsCount, callback, error, null /*progress*/, server, gSettings, mapInfo); }); } else { mapName = mapInfo.Name; CreateRoom(mapName, playerIndex, botIndex, playersCount, botsCount, callback, error, null /*progress*/, server, gSettings, mapInfo); } }); } else { InitGame(mapName, playerIndex, botIndex, playersCount, botsCount, callback, error); } }
private static void InitGame(string mapName, int playerIndex, int botIndex, int playersCount, int botsCount, Action callback, Action <Error> error) { //IProgressIndicator progress = Dependencies.Progress; IGameServer server = Dependencies.GameServer; IGlobalSettings gSettings = Dependencies.Settings; if (playerIndex < playersCount) { server.Login(m_playerNames[playerIndex], "welcome", gSettings.ClientId, (e1, playerId, pwdHash) => { if (server.HasError(e1)) { error(e1); //progress.IsVisible = false; server.SignUp(m_playerNames[playerIndex], "welcome", gSettings.ClientId, (e100, p, pwdHash2) => { if (server.HasError(e100)) { error(e100); //progress.IsVisible = false; return; } LoginOrSignupCompleted(mapName, playerIndex, botIndex, playersCount, botsCount, callback, error); }); } else { LoginOrSignupCompleted(mapName, playerIndex, botIndex, playersCount, botsCount, callback, error); } }); } else { if (botIndex == botsCount) { Launch(mapName, callback, error, null, /*progress*/ server, gSettings); } else { server.CreateBot(gSettings.ClientId, m_playerNames[playersCount + botIndex], BotType.Hard, (e4, botId, roomWithBot) => { if (server.HasError(e4)) { error(e4); //progress.IsVisible = false; return; } botIndex++; if (botIndex == botsCount) { Launch(mapName, callback, error, null /*progress*/, server, gSettings); } else { InitGame(mapName, playerIndex, botIndex, playersCount, botsCount, callback, error); } }); } } }
private void GetPlayers() { m_progress.IsVisible = true; m_readyToGo = 0; for (int i = 0; i < m_playerMenu.Length; ++i) { m_playerMenu[i].LocalPlayerIndex = i; } for (int i = 0; i < m_playerMenu.Length; ++i) { m_playerMenu[i].Player = null; } IGameServer gameServer = Dependencies.GameServer; gameServer.GetPlayers(m_gSettings.ClientId, (error, players) => { if (gameServer.HasError(error)) { m_progress.IsVisible = false; m_notification.ShowError(error); return; } HandleDevicesChange(); int playersCount = Mathf.Min(m_inputManager.DeviceCount, players.Length); for (int i = 0; i < playersCount; ++i) { m_playerMenu[i].Player = players[i]; } for (int i = 0; i < m_inputManager.DeviceCount; ++i) { m_playerMenu[i].IsVirtualKeyboardEnabled = !m_inputManager.IsKeyboardAndMouse(i); } if (m_inputManager.DeviceCount < players.Length) { List <Guid> logoffPlayers = new List <Guid>(); for (int i = m_inputManager.DeviceCount; i < players.Length; ++i) { logoffPlayers.Add(players[i].Id); } gameServer.Logoff(m_gSettings.ClientId, logoffPlayers.ToArray(), (error2, playerIds) => { m_progress.IsVisible = false; if (gameServer.HasError(error2)) { m_notification.ShowError(error2); return; } }); } else { m_progress.IsVisible = false; } for (int i = players.Length; i < m_inputManager.DeviceCount; ++i) { m_playerMenu[i].TryAutoLogin(); } }); }