Esempio n. 1
0
        public void ReturnEmptyResponseWhenSuccessfullyDeletingInvite()
        {
            // Setup the client such that it will successfully delete the invite.
            var senderInvites = new PlayerInvites(SenderPlayerId);

            senderInvites.OutboundInviteIds.Add(_invite.Id);
            var receiverInvites = new PlayerInvites(ReceiverPlayerId);

            receiverInvites.InboundInviteIds.Add(_invite.Id);

            var entriesToDelete = new List <Entry>();
            var entriesToUpdate = new List <Entry>();

            _mockMemoryStoreClient.Setup(client => client.GetAsync <InviteDataModel>(_invite.Id)).ReturnsAsync(_invite);
            _mockMemoryStoreClient.Setup(client => client.GetAsync <PlayerInvites>(SenderPlayerId))
            .ReturnsAsync(senderInvites);
            _mockMemoryStoreClient.Setup(client => client.GetAsync <PlayerInvites>(ReceiverPlayerId))
            .ReturnsAsync(receiverInvites);
            _mockTransaction.Setup(tr => tr.DeleteAll(It.IsAny <IEnumerable <Entry> >()))
            .Callback <IEnumerable <Entry> >(entries => entriesToDelete.AddRange(entries));
            _mockTransaction.Setup(tr => tr.UpdateAll(It.IsAny <IEnumerable <Entry> >()))
            .Callback <IEnumerable <Entry> >(entries => entriesToUpdate.AddRange(entries));
            _mockTransaction.Setup(tr => tr.Dispose());

            // Verify that an empty response was returned.
            var context = Util.CreateFakeCallContext(SenderPlayerId, "");
            var request = new DeleteInviteRequest {
                InviteId = _invite.Id
            };

            Assert.AreEqual(new DeleteInviteResponse(), _inviteService.DeleteInvite(request, context).Result);

            // Verify that the invite was deleted.
            Assert.AreEqual(1, entriesToDelete.Count);
            Assert.IsInstanceOf <InviteDataModel>(entriesToDelete[0]);
            var inviteDeleted = (InviteDataModel)entriesToDelete[0];

            Assert.AreEqual(_invite.Id, inviteDeleted.Id);
            Assert.AreEqual(_invite.SenderId, inviteDeleted.SenderId);
            Assert.AreEqual(_invite.ReceiverId, inviteDeleted.ReceiverId);
            Assert.AreEqual(_invite.PartyId, inviteDeleted.PartyId);

            // Verify that the sender's and receiver's player invites have been updated accordingly.
            Assert.AreEqual(2, entriesToUpdate.Count);
            Assert.IsInstanceOf <PlayerInvites>(entriesToUpdate[0]);
            Assert.IsInstanceOf <PlayerInvites>(entriesToUpdate[1]);

            var updatedSenderInvites = (PlayerInvites)entriesToUpdate[0];

            Assert.AreEqual(SenderPlayerId, updatedSenderInvites.Id);
            Assert.That(updatedSenderInvites.OutboundInviteIds, Does.Not.Contain(_invite.Id));

            var updatedReceiverInvites = (PlayerInvites)entriesToUpdate[1];

            Assert.AreEqual(ReceiverPlayerId, updatedReceiverInvites.Id);
            Assert.That(updatedReceiverInvites.InboundInviteIds, Does.Not.Contain(_invite.Id));
        }
Esempio n. 2
0
        public void SetUp()
        {
            _partyToJoin   = new PartyDataModel(TestLeaderId, Pit);
            _playerInvites = new PlayerInvites(TestPlayerId, ImmutableHashSet.Create("invite"), ImmutableHashSet <string> .Empty);
            _invite        = new Invite(TestLeaderId, TestPlayerId, _partyToJoin.Id);

            _mockTransaction       = new Mock <ITransaction>(MockBehavior.Strict);
            _mockMemoryStoreClient = new Mock <IMemoryStoreClient>(MockBehavior.Strict);
            _mockMemoryStoreClient.Setup(client => client.Dispose()).Verifiable();
            _mockMemoryStoreClient.Setup(client => client.CreateTransaction()).Returns(_mockTransaction.Object);

            var memoryStoreClientManager = new Mock <IMemoryStoreClientManager <IMemoryStoreClient> >(MockBehavior.Strict);

            memoryStoreClientManager.Setup(manager => manager.GetClient()).Returns(_mockMemoryStoreClient.Object);
            _partyService = new PartyServiceImpl(memoryStoreClientManager.Object);
        }
        public void ReturnAnEquivalentPlayerInvites()
        {
            var playerInvites = new PlayerInvites("player");

            playerInvites.InboundInviteIds.Add("in1");
            playerInvites.InboundInviteIds.Add("in2");
            playerInvites.OutboundInviteIds.Add("out1");
            playerInvites.OutboundInviteIds.Add("out2");

            var serializedPlayerInvites   = JsonConvert.SerializeObject(playerInvites);
            var deserializedPlayerInvites = JsonConvert.DeserializeObject <PlayerInvites>(serializedPlayerInvites);

            Assert.AreEqual(playerInvites.Id, deserializedPlayerInvites.Id);
            CollectionAssert.AreEquivalent(playerInvites.InboundInviteIds, deserializedPlayerInvites.InboundInviteIds);
            CollectionAssert.AreEquivalent(playerInvites.OutboundInviteIds,
                                           deserializedPlayerInvites.OutboundInviteIds);
        }
