private void GiftSentEventHandler(RoomMessage msg, bool broadcastMsg)
        {
            switch (msg.GiftResult)
            {
            case SendGiftResult.Succeed:
                UserViewModel sender   = null;
                UserViewModel receiver = null;
                GiftViewModel gift     = ApplicationVM.LocalCache.AllGiftVMs.FirstOrDefault(g => g.Id == msg.ItemId);
                lock (UserVMs)
                {
                    sender = UserVMs.FirstOrDefault(u => u.Id == msg.SenderId);
                    if (sender == null)     //maybe comes from other room.(e.g. RunWay message)
                    {
                        sender = ApplicationVM.LocalCache.AllUserVMs[msg.SenderId];
                        if (sender != null && !sender.IsInitialized)
                        {
                            sender.Initialize();
                        }
                    }
                    receiver = UserVMs.FirstOrDefault(u => u.Id == msg.ReceiverId);
                    if (receiver == null)
                    {
                        receiver = ApplicationVM.LocalCache.AllUserVMs[msg.ReceiverId];
                        if (receiver != null && !receiver.IsInitialized)
                        {
                            receiver.Initialize();
                        }
                    }
                }
                if (sender != null && receiver != null && gift != null)
                {
                    string header = "<img title='" + sender.RoleVM.Name + "' src='" + sender.ImageVM.StaticImageFile + "'><u style='color:gold;margin-right:10px'><span  onclick='window.external.SelectUser(" + sender.Id + ")'" +
                                    " oncontextmenu='window.external.SelectUser(" + sender.Id + ")'/>" + sender.NickName + "(" + sender.Id + ")" + "</span></u></img> 送给 " +
                                    "<img title='" + receiver.RoleVM.Name + "' src='" + receiver.ImageVM.StaticImageFile + "'><u style='color:purple;margin-left:10px;margin-right:10px'><span onclick='window.external.SelectUser(" + receiver.Id + ")'" +
                                    "oncontextmenu='window.external.SelectUser(" + receiver.Id + ")'/>" + receiver.NickName + "(" + receiver.Id + ")" + "</span></u></img>";
                    string htmlmsg = header;

                    //for horn message
                    if (msg.Count >= gift.RoomBroadCast || msg.Count >= gift.WorldBroadCast)
                    {
                        htmlmsg    += "<span>" + msg.Count + gift.Unit + gift.Name + "<img src='" + gift.ImageVM.DynamicImageFile + "'/></span>";
                        msg.Content = htmlmsg;
                        ShowHornMsg(msg);
                    }

                    //for RunWay message
                    if (msg.Count >= gift.RunWay)
                    {
                        htmlmsg  = string.Empty;
                        htmlmsg += "<img style='margin-left:20px;margin-right:20px;' src='" + gift.ImageVM.DynamicImageFile + "'/>";
                        htmlmsg += header + msg.Count + gift.Unit + gift.Name +
                                   "<span style='color:blue;font-size:18px'>" + msg.Time + "</span>";
                        CallJavaScript("ScrollMessage", htmlmsg);
                    }

                    if (!broadcastMsg)
                    {
                        //for public gift message
                        htmlmsg  = header;
                        htmlmsg += "<span>一" + gift.Unit + gift.Name + "<img src='" + gift.ImageVM.DynamicImageFile + "'/>, 共";
                        CallJavaScript("GiftSent", htmlmsg, msg.Count, gift.Unit);

                        //for private gift message
                        if (Me.Id == msg.ReceiverId)
                        {
                            htmlmsg  = header;
                            htmlmsg += "<span>一" + gift.Unit + gift.Name;
                            JavaScriptSerializer js = new JavaScriptSerializer();
                            CallJavaScript("ShowPrivateChatMessage", htmlmsg, false);
                        }


                        if (Me.Id == msg.SenderId)
                        {
                            Me.Money -= msg.Count * gift.Price;
                            CallJavaScript("InitMe", Me.GetJson(true));
                        }

                        else if (Me.Id == msg.ReceiverId)
                        {
                            Me.Score += msg.Count * gift.Score;
                            CallJavaScript("InitMe", Me.GetJson(true));
                        }
                    }
                }
                break;

            case SendGiftResult.CannotReceiveGift:
            case SendGiftResult.CannotSendGift:
            case SendGiftResult.NotEnoughMoney:
            case SendGiftResult.UnkownError:
                CallJavaScript("AlertMessage", Messages.ResourceManager.GetString(msg.GiftResult.ToString()));
                break;

            default:
                break;
            }
        }
        private void RoomMessageReceivedEventHandler(int roomId, RoomMessage msg)
        {
            //Public or Private chat message must be broadcasted in the same room
            if (msg.MessageType == RoomMessageType.PrivateChatMessage || msg.MessageType == RoomMessageType.PublicChatMessage)
            {
                if (roomId == RoomVM.Id)
                {
                    ShowChatMessage(msg);
                }
            }
            else if (msg.MessageType == RoomMessageType.GiftMessage)
            {
                if (roomId == RoomVM.Id)
                {
                    GiftSentEventHandler(msg, false);
                }
                else
                {
                    GiftViewModel gift = ApplicationVM.LocalCache.AllGiftVMs.FirstOrDefault(g => g.Id == msg.ItemId);
                    if (msg.Count >= gift.RunWay) //RunWay message can be broadcasted among the rooms
                    {
                        GiftSentEventHandler(msg, true);
                    }
                }
            }
            else if (msg.MessageType == RoomMessageType.HornMessage ||
                     msg.MessageType == RoomMessageType.HallHornMessage ||
                     msg.MessageType == RoomMessageType.GlobalHornMessage)
            {
                switch (msg.MsgResult)
                {
                case MessageResult.Succeed:
                    if (msg.SenderId == Me.Id)
                    {
                        if (msg.MessageType == RoomMessageType.HornMessage)
                        {
                            Me.Money -= ApplicationVM.LocalCache.HornMsgMoney;
                        }
                        if (msg.MessageType == RoomMessageType.HallHornMessage)
                        {
                            Me.Money -= ApplicationVM.LocalCache.HallHornMsgMoney;
                        }
                        if (msg.MessageType == RoomMessageType.GlobalHornMessage)
                        {
                            Me.Money -= ApplicationVM.LocalCache.GlobalHornMsgMoney;
                        }
                        CallJavaScript("InitMe", Me.GetJson(true));
                    }
                    ShowHornMsg(msg);
                    break;

                case MessageResult.NotEnoughMoney:
                case MessageResult.NotEnoughPrivilege:
                case MessageResult.UnkownError:
                    CallJavaScript("AlertMessage", Messages.ResourceManager.GetString(msg.MsgResult.ToString()));
                    break;

                default:
                    break;
                }
            }
        }
