public virtual async void SetSelectedSongAsync(PlayerInfo sender, SongInfo song) { if (sender.Equals(roomHost)) { selectedSong = song; NetOutgoingMessage outMsg = HubListener.ListenerServer.CreateMessage(); if (selectedSong == null) { switch (roomSettings.SelectionType) { case SongSelectionType.Manual: { roomState = RoomState.SelectingSong; outMsg.Write((byte)CommandType.SetSelectedSong); outMsg.Write((int)0); BroadcastPacket(outMsg, NetDeliveryMethod.ReliableOrdered); BroadcastWebSocket(CommandType.SetSelectedSong, null); } break; case SongSelectionType.Random: { roomState = RoomState.Preparing; Random rand = new Random(); randomSongTask = BeatSaver.GetRandomSong(); selectedSong = await randomSongTask; randomSongTask = null; outMsg.Write((byte)CommandType.SetSelectedSong); selectedSong.AddToMessage(outMsg); BroadcastPacket(outMsg, NetDeliveryMethod.ReliableOrdered); BroadcastWebSocket(CommandType.SetSelectedSong, selectedSong); ReadyStateChanged(roomHost, true); } break; } } else { roomState = RoomState.Preparing; outMsg.Write((byte)CommandType.SetSelectedSong); selectedSong.AddToMessage(outMsg); BroadcastPacket(outMsg, NetDeliveryMethod.ReliableOrdered); BroadcastWebSocket(CommandType.SetSelectedSong, selectedSong); ReadyStateChanged(roomHost, true); } } else { Logger.Instance.Warning($"{sender.playerName}:{sender.playerId} tried to select song, but he is not the host"); } }
public async void StartChannel(int newChannelId) { channelId = newChannelId; channelInfo = new ChannelInfo() { channelId = channelId, name = Settings.Instance.Radio.RadioChannels[channelId].ChannelName, currentSong = null, currentLevelOptions = new LevelOptionsInfo(Settings.Instance.Radio.RadioChannels[channelId].PreferredDifficulty, new GameplayModifiers() { noFail = true }, "Standard"), playerCount = 0, iconUrl = Settings.Instance.Radio.RadioChannels[channelId].ChannelIconUrl, state = ChannelState.NextSong, ip = "", port = 0 }; if (File.Exists($"RadioQueue{channelId}.json")) { try { Queue <SongInfo> queue = JsonConvert.DeserializeObject <Queue <SongInfo> >(File.ReadAllText($"RadioQueue{channelId}.json")); if (queue != null) { radioQueue = queue; } } catch (Exception e) { Logger.Instance.Warning("Unable to load radio queue! Exception: " + e); } } if (radioQueue.Count > 0) { channelInfo.currentSong = radioQueue.Dequeue(); try { File.WriteAllText($"RadioQueue{channelId}.json", JsonConvert.SerializeObject(radioQueue, Formatting.Indented)); } catch { } } else { if (Settings.Instance.Radio.RadioChannels[channelId].DefaultSongIDs.Count > 0) { defaultSongs = await BeatSaver.ConvertSongIDsAsync(Settings.Instance.Radio.RadioChannels[channelId].DefaultSongIDs); channelInfo.currentSong = defaultSongs.Random(); } else { channelInfo.currentSong = await BeatSaver.GetRandomSong(); } } HighResolutionTimer.LoopTimer.Elapsed += RadioLoop; }
public async void RadioLoop(object sender, HighResolutionTimerElapsedEventArgs e) { if (randomSongTask != null) { return; } channelInfo.playerCount = radioClients.Count; channelInfo.name = Settings.Instance.Radio.RadioChannels[channelId].ChannelName; channelInfo.iconUrl = Settings.Instance.Radio.RadioChannels[channelId].ChannelIconUrl; if (radioClients.Count == 0) { channelInfo.state = ChannelState.NextSong; return; } NetOutgoingMessage outMsg = HubListener.ListenerServer.CreateMessage(); switch (channelInfo.state) { case ChannelState.InGame: //--> Results { if (DateTime.Now.Subtract(songStartTime).TotalSeconds >= channelInfo.currentSong.songDuration) { channelInfo.state = ChannelState.Results; resultsStartTime = DateTime.Now; outMsg.Write((byte)CommandType.GetChannelInfo); outMsg.Write((byte)0); channelInfo.AddToMessage(outMsg); BroadcastPacket(outMsg, NetDeliveryMethod.ReliableOrdered); if (radioClients.Count > 0) { BroadcastWebSocket(CommandType.GetChannelInfo, channelInfo); } } } break; case ChannelState.Results: //--> NextSong { if (DateTime.Now.Subtract(resultsStartTime).TotalSeconds >= Settings.Instance.Radio.ResultsShowTime) { channelInfo.state = ChannelState.NextSong; if (radioQueue.Count > 0) { channelInfo.currentSong = radioQueue.Dequeue(); try { File.WriteAllText($"RadioQueue{channelId}.json", JsonConvert.SerializeObject(radioQueue, Formatting.Indented)); } catch { } } else { if (defaultSongs.Count > 1) { channelInfo.currentSong = defaultSongs.Random(lastSong); } else { randomSongTask = BeatSaver.GetRandomSong(); channelInfo.currentSong = await randomSongTask; randomSongTask = null; } lastSong = channelInfo.currentSong; } outMsg.Write((byte)CommandType.SetSelectedSong); channelInfo.currentSong.AddToMessage(outMsg); BroadcastPacket(outMsg, NetDeliveryMethod.ReliableOrdered); nextSongScreenStartTime = DateTime.Now; if (radioClients.Count > 0) { BroadcastWebSocket(CommandType.SetSelectedSong, channelInfo.currentSong); } } } break; case ChannelState.NextSong: //--> InGame { if (DateTime.Now.Subtract(nextSongScreenStartTime).TotalSeconds >= Settings.Instance.Radio.NextSongPrepareTime) { channelInfo.state = ChannelState.InGame; outMsg.Write((byte)CommandType.StartLevel); //outMsg.Write((byte)Settings.Instance.Radio.RadioChannels[channelId].PreferredDifficulty); channelInfo.currentLevelOptions.AddToMessage(outMsg); channelInfo.currentSong.songDuration = Misc.Math.Median(songDurationResponses.Values.ToArray()); songDurationResponses.Clear(); requestingSongDuration = false; channelInfo.currentSong.AddToMessage(outMsg); BroadcastPacket(outMsg, NetDeliveryMethod.ReliableOrdered); songStartTime = DateTime.Now; if (radioClients.Count > 0) { BroadcastWebSocket(CommandType.StartLevel, new SongWithOptions(channelInfo.currentSong, channelInfo.currentLevelOptions)); } } else if (DateTime.Now.Subtract(nextSongScreenStartTime).TotalSeconds >= Settings.Instance.Radio.NextSongPrepareTime * 0.75 && !requestingSongDuration) { outMsg.Write((byte)CommandType.GetSongDuration); channelInfo.currentSong.AddToMessage(outMsg); songDurationResponses.Clear(); requestingSongDuration = true; BroadcastPacket(outMsg, NetDeliveryMethod.ReliableOrdered); if (radioClients.Count > 0) { BroadcastWebSocket(CommandType.GetSongDuration, channelInfo.currentSong); } } } break; } if (outMsg.LengthBytes > 0) { outMsg = HubListener.ListenerServer.CreateMessage(); } outMsg.Write((byte)CommandType.UpdatePlayerInfo); switch (channelInfo.state) { case ChannelState.NextSong: { outMsg.Write((float)DateTime.Now.Subtract(nextSongScreenStartTime).TotalSeconds); outMsg.Write(Settings.Instance.Radio.NextSongPrepareTime); } break; case ChannelState.InGame: { outMsg.Write((float)DateTime.Now.Subtract(songStartTime).TotalSeconds); outMsg.Write(channelInfo.currentSong.songDuration); } break; case ChannelState.Results: { outMsg.Write((float)DateTime.Now.Subtract(resultsStartTime).TotalSeconds); outMsg.Write(Settings.Instance.Radio.ResultsShowTime); } break; } outMsg.Write(radioClients.Count); radioClients.ForEach(x => x.playerInfo.AddToMessage(outMsg)); BroadcastPacket(outMsg, NetDeliveryMethod.UnreliableSequenced); if (radioClients.Count > 0) { BroadcastWebSocket(CommandType.UpdatePlayerInfo, radioClients.Select(x => x.playerInfo).ToArray()); } }
public virtual async void RoomLoopAsync(object sender, HighResolutionTimerElapsedEventArgs e) { if (randomSongTask != null) { return; } NetOutgoingMessage outMsg = HubListener.ListenerServer.CreateMessage(); switch (roomState) { case RoomState.InGame: { if (DateTime.Now.Subtract(_songStartTime).TotalSeconds >= selectedSong.songDuration) { roomState = RoomState.Results; _resultsStartTime = DateTime.Now; outMsg.Write((byte)CommandType.GetRoomInfo); GetRoomInfo().AddToMessage(outMsg); BroadcastPacket(outMsg, NetDeliveryMethod.ReliableOrdered); if (roomClients.Count > 0) { BroadcastWebSocket(CommandType.GetRoomInfo, GetRoomInfo()); } } } break; case RoomState.Results: { if (DateTime.Now.Subtract(_resultsStartTime).TotalSeconds >= resultsShowTime) { roomState = RoomState.SelectingSong; selectedSong = null; outMsg.Write((byte)CommandType.SetSelectedSong); outMsg.Write((int)0); BroadcastPacket(outMsg, NetDeliveryMethod.ReliableOrdered); BroadcastWebSocket(CommandType.SetSelectedSong, null); } } break; case RoomState.SelectingSong: { switch (roomSettings.SelectionType) { case SongSelectionType.Random: { roomState = RoomState.Preparing; Random rand = new Random(); randomSongTask = BeatSaver.GetRandomSong(); selectedSong = await randomSongTask; randomSongTask = null; outMsg.Write((byte)CommandType.SetSelectedSong); selectedSong.AddToMessage(outMsg); BroadcastPacket(outMsg, NetDeliveryMethod.ReliableOrdered); BroadcastWebSocket(CommandType.SetSelectedSong, selectedSong); ReadyStateChanged(roomHost, true); } break; } } break; } if (outMsg.LengthBytes > 0) { outMsg = HubListener.ListenerServer.CreateMessage(); } outMsg.Write((byte)CommandType.UpdatePlayerInfo); switch (roomState) { case RoomState.SelectingSong: { outMsg.Write((float)0); outMsg.Write((float)0); } break; case RoomState.Preparing: { outMsg.Write((float)0); outMsg.Write((float)0); } break; case RoomState.InGame: { outMsg.Write((float)DateTime.Now.Subtract(_songStartTime).TotalSeconds); outMsg.Write(selectedSong.songDuration); } break; case RoomState.Results: { outMsg.Write((float)DateTime.Now.Subtract(_resultsStartTime).TotalSeconds); outMsg.Write(resultsShowTime); } break; } outMsg.Write(roomClients.Count); for (int i = 0; i < roomClients.Count; i++) { if (i < roomClients.Count) { if (roomClients[i] != null) { roomClients[i].playerInfo.AddToMessage(outMsg); } } } BroadcastPacket(outMsg, NetDeliveryMethod.UnreliableSequenced); if (roomClients.Count > 0) { BroadcastWebSocket(CommandType.UpdatePlayerInfo, roomClients.Select(x => x.playerInfo).ToArray()); } }