1
0
Fork 0
mirror of https://github.com/sbrl/Nibriboard.git synced 2018-01-10 21:33:49 +00:00

[server] Add protocol event to remove a line segment by unique id

This commit is contained in:
Starbeamrainbowlabs 2017-12-08 16:15:43 +00:00
parent 56fd7d8460
commit 5c12d13600
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
5 changed files with 77 additions and 9 deletions

View file

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.IO;
using Nibriboard.RippleSpace;
namespace Nibriboard.Client.Messages
{
public class LineRemoveMessage : Message
{
/// <summary>
/// A reference to the chunk that contains the line to remove.
/// </summary>
public RawChunkReference ContainingChunk;
/// <summary>
/// The unique id of the line segment to remove.
/// </summary>
public string UniqueId;
public LineRemoveMessage()
{
}
/// <summary>
/// Returns the raw ContainingChunk as a ChunkReference.
/// </summary>
/// <param name="plane">The plane to put the chunk reference on.</param>
/// <returns>The containing chunk as a regular chunk reference.</returns>
public ChunkReference ConvertedContainingChunk(Plane plane)
{
if(ContainingChunk.planeName as string != plane.Name)
throw new InvalidDataException($"Error: A raw reference was for the plane " +
"'{rawRef.planeName}', but the plane '{plane.Name}' " +
"was specified as the plane to lay the chunk references onto!");
return new ChunkReference(plane, ContainingChunk.x, ContainingChunk.y);
}
}
}

View file

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Reflection;
@ -59,6 +58,7 @@ namespace Nibriboard.Client
["LineStart"] = typeof(LineStartMessage),
["LinePart"] = typeof(LinePartMessage),
["LineComplete"] = typeof(LineCompleteMessage),
["LineRemove"] = typeof(LineRemoveMessage),
["ViewportUpdate"] = typeof(ViewportUpdateMessage)
};
@ -189,7 +189,7 @@ namespace Nibriboard.Client
#region Message Sending
/// <summary>
/// Sends a <see cref="Nibriboard.Client.Messages.Message"/> to the client.
/// Sends a <see cref="Message"/> to the client.
/// If you *really* need to send a raw message to the client, you can do so with the SendRawa() method.
/// </summary>
/// <param name="message">The message to send.</param>
@ -487,6 +487,19 @@ namespace Nibriboard.Client
await CurrentPlane.AddLine(line);
}
/// <summary>
/// Handles messages requesting that a line be removed from a chunk.
/// </summary>
/// <param name="message">The message to handle.</param>
protected async Task handleLineRemoveMessage(LineRemoveMessage message)
{
bool removeSuccess = await CurrentPlane.RemoveLineSegment(
message.ConvertedContainingChunk(CurrentPlane),
message.UniqueId
);
Log.WriteLine("[NibriClient#{1}] " + (removeSuccess ? "Removed" : "Failed to remove") + " line segment with unique id {0} from {1}", message.UniqueId, message.ConvertedContainingChunk(CurrentPlane));
}
#endregion
#region RippleSpace Event Handlers

View file

@ -137,6 +137,7 @@
<Compile Include="Client\Messages\ViewportUpdateMessage.cs" />
<Compile Include="NibriboardApp.cs" />
<Compile Include="Utilities\LineSimplifier.cs" />
<Compile Include="Client\Messages\LineRemoveMessage.cs" />
</ItemGroup>
<!--
<ItemGroup>

View file

@ -199,6 +199,15 @@ namespace Nibriboard.RippleSpace
OnChunkUpdate(this, new ChunkUpdateEventArgs() { UpdateType = ChunkUpdateType.Addition });
}
/// <summary>
/// Removes the line in this chunk with the specified unique id.
/// </summary>
/// <param name="targetLineUniqueId">The id of the line to remove.</param>
public bool Remove(string targetLineUniqueId)
{
return lines.RemoveAll((line) => line.UniqueId == targetLineUniqueId) > 0;
}
public IEnumerator<DrawnLine> GetEnumerator()
{
UpdateAccessTime();

View file

@ -229,6 +229,12 @@ namespace Nibriboard.RippleSpace
}
}
public async Task<bool> RemoveLineSegment(ChunkReference containingChunk, string targetLineUniqueId)
{
Chunk chunk = await FetchChunk(containingChunk);
return chunk.Remove(targetLineUniqueId);
}
public void PerformMaintenance()
{
// Be lazy and don't bother to perform maintenance if it's not needed