Beispiel #1
0
 public BackendClientMessageInspector()
 {
     if (EngineContext.Current.IsRegistered <ServerNotification>())
     {
         _serverNotification = EngineContext.Current.Resolve <ServerNotification>();
     }
 }
Beispiel #2
0
        public static void SendNotification(string type, object data, Guid sendTo)
        {
            var notification = new ServerNotification
            {
                Data = data,
                Type = type,
            };

            Send(ApiCode.Notification, notification, sendTo);
        }
Beispiel #3
0
        public static void SendNotification(string type, object data)
        {
            var notification = new ServerNotification
            {
                Data = data,
                Type = type,
            };

            Send(ApiCode.Notification, notification, Room.GetState.UserSessions);
        }
		private Task AcknowledgeServerNotificationAsync(ServerNotification notification, NotificationResult result, string customType = null)
		{
            _logger.LogInformation("Acknowledging notification {0} of type {1}{2}, result {3}",
                notification.NotificationId,
                notification.Type,
                customType == null ? String.Empty : " - " + customType,
                result);

			var acknowledgement = new ClientNotificationWithAcknowledgement
			{
				Type = "Acknowledgement",
				AcknowledgementDetail = new ClientNotificationAcknowledgement
				{
					ServerNotificationId = notification.NotificationId,
					Result = result,
					SentTime = notification.CreatedOn,
					Type = notification.Type,
					CustomNotificationType = customType
				}
			};

			return QueueClientNotificationAsync(acknowledgement);
		}
		private async Task HandleDonkyNotificationAsync(ServerNotification notification)
		{
            _logger.LogInformation("Processing Donky notification {0} with type {1}",
                notification.NotificationId, notification.Type);

			var subscriptions = _donkySubscriptions.Where(s => s.Type == notification.Type).ToList();
			if (subscriptions.Count == 0)
			{
#if DEBUG
                _logger.LogDebug("No subscription found for notification of type {0}.  Valid types are: {1}",
                    notification.Type, String.Join(",", subscriptions.Select(s => s.Type).Distinct()));
#endif
				// Queue ack and report no subscriptions
				await AcknowledgeServerNotificationAsync(notification, NotificationResult.DeliveredNoSubscription);
			}
			else
			{
				var acknowledged = false;
				foreach (var subscription in subscriptions)
				{
					var failed = false;
					try
					{
						await subscription.Handler(notification);

						if (!acknowledged && subscriptions.Any(s => s.AutoAcknowledge))
						{
							await AcknowledgeServerNotificationAsync(notification, NotificationResult.Delivered);
							acknowledged = true;
						}
					}
					catch (Exception exception)
					{
                        _logger.LogError(exception, "Failure in notification handler for notification type {0}", notification.Type);
						failed = true;
					}
					if (failed)
					{
						await AcknowledgeServerNotificationAsync(notification, NotificationResult.Failed);
					}
				}
			}
		}
		private async Task HandleCustomNotificationAsync(ServerNotification notification)
		{
			var customType = notification.Data.Value<string>("customType");
		
            _logger.LogInformation("Processing custom notification {0} with type {1}",
                notification.NotificationId, customType);
			var subscriptions = _customSubscriptions.Where(s => s.Type == customType).ToList();
			if (subscriptions.Count == 0)
			{
				// Queue ack and report no subscriptions
				await AcknowledgeServerNotificationAsync(notification, NotificationResult.DeliveredNoSubscription, customType);
			}
			else
			{
				var acknowledged = false;

				foreach (var subscription in subscriptions)
				{
					var failed = false;
					try
					{
						await subscription.Handler(notification);

						if (!acknowledged)
						{
							await AcknowledgeServerNotificationAsync(notification, NotificationResult.Delivered, customType);
							acknowledged = true;
						}
					}
					catch (Exception exception)
					{
                        _logger.LogError(exception, "Failure in notification handler for custom type {0}", customType);
						failed = true;
					}
					if (failed)
					{
						await AcknowledgeServerNotificationAsync(notification, NotificationResult.Failed, customType);
					}
				}
			}
		}
		private bool IsDuplicate(ServerNotification notification)
		{
			lock(_recentNotificationLock)
			{
				if (_recentNotificationIds.Contains(notification.NotificationId))
				{
					return true;
				}

				_recentNotificationIds.Enqueue(notification.NotificationId);

				while (_recentNotificationIds.Count > 100)
				{
					// Remove oldest ids to keep a rolling buffer.
					_recentNotificationIds.Dequeue();
				}

				return false;
			}
		}
		private async Task HandleServerNotificationAsync(ServerNotification notification)
		{
#if DEBUG
            _logger.LogDebug("ServerNotification of type {0}: {1}", notification.Type,
                notification.Data.ToString());
#endif

			if (!IsDuplicate(notification))
			{
				if (notification.Type.ToLowerInvariant() == "custom")
				{
					await HandleCustomNotificationAsync(notification);
				}
				else
				{
					await HandleDonkyNotificationAsync(notification);
				}
			}
			else 
			{
				_logger.LogWarning("Ignoring notification {0} as it was detected as a duplicate", notification.NotificationId);
			}
		}
		private static ServerNotification CreateInternalServerNotification(Services.Notification.ServerNotification notification)
		{
			var serverNotification = new ServerNotification
			{
				CreatedOn = notification.CreatedOn,
				Data = notification.Data,
				NotificationId = notification.Id,
				Type = notification.Type
			};
			return serverNotification;
		}
