def handle_daily(username, args): output = '' do_daily = False stream_uptime = get_uptime() if stream_uptime: now = datetime.datetime.now(datetime.timezone.utc) coin_entries = CoinEntry.objects.filter(username__iexact=username) if len(coin_entries) == 0: coin_entry = CoinEntry(username=username) do_daily = True else: coin_entry = coin_entries[0] if now < coin_entry.last_daily: # The last daily time is in the future. # This can occur if system time was modified for Desmume/in-game clock purposes. # Allow the daily to proceed, and the last daily time will be set back to a normal value. do_daily = True elif coin_entry.last_daily < (now - stream_uptime): do_daily = True else: output = '@' + username + ' You have already received a daily bonus this stream!' if do_daily: crit_string = '' miss_string = '' amount = random.randint(0, 100) if amount >= 50: crit = random.randint(1, 16) if 16 == crit: amount *= 2 crit_string = 'A critical hit! ' elif amount == 0: miss_string = 'It missed! ' coin_entry.coins += amount coin_entry.last_daily = now coin_entry.save() output = '@' + username + ' You received ' + str( amount) + ' coins! ' output += crit_string output += miss_string output += 'Your balance is now ' + str(coin_entry.coins) + ' coins' else: output = 'Try !daily again next time the stream is live!' return output
def handle_uptime(args): output = 'Stream is offline' uptime = get_uptime() if uptime: output = 'The current stream uptime is ' if uptime.days > 0: output += f'{uptime.days} days, ' hours = uptime.seconds // 3600 if hours > 0: output += f'{hours} hours, ' minutes = (uptime.seconds // 60) % 60 output += f'{minutes} minutes' return output
def handle_daily(username, args): output = '' do_daily = False stream_uptime = get_uptime() if stream_uptime: now = datetime.datetime.now(datetime.timezone.utc) coin_entries = CoinEntry.objects.filter(username__iexact=username) if len(coin_entries) == 0: coin_entry = CoinEntry(username=username) do_daily = True else: coin_entry = coin_entries[0] if coin_entry.last_daily < (now - stream_uptime): do_daily = True else: output = '@' + username + ' You have already received a daily bonus this stream!' if do_daily: crit_string = '' miss_string = '' amount = random.randint(0, 100) if amount >= 50: crit = random.randint(1, 16) if 16 == crit: amount *= 2 crit_string = 'A critical hit! ' elif amount == 0: miss_string = 'It missed! ' coin_entry.coins += amount coin_entry.last_daily = now coin_entry.save() output = '@' + username + ' You received ' + str( amount) + ' coins! ' output += crit_string output += miss_string output += 'Your balance is now ' + str(coin_entry.coins) + ' coins' else: output = 'Try !daily again next time the stream is live!' return output
def timed_messages(request): response_text = '' now = datetime.datetime.now(datetime.timezone.utc) timed_messages = TimedMessage.objects.all() for timed_message in timed_messages: interval = datetime.timedelta(minutes=timed_message.minutes_interval) if now - timed_message.last_output_time > interval: should_output = False if timed_message.stream_status == UNSPECIFIED: should_output = True else: uptime = get_uptime() if uptime: # Stream is live if timed_message.stream_status == ONLINE_ONLY: should_output = True else: # Stream is not live if timed_message.stream_status == OFFLINE_ONLY: should_output = True if should_output: if len(timed_message.message.prefix) > 0: response_text = timed_message.message.prefix + ' ' + timed_message.message.output_text else: response_text = timed_message.message.output_text response_text = populate_placeholders(response_text) timed_message.last_output_time = now if timed_message.max_output_count > 0: timed_message.current_output_count += 1 timed_message.save() if timed_message.max_output_count > 0 and timed_message.current_output_count >= timed_message.max_output_count: timed_message.message.delete() timed_message.delete() # Only output one timed message at a time. Others will be picked up next time around. break return HttpResponse(response_text)