mirror of
https://github.com/ppy/osu.git
synced 2025-01-14 23:22:55 +08:00
Add back basic API support for channel/message retrieval.
This commit is contained in:
parent
cd57661aa2
commit
28045b7136
128
osu.Desktop.VisualTests/Tests/TestCaseChatDisplay.cs
Normal file
128
osu.Desktop.VisualTests/Tests/TestCaseChatDisplay.cs
Normal file
@ -0,0 +1,128 @@
|
||||
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
|
||||
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
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;
|
||||
using osu.Game.Online.API.Requests;
|
||||
using osu.Game.Online.Chat;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
||||
namespace osu.Desktop.Tests
|
||||
{
|
||||
class TestCaseChatDisplay : TestCase
|
||||
{
|
||||
private ScheduledDelegate messageRequest;
|
||||
|
||||
public override string Name => @"Chat";
|
||||
public override string Description => @"Testing API polling";
|
||||
|
||||
List<Channel> channels = new List<Channel>();
|
||||
private FlowContainer flow;
|
||||
|
||||
Scheduler scheduler = new Scheduler();
|
||||
|
||||
private APIAccess api => ((OsuGameBase)Game).API;
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
|
||||
flow = new FlowContainer()
|
||||
{
|
||||
Direction = FlowDirection.VerticalOnly,
|
||||
LayoutDuration = 100,
|
||||
LayoutEasing = EasingTypes.Out,
|
||||
Padding = new Vector2(1, 1)
|
||||
};
|
||||
|
||||
|
||||
lastMessageId = null;
|
||||
|
||||
if (api.State != APIAccess.APIState.Online)
|
||||
api.OnStateChange += delegate { initializeChannels(); };
|
||||
else
|
||||
initializeChannels();
|
||||
|
||||
|
||||
ScrollContainer scrolling = new ScrollContainer()
|
||||
{
|
||||
Size = new Vector2(1, 0.5f)
|
||||
};
|
||||
|
||||
scrolling.Add(flow);
|
||||
Add(scrolling);
|
||||
}
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
scheduler.Update();
|
||||
base.Update();
|
||||
}
|
||||
|
||||
private void initializeChannels()
|
||||
{
|
||||
if (api.State != APIAccess.APIState.Online)
|
||||
return;
|
||||
|
||||
messageRequest?.Cancel();
|
||||
|
||||
ListChannelsRequest req = new ListChannelsRequest();
|
||||
req.Success += delegate (List<Channel> channels)
|
||||
{
|
||||
this.channels = channels;
|
||||
messageRequest = scheduler.AddDelayed(requestNewMessages, 1000, true);
|
||||
};
|
||||
api.Queue(req);
|
||||
}
|
||||
|
||||
long? lastMessageId = null;
|
||||
|
||||
private void requestNewMessages()
|
||||
{
|
||||
messageRequest.Wait();
|
||||
|
||||
Channel channel = channels.Find(c => c.Name == "#osu");
|
||||
|
||||
GetMessagesRequest gm = new GetMessagesRequest(new List<Channel> { channel }, lastMessageId);
|
||||
gm.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);
|
||||
//}
|
||||
|
||||
channel.Messages.Add(m);
|
||||
}
|
||||
|
||||
lastMessageId = messages.LastOrDefault()?.Id ?? lastMessageId;
|
||||
|
||||
Debug.Write("success!");
|
||||
messageRequest.Continue();
|
||||
};
|
||||
gm.Failure += delegate
|
||||
{
|
||||
Debug.Write("failure!");
|
||||
messageRequest.Continue();
|
||||
};
|
||||
|
||||
api.Queue(gm);
|
||||
}
|
||||
}
|
||||
}
|
@ -150,6 +150,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Tests\TestCaseChatDisplay.cs" />
|
||||
<Compile Include="Tests\TestCaseTextAwesome.cs" />
|
||||
<Compile Include="VisualTestGame.cs" />
|
||||
</ItemGroup>
|
||||
|
@ -10,11 +10,11 @@ using osu.Game.Online.API.Requests;
|
||||
|
||||
namespace osu.Game.Online.API
|
||||
{
|
||||
internal class APIAccess
|
||||
public class APIAccess
|
||||
{
|
||||
private OAuth authentication;
|
||||
|
||||
internal string Endpoint = @"https://new.ppy.sh";
|
||||
public string Endpoint = @"https://new.ppy.sh";
|
||||
const string ClientId = @"daNBnfdv7SppRVc61z0XuOI13y6Hroiz";
|
||||
const string ClientSecret = @"d6fgZuZeQ0eSXkEj5igdqQX6ztdtS6Ow";
|
||||
|
||||
@ -52,7 +52,7 @@ namespace osu.Game.Online.API
|
||||
|
||||
Logger log;
|
||||
|
||||
internal APIAccess()
|
||||
public APIAccess()
|
||||
{
|
||||
authentication = new OAuth(ClientId, ClientSecret, Endpoint);
|
||||
log = Logger.GetLogger(LoggingTarget.Network);
|
||||
@ -61,7 +61,7 @@ namespace osu.Game.Online.API
|
||||
thread.Start();
|
||||
}
|
||||
|
||||
internal string AccessToken => authentication.RequestAccessToken();
|
||||
public string AccessToken => authentication.RequestAccessToken();
|
||||
|
||||
/// <summary>
|
||||
/// Number of consecutive requests which failed due to network issues.
|
||||
@ -221,16 +221,16 @@ namespace osu.Game.Online.API
|
||||
}
|
||||
}
|
||||
|
||||
internal void Queue(APIRequest request)
|
||||
public void Queue(APIRequest request)
|
||||
{
|
||||
queue.Enqueue(request);
|
||||
}
|
||||
|
||||
internal event StateChangeDelegate OnStateChange;
|
||||
public event StateChangeDelegate OnStateChange;
|
||||
|
||||
internal delegate void StateChangeDelegate(APIState oldState, APIState newState);
|
||||
public delegate void StateChangeDelegate(APIState oldState, APIState newState);
|
||||
|
||||
internal enum APIState
|
||||
public enum APIState
|
||||
{
|
||||
/// <summary>
|
||||
/// We cannot login (not enough credentials).
|
||||
@ -268,7 +268,7 @@ namespace osu.Game.Online.API
|
||||
}
|
||||
}
|
||||
|
||||
internal void Logout()
|
||||
public void Logout()
|
||||
{
|
||||
authentication.Clear();
|
||||
State = APIState.Offline;
|
||||
|
@ -11,7 +11,7 @@ namespace osu.Game.Online.API
|
||||
/// An API request with a well-defined response type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the response (used for deserialisation).</typeparam>
|
||||
internal class APIRequest<T> : APIRequest
|
||||
public class APIRequest<T> : APIRequest
|
||||
{
|
||||
protected override WebRequest CreateWebRequest() => new JsonWebRequest<T>(Uri);
|
||||
|
||||
@ -36,7 +36,7 @@ namespace osu.Game.Online.API
|
||||
/// <summary>
|
||||
/// The maximum amount of time before this request will fail.
|
||||
/// </summary>
|
||||
internal int Timeout = WebRequest.DEFAULT_TIMEOUT;
|
||||
public int Timeout = WebRequest.DEFAULT_TIMEOUT;
|
||||
|
||||
protected virtual string Target => string.Empty;
|
||||
|
||||
@ -46,11 +46,11 @@ namespace osu.Game.Online.API
|
||||
|
||||
private double remainingTime => Math.Max(0, Timeout - (DateTime.Now.TotalMilliseconds() - (startTime ?? 0)));
|
||||
|
||||
internal bool ExceededTimeout => remainingTime == 0;
|
||||
public bool ExceededTimeout => remainingTime == 0;
|
||||
|
||||
private double? startTime;
|
||||
|
||||
internal double StartTime => startTime ?? -1;
|
||||
public double StartTime => startTime ?? -1;
|
||||
|
||||
private APIAccess api;
|
||||
protected WebRequest WebRequest;
|
||||
@ -58,7 +58,7 @@ namespace osu.Game.Online.API
|
||||
public event APISuccessHandler Success;
|
||||
public event APIFailureHandler Failure;
|
||||
|
||||
internal void Perform(APIAccess api)
|
||||
public void Perform(APIAccess api)
|
||||
{
|
||||
if (startTime == null)
|
||||
startTime = DateTime.Now.TotalMilliseconds();
|
||||
@ -79,7 +79,7 @@ namespace osu.Game.Online.API
|
||||
//});
|
||||
}
|
||||
|
||||
internal void Fail(Exception e)
|
||||
public void Fail(Exception e)
|
||||
{
|
||||
WebRequest?.Abort();
|
||||
//OsuGame.Scheduler.Add(delegate {
|
||||
|
@ -7,7 +7,7 @@ using osu.Game.Online.Chat;
|
||||
|
||||
namespace osu.Game.Online.API.Requests
|
||||
{
|
||||
internal class GetMessagesRequest : APIRequest<List<Message>>
|
||||
public class GetMessagesRequest : APIRequest<List<Message>>
|
||||
{
|
||||
List<Channel> channels;
|
||||
long? since;
|
||||
|
@ -6,7 +6,7 @@ using osu.Game.Online.Chat;
|
||||
|
||||
namespace osu.Game.Online.API.Requests
|
||||
{
|
||||
internal class ListChannelsRequest : APIRequest<List<Channel>>
|
||||
public class ListChannelsRequest : APIRequest<List<Channel>>
|
||||
{
|
||||
protected override string Target => @"chat/channels";
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ namespace osu.Game.Online.Chat
|
||||
internal string Content;
|
||||
|
||||
[JsonProperty(@"sender")]
|
||||
internal string User;
|
||||
internal User User;
|
||||
|
||||
[JsonConstructor]
|
||||
public Message()
|
||||
|
16
osu.Game/Online/User.cs
Normal file
16
osu.Game/Online/User.cs
Normal file
@ -0,0 +1,16 @@
|
||||
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
|
||||
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace osu.Game.Online
|
||||
{
|
||||
public class User
|
||||
{
|
||||
[JsonProperty(@"username")]
|
||||
public string Name;
|
||||
|
||||
[JsonProperty(@"colour")]
|
||||
public string Colour;
|
||||
}
|
||||
}
|
@ -25,14 +25,6 @@ namespace osu.Game
|
||||
Add(new MainMenu());
|
||||
}
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
{
|
||||
//refresh token may have changed.
|
||||
Config.Set(OsuConfig.Token, API.Token);
|
||||
|
||||
base.Dispose(isDisposing);
|
||||
}
|
||||
|
||||
public override bool Invalidate(Invalidation invalidation = Invalidation.All, Drawable source = null, bool shallPropagate = true)
|
||||
{
|
||||
if (!base.Invalidate(invalidation, source, shallPropagate)) return false;
|
||||
|
@ -15,7 +15,7 @@ namespace osu.Game
|
||||
|
||||
protected override string MainResourceFile => @"osu.Game.Resources.dll";
|
||||
|
||||
internal APIAccess API;
|
||||
public APIAccess API;
|
||||
|
||||
protected override Container AddTarget => ratioContainer?.IsLoaded == true ? ratioContainer : base.AddTarget;
|
||||
|
||||
@ -49,5 +49,14 @@ namespace osu.Game
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
{
|
||||
//refresh token may have changed.
|
||||
Config.Set(OsuConfig.Token, API.Token);
|
||||
Config.Save();
|
||||
|
||||
base.Dispose(isDisposing);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -62,6 +62,7 @@
|
||||
<Compile Include="Online\API\Requests\ListChannels.cs" />
|
||||
<Compile Include="Online\Chat\Channel.cs" />
|
||||
<Compile Include="Online\Chat\Message.cs" />
|
||||
<Compile Include="Online\User.cs" />
|
||||
<Compile Include="OsuGame.cs" />
|
||||
<Compile Include="OsuGameBase.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user