1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 10:18:22 +08:00

General improvements to chat querying and logic organisation.

This commit is contained in:
Dean Herbert 2016-10-07 22:51:10 +09:00
parent 965cd6539f
commit 3067c890ce
6 changed files with 173 additions and 65 deletions

View File

@ -5,7 +5,6 @@ using OpenTK;
using osu.Framework.GameModes.Testing;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Transformations;
using osu.Framework.Threading;
using osu.Game;
using osu.Game.Online.API;
@ -15,7 +14,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using osu.Framework.Graphics.Sprites;
using osu.Game.Online.Chat.Display.osu.Online.Social;
using osu.Game.Online.Chat.Display;
namespace osu.Desktop.Tests
{
@ -26,41 +25,22 @@ namespace osu.Desktop.Tests
public override string Name => @"Chat";
public override string Description => @"Testing API polling";
private List<Channel> channels = new List<Channel>();
private FlowContainer flow;
FlowContainer flow;
private Scheduler scheduler = new Scheduler();
private APIAccess api => ((OsuGameBase)Game).API;
private long? lastMessageId;
private ChannelDisplay channelDisplay;
public override void Reset()
{
base.Reset();
lastMessageId = null;
if (api.State != APIAccess.APIState.Online)
api.OnStateChange += delegate { initializeChannels(); };
else
initializeChannels();
Add(new ScrollContainer()
{
Size = new Vector2(1, 0.5f),
Children = new Drawable[]
{
flow = new FlowContainer
{
Direction = FlowDirection.VerticalOnly,
RelativeSizeAxes = Axes.X,
LayoutDuration = 100,
LayoutEasing = EasingTypes.Out,
Padding = new Vector2(1, 1)
}
}
});
}
protected override void Update()
@ -69,60 +49,87 @@ namespace osu.Desktop.Tests
base.Update();
}
private long? lastMessageId;
List<Channel> careChannels;
private void initializeChannels()
{
careChannels = new List<Channel>();
if (api.State != APIAccess.APIState.Online)
return;
Add(flow = new FlowContainer
{
RelativeSizeAxes = Axes.Both,
Direction = FlowDirection.VerticalOnly
});
SpriteText loading;
Add(loading = new SpriteText
{
Text = @"Loading available channels...",
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
TextSize = 40,
});
messageRequest?.Cancel();
ListChannelsRequest req = new ListChannelsRequest();
req.Success += delegate (List<Channel> channels)
{
this.channels = channels;
messageRequest = scheduler.AddDelayed(requestNewMessages, 1000, true);
Game.Scheduler.Add(delegate
{
loading.FadeOut(100);
});
addChannel(channels.Find(c => c.Name == @"#osu"));
addChannel(channels.Find(c => c.Name == @"#lobby"));
addChannel(channels.Find(c => c.Name == @"#english"));
messageRequest = scheduler.AddDelayed(() => FetchNewMessages(api), 1000, true);
};
api.Queue(req);
}
private void requestNewMessages()
private void addChannel(Channel channel)
{
messageRequest.Wait();
flow.Add(channelDisplay = new ChannelDisplay(channel)
{
Size = new Vector2(1, 0.3f)
});
Channel channel = channels.Find(c => c.Name == "#osu");
careChannels.Add(channel);
}
GetMessagesRequest gm = new GetMessagesRequest(new List<Channel> { channel }, lastMessageId);
gm.Success += delegate (List<Message> messages)
GetMessagesRequest fetchReq;
public void FetchNewMessages(APIAccess api)
{
if (fetchReq != null) return;
fetchReq = new GetMessagesRequest(careChannels, lastMessageId);
fetchReq.Success += delegate (List<Message> messages)
{
foreach (Message m in messages)
{
//m.LineWidth = this.Size.X; //this is kinda ugly.
//m.Drawable.Depth = m.Id;
//m.Drawable.FadeInFromZero(800);
//flow.Add(m.Drawable);
//if (osu.Messages.Count > 50)
//{
// osu.Messages[0].Drawable.Expire();
// osu.Messages.RemoveAt(0);
//}
flow.Add(new ChatLine(m));
channel.Messages.Add(m);
careChannels.Find(c => c.Id == m.ChannelId).AddNewMessages(m);
}
lastMessageId = messages.LastOrDefault()?.Id ?? lastMessageId;
Debug.Write("success!");
messageRequest.Continue();
fetchReq = null;
};
gm.Failure += delegate
fetchReq.Failure += delegate
{
Debug.Write("failure!");
messageRequest.Continue();
fetchReq = null;
};
api.Queue(gm);
api.Queue(fetchReq);
}
}
}

