Skip to content

Commit

Permalink
More deep integration of search engine. Fixed a bug with '-' not bein…
Browse files Browse the repository at this point in the history
…g handled correctly. New RefreshSongs command updates the song list. Allows testing of songs. Delete is currently not supported. Beware.

This is another pre-release. Not for general release yet.
  • Loading branch information
angturil committed Apr 8, 2019
1 parent 9e0685b commit 029626a
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 175 deletions.
18 changes: 10 additions & 8 deletions EnhancedTwitchIntegration/Bot/ChatCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -712,21 +712,23 @@ private void showFormatList (TwitchUser requestor, string request)

private IEnumerator LookupSongs(ParseState state)
{
bool isBeatSaverId = _digitRegex.IsMatch(state.parameter) || _beatSaverRegex.IsMatch(state.parameter);

var id = GetBeatSaverId(state.parameter);

string requestUrl = isBeatSaverId ? "https://beatsaver.com/api/songs/detail" : "https://beatsaver.com/api/songs/search/song";
using (var web = UnityWebRequest.Get($"{requestUrl}/{normalize.NormalizeBeatSaverString(state.parameter)}"))
JSONNode result = null;
string requestUrl = (id!="") ? $"https://beatsaver.com/api/songs/detail/{normalize.RemoveSymbols(ref state.parameter,normalize._SymbolsNoDash)}" : $"https://beatsaver.com/api/songs/search/song/{normalize.NormalizeBeatSaverString(state.parameter)}";
using (var web = UnityWebRequest.Get($"{requestUrl}"))
{
yield return web.SendWebRequest();
if (web.isNetworkError || web.isHttpError)
{
Plugin.Log($"Error {web.error} occured when trying to request song {state.parameter}!");
//QueueChatMessage($"Invalid BeatSaver ID \"{state.parameter}\" specified.");
//yield break;
Plugin.Log($"Error {web.error} occured when trying to request song {requestUrl}!");
}
else
{
result = JSON.Parse(web.downloadHandler.text);
}

JSONNode result = JSON.Parse(web.downloadHandler.text);

string errorMessage="";
SongFilter filter = SongFilter.none;
if (state.flags.HasFlag(CmdFlags.NoFilter)) filter = SongFilter.Queue;
Expand Down
3 changes: 0 additions & 3 deletions EnhancedTwitchIntegration/Bot/Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ public class KEYBOARD
/23 (!!) (@@) [SPACE]/40 (##) (__)";


public const string BOTKEYS =
@"[ADD]/0'!bsr ' [UPTIME]/0' !uptime%CR%' [Cat1]/0' sehriaCat1' [Cat2]/0' sehriaCat2' [sehriaXD]/0' sehriaXD' [tohruSway]/0' tohruSway' [Rainbow]/0' RainbowDance' [Love]/0' sehriaLove' [Wink]/0' sehriaWink'
";



Expand Down
90 changes: 76 additions & 14 deletions EnhancedTwitchIntegration/Bot/RequestBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static void OnLoad()

var mykeyboard = new KEYBOARD(KeyboardContainer, "");
#if UNRELEASED
mykeyboard.AddKeys(KEYBOARD.BOTKEYS, 0.4f);
mykeyboard.AddKeys(BOTKEYS, 0.4f);
#endif
mykeyboard.AddKeys(KEYBOARD.QWERTY); // You can replace this with DVORAK if you like
mykeyboard.DefaultActions();
Expand All @@ -148,7 +148,6 @@ public static void OnLoad()
// The UI for this might need a bit of work.

AddKeyboard(mykeyboard, "RightPanel.kbd");

}

if (_songRequestMenu == null)
Expand All @@ -158,7 +157,6 @@ public static void OnLoad()
_songRequestMenu.SetRightViewController(_KeyboardViewController, false);
}


SongListUtils.Initialize();

WriteQueueSummaryToFile();
Expand Down Expand Up @@ -550,6 +548,34 @@ private List<JSONObject> GetSongListFromResults(JSONNode result, ref string erro
return songs;
}

private IEnumerator UpdateSongMap(JSONObject song)
{

yield return Utilities.Download($"https://beatsaver.com/api/songs/detail/{song["id"].Value.ToString()}", Utilities.DownloadType.Raw, null,
// Download success
(web) =>
{
var result = JSON.Parse(web.downloadHandler.text);
QueueChatMessage($"{result.AsObject}");
if (result != null && result["id"].Value != "")
{
song = result.AsObject;
new SongMap(result.AsObject);
}
},
// Download failed, song probably doesn't exist on beatsaver
(web) =>
{
; //errorMessage = $"Invalid BeatSaver ID \"{request}\" specified. {requestUrl}";
});

}


// BUG: Testing major changes. This will get seriously refactored soon.
private IEnumerator CheckRequest(RequestInfo requestInfo)
{
TwitchUser requestor = requestInfo.requestor;
Expand All @@ -558,18 +584,21 @@ private IEnumerator CheckRequest(RequestInfo requestInfo)
string normalrequest= normalize.NormalizeBeatSaverString(requestInfo.request);

// Special code for numeric searches
if (requestInfo.isBeatSaverId)

var id = GetBeatSaverId(normalize.RemoveSymbols(ref request, normalize._SymbolsNoDash));

//QueueChatMessage($"id={id} , request={normalrequest}");

if (id!="")
{
// Remap song id if entry present. This is one time, and not correct as a result. No recursion right now, could be confusing to the end user.
string[] requestparts = request.Split(new char[] { '-' }, 2);

if (requestparts.Length > 0 && songremap.ContainsKey(requestparts[0]) && !requestInfo.flags.HasFlag(CmdFlags.NoFilter))
if (songremap.ContainsKey(id) && !requestInfo.flags.HasFlag(CmdFlags.NoFilter))
{
request = songremap[requestparts[0]];
QueueChatMessage($"Remapping request {requestInfo.request} to {normalrequest}");
request = songremap[id];
QueueChatMessage($"Remapping request {requestInfo.request} to {request}");
}

string requestcheckmessage = IsRequestInQueue(normalrequest); // Check if requested ID is in Queue
string requestcheckmessage = IsRequestInQueue(normalize.RemoveSymbols(ref request, normalize._SymbolsNoDash)); // Check if requested ID is in Queue
if (requestcheckmessage != "")
{
QueueChatMessage(requestcheckmessage);
Expand All @@ -582,7 +611,7 @@ private IEnumerator CheckRequest(RequestInfo requestInfo)
string errorMessage = "";

// Get song query results from beatsaver.com
string requestUrl = requestInfo.isBeatSaverId ? $"https://beatsaver.com/api/songs/detail/{normalrequest}" : $"https://beatsaver.com/api/songs/search/song/{normalrequest}";
string requestUrl = (id!="") ? $"https://beatsaver.com/api/songs/detail/{normalize.RemoveSymbols(ref request, normalize._SymbolsNoDash)}" : $"https://beatsaver.com/api/songs/search/song/{normalrequest}";
yield return Utilities.Download(requestUrl, Utilities.DownloadType.Raw, null,
// Download success
(web) =>
Expand Down Expand Up @@ -630,8 +659,41 @@ private IEnumerator CheckRequest(RequestInfo requestInfo)
QueueChatMessage(errorMessage);
yield break;
}

JSONObject song = songs[0];



// Song requests should try to be current

if ((song["downloadUrl"].Value==""))
{
//QueueChatMessage($"song: {song["id"].Value.ToString()} ,{song["songName"].Value}");

yield return Utilities.Download($"https://beatsaver.com/api/songs/detail/{song["id"].Value.ToString()}", Utilities.DownloadType.Raw, null,
// Download success
(web) =>
{
result = JSON.Parse(web.downloadHandler.text);
var newsong = result["song"].AsObject;
if (result != null && newsong["version"].Value != "")
{
new SongMap(newsong);
song = newsong;
}
},
// Download failed, song probably doesn't exist on beatsaver
(web) =>
{
// Let player know that the song is not current on BeatSaver
requestInfo.requestInfo += " *LOCAL ONLY*";
; //errorMessage = $"Invalid BeatSaver ID \"{request}\" specified. {requestUrl}";
});

// //UpdateSongMap(song);
}

var song = songs[0];

RequestTracker[requestor.id].numRequests++;

Expand Down Expand Up @@ -877,6 +939,7 @@ public static void Next()

private string GetBeatSaverId(string request)
{
request=normalize.RemoveSymbols(ref request, normalize._SymbolsNoDash);
if (_digitRegex.IsMatch(request)) return request;
if (_beatSaverRegex.IsMatch(request))
{
Expand Down Expand Up @@ -908,7 +971,6 @@ private void ProcessSongRequest(TwitchUser requestor, string request, CmdFlags f
return;
}


if (!RequestTracker.ContainsKey(requestor.id))
RequestTracker.Add(requestor.id, new RequestUserTracker());

Expand Down Expand Up @@ -948,7 +1010,7 @@ private void ProcessSongRequest(TwitchUser requestor, string request, CmdFlags f
}

// BUG: Need to clean up the new request pipeline
string testrequest = normalize.NormalizeBeatSaverString(request);
string testrequest = normalize.RemoveSymbols(ref request,normalize._SymbolsNoDash);

RequestInfo newRequest = new RequestInfo(requestor, request, DateTime.UtcNow, _digitRegex.IsMatch(testrequest) || _beatSaverRegex.IsMatch(testrequest), flags, info);

Expand Down
14 changes: 9 additions & 5 deletions EnhancedTwitchIntegration/Bot/commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ the command configuration file (srmcommands.ini).
new COMMAND(new string[] { "!request", "!bsr", "!add", "!sr","!srm" }).Action(ProcessSongRequest).Help(Everyone, "usage: %alias%<songname> or <song id>, omit <,>'s. %|%This adds a song to the request queue. Try and be a little specific. You can look up songs on %beatsaver%", _atleast1);
new COMMAND(new string[] { "!lookup", "!find" }).Coroutine(LookupSongs).Help(Mod | Sub | VIP, "usage: %alias%<song name> or <beatsaber id>, omit <>'s.%|%Get a list of songs from %beatsaver% matching your search criteria.", _atleast1);

new COMMAND("!link").Action(ShowSongLink).Help(Everyone, "usage: %alias%|%... Shows song details, and an %beatsaver% link to the current song", _nothing);
new COMMAND("!link").Action(ShowSongLink).Help(Everyone, "usage: %alias% %|%... Shows song details, and an %beatsaver% link to the current song", _nothing);

new COMMAND("!open").Action(OpenQueue).Help(Mod, "usage: %alias%%|%... Opens the queue allowing song requests.", _nothing);
new COMMAND("!close").Action(CloseQueue).Help(Mod, "usage: %alias%%|%... Closes the request queue.", _nothing);

new COMMAND("!queue").Action(ListQueue).Help(Everyone, "usage: %alias%%|% ... Displays a list of the currently requested songs.", _nothing);
new COMMAND("!played").Action(ShowSongsplayed).Help(Mod, "usage: %alias%%|%... Displays all the songs already played this session.", _nothing);
new COMMAND("!history").Action(ShowHistory).Help(Mod, "usage: %alias% %|% Shows a list of the recently played songs, starting from the most recent.", _nothing);
new COMMAND("!who").Action(Who).Help(Mod, "usage: %alias% <songid or name>%|%Find out who requested the song in the currently queue or recent history.", _atleast1);
new COMMAND("!who").Action(Who).Help(Sub | VIP | Mod, "usage: %alias% <songid or name>%|%Find out who requested the song in the currently queue or recent history.", _atleast1);

new COMMAND ("!modadd").Action(ModAdd).Help(Mod, "usage: %alias%<songname> or <song id>, omit <,>'s. %|%This adds a song to the request queue. This ignores ALL filters including bans.", _atleast1);
new COMMAND("!mtt").Action(MoveRequestToTop).Help(Mod, "usage: %alias%<songname>,<username>,<song id> %|%... Moves a song to the top of the request queue.", _atleast1);
Expand All @@ -152,7 +152,7 @@ the command configuration file (srmcommands.ini).
new COMMAND("!clearalreadyplayed").Action(ClearDuplicateList).Help(Mod, "usage: %alias%%|%... clears the list of already requested songs, allowing them to be requested again.", _nothing); // Needs a better name
new COMMAND("!restore").Action(restoredeck).Help(Mod, "usage: %alias%%|%... Restores the request queue from the previous session. Only useful if you have persistent Queue turned off.", _nothing);

new COMMAND("!about").Help(CmdFlags.Local | CmdFlags.SilentCheck , $"Song Request Manager version {Plugin.Instance.Version}. Github angturil/SongRequestManager", _fail); // Help commands have no code
new COMMAND("!about").Help(CmdFlags.Broadcaster | CmdFlags.SilentCheck , $"Song Request Manager version {Plugin.Instance.Version}. Github angturil/SongRequestManager", _fail); // Help commands have no code
new COMMAND(new string[] { "!help" }).Action(help).Help(Everyone, "usage: %alias%<command name>, or just %alias%to show a list of all commands available to you.", _anything);
new COMMAND("!commandlist").Action(showCommandlist).Help(Everyone, "usage: %alias%%|%... Displays all the bot commands available to you.", _nothing);

Expand All @@ -175,9 +175,14 @@ the command configuration file (srmcommands.ini).
new COMMAND(new string[] { "!addnew", "!addlatest" }).Coroutine(addsongsFromnewest).Help(Mod, "usage: %alias% <listname>%|%... Adds the latest maps from %beatsaver%, filtered by the previous selected allowmappers command", _nothing);
new COMMAND("!backup").Action(Backup).Help(CmdFlags.Broadcaster, "Backup %SRM% directory.", _anything);

new COMMAND("!refreshsongs").Coroutine(RefreshSongs).Help(Broadcaster, "Adds custom songs to bot list. This is a pre-release feature.");


#if UNRELEASED


new COMMAND("!downloadsongs").Coroutine(DownloadEverything).Help(Broadcaster, "Adds custom songs to bot list. This is a pre-release feature.");

// These comments contain forward looking statement that are absolutely subject to change. I make no commitment to following through
// on any specific feature,interface or implementation. I do not promise to make them generally available. Its probably best to avoid using or making assumptions based on these.

Expand Down Expand Up @@ -226,13 +231,12 @@ the command configuration file (srmcommands.ini).
new COMMAND("!joinrooms").Coroutine(GetRooms).Help(Broadcaster, "usage: %alias% %|% This is not fully functional, allows the bot to accept commands from your other rooms.") ;
new COMMAND("!savecommands").Action(SaveCommands);

new COMMAND("!updatesongs").Coroutine(DownloadEverything).Help(Broadcaster,"test");

#endif
#endregion

#region Text Format fields

//would be good to use reflections for these
new COMMAND("AddSongToQueueText", AddSongToQueueText); // These variables are bound due to class reference assignment
new COMMAND("LookupSongDetail", LookupSongDetail);
Expand Down
Loading

0 comments on commit 029626a

Please sign in to comment.