Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorits your bedtime <23366927+itsyourbedtime@users.noreply.github.com>2021-10-07 16:32:37 +0300
committerGitHub <noreply@github.com>2021-10-07 16:32:37 +0300
commit5dbfe3d90afaad41d86ab4e6e402031b290d0c2e (patch)
tree206980b7f1b50d48fce4fb6ad7b49d8dbcda3fe4 /applications/dolphin
parent1dd96419ab3c82306d5d55c7053117624207ef3e (diff)
Dolphin_srv timestamp and butthurt; Desktop_srv refactoring (#750)
* dolphin_srv: save timestamp on deed; desktop_srv minor refactoring * dolphin_srv timegated butthurt increse, desktop refactoring * desktop app: update debug screen * remove debug logging * increasing icounter affects butthurt value * Dolphin: correct error message on DolphinStore load Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Diffstat (limited to 'applications/dolphin')
-rw-r--r--applications/dolphin/dolphin.c19
-rw-r--r--applications/dolphin/dolphin.h1
-rw-r--r--applications/dolphin/helpers/dolphin_state.c60
-rw-r--r--applications/dolphin/helpers/dolphin_state.h12
4 files changed, 74 insertions, 18 deletions
diff --git a/applications/dolphin/dolphin.c b/applications/dolphin/dolphin.c
index b30fe0d6..f1f1e5e5 100644
--- a/applications/dolphin/dolphin.c
+++ b/applications/dolphin/dolphin.c
@@ -1,6 +1,6 @@
#include "dolphin_i.h"
#include <furi.h>
-
+#define DOLPHIN_TIMEGATE 86400 // one day
#define DOLPHIN_LOCK_EVENT_FLAG (0x1)
void dolphin_deed(Dolphin* dolphin, DolphinDeed deed) {
@@ -77,6 +77,21 @@ void dolphin_event_release(Dolphin* dolphin, DolphinEvent* event) {
}
}
+static void dolphin_check_butthurt(DolphinState* state) {
+ furi_assert(state);
+ float diff_time = difftime(dolphin_state_get_timestamp(state), dolphin_state_timestamp());
+
+#if 0
+ FURI_LOG_I("dolphin-state", "Butthurt check, time since deed %.0f", fabs(diff_time));
+#endif
+
+ if((fabs(diff_time)) > DOLPHIN_TIMEGATE) {
+ // increase butthurt
+ FURI_LOG_I("dolphin-state", "Increasing butthurt");
+ dolphin_state_butthurted(state);
+ }
+}
+
int32_t dolphin_srv(void* p) {
Dolphin* dolphin = dolphin_alloc();
furi_record_create("dolphin", dolphin);
@@ -91,11 +106,13 @@ int32_t dolphin_srv(void* p) {
} else if(event.type == DolphinEventTypeStats) {
event.stats->icounter = dolphin_state_get_icounter(dolphin->state);
event.stats->butthurt = dolphin_state_get_butthurt(dolphin->state);
+ event.stats->timestamp = dolphin_state_get_timestamp(dolphin->state);
} else if(event.type == DolphinEventTypeFlush) {
dolphin_state_save(dolphin->state);
}
dolphin_event_release(dolphin, &event);
} else {
+ dolphin_check_butthurt(dolphin->state);
dolphin_state_save(dolphin->state);
}
}
diff --git a/applications/dolphin/dolphin.h b/applications/dolphin/dolphin.h
index e0ac9307..c8edca61 100644
--- a/applications/dolphin/dolphin.h
+++ b/applications/dolphin/dolphin.h
@@ -7,6 +7,7 @@ typedef struct Dolphin Dolphin;
typedef struct {
uint32_t icounter;
uint32_t butthurt;
+ uint64_t timestamp;
} DolphinStats;
/** Deed complete notification. Call it on deed completion.
diff --git a/applications/dolphin/helpers/dolphin_state.c b/applications/dolphin/helpers/dolphin_state.c
index a525f442..240b7d95 100644
--- a/applications/dolphin/helpers/dolphin_state.c
+++ b/applications/dolphin/helpers/dolphin_state.c
@@ -25,6 +25,7 @@ typedef struct {
uint32_t flags;
uint32_t icounter;
uint32_t butthurt;
+ uint64_t timestamp;
} DolphinStoreData;
typedef struct {
@@ -107,8 +108,12 @@ bool dolphin_state_load(DolphinState* dolphin_state) {
File* file = storage_file_alloc(dolphin_state->fs_api);
bool load_result = storage_file_open(file, DOLPHIN_STORE_KEY, FSAM_READ, FSOM_OPEN_EXISTING);
-
- if(load_result) {
+ if(!load_result) {
+ FURI_LOG_E(
+ "dolphin-state",
+ "Load failed. Storage returned: %s",
+ storage_file_get_error_desc(file));
+ } else {
uint16_t bytes_count = storage_file_read(file, &store, sizeof(DolphinStore));
if(bytes_count != sizeof(DolphinStore)) {
@@ -117,12 +122,8 @@ bool dolphin_state_load(DolphinState* dolphin_state) {
}
if(!load_result) {
- FURI_LOG_E(
- "dolphin-state",
- "Load failed. Storage returned: %s",
- storage_file_get_error_desc(file));
+ FURI_LOG_E("dolphin-state", "DolphinStore size mismatch");
} else {
- FURI_LOG_I("dolphin-state", "State loaded, verifying header");
if(store.header.magic == DOLPHIN_STORE_HEADER_MAGIC &&
store.header.version == DOLPHIN_STORE_HEADER_VERSION) {
FURI_LOG_I(
@@ -150,7 +151,7 @@ bool dolphin_state_load(DolphinState* dolphin_state) {
} else {
FURI_LOG_E(
"dolphin-state",
- "Magic(%d != %d) and Version(%d != %d) mismatch",
+ "Magic(%d != %d) or Version(%d != %d) mismatch",
store.header.magic,
DOLPHIN_STORE_HEADER_MAGIC,
store.header.version,
@@ -171,17 +172,45 @@ void dolphin_state_clear(DolphinState* dolphin_state) {
memset(&dolphin_state->data, 0, sizeof(DolphinStoreData));
}
+uint64_t dolphin_state_timestamp() {
+ RTC_TimeTypeDef time;
+ RTC_DateTypeDef date;
+ struct tm current;
+
+ HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
+ HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
+
+ current.tm_year = date.Year + 100;
+ current.tm_mday = date.Date;
+ current.tm_mon = date.Month - 1;
+
+ current.tm_hour = time.Hours;
+ current.tm_min = time.Minutes;
+ current.tm_sec = time.Seconds;
+
+ return mktime(&current);
+}
+
void dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed) {
const DolphinDeedWeight* deed_weight = dolphin_deed_weight(deed);
int32_t icounter = dolphin_state->data.icounter + deed_weight->icounter;
+ int32_t butthurt = dolphin_state->data.butthurt;
if(icounter >= 0) {
dolphin_state->data.icounter = icounter;
+ dolphin_state->data.butthurt = MAX(butthurt - deed_weight->icounter, 0);
+ dolphin_state->data.timestamp = dolphin_state_timestamp();
}
dolphin_state->dirty = true;
}
+void dolphin_state_butthurted(DolphinState* dolphin_state) {
+ dolphin_state->data.butthurt++;
+ dolphin_state->data.timestamp = dolphin_state_timestamp();
+ dolphin_state->dirty = true;
+}
+
uint32_t dolphin_state_get_icounter(DolphinState* dolphin_state) {
return dolphin_state->data.icounter;
}
@@ -190,13 +219,14 @@ uint32_t dolphin_state_get_butthurt(DolphinState* dolphin_state) {
return dolphin_state->data.butthurt;
}
-uint32_t dolphin_state_get_level(DolphinState* dolphin_state) {
- return 0.5f +
- sqrtf(1.0f + 8.0f * ((float)dolphin_state->data.icounter / DOLPHIN_LVL_THRESHOLD)) /
- 2.0f;
+uint64_t dolphin_state_get_timestamp(DolphinState* dolphin_state) {
+ return dolphin_state->data.timestamp;
+}
+
+uint32_t dolphin_state_get_level(uint32_t icounter) {
+ return 0.5f + sqrtf(1.0f + 8.0f * ((float)icounter / DOLPHIN_LVL_THRESHOLD)) / 2.0f;
}
-uint32_t dolphin_state_xp_to_levelup(DolphinState* dolphin_state, uint32_t level, bool remaining) {
- return (DOLPHIN_LVL_THRESHOLD * level * (level + 1) / 2) -
- (remaining ? dolphin_state->data.icounter : 0);
+uint32_t dolphin_state_xp_to_levelup(uint32_t icounter, uint32_t level, bool remaining) {
+ return (DOLPHIN_LVL_THRESHOLD * level * (level + 1) / 2) - (remaining ? icounter : 0);
} \ No newline at end of file
diff --git a/applications/dolphin/helpers/dolphin_state.h b/applications/dolphin/helpers/dolphin_state.h
index cbd9611a..7c4a33cd 100644
--- a/applications/dolphin/helpers/dolphin_state.h
+++ b/applications/dolphin/helpers/dolphin_state.h
@@ -3,6 +3,8 @@
#include "dolphin_deed.h"
#include <stdbool.h>
#include <stdint.h>
+#include <rtc.h>
+#include <time.h>
typedef struct DolphinState DolphinState;
@@ -16,12 +18,18 @@ bool dolphin_state_load(DolphinState* dolphin_state);
void dolphin_state_clear(DolphinState* dolphin_state);
+uint64_t dolphin_state_timestamp();
+
void dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed);
+void dolphin_state_butthurted(DolphinState* dolphin_state);
+
uint32_t dolphin_state_get_icounter(DolphinState* dolphin_state);
uint32_t dolphin_state_get_butthurt(DolphinState* dolphin_state);
-uint32_t dolphin_state_get_level(DolphinState* dolphin_state);
+uint64_t dolphin_state_get_timestamp(DolphinState* dolphin_state);
+
+uint32_t dolphin_state_get_level(uint32_t icounter);
-uint32_t dolphin_state_xp_to_levelup(DolphinState* dolphin_state, uint32_t level, bool remaining); \ No newline at end of file
+uint32_t dolphin_state_xp_to_levelup(uint32_t icounter, uint32_t level, bool remaining); \ No newline at end of file