Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge Staging #160

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions mafiasi/base/special_day_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,65 @@ def __init__(self, get_response):

def __call__(self, request):
request.session["specialDay"] = False
request.session["specialFeature"] = False
request.session["specialFeatureClasses"] = ""
request.session["specialFeatureDisabled"] = False
feature = ""

# check if user wants to persist feature for this session (or reset it)
if request.GET.get("persistSpecialFeatureForThisSession", "undefined") == "true":
request.session["specialFeaturePersist"] = True
elif request.GET.get("persistSpecialFeatureForThisSession", "undefined") == "false":
request.session["specialFeaturePersist"] = False

# reset feature if persist is not true
if not request.session.get("specialFeaturePersist", False):
request.session["specialFeature"] = False

# get date in current timezone
n = localdate(now(), get_current_timezone())

# check if user disabled special day surprises via cookie
if request.COOKIES.get("disable-special", False):
request.session["specialFeature"] = "DISABLED"
request.session["specialFeatureDisabled"] = True

# check if user enabled feature manually via GET parameter
elif request.GET.get("specialFeature", False):
feature = request.GET.get("specialFeature", False)

# check if feature is already set (if persisted and not reset above)
elif request.session["specialFeature"]:
feature = request.session["specialFeature"]

else:
# check if it's a special day

# first of april (april fools)
if n.day == 1 and n.month == 4:
request.session["specialDay"] = "April Fools"
option = random.randint(0, 2)
if option == 0:
request.session["specialFeature"] = "upsideDown"
request.session["specialFeatureClasses"] += " first-of-april"
feature = "upsideDown"
elif option == 1:
request.session["specialFeature"] = "uwu"
translation.activate("en-uwu")
request.LANGUAGE_CODE = translation.get_language()
feature = "uwu"
elif option == 2:
request.session["specialFeature"] = "mafiasiPurple"

feature = "mafiasiPurple"
# winter season
if n.month == 12 and n.day >= 10:
request.session["specialFeature"] = "winter"
feature = "winter"

# make sure session hold the current feature
request.session["specialFeature"] = feature

# handle feature
if feature == "upsideDown":
request.session["specialFeatureClasses"] += " first-of-april"
elif feature == "uwu":
translation.activate("en-uwu")
request.LANGUAGE_CODE = translation.get_language()
elif feature == "mafiasiPurple":
pass
elif feature == "winter":
pass

# call view
response = self.get_response(request)
Expand Down
15 changes: 14 additions & 1 deletion mafiasi/base/static/js/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ function toggleTheme() {
document.documentElement.dataset.theme = theme
}

document.querySelector('#theme-toggle').addEventListener('click', toggleTheme)

function disableSpecialFeature() {
const tomorrow = new Date()
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(0, 0, 0);
document.cookie = `disable-special=True; path=/; expires=${tomorrow.toUTCString()}; Secure`;
window.location.reload()
let url = new URL(location.href);
url.searchParams.delete('specialFeature');
url.searchParams.delete('persistSpecialFeatureForThisSession');
window.location.href = url
}

function enableSpecialFeature() {
Expand All @@ -25,3 +30,11 @@ const specialOff = document.querySelector('#special-off');
const specialOn = document.querySelector('#special-on');
if(specialOff) specialOff.addEventListener('click', disableSpecialFeature)
if(specialOn) specialOn.addEventListener('click', enableSpecialFeature)

if("feature" in document.querySelector('html').dataset){
const feature = document.querySelector('html').dataset.feature
const url = new URL(location.href);
url.searchParams.set("specialFeature", feature);
url.searchParams.set("persistSpecialFeatureForThisSession", "featurePersisted" in document.querySelector('html').dataset ? 'true' : 'undefined');
history.pushState({}, "", url);
}
6 changes: 3 additions & 3 deletions mafiasi/base/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% load static %}
{% value_from_settings BANNER_IMG as banner_img %}
<!DOCTYPE html>
<html>
<html {% if request.session.specialFeature %}data-feature="{{request.session.specialFeature}}"{% endif %}{% if request.session.specialFeaturePersist %}data-feature-persisted="true"{% endif %}>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>{% block wtitle %}{% endblock %} &middot; {% value_from_settings PROJECT_NAME %}</title>
Expand Down Expand Up @@ -37,11 +37,11 @@
{% endif %}
<div class="container">
<a href="/" class="navbar-brand">{% value_from_settings PROJECT_BANNER %}</a>
{% if request.session.specialFeature == 'DISABLED' %}
{% if request.session.specialFeatureDisabled %}
<a class="btn btn-success btn-xs special-toggle" href="#" id="special-on">
<span class="glyphicon glyphicon-gift"></span>
</a>
{% else %}
{% elif request.session.specialFeature %}
<a class="btn btn-danger btn-xs special-toggle" href="#" id="special-off">
<span class="glyphicon glyphicon-gift"></span> {% trans "Disable surprise" %}
</a>
Expand Down
Loading