public async Task JoinParty(string partyName, string spectatorCode, string playerObj) { // if spectatorCode is empty, fetch it if (string.IsNullOrEmpty(spectatorCode)) { spectatorCode = SpectatorHelper.ToSpectatorCode(partyName, _key); } // if partyname is empty, fetch it else if (string.IsNullOrEmpty(partyName)) { partyName = SpectatorHelper.ToPartyName(spectatorCode, _key); } var player = CompressionHelper.Decompress <PlayerModel>(playerObj); player.ConnectionID = Context.ConnectionId; var update = await _mongoRepository.UpdatePartyNameInConnectionIndex(ConnectionId, partyName); if (update == null) { await AddToIndex(partyName); } var party = await _mongoRepository.GetParty(partyName); if (party == null) { if (player.IsSpectator) { await Clients.Caller.SendAsync("GroupNotFoundOrEmpty"); } else { player.IsLeader = true; party = new PartyModel() { Name = partyName, Players = new List <PlayerModel> { player }, SpectatorCode = spectatorCode }; await _mongoRepository.CreateParty(party); party.Name = ""; await Clients.Caller.SendAsync("EnteredParty", CompressionHelper.Compress(party), CompressionHelper.Compress(player)); } } else { if (player.IsSpectator && !party.Players.Any(x => !x.IsSpectator)) { await Clients.Caller.SendAsync("GroupNotFoundOrEmpty"); } else { var oldPlayer = party.Players.FirstOrDefault(x => x.ConnectionID == player.ConnectionID); // if the party were joining doesnt have a leader, make the player leader if (!party.Players.Any(x => x.IsLeader)) { player.IsLeader = true; } if (oldPlayer == null) { await _mongoRepository.AddPlayerToParty(partyName, player); } else { await _mongoRepository.UpdatePlayerInParty(partyName, player); } party = await _mongoRepository.GetParty(partyName); party.Name = ""; await Clients.Caller.SendAsync("EnteredParty", CompressionHelper.Compress(party), CompressionHelper.Compress(player)); } } await Groups.AddToGroupAsync(Context.ConnectionId, partyName); await Clients.OthersInGroup(partyName).SendAsync("PlayerJoined", CompressionHelper.Compress(player)); if (!player.IsSpectator) { await Clients.Group(partyName).SendAsync("PlayerUpdated", CompressionHelper.Compress(player)); } }