Example #1
0
        public SendGiftResult SendGift(int roomId, int senderId, int receiverId, int giftId, int count)
        {
            SendGiftResult result   = SendGiftResult.Succeed;
            var            sender   = userCache[senderId];
            var            receiver = userCache[receiverId];

            if (!cache.HasCommand(roomId, Applications._9258App.FrontendCommands.SendGiftCommandId, senderId, sender.UserInfo.Role_Id, -1))
            {
                result = SendGiftResult.CannotSendGift;
            }
            else if (!cache.HasCommand(roomId, Applications._9258App.FrontendCommands.ReceiveGiftCommandId, receiverId, receiver.UserInfo.Role_Id, -1))
            {
                result = SendGiftResult.CannotReceiveGift;
            }
            else
            {
                var gift = cache.Gifts.FirstOrDefault(g => g.Id == giftId);
                try
                {
                    if (!sender.UserInfo.Money.HasValue || sender.UserInfo.Money < gift.Price * count)
                    {
                        result = SendGiftResult.NotEnoughMoney;
                    }
                    else
                    {
                        sender.UserInfo.Money -= gift.Price * count;
                        var usr = userCache[senderId];
                        dataServiceClient.UpdateUserInfo(senderId, usr.DataServiceToken, sender.UserInfo);

                        if (!receiver.UserInfo.Score.HasValue)
                        {
                            receiver.UserInfo.Score = 0;
                        }
                        receiver.UserInfo.Score += gift.Score * count;
                        usr = userCache[receiverId];
                        dataServiceClient.UpdateUserInfo(receiverId, usr.DataServiceToken, receiver.UserInfo);
                    }
                }
                catch
                {
                    result = SendGiftResult.UnkownError;
                }
            }
            return(result);
        }
Example #2
0
        public void SendGift(int roomId, int receiverId, int giftId, int count)
        {
            var            receiver = userCache[roomId][receiverId];
            SendGiftResult result   = SendGiftResult.Succeed;

            if (!cache.HasCommand(roomId, Applications._9258App.FrontendCommands.SendGiftCommandId, unc.User.Id, unc.UserInfo.Role_Id, -1))
            {
                result = SendGiftResult.CannotSendGift;
            }
            else if (!cache.HasCommand(roomId, Applications._9258App.FrontendCommands.ReceiveGiftCommandId, receiverId, receiver.UserInfo.Role_Id, -1))
            {
                result = SendGiftResult.CannotReceiveGift;
            }
            else
            {
                var gift = cache.Gifts.FirstOrDefault(g => g.Id == giftId);
                try
                {
                    if (!unc.UserInfo.Money.HasValue || unc.UserInfo.Money < gift.Price * count)
                    {
                        result = SendGiftResult.NotEnoughMoney;
                    }
                    else
                    {
                        result = client.SendGift(roomId, unc.User.Id, receiverId, giftId, count);
                        if (result == SendGiftResult.Succeed)
                        {
                            unc.UserInfo.Money -= gift.Price * count;
                            if (!receiver.UserInfo.Score.HasValue)
                            {
                                receiver.UserInfo.Score = 0;
                            }
                            receiver.UserInfo.Score += gift.Score * count;
                        }
                    }
                }
                catch
                {
                    result = SendGiftResult.UnkownError;
                }
            }

            RoomMessage msg = new RoomMessage
            {
                GiftResult  = result,
                MessageType = RoomMessageType.GiftMessage,
                SenderId    = unc.User.Id,
                ReceiverId  = receiverId,
                Count       = count,
                ItemId      = giftId,
                Time        = DateTime.Now.ToString("MM月dd日 HH:mm", CultureInfo.CreateSpecificCulture("zh-CN"))
            };

            switch (result)
            {
            case SendGiftResult.CannotReceiveGift:
            case SendGiftResult.CannotSendGift:
            case SendGiftResult.NotEnoughMoney:
            case SendGiftResult.UnkownError:
                unc.Callback.RoomMessageReceived(roomId, msg);
                break;

            case SendGiftResult.Succeed:
                var gift = cache.Gifts.FirstOrDefault(g => g.Id == giftId);
                if (count >= gift.RunWay)
                {
                    //SenderId as -1 so the sender will receive thsi message
                    //first parameter as -1 means all the rooms will receive the message
                    BroadCast(-1, (u) => u.Callback.RoomMessageReceived(roomId, msg), -1);
                    RoomsPermanentMsg = msg;
                }
                else
                {
                    //SenderId as -1 so the sender will receive thsi message
                    BroadCast(roomId, (u) => u.Callback.RoomMessageReceived(roomId, msg), -1);
                }
                break;
            }
        }