Skip to content

Commit

Permalink
add ExtraApi
Browse files Browse the repository at this point in the history
/api/extra/fcpp/{acc}
  • Loading branch information
KedamaOvO committed Apr 9, 2019
1 parent f379aab commit 3a4678c
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 42 deletions.
1 change: 1 addition & 0 deletions OsuDataDistributeRestful.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="Server\RouteAttribute.cs" />
<Compile Include="Server\Api\RtppdApis.cs" />
<Compile Include="Server\ServerBase.cs" />
<Compile Include="Server\Api\ExtraApis.cs" />
<Compile Include="Setting.cs" />
<Compile Include="Server\FileServer.cs" />
</ItemGroup>
Expand Down
35 changes: 23 additions & 12 deletions OsuDataDistributeRestfulPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,46 +54,57 @@ private void PrintLanAddress()

#region Initializtion

private void ORTDP_Initialize()
private Plugin ORTDP_Initialize()
{
var plugin = getHoster().EnumPluings().Where(p => p.Name == "OsuRTDataProvider").FirstOrDefault();
if (plugin != null)
{
ApiServer.RegisterResource(new OrtdpApis(plugin));
return plugin;
}
else
IO.CurrentIO.WriteColor($"[ODDR]Not Found OsuRTDataProvider", ConsoleColor.Red);

IO.CurrentIO.WriteColor($"[ODDR]Not Found OsuRTDataProvider", ConsoleColor.Red);
return null;
}

private void RTPPD_Initialize()
private Plugin RTPPD_Initialize()
{
var plugin = getHoster().EnumPluings().Where(p => p.Name == "RealTimePPDisplayer").FirstOrDefault();
if (plugin != null)
{
ApiServer.RegisterResource(new RtppdApis(plugin));
return plugin;
}
else
IO.CurrentIO.WriteColor($"[ODDR]Not Found RealTimePPDisplayer", ConsoleColor.Red);

IO.CurrentIO.WriteColor($"[ODDR]Not Found RealTimePPDisplayer", ConsoleColor.Red);
return null;
}

private void OLSP_Initialize()
private Plugin OLSP_Initialize()
{
var plugin = getHoster().EnumPluings().Where(p => p.Name == "OsuLiveStatusPanelPlugin").FirstOrDefault();
if (plugin != null)
{
ApiServer.RegisterResource(new OlspApis(plugin));
return plugin;
}
else
IO.CurrentIO.WriteColor($"[ODDR]Not Found OsuLiveStatusPanel", ConsoleColor.Red);

IO.CurrentIO.WriteColor($"[ODDR]Not Found OsuLiveStatusPanel", ConsoleColor.Red);
return null;
}

#endregion Initializtion

private void Initialize()
{
ORTDP_Initialize();
RTPPD_Initialize();
OLSP_Initialize();
var ortdp = ORTDP_Initialize();
var rtppd = RTPPD_Initialize();
var olsp = OLSP_Initialize();

if(ortdp !=null && rtppd != null)
{
ApiServer.RegisterResource(new ExtraApis(ortdp,rtppd));
}
}

public override void OnEnable()
Expand Down
19 changes: 16 additions & 3 deletions Server/ActionResult.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.IO;
using Newtonsoft.Json;
using System.IO;

