private async Task AddPlayer(PlayerInfo pi) { pi.client.OnClientClose += () => { _ = PlayerClose(pi); }; pi.playerData.id = playerIndex; playerIndex++; pi.playerData.telemetry.pid = pi.playerData.id; await pi.client.SendAsync(new NetworkDataPlayerInfo(pi.playerData)); pi.client.OnMessageReceived += OnMessage; using (var l = await playerData.WaitAsync()) { l.Value.Add(pi.playerData.id, pi); } using (var l = await channels.WaitAsync()) { l.Value[pi.playerData.channel].players++; Console.WriteLine("added player count " + l.Value[pi.playerData.channel].players); } using (var l = await channelPlayerData.WaitAsync()) { l.Value[pi.playerData.channel].Add(pi); } using (var l = await clientData.WaitAsync()) { l.Value.Add(pi.client, pi); } }
private async Task CreateChannels() { playerData = new Locked <Dictionary <int, PlayerInfo> >(new Dictionary <int, PlayerInfo>()); clientData = new Locked <Dictionary <NetworkClient, PlayerInfo> >(new Dictionary <NetworkClient, PlayerInfo>()); channelPlayerData = new Locked <List <PlayerInfo>[]>(new List <PlayerInfo> [MAX_CHANNELS]); channels = new Locked <ChannelInfo[]>(new ChannelInfo[MAX_CHANNELS]); channelTask = new Task[MAX_CHANNELS]; using (var l = await channels.WaitAsync()) { for (int i = 0; i < MAX_CHANNELS; i++) { l.Value[i] = new ChannelInfo { players = 0, channelStatus = ChannelStatus.Open, maxPlayers = MAX_PLAYERS }; } } using (var l = await channelPlayerData.WaitAsync()) { for (int i = 0; i < MAX_CHANNELS; i++) { l.Value[i] = new List <PlayerInfo>(); channelTask[i] = ChannelUpdates(i); } } }
public async Task SelectChannel(NetworkClient client, string name) { using (var channelData = await channels.WaitAsync()) { _ = client.SendAsync(new NetworkDataChannelList(channelData.Value)); } NetworkData nd = await client.ReadAsync(); if (nd is NetworkDataChannelSelect sel) { using (var channelData = await channels.WaitAsync()) { if (sel.channel >= MAX_CHANNELS || sel.channel < 0) { client.Close(); return; } else { if (channelData.Value[sel.channel].channelStatus != ChannelStatus.Open) { client.Close(); return; } } } await AddPlayer(new PlayerInfo() { client = client, playerData = new PlayerData() { name = name, channel = sel.channel } }); } else { client.Close(); } }