private void SetupAnalyticsExpectation(InviteDataModel expectedCreatedInvite)
 {
     _mockAnalyticsSender.Setup(sender =>
                                sender.Send(AnalyticsConstants.InviteClass, AnalyticsEventType,
                                            new Dictionary <string, string> {
         { AnalyticsConstants.PartyId, _party.Id },
         { AnalyticsConstants.PlayerIdInviter, SenderPlayerId },
         { AnalyticsConstants.InviteId, expectedCreatedInvite.Id }
     }, ReceiverPlayerId));
 }
 private static InviteProto ConvertToProto(InviteDataModel invite)
 {
     return(new InviteProto
     {
         Id = invite.Id,
         SenderPlayerId = invite.SenderId,
         ReceiverPlayerId = invite.ReceiverId,
         PartyId = invite.PartyId,
         Metadata = { invite.Metadata },
         CurrentStatus = ConvertToProto(invite.CurrentStatus)
     });
 }
Пример #3
0
        public void SetUp()
        {
            _invite = new InviteDataModel(SenderPlayerId, ReceiverPlayerId, PartyId);

            _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);
        }
Пример #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 void ReturnInviteIdWhenCreatingInviteSuccessfullyWithExistingPlayerInvites()
        {
            var expectedCreatedInvite = new InviteDataModel(SenderPlayerId, ReceiverPlayerId, _party.Id, _metadata);

            // Setup the client so that it will successfully create an invite.
            _mockMemoryStoreClient.Setup(client => client.GetAsync <Member>(SenderPlayerId))
            .ReturnsAsync(_party.GetMember(SenderPlayerId));
            _mockMemoryStoreClient.Setup(client => client.GetAsync <PartyDataModel>(_party.Id)).ReturnsAsync(_party);
            _mockMemoryStoreClient.Setup(client => client.GetAsync <InviteDataModel>(expectedCreatedInvite.Id))
            .ReturnsAsync((InviteDataModel)null);
            _mockMemoryStoreClient.Setup(client => client.GetAsync <PlayerInvites>(SenderPlayerId))
            .ReturnsAsync(new PlayerInvites(SenderPlayerId));
            _mockMemoryStoreClient.Setup(client => client.GetAsync <PlayerInvites>(ReceiverPlayerId))
            .ReturnsAsync(new PlayerInvites(ReceiverPlayerId));

            var entriesCreated = new List <Entry>();
            var entriesUpdated = new List <Entry>();

            _mockTransaction.Setup(tr => tr.CreateAll(It.IsAny <IEnumerable <Entry> >()))
            .Callback <IEnumerable <Entry> >(entries => entriesCreated.AddRange(entries));
            _mockTransaction.Setup(tr => tr.UpdateAll(It.IsAny <IEnumerable <Entry> >()))
            .Callback <IEnumerable <Entry> >(entries => entriesUpdated.AddRange(entries));
            _mockTransaction.Setup(tr => tr.Dispose());
            SetupAnalyticsExpectation(expectedCreatedInvite);

            // Check that the RPC has completely successfully and that an empty response was returned.
            var context = Util.CreateFakeCallContext(SenderPlayerId, "");
            var request = new CreateInviteRequest {
                ReceiverPlayerId = ReceiverPlayerId
            };

            request.Metadata.Add(_metadata);
            var response = _inviteService.CreateInvite(request, context).Result;

            Assert.AreEqual(expectedCreatedInvite.Id, response.InviteId);

            // Check that an Invite entry has been created.
            Assert.AreEqual(1, entriesCreated.Count);
            Assert.IsInstanceOf <InviteDataModel>(entriesCreated[0]);

            var inviteCreated = (InviteDataModel)entriesCreated[0];

            Assert.AreEqual(SenderPlayerId, inviteCreated.SenderId);
            Assert.AreEqual(ReceiverPlayerId, inviteCreated.ReceiverId);
            Assert.AreEqual(_party.Id, inviteCreated.PartyId);
            Assert.AreEqual(InviteDataModel.Status.Pending, inviteCreated.CurrentStatus);
            CollectionAssert.AreEquivalent(_metadata, inviteCreated.Metadata);

            // Verify that the player invites for the sender and receiver have been updated since they already existed
            // in the memory store.
            Assert.AreEqual(2, entriesUpdated.Count);
            var senderPlayerInvites = (PlayerInvites)entriesUpdated[0];

            Assert.AreEqual(SenderPlayerId, senderPlayerInvites.Id);
            Assert.That(senderPlayerInvites.OutboundInviteIds, Contains.Item(expectedCreatedInvite.Id));

            Assert.IsInstanceOf <PlayerInvites>(entriesUpdated[1]);
            var receiverPlayerInvites = (PlayerInvites)entriesUpdated[1];

            Assert.AreEqual(ReceiverPlayerId, receiverPlayerInvites.Id);
            Assert.That(receiverPlayerInvites.InboundInviteIds, Contains.Item(expectedCreatedInvite.Id));

            _mockAnalyticsSender.VerifyAll();
        }
        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
                });
            }
        }