🚧 Support configurable timezone & add some more achievements
This commit is contained in:
1 parent
410695213b
commit
b42bef66d0
5 files changed
+88
-4
No files matched your search
@@ -60,6 +60,7 @@ core_deps = [
|
||||
"pydantic==1.10.12", # to be kept pinned until https://github.com/pydantic/pydantic/issues/7689 is resolved
|
||||
"pylru>=1.2.1,<2",
|
||||
"pyserial>=3.5,<4",
|
||||
"pytz",
|
||||
"PyYAML>=6.0.1,<7", # changelog at https://github.com/yaml/pyyaml/blob/master/CHANGES
|
||||
"requests>=2.28.2,<3",
|
||||
"sarge==0.1.7.post1",
|
||||
|
||||
@@ -51,10 +51,25 @@ class AchievementsPlugin(
|
||||
|
||||
self._pause_counter = 0 # not persisted, as it depends on the current print
|
||||
|
||||
self._tz = None
|
||||
|
||||
def initialize(self):
|
||||
self._load_data_file()
|
||||
return super().initialize()
|
||||
|
||||
def _now(self):
|
||||
if self._tz is None:
|
||||
import pytz
|
||||
|
||||
timezone = self._settings.get(["timezone"])
|
||||
try:
|
||||
self._tz = pytz.timezone(timezone)
|
||||
except Exception:
|
||||
self._logger.exception(
|
||||
f"Cannot load timezone {timezone}, falling back to server timezone"
|
||||
)
|
||||
return datetime.datetime.now(tz=self._tz)
|
||||
|
||||
##~~ Additional permissions hook
|
||||
|
||||
def get_additional_permissions(self):
|
||||
@@ -108,7 +123,7 @@ class AchievementsPlugin(
|
||||
from octoprint.events import Events
|
||||
|
||||
changed = False
|
||||
now = datetime.datetime.now()
|
||||
now = self._now()
|
||||
|
||||
if event == Events.PRINT_STARTED:
|
||||
self._pause_counter = 0
|
||||
@@ -173,6 +188,7 @@ class AchievementsPlugin(
|
||||
self._data.stats.prints_finished += 1
|
||||
self._data.stats.print_duration_total += payload["time"]
|
||||
self._data.stats.print_duration_finished += payload["time"]
|
||||
self._data.state.consecutive_prints_cancelled_today = 0
|
||||
|
||||
if payload["time"] > self._data.stats.longest_print_duration:
|
||||
self._data.stats.longest_print_duration = payload["time"]
|
||||
@@ -218,6 +234,11 @@ class AchievementsPlugin(
|
||||
self._data.state.consecutive_prints_of_same_file = 1
|
||||
self._data.state.file_last_print = loc
|
||||
|
||||
## total finished print duration
|
||||
|
||||
if self._data.stats.print_duration_finished >= 404 * 60 * 60:
|
||||
self._trigger_achievement(Achievements.ACHIEVEMENT_NOT_FOUND)
|
||||
|
||||
changed = True
|
||||
|
||||
elif event == Events.PRINT_FAILED or event == Events.PRINT_CANCELLED:
|
||||
@@ -231,6 +252,17 @@ class AchievementsPlugin(
|
||||
if payload["progress"] > 95:
|
||||
self._trigger_achievement(Achievements.SO_CLOSE, write=False)
|
||||
|
||||
## cancelled per day
|
||||
|
||||
if now.date().isoformat() != self._data.state.date_last_cancelled_print:
|
||||
self._data.state.date_last_cancelled_print = now.date().isoformat()
|
||||
self._data.state.prints_cancelled_today = 0
|
||||
self._data.state.prints_cancelled_today += 1
|
||||
self._data.state.consecutive_prints_cancelled_today += 1
|
||||
|
||||
if self._data.state.consecutive_prints_cancelled_today >= 10:
|
||||
self._trigger_achievement(Achievements.ONE_OF_THOSE_DAYS, write=False)
|
||||
|
||||
changed = True
|
||||
|
||||
elif event == Events.PRINT_PAUSED:
|
||||
@@ -289,7 +321,7 @@ class AchievementsPlugin(
|
||||
**a.dict(),
|
||||
)
|
||||
for a in Achievements.all()
|
||||
if not a.hidden or self._has_achievement(a)
|
||||
# if not a.hidden or self._has_achievement(a)
|
||||
]
|
||||
achievements.sort(key=lambda a: a.name)
|
||||
|
||||
@@ -317,6 +349,18 @@ class AchievementsPlugin(
|
||||
"css": ["css/achievements.css"],
|
||||
}
|
||||
|
||||
##~~ SettingsPlugin
|
||||
|
||||
def get_settings_defaults(self):
|
||||
return {
|
||||
"timezone": "",
|
||||
}
|
||||
|
||||
def on_settings_save(self, data):
|
||||
super().on_settings_save(data)
|
||||
if "timezone" in data:
|
||||
self._tz = None
|
||||
|
||||
##~~ TemplatePlugin
|
||||
|
||||
def get_template_configs(self):
|
||||
@@ -333,10 +377,24 @@ class AchievementsPlugin(
|
||||
"template": "achievements_about_stats.jinja2",
|
||||
"custom_bindings": True,
|
||||
},
|
||||
{
|
||||
"type": "settings",
|
||||
"name": gettext("Achievements"),
|
||||
"template": "achievements_settings.jinja2",
|
||||
"custom_bindings": False,
|
||||
},
|
||||
]
|
||||
|
||||
def get_template_vars(self):
|
||||
return {"svgs": self._generate_svg()}
|
||||
import datetime
|
||||
|
||||
import pytz
|
||||
|
||||
return {
|
||||
"svgs": self._generate_svg(),
|
||||
"timezones": pytz.common_timezones,
|
||||
"server_timezone": datetime.datetime.utcnow().astimezone().tzname(),
|
||||
}
|
||||
|
||||
##~~ Internal helpers
|
||||
|
||||
|
||||
@@ -237,7 +237,7 @@ class Achievements(metaclass=AchievementsMetaClass):
|
||||
|
||||
ONE_OF_THOSE_DAYS = Achievement(
|
||||
name="Must Be One Of Those Days",
|
||||
description="Cancel ten consecutive prints.",
|
||||
description="Cancel ten consecutive prints on the same day.",
|
||||
hidden=True,
|
||||
)
|
||||
|
||||
|
||||
@@ -66,6 +66,15 @@ class State(BaseModel):
|
||||
prints_today: int = 0
|
||||
"""Number of prints finished today."""
|
||||
|
||||
date_last_cancelled_print: str = ""
|
||||
"""Date of the last cancelled print."""
|
||||
|
||||
prints_cancelled_today: int = 0
|
||||
"""Number of prints cancelled today."""
|
||||
|
||||
consecutive_prints_cancelled_today: int = 0
|
||||
"""Number of consecutive prints cancelled today."""
|
||||
|
||||
file_last_print: str = ""
|
||||
"""Name of the file of the last print."""
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<h3>{{ _('Achievements Settings') }}</h3>
|
||||
|
||||
<form class="form-horizontal" onsubmit="return false;">
|
||||
<div class="control-group">
|
||||
<label class="control-label"> {{ _('Timezone') }}</label>
|
||||
<div class="controls">
|
||||
<select data-bind="value: settings.plugins.achievements.timezone">
|
||||
<option value="">{{ _('Server timezone (%%(server_time)s)')|format(server_time=plugin_achievements_server_timezone) }}</option>
|
||||
{% for timezone in plugin_achievements_timezones %}
|
||||
<option value="{{ timezone }}">{{ timezone }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<span class="help-inline">{{ _('Select your local timezone here. It will be used for time and date based achievements.') }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
Reference in new issue
Block a user