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

View File

@ -2,7 +2,7 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using Humanizer; using System.Linq;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace osu.Game.Online.API.Requests.Responses namespace osu.Game.Online.API.Requests.Responses
@ -39,33 +39,45 @@ namespace osu.Game.Online.API.Requests.Responses
public string Username; public string Username;
} }
public KudosuSource Source;
public KudosuAction Action;
[JsonProperty("action")] [JsonProperty("action")]
private string action private string action
{ {
set set
{ {
//We will receive something like "event.action" or just "action" // incoming action may contain a prefix. if it doesn't, it's a legacy forum event.
string parsed = value.Contains(".") ? value.Split('.')[0].Pascalize() + value.Split('.')[1].Pascalize() : value.Pascalize();
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 public enum KudosuAction
{ {
AllowKudosuGive, Give,
DeleteReset, Reset,
DenyKudosuReset, Revoke,
ForumGive,
ForumReset,
ForumRevoke,
RecalculateGive,
RecalculateReset,
RestoreGive,
VoteGive,
VoteReset,
} }
} }

View File

@ -51,91 +51,97 @@ namespace osu.Game.Overlays.Profile.Sections.Kudosu
private void load() private void load()
{ {
date.Colour = colours.GreySeafoamLighter; date.Colour = colours.GreySeafoamLighter;
var formattedSource = MessageFormatter.FormatText(getString(historyItem));
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;
});
}
linkFlowContainer.AddLinks(formattedSource.Text, formattedSource.Links); 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 (item.Source)
{
case KudosuSource.AllowKudosu:
switch (item.Action)
{
case KudosuAction.Give:
return $"Received {amount} from kudosu deny repeal of modding post {post}";
}
break;
case KudosuSource.DenyKudosu:
switch (item.Action)
{
case KudosuAction.Reset:
return $"Denied {amount} from modding post {post}";
}
break;
case KudosuSource.Delete:
switch (item.Action)
{
case KudosuAction.Reset:
return $"Lost {amount} from modding post deletion of {post}";
}
break;
case KudosuSource.Restore:
switch (item.Action)
{
case KudosuAction.Give:
return $"Received {amount} from modding post restoration of {post}";
}
break;
case KudosuSource.Vote:
switch (item.Action)
{
case KudosuAction.Give:
return $"Received {amount} from obtaining votes in modding post of {post}";
case KudosuAction.Reset:
return $"Lost {amount} from losing votes in modding post of {post}";
}
break;
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) switch (historyItem.Action)
{ {
case KudosuAction.VoteGive: case KudosuAction.Give:
return @"from obtaining votes in modding post of"; return $"Received {amount} from {giver} for a post at {post}";
case KudosuAction.ForumGive: case KudosuAction.Reset:
return $@"from {userLink()} for a post at"; return $"Kudosu reset by {giver} for the post {post}";
case KudosuAction.ForumReset: case KudosuAction.Revoke:
return $@"Kudosu reset by {userLink()} for the post"; return $"Denied kudosu by {giver} for the post {post}";
case KudosuAction.VoteReset:
return @"from losing votes in modding post of";
case KudosuAction.DenyKudosuReset:
return @"from modding post";
case KudosuAction.ForumRevoke:
return $@"Denied kudosu by {userLink()} for the post";
case KudosuAction.AllowKudosuGive:
return @"from kudosu deny repeal of modding post";
case KudosuAction.DeleteReset:
return @"from modding post deletion of";
case KudosuAction.RestoreGive:
return @"from modding post restoration of";
case KudosuAction.RecalculateGive:
return @"from votes recalculation in modding post of";
case KudosuAction.RecalculateReset:
return @"from votes recalculation in modding post of";
default:
return @"from unknown event";
}
} }
private string getPrefix(APIKudosuHistory historyItem) break;
{
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)";
} }
} }
} }