List <Data> GetServers(int Offset, int count = 6) { var index = (count - 1) * Offset; return(ConnectedClients.Select(o => o.Data).Where(o => o.ID != -1).ToList().GetRange(index, count + index >= ConnectedClients.Count(o => o.Data.ID != -1) ? Math.Clamp(ConnectedClients.Count(o => o.Data.ID != -1) - index, 0, 6) : count + index)); }
void ServerStateControllerThread() { Stopwatch _timer = new Stopwatch(); _timer.Start(); int _timerSeconds = 0; TimeSpan _lastTime = new TimeSpan(); float lobbyTimer = 0; float sendTimer = 0; int lobbyTime = Settings.Instance.Server.LobbyTime; TimeSpan deltaTime; while (ServerStateThread.IsAlive) { deltaTime = (_timer.Elapsed - _lastTime); _lastTime = _timer.Elapsed; switch (serverState) { case ServerState.Lobby: { lobbyTimer += (float)deltaTime.TotalSeconds; if (clients.Count == 0) { lobbyTimer = 0; } if ((int)Math.Ceiling(lobbyTimer) > _timerSeconds && _timerSeconds > -1) { _timerSeconds = Math.Ceiling(lobbyTimer); SendToAllClients(JsonConvert.SerializeObject( new ServerCommand(ServerCommandType.SetLobbyTimer, Math.Max(lobbyTime - _timerSeconds, 0)))); } if (lobbyTimer >= lobbyTime / 2 && currentSongIndex == -1) { currentSongIndex = lastSelectedSong; currentSongIndex++; if (currentSongIndex >= availableSongs.Count) { currentSongIndex = 0; } SendToAllClients(JsonConvert.SerializeObject(new ServerCommand( ServerCommandType.SetSelectedSong, _selectedLevelID: availableSongs[currentSongIndex].levelId, _difficulty: GetPreferredDifficulty(availableSongs[currentSongIndex])))); } if (lobbyTimer >= lobbyTime) { SendToAllClients(JsonConvert.SerializeObject(new ServerCommand( ServerCommandType.SetSelectedSong, _selectedLevelID: availableSongs[currentSongIndex].levelId, _difficulty: GetPreferredDifficulty(availableSongs[currentSongIndex])))); SendToAllClients( JsonConvert.SerializeObject( new ServerCommand(ServerCommandType.StartSelectedSongLevel))); serverState = ServerState.Playing; Logger.Instance.Log("Starting song " + availableSongs[currentSongIndex].songName + " " + availableSongs[currentSongIndex].songSubName + "..."); _timerSeconds = 0; lobbyTimer = 0; } } ; break; case ServerState.Playing: { sendTimer += (float)deltaTime.TotalSeconds; playTime += deltaTime; if (sendTimer >= 1f) { SendToAllClients(JsonConvert.SerializeObject(new ServerCommand( ServerCommandType.SetPlayerInfos, _playerInfos: (clients.Where(x => x.playerInfo != null) .OrderByDescending(x => x.playerInfo.playerScore) .Select(x => JsonConvert.SerializeObject(x.playerInfo))).ToArray()))); sendTimer = 0f; } if (playTime.TotalSeconds >= availableSongs[currentSongIndex].duration.TotalSeconds + 5f) { playTime = new TimeSpan(); sendTimer = 0f; serverState = ServerState.Lobby; lastSelectedSong = currentSongIndex; currentSongIndex = -1; Logger.Instance.Log("Returning to lobby..."); } if (clients.Count == 0 && playTime.TotalSeconds > 5) { playTime = new TimeSpan(); sendTimer = 0f; serverState = ServerState.Lobby; lastSelectedSong = currentSongIndex; currentSongIndex = -1; Logger.Instance.Log("Returning to lobby(NO PLAYERS)..."); } } ; break; } Thread.Sleep(2); } }
void ServerLoop() { Stopwatch _timer = new Stopwatch(); _timer.Start(); int _timerSeconds = 0; TimeSpan _lastTime = new TimeSpan(); float lobbyTimer = 0; float sendTimer = 0; float sendTime = 1f / 20; int lobbyTime = Settings.Instance.Server.LobbyTime; TimeSpan deltaTime; while (ServerLoopThread.IsAlive) { deltaTime = (_timer.Elapsed - _lastTime); _lastTime = _timer.Elapsed; try { switch (serverState) { case ServerState.Preparing: case ServerState.Voting: { sendTimer += (float)deltaTime.TotalSeconds; lobbyTimer += (float)deltaTime.TotalSeconds; if (clients.Count == 0) { lobbyTimer = 0; } if (Math.Ceiling(lobbyTimer) > _timerSeconds && _timerSeconds > -1) { _timerSeconds = Math.Ceiling(lobbyTimer); SendToAllClients( new ServerCommand(ServerCommandType.SetLobbyTimer, Math.Max(lobbyTime - _timerSeconds, 0))); } if (sendTimer >= sendTime) { SendToAllClients(new ServerCommand( ServerCommandType.SetPlayerInfos, _playerInfos: (clients.Where(x => x != null && x.playerInfo != null) .Select(x => JsonConvert.SerializeObject(x.playerInfo))).ToArray() )); sendTimer = 0f; } if (currentSongIndex == -1 && availableSongs.Count > 0) { switch (Settings.Instance.AvailableSongs.SongOrder) { case Settings.SongOrder.Voting: { if (lobbyTimer >= lobbyTime / 2) { if (clients.Any(x => x.votedFor != null)) { string selectedLevelId = clients.Where(x => x.votedFor != null).Select(x => x.votedFor).GroupBy(v => v).OrderByDescending(g => g.Count()).First().Key.levelId; CustomSongInfo song = availableSongs.FirstOrDefault(x => x.levelId == selectedLevelId); if (song != null) { currentSongIndex = availableSongs.IndexOf(song); } else { currentSongIndex = lastSelectedSong + 1; } } else { currentSongIndex = lastSelectedSong + 1; } } }; break; case Settings.SongOrder.Shuffle: { Random rand = new Random(); currentSongIndex = rand.Next(availableSongs.Count); if (currentSongIndex == lastSelectedSong) { currentSongIndex = lastSelectedSong + 1; } }; break; case Settings.SongOrder.List: { currentSongIndex = lastSelectedSong + 1; }; break; case Settings.SongOrder.Manual: { serverState = ServerState.Preparing; lobbyTimer = 0; }; break; } if (currentSongIndex >= availableSongs.Count) { currentSongIndex = 0; } if (currentSongIndex != -1) { serverState = ServerState.Preparing; SendToAllClients(new ServerCommand( ServerCommandType.SetSelectedSong, _difficulty: GetPreferredDifficulty(availableSongs[currentSongIndex])), wss); Logger.Instance.Log($"Next song is {availableSongs[currentSongIndex].songName} {availableSongs[currentSongIndex].songSubName}"); } } if (lobbyTimer >= lobbyTime) { if (availableSongs.Count > 0) { SendToAllClients(new ServerCommand( ServerCommandType.StartSelectedSongLevel, _difficulty: GetPreferredDifficulty(availableSongs[currentSongIndex])), wss); serverState = ServerState.Playing; Logger.Instance.Log("Starting song " + availableSongs[currentSongIndex].songName + " " + availableSongs[currentSongIndex].songSubName + "..."); } _timerSeconds = 0; lobbyTimer = 0; } }; break; case ServerState.Playing: { sendTimer += (float)deltaTime.TotalSeconds; playTime += deltaTime; if (sendTimer >= sendTime) { SendToAllClients(new ServerCommand( ServerCommandType.SetPlayerInfos, _playerInfos: (clients.Where(x => x.playerInfo != null) .OrderByDescending(x => x.playerInfo.playerScore) .Select(x => JsonConvert.SerializeObject(x.playerInfo))).ToArray(), _selectedSongDuration: availableSongs[currentSongIndex].duration.TotalSeconds, _selectedSongPlayTime: playTime.TotalSeconds), wss); sendTimer = 0f; } if (playTime.TotalSeconds >= availableSongs[currentSongIndex].duration.TotalSeconds + 15f) { playTime = new TimeSpan(); sendTimer = 0f; serverState = ServerState.Voting; lastSelectedSong = currentSongIndex; currentSongIndex = -1; Logger.Instance.Log("Returning to lobby..."); SendToAllClients(new ServerCommand(ServerCommandType.SetServerState)); } if (clients.Count(x => x.state == ClientState.Playing) == 0 && playTime.TotalSeconds > 5 && playTime.TotalSeconds <= availableSongs[currentSongIndex].duration.TotalSeconds - 10f) { playTime = new TimeSpan(); sendTimer = 0f; serverState = ServerState.Voting; lastSelectedSong = currentSongIndex; currentSongIndex = -1; Logger.Instance.Log("Returning to lobby (NO PLAYERS)..."); SendToAllClients(new ServerCommand(ServerCommandType.SetServerState)); } }; break; } Console.Title = string.Format(TitleFormat, Settings.Instance.Server.ServerName, clients.Count); }catch (Exception e) { Logger.Instance.Error($"Server loop exception: {e}"); } Thread.Sleep(5); } }