break; } $result = $result . json_encode($searchResults) . "]"; } else { $result = $result . "\"result\":\"error\", \"message\":\"Query not set\""; } break; case "queue-add": if (isset($id)) { $added = addSongToQueue($id); unset($_POST['id']); if ($added == R_SUCCESS) { $result = $result . "\"result\":\"success\", \"message\":\"Song added\""; } else { if ($added == R_SONG_REQUEST_TOO_SOON) { $timeSince = time() - getSongRequestTime($id); $t = ceil((SONG_REQUEST_LIMIT - $timeSince) / 60); $s = $t != 1 ? 's' : ''; $result = $result . "\"result\":\"error\", \"message\":\"Song requested too soon. It can be requested again in {$t} minute{$s}\""; } } } else { $result = $result . "\"result\":\"error\", \"message\":\"ID not set\""; } break; case "queue-list": $currentQueue = getQueue(); $result = $result . "[" . json_encode($currentQueue) . "]"; } $result = $result . "}}"; echo $result;
/** * Checks whether a song has been requested too soon. * * @param $id The ID of the song to check. * * @return True if the song was requested too soon ago; false otherwise. */ function songRequestIsTooSoon($id) { if (!songWasRequested($id)) { addSongRequestTime($id); } $lastRequest = getSongRequestTime($id); return time() - $lastRequest < SONG_REQUEST_LIMIT; }