From 17a85b21975bc6d49ed9e678827cededf5de41d0 Mon Sep 17 00:00:00 2001 From: Josh Washburne Date: Mon, 26 Mar 2018 15:35:31 -0400 Subject: [PATCH] Add calculating waittimes for playing/requesting songs. --- savepointradio/radio/utils.py | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 savepointradio/radio/utils.py diff --git a/savepointradio/radio/utils.py b/savepointradio/radio/utils.py new file mode 100644 index 0000000..1c70100 --- /dev/null +++ b/savepointradio/radio/utils.py @@ -0,0 +1,45 @@ +from datetime import timedelta +from decimal import * +from random import randint + +from django.db import models +from django.utils import timezone + +from core.utils import get_setting +from .models import Song + + +# Set decimal precision +getcontext().prec = 16 + + +def get_playlist_length(): + """ + Total length of available songs in the playlist (in seconds). + """ + songs = Song.music.available() + return songs.aggregate(models.Sum('length'))['length__sum'] + + +def get_waittime(): + """ + Default length in seconds before a song can be played again. This is based + on the replay ratio set in the application settings. + """ + waittime = get_playlist_length() * Decimal(get_setting('replay_ratio')) + return waittime.quantize(Decimal('.01'), rounding=ROUND_UP) + + +def get_next_allowed_play_time(song): + """ + Length of time in seconds before a song can be played again. + """ + if not song.last_played: + return Decimal('0.00') + + waittime = get_waittime() + nextplay = song.last_played + timedelta(seconds=float(waittime)) + current = (nextplay - timezone.now()).total_seconds() + + return Decimal(current if current > 0 else 0).quantize(Decimal('.01'), + rounding=ROUND_UP)