Beispiel #10
0
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Dictionary <Coord, MapType> changes = null;

            map.MoveNext();
            changes = map.GetChanges();
            Dispatcher.BeginInvoke(new Action(() =>
            {
                UpdateMap(changes);
            }));
            if (changes.Count > 0)
            {
                byte[] buff = new byte[1] {
                    (byte)lib.DataType.MapData
                }.Concat(GetBuffer(changes)).ToArray();
                server.SendToAllAsync(buff);
            }
            List <byte> died = new List <byte>();
            int         liveCount = 0, liveIndex = -1;

            for (int i = 0; i < map.SnakeCount; i++)
            {
                if (map.NextStatus[i] == MapType.HiddenSuperFruitAsShortenFruit)
                {
                    ServerNotification sn = new ServerNotification()
                    {
                        Index         = i,
                        ElseMessage   = string.Format("赞! 给玩家{0}点赞, 吃到了伪装过后的炒鸡果", i + 1),
                        PlayerMessage = string.Format("66666")
                    };
                    server.SendToAllAsync(sn, 1).Wait();
                }
                else if (map.NextStatus[i] == MapType.HiddenSuperShortenFruitAsSuperFruit)
                {
                    ServerNotification sn = new ServerNotification()
                    {
                        Index         = i,
                        ElseMessage   = string.Format("哈哈 让我们一起来蛤玩家{0}, 吃到了假的炒鸡果", i + 1),
                        PlayerMessage = string.Format("吃到假货了吧 蛤你!")
                    };
                    server.SendToAllAsync(sn, 1).Wait();
                }
                else if (map.NextStatus[i] == MapType.BlindFruit || map.NextStatus[i] == MapType.RushFruit || map.NextStatus[i] == MapType.SlowFruit || map.NextStatus[i] == MapType.HardFruit)
                {
                    if (map.Snakes[i].Inventory.ContainsKey(map.NextStatus[i]))
                    {
                        clients[i].SendAsync(new byte[]
                        {
                            (byte)DataType.AddInventory,
                            (byte)map.NextStatus[i],
                            map.Snakes[i].Inventory[map.NextStatus[i]]
                        }).Wait();
                    }
                }

                if (playerStatus[i] == PlayerStatus.Playing)
                {
                    liveCount++;
                    liveIndex = i;
                }
                if (playerStatus[i] == PlayerStatus.Playing && map.RunSnakes[i] == false)
                {
                    died.Add((byte)i);
                    playerStatus[i] = PlayerStatus.Died;
                }
            }
            if (died.Count > 0)
            {
                byte[] buff = new byte[] { (byte)lib.DataType.SnakeDied, (byte)died.Count }.Concat(died).ToArray();
                server.SendToAllAsync(buff).Wait();
            }
            if (liveCount == 1 && map.SnakeCount > 1)
            {
                server.SendToAllAsync(new byte[] { (byte)lib.DataType.GameEnd, (byte)liveIndex });
                timer.Enabled = false;
            }
        }