View File

@ -1,8 +1,14 @@
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Newtonsoft.Json;
using osu.Framework.Configuration;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
namespace osu.Game.Online.Chat
{
@ -28,5 +34,26 @@ namespace osu.Game.Online.Chat
public Channel()
{
}
public event Action<Message[]> NewMessagesArrived;
public void AddNewMessages(params Message[] messages)
{
Messages.AddRange(messages);
purgeOldMessages();
NewMessagesArrived?.Invoke(messages);
}
private void purgeOldMessages()
{
const int max_history = 50;
int messageCount = Messages.Count;
if (messageCount > 50)
{
Messages.RemoveRange(0, messageCount - max_history);
}
}
}
}

View File

@ -0,0 +1,78 @@
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Transformations;
using osu.Game.Online.Chat.Display.osu.Online.Social;
using OpenTK;
namespace osu.Game.Online.Chat.Display
{
public class ChannelDisplay : FlowContainer
{
private readonly Channel channel;
private FlowContainer flow;
public ChannelDisplay(Channel channel)
{
this.channel = channel;
newMessages(channel.Messages);
channel.NewMessagesArrived += newMessages;
RelativeSizeAxes = Axes.X;
Direction = FlowDirection.VerticalOnly;
Add(new SpriteText
{
Text = channel.Name
});
Add(new ScrollContainer
{
RelativeSizeAxes = Axes.X,
Size = new Vector2(1, 200),
Children = new Drawable[]
{
flow = new FlowContainer
{
Direction = FlowDirection.VerticalOnly,
RelativeSizeAxes = Axes.X,
LayoutEasing = EasingTypes.Out,
Padding = new Vector2(1, 1)
}
}
});
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
channel.NewMessagesArrived -= newMessages;
}
public override void Load()
{
base.Load();
newMessages(channel.Messages);
}
private void newMessages(IEnumerable<Message> newMessages)
{
if (!IsLoaded) return;
var displayMessages = newMessages.Skip(Math.Max(0, newMessages.Count() - 20));
//up to last 20 messages
foreach (Message m in displayMessages)
flow.Add(new ChatLine(m));
while (flow.Children.Count() > 20)
flow.Remove(flow.Children.First());
}
}
}

View File

@ -1,6 +1,8 @@
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Drawables;
@ -14,11 +16,11 @@ namespace osu.Game.Online.Chat.Display
{
public class ChatLine : AutoSizeContainer
{
private readonly Message msg;
public readonly Message Message;
public ChatLine(Message msg)
public ChatLine(Message message)
{
this.msg = msg;
this.Message = message;
}
public override void Load()
@ -27,34 +29,27 @@ namespace osu.Game.Online.Chat.Display
RelativeSizeAxes = Axes.X;
Add(new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Aqua,
Alpha = 0.2f
});
Add(new SpriteText
{
Text = msg.Timestamp.ToLocalTime().ToLongTimeString(),
Text = Message.Timestamp.ToLocalTime().ToLongTimeString(),
Colour = new Color4(128, 128, 128, 255)
});
Add(new SpriteText
{
Text = msg.User.Name,
Text = Message.User.Name,
Origin = Anchor.TopRight,
RelativePositionAxes = Axes.X,
Position = new Vector2(0.14f,0),
Position = new Vector2(0.2f,0),
});
Add(new SpriteText
{
Text = msg.Content,
Text = Message.Content,
RelativePositionAxes = Axes.X,
Position = new Vector2(0.15f, 0),
Position = new Vector2(0.22f, 0),
RelativeSizeAxes = Axes.X,
Size = new Vector2(0.85f, 1),
Size = new Vector2(0.78f, 1),
});
}
}

View File

@ -12,10 +12,10 @@ namespace osu.Game.Online.Chat
public long Id;
[JsonProperty(@"user_id")]
public string UserId;
public int UserId;
[JsonProperty(@"channel_id")]
public string ChannelId;
public int ChannelId;
[JsonProperty(@"timestamp")]
public DateTime Timestamp;

View File

@ -120,6 +120,7 @@
<Compile Include="Online\API\SecurePassword.cs" />
<Compile Include="Online\API\Requests\ListChannels.cs" />
<Compile Include="Online\Chat\Channel.cs" />
<Compile Include="Online\Chat\Display\ChannelDisplay.cs" />
<Compile Include="Online\Chat\Display\ChatLine.cs" />
<Compile Include="Online\Chat\Message.cs" />
<Compile Include="Online\User.cs" />