namespace OsuDataDistributeRestful.Server
{
internal class ActionResult
public class ActionResult
{
/// <summary>
/// MIME
Expand All @@ -11,13 +12,25 @@ internal class ActionResult

public int Code { get; private set; } = 200;

public string Reason { get; private set; } = string.Empty;

public object Data { get; private set; }

public ActionResult(object a, int code = 200)
public Formatting Formatting { get; set; } = Formatting.None;

public ActionResult(object a)
{
ContentType = "text/json; charset=UTF-8";
Data = a;
Code = 200;
}

public ActionResult(object a, int code, string reason)
{
ContentType = "text/json; charset=UTF-8";
Data = a;
Code = code;
Reason = reason;
}

public ActionResult(Stream s, int code = 200)
Expand Down
111 changes: 111 additions & 0 deletions Server/Api/ExtraApis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using OsuDataDistributeRestful.Server.Api;
using OsuRTDataProvider;
using OsuRTDataProvider.BeatmapInfo;
using OsuRTDataProvider.Listen;
using OsuRTDataProvider.Mods;
using RealTimePPDisplayer;
using RealTimePPDisplayer.Beatmap;
using RealTimePPDisplayer.Calculator;
using Sync.Plugins;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OsuDataDistributeRestful.Server
{
[Route("/api/extra")]
class ExtraApis:IApi
{
private OsuRTDataProviderPlugin _ortdp;
private RealTimePPDisplayerPlugin _rtppd;
private OsuPlayMode _mode;
private uint _mods;

private PerformanceCalculatorBase _stdPpCalculator;
private PerformanceCalculatorBase _taikoPpCalculator;
private PerformanceCalculatorBase _maniaPpCalculator;
private PerformanceCalculatorBase _ctbPpCalculator;

private Beatmap _beatmap;
private BeatmapReader _reader;

public ExtraApis(Plugin ortdp,Plugin rtppd)
{
_ortdp = ortdp as OsuRTDataProviderPlugin;
_rtppd = rtppd as RealTimePPDisplayerPlugin;

_ortdp.ListenerManager.OnPlayModeChanged += (l, c) => _mode = c;
_ortdp.ListenerManager.OnModsChanged += (c) => _mods = (uint)c.Mod;
_ortdp.ListenerManager.OnBeatmapChanged += (b) => _beatmap = b;
}

[Route("/fcpp/{acc}")]
public object GetFcpp(double acc)
{
PerformanceCalculatorBase cal = GetCalculator();

if (cal is null)
{
return new ActionResult(null, 500, "The server could not get pp calculator.");
}

if (_beatmap is null)
{
return new ActionResult(null, 500, "The server could not get beatmap.");
}

if (_reader is null ||
_reader.OrtdpBeatmap.BeatmapID != _beatmap.BeatmapID)
{
_reader = new BeatmapReader(_beatmap, (int)_mode);
}

if(_reader is null)
{
return new ActionResult(null, 500, "The server could not create BeatmapReader.");
}

cal.AccuracyRound(acc, _reader.ObjectsCount, 0, out int n300, out int n100, out int n50);

cal.Mods = _mods;
cal.Beatmap = _reader;
cal.Count50 = n50;
cal.Count100 = n100;
cal.Count300 = n300;
cal.CountMiss = 0;

var pptuple = cal.GetPerformance();

return new ActionResult(new
{
pp = pptuple.FullComboPP,
aim_pp = pptuple.FullComboAimPP,
speed_pp = pptuple.FullComboSpeedPP,
acc_pp = pptuple.FullComboAccuracyPP,
});
}

private PerformanceCalculatorBase GetCalculator()
{
switch (_mode)
{
case OsuPlayMode.Osu:
_stdPpCalculator = _stdPpCalculator ?? new StdPerformanceCalculator();
return _stdPpCalculator;
case OsuPlayMode.Taiko:
_taikoPpCalculator = _taikoPpCalculator ?? new TaikoPerformanceCalculator();
return _taikoPpCalculator;
case OsuPlayMode.Mania:
_maniaPpCalculator = _maniaPpCalculator ?? new ManiaPerformanceCalculator();
return _maniaPpCalculator;
case OsuPlayMode.CatchTheBeat:
_ctbPpCalculator = _ctbPpCalculator ?? new CatchTheBeatPerformanceCalculator();
return _ctbPpCalculator;
default:
return null;
}
}
}
}
10 changes: 6 additions & 4 deletions Server/Api/OlspApis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public OlspApis(Plugin olsp_plguin)
public ActionResult GetDictValue(string providable_data_name)
{
if (!olsp.EnumProvidableDataName().Any(p => p == providable_data_name))
return new ActionResult(new { code = 404 }, 404);
{
return new ActionResult(null, 400, $"Invalid argument. Parameters:{string.Join(", ",olsp.EnumProvidableDataName())}");
}

var result = olsp.GetData(providable_data_name);
return new ActionResult(new
Expand All @@ -47,7 +49,7 @@ public ActionResult GetBackgoundImage()
ContentType = GetContentType(ext)
};
}
return new ActionResult(new { code = 404, message = "background image not found" }, 200);
return new ActionResult(null, 404, "No background image found");
}

[Route("/image/output")]
Expand All @@ -66,7 +68,7 @@ public ActionResult GetOuputBackgoundImage()
ContentType = GetContentType(ext)
};
}
return new ActionResult(new { code = 404, message = "output image not found" }, 200);
return new ActionResult(null, 404, "No output image found");
}