Esempio n. 4
0
        public void SetUp()
        {
            _storedOutboundInvite = new InviteDataModel(PlayerId1, PlayerId2, PartyId1);
            _storedInboundInvite  = new InviteDataModel(PlayerId2, PlayerId3, PartyId2);
            _storedPlayerInvites  = new PlayerInvites(PlayerId2);
            _storedPlayerInvites.InboundInviteIds.Add(_storedInboundInvite.Id);
            _storedPlayerInvites.OutboundInviteIds.Add(_storedOutboundInvite.Id);

            _mockTransaction       = new Mock <ITransaction>(MockBehavior.Strict);
            _mockMemoryStoreClient = new Mock <IMemoryStoreClient>(MockBehavior.Strict);
            _mockMemoryStoreClient.Setup(client => client.CreateTransaction()).Returns(_mockTransaction.Object);
            _mockMemoryStoreClient.Setup(client => client.Dispose()).Verifiable();

            var memoryStoreClientManager = new Mock <IMemoryStoreClientManager <IMemoryStoreClient> >(MockBehavior.Strict);

            memoryStoreClientManager.Setup(manager => manager.GetClient()).Returns(_mockMemoryStoreClient.Object);
            _inviteService = new InviteServiceImpl(memoryStoreClientManager.Object, new NullAnalyticsSender());
        }
        public override async Task <CreateInviteResponse> CreateInvite(CreateInviteRequest request, ServerCallContext context)
        {
            var playerId = AuthHeaders.ExtractPlayerId(context);

            if (string.IsNullOrEmpty(request.ReceiverPlayerId))
            {
                throw new RpcException(
                          new Status(StatusCode.InvalidArgument, "Expected a non-empty receiver player id"));
            }

            using (var memClient = _memoryStoreClientManager.GetClient())
            {
                var party = await GetPartyByPlayerId(memClient, playerId);

                // This extra check is necessary because the player might have meanwhile left the party (between the
                // Get<Member> and Get<PartyDataModel> calls).
                if (party?.GetMember(playerId) == null)
                {
                    throw new RpcException(new Status(StatusCode.FailedPrecondition,
                                                      "The player creating this invite is not a member of any party"));
                }

                if (party.GetMember(request.ReceiverPlayerId) != null)
                {
                    throw new RpcException(new Status(StatusCode.FailedPrecondition,
                                                      "The receiving player is already a member of the party"));
                }

                var entitiesToCreate = new List <Entry>();
                var entitiesToUpdate = new List <Entry>();

                var invite = new InviteDataModel(playerId, request.ReceiverPlayerId, party.Id, request.Metadata);
                entitiesToCreate.Add(invite);

                var senderPlayerInvites = await memClient.GetAsync <PlayerInvites>(playerId);

                if (senderPlayerInvites == null)
                {
                    senderPlayerInvites = new PlayerInvites(playerId);
                    entitiesToCreate.Add(senderPlayerInvites);
                }
                else
                {
                    entitiesToUpdate.Add(senderPlayerInvites);
                }

                senderPlayerInvites.OutboundInviteIds.Add(invite.Id);

                var receiverPlayerInvites = await memClient.GetAsync <PlayerInvites>(request.ReceiverPlayerId);

                if (receiverPlayerInvites == null)
                {
                    receiverPlayerInvites = new PlayerInvites(request.ReceiverPlayerId);
                    entitiesToCreate.Add(receiverPlayerInvites);
                }
                else
                {
                    entitiesToUpdate.Add(receiverPlayerInvites);
                }

                receiverPlayerInvites.InboundInviteIds.Add(invite.Id);

                using (var transaction = memClient.CreateTransaction())
                {
                    transaction.CreateAll(entitiesToCreate);
                    transaction.UpdateAll(entitiesToUpdate);
                }

                return(new CreateInviteResponse {
                    InviteId = invite.Id
                });
            }
        }