public void SendRoomMessage(int roomId, RoomMessage message) { MessageResult result = MessageResult.Succeed; //TO Adjust the message, for example, whether it should be scroll, horn, etc. switch (message.MessageType) { case RoomMessageType.AnnoucementMessage: break; case RoomMessageType.StampMessage: break; case RoomMessageType.PublicChatMessage: BroadCast(roomId, (u) => u.Callback.RoomMessageReceived(roomId, message),unc.User.Id); break; case RoomMessageType.PrivateChatMessage: var uncLocalPri = userCache[roomId][message.ReceiverId]; if (uncLocalPri != null) { try { uncLocalPri.Callback.RoomMessageReceived(roomId, message); } catch { LeaveRoom(roomId, uncLocalPri.User.Id); } } break; case RoomMessageType.HornMessage: result = client.SendHornMessage(roomId, unc.User.Id, Applications._9258App.FrontendCommands.HornCommandId); message.MsgResult = result; if (result == MessageResult.Succeed) { var room = cache.Rooms.FirstOrDefault(r => r.Id == roomId); if (room != null) { var roomGroupId = room.RoomGroup_Id; if (roomGroupId != null) { var roomGroup = cache.RoomGroups.FirstOrDefault(r=>r.Id == roomGroupId); if (roomGroup != null) { var hallGroupId = roomGroup.ParentGroup_Id; if (hallGroupId != null) { BroadCastHornMsg((int)hallGroupId, (u) => u.Callback.RoomMessageReceived((int)hallGroupId, message), unc.User.Id); } } } } } else unc.Callback.RoomMessageReceived(roomId, message); break; case RoomMessageType.HallHornMessage: result = client.SendHornMessage(roomId, unc.User.Id, Applications._9258App.FrontendCommands.HallHornCommandId); message.MsgResult = result; if (result == MessageResult.Succeed) { var room2 = cache.Rooms.FirstOrDefault(r => r.Id == roomId); if (room2 != null) { var roomGroupId = room2.RoomGroup_Id; if (roomGroupId != null) { var roomGroup = cache.RoomGroups.FirstOrDefault(r=>r.Id == roomGroupId); if (roomGroup != null) { var hallGroupId = roomGroup.ParentGroup_Id; if (hallGroupId != null) { var hallGroup = cache.RoomGroups.FirstOrDefault(r => r.Id == hallGroupId); if (hallGroup != null) { var tempId = hallGroup.ParentGroup_Id; if (tempId != null) { BroadCastHornMsg((int)tempId, (u) => u.Callback.RoomMessageReceived((int)tempId, message), unc.User.Id); } } } } } } } else unc.Callback.RoomMessageReceived(roomId, message); break; case RoomMessageType.GlobalHornMessage: result = client.SendHornMessage(roomId, unc.User.Id, Applications._9258App.FrontendCommands.GlobalHornCommandId); message.MsgResult = result; if (result == MessageResult.Succeed) { BroadCastHornMsg(-1, (u) => u.Callback.RoomMessageReceived(roomId, message), unc.User.Id); } else unc.Callback.RoomMessageReceived(roomId, message); break; } }
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; } }
private void ShowChatMessage(RoomMessage msg) { if (msg.MsgResult == MessageResult.Succeed) { //The message comes from me, just dispay it as the face (image) has the correct path string contentWithHeader = string.Empty; if (msg.SenderId != Me.Id) { //Need to read the face(image) bytes to file string root = System.IO.Path.Combine(Environment.CurrentDirectory, "Images\\ReceiveMessageImage\\" + msg.SenderId); if (!Directory.Exists(root)) { Directory.CreateDirectory(root); } foreach (MotionImagesMessage mImageMsg in msg.MotionMessages) { if (mImageMsg.IsCustomizedImage) //customized motion, need to store first { string filePath = Path.Combine(root, mImageMsg.Path); if (!File.Exists(filePath)) { File.WriteAllBytes(filePath, mImageMsg.Bytes); } //msg.Content = UpdateMessageImagePath(msg.Content, mImageMsg.Offset, filePath); } } } JavaScriptSerializer js = new JavaScriptSerializer(); if (msg.MessageType == RoomMessageType.PublicChatMessage) CallJavaScript("ShowPublicChatMessage", js.Serialize(msg.Content), true); else if (msg.MessageType == RoomMessageType.PrivateChatMessage) CallJavaScript("ShowPrivateChatMessage", js.Serialize(msg.Content), true); } else { msg.Content = string.Empty; switch (msg.MsgResult) { case MessageResult.UnkownError: msg.Content = Resource.Messages.GeneralError; break; case MessageResult.NotEnoughMoney: msg.Content = Resource.Messages.NotEnoughMoney; break; case MessageResult.NotEnoughPrivilege: msg.Content = Resource.Messages.NoPrivilege; break; default: break; } JavaScriptSerializer js = new JavaScriptSerializer(); CallJavaScript("AlertMessage", js.Serialize(msg.Content)); } }
private RoomMessage InitAnnouncementMsg(RoomMessageType type, string message) { RoomMessage msg = new RoomMessage(); msg.MessageType = type; msg.Content = message; msg.SenderId = Me.Id; msg.IsHorn = true; msg.Time = DateTime.Now.ToString(); return msg; }
public void SendChatMessage(string message, int toUserId, bool isPrivate) { RoomMessage msg = new RoomMessage(); msg.MotionMessages = new List<MotionImagesMessage>(); msg.MessageType = isPrivate ? RoomMessageType.PrivateChatMessage : RoomMessageType.PublicChatMessage; msg.SenderId = Me.Id; msg.ReceiverId = toUserId; msg.Content = message; msg.Time = DateTime.Now.ToString(); //int offset = 0; //int firstImgIndex = message.IndexOf("<IMG"); //while (firstImgIndex != -1) //{ // message = message.Substring(firstImgIndex, message.Length - firstImgIndex); // int srcStartIndex = message.IndexOf("\""); // if (srcStartIndex != -1) // { // message = message.Substring(srcStartIndex + 1, message.Length - srcStartIndex - 1); // int srcEndIndex = message.IndexOf("\""); // string path = message.Substring(0, srcEndIndex); // var img = ApplicationVM.LocalCache.AllImages[BuiltIns.SmileImageType.Id].Values.FirstOrDefault( // r => r.StaticImageFile == path); // if (img == null) // customized motion image // { // MotionImagesMessage mImageMsg = new MotionImagesMessage(); // mImageMsg.IsCustomizedImage = true; // mImageMsg.Id = -1; // mImageMsg.Bytes = Utility.GetImageBytesFromFile(path); // mImageMsg.Offset = firstImgIndex + offset; // mImageMsg.Path = path; // msg.MotionMessages.Add(mImageMsg); // } // message = message.Substring(srcEndIndex + 1, message.Length - srcEndIndex - 1); // offset += firstImgIndex + srcStartIndex + 1 + srcEndIndex + 1; // firstImgIndex = message.IndexOf("<IMG"); // } //} RoomClient.SendRoomMessage(RoomVM.Id, msg); }
public void ShowHornMsg(RoomMessage msg) { JavaScriptSerializer js = new JavaScriptSerializer(); CallJavaScript("ShowHornMessage", js.Serialize(msg.Content)); }
public void SendRoomMessage(int roomId, YoYoStudio.Model.Chat.RoomMessage message) { base.Channel.SendRoomMessage(roomId, message); }
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; } } }