Skip to content

Commit

Permalink
Implement /me command, handling of is_action messages
Browse files Browse the repository at this point in the history
  • Loading branch information
walle303 committed May 29, 2017
1 parent ab3551b commit f22826d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
3 changes: 2 additions & 1 deletion osu.Game/Online/API/Requests/PostMessageRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ protected override WebRequest CreateWebRequest()
req.Method = HttpMethod.POST;
req.AddParameter(@"target_type", message.TargetType.GetDescription());
req.AddParameter(@"target_id", message.TargetId.ToString());
req.AddParameter(@"is_action", message.IsAction.ToString());
req.AddParameter(@"message", message.Content);

return req;
}

protected override string Target => @"chat/messages";
}
}
}
2 changes: 1 addition & 1 deletion osu.Game/Online/Chat/ErrorMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ public ErrorMessage(string message) : base(errorId--)
};
}
}
}
}
11 changes: 6 additions & 5 deletions osu.Game/Online/Chat/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public class Message : IComparable<Message>, IEquatable<Message>
public DateTimeOffset Timestamp;

[JsonProperty(@"content")]
public string Content;
public string Content;

[JsonProperty(@"is_action")]
public bool IsAction;

[JsonProperty(@"sender")]
public User Sender;
Expand All @@ -46,10 +49,8 @@ public Message(long id)

public bool Equals(Message other) => Id == other?.Id;

public override int GetHashCode() => Id.GetHashCode();

public bool IsAction() => (Content.StartsWith("\u0001ACTION") && Content.EndsWith("\u0001"));
}
public override int GetHashCode() => Id.GetHashCode();
}

public enum TargetType
{
Expand Down
10 changes: 5 additions & 5 deletions osu.Game/Overlays/Chat/ChatLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace osu.Game.Overlays.Chat
{
public class ChatLine : Container
{
public readonly Message Message;
public readonly Message Message;

private static readonly Color4[] username_colours = {
OsuColour.FromHex("588c7e"),
Expand Down Expand Up @@ -97,7 +97,7 @@ public ChatLine(Message message)
new OsuSpriteText
{
Font = @"Exo2.0-BoldItalic",
Text = Message.IsAction() ? $@"*{message.Sender.Username}" : $@"{message.Sender.Username}:",
Text = Message.IsAction ? $@"*{message.Sender.Username}" : $@"{message.Sender.Username}:",
Colour = getUsernameColour(Message),
TextSize = text_size,
Origin = Anchor.TopRight,
Expand All @@ -109,13 +109,13 @@ public ChatLine(Message message)
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Left = message_padding + (Message.IsAction() ? 5 : 15) },
Padding = new MarginPadding { Left = message_padding + (Message.IsAction ? 5 : 15) },
Children = new Drawable[]
{
new OsuSpriteText
{
Text = Message.IsAction() ? $@"{message.Content.Trim("\u0001"[0]).Substring(7)}" : message.Content,
Colour = Message.IsAction() ? getUsernameColour(Message) : Color4.White,
Text = message.Content,
Colour = Message.IsAction ? getUsernameColour(Message) : Color4.White,
TextSize = text_size,
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Expand Down
32 changes: 27 additions & 5 deletions osu.Game/Overlays/ChatOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ private void fetchNewMessages()
private void postMessage(TextBox textbox, bool newText)
{
var postText = textbox.Text;
bool postIsAction = false;

if (string.IsNullOrEmpty(postText))
return;
Expand All @@ -356,10 +357,30 @@ private void postMessage(TextBox textbox, bool newText)

if (postText[0] == '/')
{
// TODO: handle commands
currentChannel.AddNewMessages(new ErrorMessage("Chat commands are not supported yet!"));
textbox.Text = string.Empty;
return;
// TODO: Add more commands
if (postText.StartsWith("/me "))
{
postText = postText.Remove(0,4);
postIsAction = true;
}
else if (postText == "/me")
{
currentChannel.AddNewMessages(new ErrorMessage("Usage: /me <action>"));
textbox.Text = string.Empty;
return;
}
else if (postText == "/help" || postText.StartsWith("/help "))
{
currentChannel.AddNewMessages(new ErrorMessage("Implemented Commands: /me, /help"));
textbox.Text = string.Empty;
return;
}
else
{
currentChannel.AddNewMessages(new ErrorMessage("Invalid command! See /help"));
textbox.Text = string.Empty;
return;
}
}

var message = new Message
Expand All @@ -368,7 +389,8 @@ private void postMessage(TextBox textbox, bool newText)
Timestamp = DateTimeOffset.Now,
TargetType = TargetType.Channel, //TODO: read this from currentChannel
TargetId = currentChannel.Id,
Content = postText
Content = postText,
IsAction = postIsAction
};

textbox.ReadOnly = true;
Expand Down

0 comments on commit f22826d

Please sign in to comment.