Exemple #3
0
 private void BuildCache()
 {
     Cache.Images.ForEach(img =>
     {
         AddImage(img);
     });
     Cache.GiftGroups.ForEach(g =>
     {
         GiftGroupViewModel giftGroupVM = new GiftGroupViewModel(g);
         LocalCache.AllGiftGroupVMs.Add(giftGroupVM);
     });
     Cache.Gifts.ForEach(g =>
     {
         GiftViewModel giftVM = new GiftViewModel(g);
         giftVM.Initialize();
         LocalCache.AllGiftVMs.Add(giftVM);
     });
     LocalCache.AllGiftGroupVMs.ForEach(g =>
     {
         g.Initialize();
     });
     Cache.RoomGroups.ForEach(rg =>
     {
         RoomGroupViewModel roomGroupVM = new RoomGroupViewModel(rg);
         LocalCache.AllRoomGroupVMs.Add(roomGroupVM);
     });
     //Create all the RoomViewModels so that we can initialize the RoomVMs of RoomGroupVM
     Cache.Rooms.ForEach(r =>
     {
         RoomViewModel roomVM = new RoomViewModel(r);
         roomVM.Initialize();
         LocalCache.AllRoomVMs.Add(roomVM);
     });
     LocalCache.AllRoomGroupVMs.ForEach(rgv =>
     {
         rgv.Initialize();
     });
     Cache.Commands.ForEach(c =>
     {
         if (c.Application_Id == ApplicationId || c.Application_Id == BuiltIns.AllApplication.Id)
         {
             CommandViewModel vm = new CommandViewModel(c);
             vm.Initialize();
             LocalCache.AllCommandVMs.Add(vm);
         }
     });
     Cache.Roles.ForEach(r =>
     {
         if (r.Application_Id == ApplicationId || r.Application_Id == BuiltIns.AllApplication.Id)
         {
             RoleViewModel vm = new RoleViewModel(r);
             LocalCache.AllRoleVMs.Add(vm);
         }
     });
     Cache.RoleCommands.ForEach(rc =>
     {
         if (rc.Application_Id == ApplicationId || rc.Application_Id == BuiltIns.AllApplication.Id)
         {
             RoleCommandViewModel vm = new RoleCommandViewModel(rc);
             vm.Initialize();
             LocalCache.AllRoleCommandVMs.Add(vm);
         }
     });
     LocalCache.AllRoleVMs.ForEach(r =>
     {
         r.Initialize();
     });
     Cache.RoomRoles.ForEach(rr =>
     {
         RoomRoleViewModel vm = new RoomRoleViewModel(rr);
         vm.Initialize();
         LocalCache.AllRoomRoleVMs.Add(vm);
     });
     Cache.ExchangeRates.ForEach(er =>
     {
         ExchangeRateViewModel vm = new ExchangeRateViewModel(er);
         LocalCache.AllExchangeRateVMs.Add(vm);
     });
 }
        public override void LoadAsync()
        {
            //Task.Factory.StartNew(() =>
            //    {
                    lock (UserVMs)
                    {
                        var users = RoomClient.GetRoomUsers(RoomVM.Id);
                        if (users != null && users.Length > 0)
                        {
                            foreach (var user in users)
                            {
                                UserEntered(user, false);
                            }
                        }
                    }

                    var micUsers = RoomClient.GetMicUsers(RoomVM.Id, MicType.Public);
                    if (micUsers != null && micUsers.Count > 0)
                    {
                        if (micUsers.ContainsKey(0) && micUsers[0].MicStatus != MicStatusMessage.MicStatus_Off)
                        {
                            FirstMicUserVM = UserVMs.FirstOrDefault(u => u.Id == micUsers[0].UserId);
                            FirstMicUserVM.OnMic(MicType.Public, 0, micUsers[0].StreamGuid, micUsers[0].MicStatus);
                            if ((FirstMicUserVM.MicStatus & MicStatusMessage.MicStatus_Audio) != MicStatusMessage.MicStatus_Off)
                            {
                                StartAudioPlaying(FirstMicUserVM.Id);
                            }
                        }
                        if (micUsers.ContainsKey(1) && micUsers[1].MicStatus != MicStatusMessage.MicStatus_Off)
                        {
                            SecondMicUserVM = UserVMs.FirstOrDefault(u => u.Id == micUsers[1].UserId);
                            SecondMicUserVM.OnMic(MicType.Public, 1, micUsers[1].StreamGuid, micUsers[1].MicStatus);
                            if ((SecondMicUserVM.MicStatus & MicStatusMessage.MicStatus_Audio) != MicStatusMessage.MicStatus_Off)
                            {
                                StartAudioPlaying(SecondMicUserVM.Id);
                            }
                        }
                        if (micUsers.ContainsKey(2) && micUsers[2].MicStatus != MicStatusMessage.MicStatus_Off)
                        {
                            ThirdMicUserVM = UserVMs.FirstOrDefault(u => u.Id == micUsers[2].UserId);
                            ThirdMicUserVM.OnMic(MicType.Public, 2, micUsers[2].StreamGuid, micUsers[2].MicStatus);
                            if ((ThirdMicUserVM.MicStatus & MicStatusMessage.MicStatus_Audio) != MicStatusMessage.MicStatus_Off)
                            {
                                StartAudioPlaying(ThirdMicUserVM.Id);
                            }
                        }
                    }


                    micUsers = RoomClient.GetMicUsers(RoomVM.Id, MicType.Private);
                    if (micUsers != null && micUsers.Count > 0)
                    {
                        foreach (var mic in micUsers.Values)
                        {
                            if (mic.MicStatus != MicStatusMessage.MicStatus_Off)
                            {
                                var uvm = UserVMs.FirstOrDefault(u => u.Id == mic.UserId);
                                if (uvm != null)
                                {
                                    PrivateMicUserVMs.Add(uvm);
                                    uvm.OnMic(MicType.Private, mic.MicIndex, mic.StreamGuid, mic.MicStatus);
                                    if ((uvm.MicStatus & MicStatusMessage.MicStatus_Audio) != MicStatusMessage.MicStatus_Off)
                                    {
                                        StartAudioPlaying(uvm.Id);
                                    }
                                }
                            }
                        }
                    }

                    micUsers = RoomClient.GetMicUsers(RoomVM.Id, MicType.Secret);
                    if (micUsers != null && micUsers.Count > 0)
                    {
                        foreach (var mic in micUsers.Values)
                        {
                            if (mic.MicStatus != MicStatusMessage.MicStatus_Off)
                            {
                                var uvm = UserVMs.FirstOrDefault(u => u.Id == mic.UserId);
                                if (uvm != null)
                                {
                                    SecretMicUserVMs.Add(uvm);
                                    uvm.OnMic(MicType.Secret, mic.MicIndex, mic.StreamGuid, mic.MicStatus);
                                    if ((uvm.MicStatus & MicStatusMessage.MicStatus_Audio) != MicStatusMessage.MicStatus_Off)
                                    {
                                        StartAudioPlaying(uvm.Id);
                                    }
                                }
                            }
                        }
                    }

                    RoomMessage msg = RoomClient.GetRoomMessage(RoomMessageType.GiftMessage);
                    if (msg != null)
                    {
                        UserViewModel sender = null;
                        UserViewModel receiver = null;
                        lock (UserVMs)
                        {
                            sender = UserVMs.FirstOrDefault(u => u.Id == msg.SenderId);
                            if (sender == null)
                            {
                                sender = ApplicationVM.LocalCache.AllUserVMs[msg.SenderId];
                                if (sender == null)
                                {
                                    var usr = RoomClient.GetUser(msg.SenderId);
                                    if (usr != null)
                                    {
                                        sender = new UserViewModel(usr);
                                        sender.Initialize();
                                        ApplicationVM.LocalCache.AllUserVMs[msg.SenderId] = sender;
                                    }
                                }
                                else 
                                {
                                    if (!sender.IsInitialized)
                                    {
                                        sender.Initialize();
                                    }
                                }
                            }
                            receiver = UserVMs.FirstOrDefault(u => u.Id == msg.ReceiverId);
                            if (receiver == null)
                            {
                                receiver = ApplicationVM.LocalCache.AllUserVMs[msg.ReceiverId];
                                if (receiver == null)
                                {
                                    var usr = RoomClient.GetUser(msg.ReceiverId);
                                    if (usr != null)
                                    {
                                        receiver = new UserViewModel(usr);
                                        receiver.Initialize();
                                        ApplicationVM.LocalCache.AllUserVMs[msg.ReceiverId] = receiver;
                                    }
                                }
                                else
                                {
                                    if (!receiver.IsInitialized)
                                    {
                                        receiver.Initialize();
                                    }
                                }
                            }
                        }
                        GiftViewModel gift = ApplicationVM.LocalCache.AllGiftVMs.FirstOrDefault(g => g.Id == msg.ItemId);
                        if (sender != null && receiver != null && gift != null)
                        {
                            string header = "<img title='" + sender.RoleVM.Name + "' src='" + sender.ImageVM.StaticImageFile + "'><u style='color:gold;margin-right:10px'><span  onclick='window.external.SelectUser(" + sender.Id + ")'" +
                                       " oncontextmenu='window.external.SelectUser(" + sender.Id + ")'/>" + sender.NickName + "(" + sender.Id + ")" + "</span></u></img> 送给 " +
                                       "<img title='" + receiver.RoleVM.Name + "' src='" + receiver.ImageVM.StaticImageFile + "'><u style='color:purple;margin-left:10px;margin-right:10px'><span onclick='window.external.SelectUser(" + receiver.Id + ")'" +
                                       "oncontextmenu='window.external.SelectUser(" + receiver.Id + ")'/>" + receiver.NickName + "(" + receiver.Id + ")" + "</span></u></img>";
                            string htmlmsg = string.Empty;
                            htmlmsg += "<img style='margin-left:20px;margin-right:20px;' src='" + gift.ImageVM.DynamicImageFile + "'/>";
                            htmlmsg += header + msg.Count + gift.Unit + gift.Name +
                                "<span style='color:blue'>" + msg.Time + "</span>";
                            CallJavaScript("ScrollMessage", htmlmsg);
                        }
                    }

                    XmlLanguage enus = XmlLanguage.GetLanguage("en-us");
                    XmlLanguage zhcn = XmlLanguage.GetLanguage("zh-cn");
                    string fontname = "";
                    foreach (FontFamily fontfamily in Fonts.SystemFontFamilies)
                    {
                        if (fontfamily.FamilyNames.ContainsKey(zhcn))
                        {
                            fontfamily.FamilyNames.TryGetValue(zhcn, out fontname);
                            FontFamilies.Insert(0, fontname);
                        }
                        else if (fontfamily.FamilyNames.ContainsKey(enus))
                        {
                            fontfamily.FamilyNames.TryGetValue(enus, out fontname);
                            FontFamilies.Add(fontname);
                        }
                    }

                    FontSizes.Add(14);
                    FontSizes.Add(16);
                    FontSizes.Add(18);
                    FontSizes.Add(20);
                    FontSizes.Add(22);
                    FontSizes.Add(24);
                    FontSizes.Add(28);
                    FontSizes.Add(32);
                    FontSizes.Add(36);

                    CallJavaScript("InitUsers", UsersJson);
                    CallJavaScript("InitFonts", FontFamiliesJson, FontSizesJson);
                    CallJavaScript("InitStamp", StampImagesJson);
                    for (int i = 0; i < MotionImagesJson.Count; i++)
                    {
                        CallJavaScript("InitFaceTab", MotionImagesJson[i], i == MotionImagesJson.Count - 1);
                    }
                    
                    CallJavaScript("InitMoneyForHorn", ApplicationVM.LocalCache.HornMsgMoney, ApplicationVM.LocalCache.HallHornMsgMoney, ApplicationVM.LocalCache.GlobalHornMsgMoney);

                    int scoreToMoney = 0;
                    if (ApplicationVM.LocalCache.AllExchangeRateVMs.Count > 0)
                    {
                        try
                        {
                            var exchangeVM = ApplicationVM.LocalCache.AllExchangeRateVMs.OrderBy(r => r).Where(r =>
                                Convert.ToDateTime(r.ValidTime) >= (DateTime.Now)).ToList().FirstOrDefault();
                            if (exchangeVM != null)
                            {
                                scoreToMoney = exchangeVM.ScoreToMoney;
                            }
                        }
                        catch (Exception ex)
                        { }
                    }

                    CallJavaScript("InitExchangeRate", scoreToMoney);
                    
                //});
        }
		private void BuildCache()
		{
			Cache.Images.ForEach(img =>
			{
                AddImage(img);
			});
			Cache.GiftGroups.ForEach(g =>
			{
				GiftGroupViewModel giftGroupVM = new GiftGroupViewModel(g);
				LocalCache.AllGiftGroupVMs.Add(giftGroupVM);
			});
			Cache.Gifts.ForEach(g =>
			{
				GiftViewModel giftVM = new GiftViewModel(g);
				giftVM.Initialize();
				LocalCache.AllGiftVMs.Add(giftVM);
			});
			LocalCache.AllGiftGroupVMs.ForEach(g =>
			{
				g.Initialize();
			});
			Cache.RoomGroups.ForEach(rg =>
			{
				RoomGroupViewModel roomGroupVM = new RoomGroupViewModel(rg);
				LocalCache.AllRoomGroupVMs.Add(roomGroupVM);
			});
			//Create all the RoomViewModels so that we can initialize the RoomVMs of RoomGroupVM
			Cache.Rooms.ForEach(r =>
			{
				RoomViewModel roomVM = new RoomViewModel(r);
				roomVM.Initialize();
				LocalCache.AllRoomVMs.Add(roomVM);
			});
			LocalCache.AllRoomGroupVMs.ForEach(rgv =>
			{
				rgv.Initialize();
			});
			Cache.Commands.ForEach(c =>
			{
				if (c.Application_Id == ApplicationId || c.Application_Id == BuiltIns.AllApplication.Id)
				{
					CommandViewModel vm = new CommandViewModel(c);
					vm.Initialize();
					LocalCache.AllCommandVMs.Add(vm);
				}
			});
			Cache.Roles.ForEach(r =>
			{
				if (r.Application_Id == ApplicationId || r.Application_Id == BuiltIns.AllApplication.Id)
				{
					RoleViewModel vm = new RoleViewModel(r);
					LocalCache.AllRoleVMs.Add(vm);
				}
			});
			Cache.RoleCommands.ForEach(rc =>
			{
				if (rc.Application_Id == ApplicationId || rc.Application_Id == BuiltIns.AllApplication.Id)
				{
					RoleCommandViewModel vm = new RoleCommandViewModel(rc);
					vm.Initialize();
					LocalCache.AllRoleCommandVMs.Add(vm);
				}
			});
			LocalCache.AllRoleVMs.ForEach(r =>
			{
				r.Initialize();
			});
			Cache.RoomRoles.ForEach(rr =>
			{
				RoomRoleViewModel vm = new RoomRoleViewModel(rr);
                vm.Initialize();
				LocalCache.AllRoomRoleVMs.Add(vm);
			});
            Cache.ExchangeRates.ForEach(er =>
                {
                    ExchangeRateViewModel vm = new ExchangeRateViewModel(er);
                    LocalCache.AllExchangeRateVMs.Add(vm);
                });

		}