[Route("/image/mods")]
Expand All @@ -85,7 +87,7 @@ public ActionResult GetModsImage()
ContentType = GetContentType(ext)
};
}
return new ActionResult(new { code = 404, message = "mods image not found" }, 200);
return new ActionResult(null, 404, "No mods image found.");
}

private string GetContentType(string fileExtention)
Expand Down
8 changes: 4 additions & 4 deletions Server/Api/OrtdpApis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public object GetBeatmap()
if (File.Exists(beatmap.FilenameFull))
return new ActionResult(File.OpenRead(beatmap.FilenameFull)) { ContentType = "text/plain; charset=utf-8" };

return new ActionResult(new { code = 404, message = "no found beatmap file" });
return new ActionResult(new { code = 404, message = "No beatmap found." });
}

[Route("/beatmap/audio")]
Expand All @@ -87,7 +87,7 @@ public ActionResult GetAudioFile()
};
}

return new ActionResult(new { code = 404 }, 200);
return new ActionResult(null, 404,"No audio found.");
}

[Route("/beatmap/background")]
Expand All @@ -108,7 +108,7 @@ public ActionResult GetBackgroundFile()
};
}

return new ActionResult(new { code = 404 }, 200);
return new ActionResult(null, 404, "No backgournd image found.");
}

[Route("/beatmap/video")]
Expand All @@ -129,7 +129,7 @@ public ActionResult GetVideoFile()
};
}

return new ActionResult(new { code = 404 }, 200);
return new ActionResult(null, 404, "No video found.");
}

#endregion Beatmap
Expand Down
9 changes: 5 additions & 4 deletions Server/Api/RtppdApis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using OsuDataDistributeRestful.Server.Api;
using RealTimePPDisplayer;
using RealTimePPDisplayer.Displayer;
using RealTimePPDisplayer.Formatter;
using Sync.Plugins;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -111,7 +112,7 @@ public object GetPPFormat()
public object GetHitCountFormat()
=> new {format = StringFormatter.GetHitCountFormatter().Format };

[Route("/formated/pp")]
[Route("/formatted/pp")]
public object GetFormatedPP()
{
List<RestfulDisplayer> displayers = EnumerateRestfulDisplayers();
Expand All @@ -123,21 +124,21 @@ public object GetFormatedPP()
};
}

[Route("/formated/pp/{0}")]
[Route("/formatted/pp/{id}")]
public object GetFormatedPP(int id)
{
RestfulDisplayer displayer = GetDisplayer(id);
return displayer.FormatPp();
}

[Route("/formated/hitCount/{0}")]
[Route("/formatted/hitCount/{id}")]
public object GetFormatedHitCount(int id)
{
RestfulDisplayer displayer = GetDisplayer(id);
return displayer.FormatHitCount();
}

[Route("/formated/hitCount")]
[Route("/formatted/hitCount")]
public object GetFormatedHitCount()
{
List<RestfulDisplayer> displayers = EnumerateRestfulDisplayers();
Expand Down
Loading

0 comments on commit 3a4678c

Please sign in to comment.