Skip to content

Commit

Permalink
Merge pull request #5 from RhythmCodec/dev
Browse files Browse the repository at this point in the history
Merge updates to master
  • Loading branch information
kami-poi committed Aug 24, 2021
2 parents 53ac4ef + 5588bab commit 1c42a0d
Show file tree
Hide file tree
Showing 18 changed files with 1,140 additions and 69 deletions.
109 changes: 72 additions & 37 deletions MorePracticeMalodyServer/Controllers/StoreController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
*
* Known bugs:
*
* When search keyword at start of Title, nothing returns.
* This is caused by EF core sql generate.
* This only tested using sqlite.
*
*/

using System;
Expand Down Expand Up @@ -74,8 +72,7 @@ public StoreController(ILogger<StoreController> logger, DataContext context, IMe
int lvle, int beta, int from)
{
// If not support the api version, throw a exception.
if (api != Consts.API_VERSION)
throw new NotSupportedException($"This server not support api version {api}.");
Util.CheckVersion(api);

var resp = new Response<SongInfo>();
// Max Items server will return.
Expand All @@ -102,20 +99,20 @@ public StoreController(ILogger<StoreController> logger, DataContext context, IMe

if (lvle != 0) temp = temp.Where(c => c.Level < lvle);

var result = temp
var result = await temp
.Select(c => c.Song)
.Distinct()
.OrderBy(s => s.SongId)
.Skip(from * maxItem)
.Take(maxItem)
.ToList(); //TODO: Save to cache.
.ToListAsync(); //TODO: Save to cache.

resp.Code = 0;
// Add song to return value.
foreach (var song in result)
resp.Data.Add(new SongInfo
{
Artist = song.Artist,
Artist = org == 0 ? song.Artist : song.OriginalArtist,
Bpm = song.Bpm,
Cover = song.Cover,
Length = song.Length,
Expand All @@ -129,19 +126,22 @@ public StoreController(ILogger<StoreController> logger, DataContext context, IMe
}

// Try to find data from memory cache first.
if (cache.TryGetValue(word.ToLower(), out List<Song> cachedSongList))
var keyword = Util.TrimSpecial(word).ToLower();
if (cache.TryGetValue(keyword, out List<Song> cachedSongList))
{
}
else // Query db and write result to cache
{
var result = context.Songs
.Where(s => s.Title.Contains(word.ToLower()) || s.OriginalTitle.Contains(word.ToLower()))
var result = await context.Songs
.Include(s => s.Charts)
.Where(s => s.SearchString.Contains(keyword) ||
s.OriginalSearchString.Contains(keyword))
.AsSplitQuery()
.AsNoTracking()
.ToList(); //BUG: This always return a empty list. Why?
// This is a bug of ef core :(
.ToListAsync();

// Create new cache entry. Set value abd expiration.
var cacheEntry = cache.CreateEntry(word);
var cacheEntry = cache.CreateEntry(keyword);
cacheEntry.Value = result;
cacheEntry.AbsoluteExpirationRelativeToNow = new TimeSpan(0, 2, 0);

Expand All @@ -157,10 +157,10 @@ public StoreController(ILogger<StoreController> logger, DataContext context, IMe
song.Charts = song.Charts
.Where(c => c.Type == ChartState.Stable)
.ToList();

song.Charts = song.Charts
.Where(c => c.Level > lvge && c.Level < lvle) // select level
.ToList();
if (lvle != 0 && lvge != 0)
song.Charts = song.Charts
.Where(c => c.Level > lvge && c.Level < lvle) // select level
.ToList();

if (mode != -1)
song.Charts = song.Charts
Expand Down Expand Up @@ -219,11 +219,47 @@ public StoreController(ILogger<StoreController> logger, DataContext context, IMe
public async Task<Response<SongInfo>> GetPromote(int uid, int api, int org, int mode, int from)
{
// If not support the api version, throw a exception.
if (api != Consts.API_VERSION)
throw new NotSupportedException($"This server not support api version {api}.");
Util.CheckVersion(api);

//TODO
return new Response<SongInfo>();
var resp = new Response<SongInfo>();
var maxCount = 50;

// Query promotes from database.
var result = await context.Promotions
.Include(p => p.Song)
.AsNoTracking()
.ToListAsync(); // TODO: Cache result?

resp.Code = 0;
// To see if has more.
if (result.Count - maxCount * from > maxCount)
{
resp.HasMore = true;
resp.Next = from + 1;
}
else
{
resp.HasMore = false;
}

// Insert to Data
for (var i = from * maxCount; resp.HasMore ? i != from * maxCount + maxCount : i != result.Count; i++)
{
var song = result[i].Song;
resp.Data.Add(new SongInfo
{
Artist = org == 0 ? song.Artist : song.OriginalArtist,
Bpm = song.Bpm,
Cover = song.Cover,
Length = song.Length,
Mode = song.Mode,
Sid = song.SongId,
Time = GetTimeStamp(song.Time),
Title = org == 0 ? song.Title : song.OriginalTitle
});
}

return resp;
}

/// <summary>
Expand All @@ -241,8 +277,7 @@ public async Task<Response<SongInfo>> GetPromote(int uid, int api, int org, int
public async Task<Response<ChartInfo>> GetChart(int uid, int api, int sid, int beta, int mode, int from)
{
// If not support the api version, throw a exception.
if (api != Consts.API_VERSION)
throw new NotSupportedException($"This server not support api version {api}.");
Util.CheckVersion(api);

var resp = new Response<ChartInfo>();
// Max Items server will return.
Expand Down Expand Up @@ -324,8 +359,7 @@ public async Task<Response<ChartInfo>> GetChart(int uid, int api, int sid, int b
public async Task<Response<SongInfo>> QuerySong(int uid, int api, int sid = -1, int cid = -1, int org = 0)
{
// If not support the api version, throw a exception.
if (api != Consts.API_VERSION)
throw new NotSupportedException($"This server does not support api version {api}.");
Util.CheckVersion(api);

// If not providing a valid SID or CID, throw a exception.
if (sid == -1 && cid == -1)
Expand Down Expand Up @@ -375,6 +409,7 @@ public async Task<Response<SongInfo>> QuerySong(int uid, int api, int sid = -1,
{
var result = await context.Charts
.Include(c => c.Song)
.AsSplitQuery()
.AsNoTracking()
.FirstAsync(c => c.ChartId == cid);

Expand All @@ -384,7 +419,7 @@ public async Task<Response<SongInfo>> QuerySong(int uid, int api, int sid = -1,

resp.Data.Add(new SongInfo
{
Artist = song.Artist,
Artist = org == 0 ? song.Artist : song.OriginalArtist,
Bpm = song.Bpm,
Cover = song.Cover,
Length = song.Length,
Expand Down Expand Up @@ -425,8 +460,7 @@ public async Task<Response<SongInfo>> QuerySong(int uid, int api, int sid = -1,
public async Task<DownloadResponse> GetDownload(int uid, int api, int cid)
{
// If not support the api version, throw a exception.
if (api != Consts.API_VERSION)
throw new NotSupportedException($"This server does not support api version {api}.");
Util.CheckVersion(api);

var resp = new DownloadResponse();
Chart chart = null;
Expand All @@ -436,6 +470,7 @@ public async Task<DownloadResponse> GetDownload(int uid, int api, int cid)
{
chart = await context.Charts
.Include(c => c.Song)
.AsSplitQuery()
.AsNoTracking()
.FirstAsync(c => c.ChartId == cid);
}
Expand All @@ -450,10 +485,11 @@ public async Task<DownloadResponse> GetDownload(int uid, int api, int cid)
// Try to find the download records with chart.
try
{
var dls = context.Downloads
var dls = await context.Downloads
.Include(d => d.Chart.Song)
.AsSplitQuery()
.AsNoTracking()
.ToList();
.ToListAsync();

if (dls.Any())
{
Expand Down Expand Up @@ -498,8 +534,7 @@ public async Task<DownloadResponse> GetDownload(int uid, int api, int cid)
public async Task<Response<EventInfo>> GetEvents(int uid, int api, int active, int from)
{
// If not support the api version, throw a exception.
if (api != Consts.API_VERSION)
throw new NotSupportedException($"This server does not support api version {api}.");
Util.CheckVersion(api);

var maxItem = 50;
var resp = new Response<EventInfo>();
Expand All @@ -514,7 +549,7 @@ public async Task<Response<EventInfo>> GetEvents(int uid, int api, int active, i
query = query.Where(e => e.Active);

// Success.
var result = query.ToList(); // TODO: Save events to cache?
var result = await query.ToListAsync(); // TODO: Save events to cache?

resp.Code = 0;
// To see if has more to send.
Expand Down Expand Up @@ -567,8 +602,7 @@ public async Task<Response<EventInfo>> GetEvents(int uid, int api, int active, i
public async Task<Response<EventChartInfo>> GetEvent(int uid, int api, int eid, int org, int from)
{
// If not support the api version, throw a exception.
if (api != Consts.API_VERSION)
throw new NotSupportedException($"This server does not support api version {api}.");
Util.CheckVersion(api);

var maxItem = 50; // Max item server will return.
var resp = new Response<EventChartInfo>();
Expand All @@ -578,6 +612,7 @@ public async Task<Response<EventChartInfo>> GetEvent(int uid, int api, int eid,
// Try to find event with eid.
var @event = await context.Events
.Include(e => e.EventCharts)
.AsSplitQuery()
.FirstAsync(e => e.EventId == eid); // TODO: Save event to cache?

// success.
Expand All @@ -603,7 +638,7 @@ public async Task<Response<EventChartInfo>> GetEvent(int uid, int api, int eid,
var chart = charts[i].Chart;
resp.Data.Add(new EventChartInfo
{
Artist = song.Artist,
Artist = org == 0 ? song.Artist : song.OriginalArtist,
Cid = chart.ChartId,
Cover = song.Cover,
Creator = chart.Creator,
Expand Down
35 changes: 26 additions & 9 deletions MorePracticeMalodyServer/Controllers/UploadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public UploadController(DataContext context, ILogger<UploadController> logger, I
[FromForm] string name, [FromForm] string hash)
{
// If not support the api version, throw a exception.
if (api != Consts.API_VERSION)
throw new NotSupportedException($"This server does not support api version {api}.");
Util.CheckVersion(api);

logger.LogInformation("Upload sign phase!");
logger.LogInformation("User {uid} trying to upload chart {cid} for song {sid}.", uid, cid, sid);

Expand Down Expand Up @@ -181,6 +181,8 @@ public UploadController(DataContext context, ILogger<UploadController> logger, I
[FromForm] string name, [FromForm] string hash, [FromForm] int size,
[FromForm] string main)
{
Util.CheckVersion(api);

var selfProvide = true;

// Check if chart exist.
Expand All @@ -205,14 +207,18 @@ public UploadController(DataContext context, ILogger<UploadController> logger, I
if (!chart.Downloads.Any())
{
for (var i = 0; i != names.Length; i++)
{
var encodedName = HttpUtility.UrlEncode(names[i]);

context.Downloads.Add(new Download
{
ChartId = cid,
File =
$"http://{Request.Host.Value}/{sid}/{cid}/{HttpUtility.UrlEncode(names[i])}", // Fix issue #1
$"http://{Request.Host.Value}/{sid}/{cid}/{encodedName}", // Fix issue #1
Hash = hashes[i],
Name = HttpUtility.UrlEncode(names[i])
Name = encodedName
});
}
}
else
{
Expand All @@ -226,14 +232,18 @@ public UploadController(DataContext context, ILogger<UploadController> logger, I
.ToList();
foreach (var add in adds)
// Add new files.
{
var encodedName = HttpUtility.UrlEncode(add);

context.Downloads.Add(new Download
{
ChartId = cid,
File =
$"http://{Request.Host.Value}/{sid}/{cid}/{HttpUtility.UrlEncode(add)}", // Fix issue #1
Hash = nameToHash[add],
Name = HttpUtility.UrlEncode(add)
$"http://{Request.Host.Value}/{sid}/{cid}/{encodedName}", // Fix issue #1
Hash = nameToHash[encodedName],
Name = encodedName
});
}

// Find what should delete.
var dels = chart.Downloads.Select(d => HttpUtility.UrlDecode(d.Name)) // Fix issue #1
Expand All @@ -243,7 +253,7 @@ public UploadController(DataContext context, ILogger<UploadController> logger, I
chart.Downloads.RemoveAll(d => d.Name == HttpUtility.UrlEncode(del)); // Fix issue #1

//Update others.
foreach (var d in chart.Downloads) d.Hash = nameToHash[d.Name];
foreach (var d in chart.Downloads) d.Hash = nameToHash[HttpUtility.UrlDecode(d.Name)];
}

await context.SaveChangesAsync();
Expand All @@ -263,7 +273,7 @@ public UploadController(DataContext context, ILogger<UploadController> logger, I
"wwwroot",
sid.ToString(), cid.ToString(),
HttpUtility.UrlEncode(hashToName[main])),
HttpUtility.UrlEncode(hashToName[main]))); // Fix issue #1
hashToName[main])); // Fix issue #1

var chartMeta = file.Meta;
var songMeta = file.Meta.Song;
Expand All @@ -288,9 +298,16 @@ public UploadController(DataContext context, ILogger<UploadController> logger, I
$"http://{Request.Host.Value}/{sid}/{cid}/{HttpUtility.UrlEncode(chartMeta.Background)}"; // Fix issue #1
chart.Song.Length = 0; // See above.
chart.Song.Mode |= 1 << chartMeta.Mode;
chart.Song.OriginalArtist = songMeta.Artistorg ?? songMeta.Artist;
chart.Song.OriginalTitle = songMeta.Titleorg ?? songMeta.Title;
chart.Song.Title = songMeta.Title;

// Prepare search string for searching.
chart.Song.SearchString =
$"{Util.TrimSpecial(songMeta.Artist).ToLower()}-{Util.TrimSpecial(songMeta.Title).ToLower()}";
chart.Song.OriginalSearchString =
$"{Util.TrimSpecial(chart.Song.OriginalArtist).ToLower()}-{Util.TrimSpecial(chart.Song.OriginalTitle).ToLower()}";

await context.SaveChangesAsync();
}
catch (FileNotFoundException) // WHY??
Expand Down
Loading

0 comments on commit 1c42a0d

Please sign in to comment.