Refactor to match web implementation 1:1

This commit is contained in:
Dean Herbert 2019-09-02 16:34:11 +09:00
parent 0985b1679f
commit d21d68b36c
3 changed files with 122 additions and 93 deletions

View File

@ -58,7 +58,8 @@ namespace osu.Game.Tests.Visual.Online
{
Amount = 10,
CreatedAt = new DateTimeOffset(new DateTime(2011, 11, 11)),
Action = KudosuAction.DenyKudosuReset,
Source = KudosuSource.DenyKudosu,
Action = KudosuAction.Reset,
Post = new APIKudosuHistory.ModdingPost
{
Title = @"Random post 1",
@ -74,7 +75,8 @@ namespace osu.Game.Tests.Visual.Online
{
Amount = 5,
CreatedAt = new DateTimeOffset(new DateTime(2012, 10, 11)),
Action = KudosuAction.ForumGive,
Source = KudosuSource.Forum,
Action = KudosuAction.Give,
Post = new APIKudosuHistory.ModdingPost
{
Title = @"Random post 2",
@ -90,7 +92,8 @@ namespace osu.Game.Tests.Visual.Online
{
Amount = 8,
CreatedAt = new DateTimeOffset(new DateTime(2013, 9, 11)),
Action = KudosuAction.ForumReset,
Source = KudosuSource.Forum,
Action = KudosuAction.Reset,
Post = new APIKudosuHistory.ModdingPost
{
Title = @"Random post 3",
@ -106,7 +109,8 @@ namespace osu.Game.Tests.Visual.Online
{
Amount = 7,
CreatedAt = new DateTimeOffset(new DateTime(2014, 8, 11)),
Action = KudosuAction.ForumRevoke,
Source = KudosuSource.Forum,
Action = KudosuAction.Revoke,
Post = new APIKudosuHistory.ModdingPost
{
Title = @"Random post 4",
@ -122,7 +126,8 @@ namespace osu.Game.Tests.Visual.Online
{
Amount = 100,
CreatedAt = new DateTimeOffset(new DateTime(2015, 7, 11)),
Action = KudosuAction.VoteGive,
Source = KudosuSource.Vote,
Action = KudosuAction.Give,
Post = new APIKudosuHistory.ModdingPost
{
Title = @"Random post 5",
@ -138,7 +143,8 @@ namespace osu.Game.Tests.Visual.Online
{
Amount = 20,
CreatedAt = new DateTimeOffset(new DateTime(2016, 6, 11)),
Action = KudosuAction.VoteReset,
Source = KudosuSource.Vote,
Action = KudosuAction.Reset,
Post = new APIKudosuHistory.ModdingPost
{
Title = @"Random post 6",
@ -154,7 +160,8 @@ namespace osu.Game.Tests.Visual.Online
{
Amount = 11,
CreatedAt = new DateTimeOffset(new DateTime(2016, 6, 11)),
Action = KudosuAction.AllowKudosuGive,
Source = KudosuSource.AllowKudosu,
Action = KudosuAction.Give,
Post = new APIKudosuHistory.ModdingPost
{
Title = @"Random post 7",
@ -170,7 +177,8 @@ namespace osu.Game.Tests.Visual.Online
{
Amount = 24,
CreatedAt = new DateTimeOffset(new DateTime(2014, 6, 11)),
Action = KudosuAction.DeleteReset,
Source = KudosuSource.Delete,
Action = KudosuAction.Reset,
Post = new APIKudosuHistory.ModdingPost
{
Title = @"Random post 8",
@ -186,7 +194,8 @@ namespace osu.Game.Tests.Visual.Online
{
Amount = 12,
CreatedAt = new DateTimeOffset(new DateTime(2016, 6, 11)),
Action = KudosuAction.RestoreGive,
Source = KudosuSource.Restore,
Action = KudosuAction.Give,
Post = new APIKudosuHistory.ModdingPost
{
Title = @"Random post 9",
@ -202,7 +211,8 @@ namespace osu.Game.Tests.Visual.Online
{
Amount = 2,
CreatedAt = new DateTimeOffset(new DateTime(2012, 6, 11)),
Action = KudosuAction.RecalculateGive,
Source = KudosuSource.Recalculate,
Action = KudosuAction.Give,
Post = new APIKudosuHistory.ModdingPost
{
Title = @"Random post 10",
@ -218,7 +228,8 @@ namespace osu.Game.Tests.Visual.Online
{
Amount = 32,
CreatedAt = new DateTimeOffset(new DateTime(2019, 8, 11)),
Action = KudosuAction.RecalculateReset,
Source = KudosuSource.Recalculate,
Action = KudosuAction.Reset,
Post = new APIKudosuHistory.ModdingPost
{
Title = @"Random post 11",

View File

@ -2,7 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using Humanizer;
using System.Linq;
using Newtonsoft.Json;
namespace osu.Game.Online.API.Requests.Responses
@ -39,33 +39,45 @@ namespace osu.Game.Online.API.Requests.Responses
public string Username;
}
public KudosuSource Source;
public KudosuAction Action;
[JsonProperty("action")]
private string action
{
set
{
//We will receive something like "event.action" or just "action"
string parsed = value.Contains(".") ? value.Split('.')[0].Pascalize() + value.Split('.')[1].Pascalize() : value.Pascalize();
// incoming action may contain a prefix. if it doesn't, it's a legacy forum event.
Action = (KudosuAction)Enum.Parse(typeof(KudosuAction), parsed);
string[] split = value.Split('.');
if (split.Length > 1)
Enum.TryParse(split.First().Replace("_", ""), true, out Source);
else
Source = KudosuSource.Forum;
Enum.TryParse(split.Last(), true, out Action);
}
}
}
public KudosuAction Action;
public enum KudosuSource
{
Unknown,
AllowKudosu,
Delete,
DenyKudosu,
Forum,
Recalculate,
Restore,
Vote
}
public enum KudosuAction
{
AllowKudosuGive,
DeleteReset,
DenyKudosuReset,
ForumGive,
ForumReset,
ForumRevoke,
RecalculateGive,
RecalculateReset,
RestoreGive,
VoteGive,
VoteReset,
Give,
Reset,
Revoke,
}
}

View File

@ -51,91 +51,97 @@ namespace osu.Game.Overlays.Profile.Sections.Kudosu
private void load()
{
date.Colour = colours.GreySeafoamLighter;
string prefix = getPrefix(historyItem);
var formattedSource = MessageFormatter.FormatText(getSource(historyItem));
if (!string.IsNullOrEmpty(prefix))
{
linkFlowContainer.AddText(prefix);
linkFlowContainer.AddText($@" {Math.Abs(historyItem.Amount)} kudosu ", t =>
{
t.Font = t.Font.With(italics: true);
t.Colour = colours.Blue;
});
}
var formattedSource = MessageFormatter.FormatText(getString(historyItem));
linkFlowContainer.AddLinks(formattedSource.Text, formattedSource.Links);
linkFlowContainer.AddText(" ");
linkFlowContainer.AddLink(historyItem.Post.Title, historyItem.Post.Url);
}
private string getSource(APIKudosuHistory historyItem)
private string getString(APIKudosuHistory item)
{
string userLink() => $"[{historyItem.Giver?.Url} {historyItem.Giver?.Username}]";
string amount = $"{Math.Abs(item.Amount)} kudosu";
string post = $"[{item.Post.Title}]({item.Post.Url})";
switch (historyItem.Action)
switch (item.Source)
{
case KudosuAction.VoteGive:
return @"from obtaining votes in modding post of";
case KudosuSource.AllowKudosu:
switch (item.Action)
{
case KudosuAction.Give:
return $"Received {amount} from kudosu deny repeal of modding post {post}";
}
case KudosuAction.ForumGive:
return $@"from {userLink()} for a post at";
break;
case KudosuAction.ForumReset:
return $@"Kudosu reset by {userLink()} for the post";
case KudosuSource.DenyKudosu:
switch (item.Action)
{
case KudosuAction.Reset:
return $"Denied {amount} from modding post {post}";
}
case KudosuAction.VoteReset:
return @"from losing votes in modding post of";
break;
case KudosuAction.DenyKudosuReset:
return @"from modding post";
case KudosuSource.Delete:
switch (item.Action)
{
case KudosuAction.Reset:
return $"Lost {amount} from modding post deletion of {post}";
}
case KudosuAction.ForumRevoke:
return $@"Denied kudosu by {userLink()} for the post";
break;
case KudosuAction.AllowKudosuGive:
return @"from kudosu deny repeal of modding post";
case KudosuSource.Restore:
switch (item.Action)
{
case KudosuAction.Give:
return $"Received {amount} from modding post restoration of {post}";
}
case KudosuAction.DeleteReset:
return @"from modding post deletion of";
break;
case KudosuAction.RestoreGive:
return @"from modding post restoration of";
case KudosuSource.Vote:
switch (item.Action)
{
case KudosuAction.Give:
return $"Received {amount} from obtaining votes in modding post of {post}";
case KudosuAction.RecalculateGive:
return @"from votes recalculation in modding post of";
case KudosuAction.Reset:
return $"Lost {amount} from losing votes in modding post of {post}";
}
case KudosuAction.RecalculateReset:
return @"from votes recalculation in modding post of";
break;
default:
return @"from unknown event";
case KudosuSource.Recalculate:
switch (item.Action)
{
case KudosuAction.Give:
return $"Received {amount} from votes recalculation in modding post of {post}";
case KudosuAction.Reset:
return $"Lost {amount} from votes recalculation in modding post of {post}";
}
break;
case KudosuSource.Forum:
string giver = $"[{item.Giver?.Username}]({item.Giver?.Url})";
switch (historyItem.Action)
{
case KudosuAction.Give:
return $"Received {amount} from {giver} for a post at {post}";
case KudosuAction.Reset:
return $"Kudosu reset by {giver} for the post {post}";
case KudosuAction.Revoke:
return $"Denied kudosu by {giver} for the post {post}";
}
break;
}
}
private string getPrefix(APIKudosuHistory historyItem)
{
switch (historyItem.Action)
{
case KudosuAction.VoteGive:
case KudosuAction.ForumGive:
case KudosuAction.AllowKudosuGive:
case KudosuAction.RestoreGive:
case KudosuAction.RecalculateGive:
return @"Received";
case KudosuAction.DenyKudosuReset:
return @"Denied";
case KudosuAction.DeleteReset:
case KudosuAction.VoteReset:
case KudosuAction.RecalculateReset:
return @"Lost";
default:
return null;
}
return $"Unknown event ({amount} change)";
}
}
}