Exemplo n.º 1
0
        /// <summary>
        /// Adds a chunk reference to the cache.
        /// If the chunk is already in the cache, then it won't be added again.
        /// </summary>
        /// <param name="chunkRef">The chunk reference to add.</param>
        public void Add(ChunkReference chunkRef)
        {
            // If this cache already contains the specified chunk reference, then we
            // probably shouldn't add it to the cache twice
            if (cache.Contains(chunkRef))
            {
                return;
            }

            cache.Add(chunkRef);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Determines whether this client can see the chunk at the specified chunk reference.
        /// </summary>
        /// <param name="chunkRef">The chunk reference to check the visibility of.</param>
        /// <returns>Whether this client can see the chunk located at the specified chunk reference</returns>
        public bool CanSee(ChunkReference chunkRef)
        {
            if (chunkRef.Plane != CurrentPlane)
            {
                return(false);
            }

            Rectangle chunkArea = chunkRef.InPlanespaceRectangle();

            return(chunkArea.Overlap(CurrentViewPort));
        }
Exemplo n.º 3
0
 /// <summary>
 /// Returns whether this cache contains the specified chunk reference.
 /// </summary>
 /// <param name="chunkRef">The chunk reference to check for.</param>
 /// <returns>Whether this cache contaisn  the specified chunk reference..</returns>
 public bool Contains(ChunkReference chunkRef)
 {
     return(cache.Contains(chunkRef));
 }
Exemplo n.º 4
0
 /// <summary>
 /// Remvoes a chunk reference from the cache.
 /// </summary>
 /// <param name="chunkRef">The chunk reference to remove.</param>
 public void Remove(ChunkReference chunkRef)
 {
     cache.Remove(chunkRef);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Handles an incoming plane change request.
        /// </summary>
        protected async Task handlePlaneChangeMessage(PlaneChangeMessage message)
        {
            Log.WriteLine("[NibriClient#{0}] Changing to plane {1}.", Id, message.NewPlaneName);

            // Create a new plane with the specified name if it doesn't exist already
            // Makes sure that the uesr has permission to do so
            // future: we might want to allow the user to specify the chunk size
            if (manager.NibriServer.PlaneManager[message.NewPlaneName] == default(Plane))
            {
                if (ConnectedUser.HasPermission("create-plane"))
                {
                    manager.NibriServer.PlaneManager.CreatePlane(new PlaneInfo(message.NewPlaneName));
                }
                else
                {
                    await Send(new ExceptionMessage(
                                   402, "Error: That plane doesn't exist, but you don't have permission to create it."
                                   ));

                    return;
                }
            }

            Plane newPlane = manager.NibriServer.PlaneManager[message.NewPlaneName];

            Console.WriteLine($"Can view any plane: ${ConnectedUser.HasPermission("view-any-plane")}");
            if (!newPlane.HasMember(ConnectedUser.Username) && !ConnectedUser.HasPermission("view-any-plane"))
            {
                await Send(new ExceptionMessage(
                               403, "Error: You don't have permission to view that plane. Try contacting it's owner!"
                               ));

                return;
            }

            // Remove the event listener from the old plane if there is indeed an old plane to remove it from
            if (CurrentPlane != null)
            {
                CurrentPlane.OnChunkUpdates -= handleChunkUpdateEvent;
            }
            // Swap out the current plane
            CurrentPlane = manager.NibriServer.PlaneManager[message.NewPlaneName];
            // Attach a listener to the new plane
            CurrentPlane.OnChunkUpdates += handleChunkUpdateEvent;

            // Tell the client that the switch over all went according to plan
            await Send(new PlaneChangeOkMessage()
            {
                NewPlaneName = message.NewPlaneName,
                GridSize     = CurrentPlane.ChunkSize
            });

            // Reset the position to (0, 0) since we've just changed planes
            Rectangle workingViewport = CurrentViewPort;

            workingViewport.X = 0;
            workingViewport.Y = 0;
            CurrentViewPort   = workingViewport;

            List <ChunkReference> initialChunks   = new List <ChunkReference>();
            ChunkReference        currentChunkRef = new ChunkReference(
                CurrentPlane,
                (int)Math.Floor(CurrentViewPort.X / CurrentPlane.ChunkSize),
                (int)Math.Floor(CurrentViewPort.Y / CurrentPlane.ChunkSize)
                );

            while (CanSee(currentChunkRef))
            {
                while (CanSee(currentChunkRef))
                {
                    initialChunks.Add(currentChunkRef);
                    currentChunkRef = currentChunkRef.Clone() as ChunkReference;
                    currentChunkRef.X++;
                }
                currentChunkRef.X = (int)Math.Floor(CurrentViewPort.X / CurrentPlane.ChunkSize);
                currentChunkRef.Y++;
            }

            await SendChunks(initialChunks);
        }
Exemplo n.º 6
0
 public static string ChunkFilePath(string planeStorageDirectory, ChunkReference chunkRef)
 {
     return(Path.Combine(planeStorageDirectory, chunkRef.AsFilepath()));
 }