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>
This commit is contained in:
its your bedtime
2021-10-07 16:32:37 +03:00
committed by GitHub
parent 1dd96419ab
commit 5dbfe3d90a
15 changed files with 152 additions and 61 deletions

View File

@@ -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);
}
}

View File

@@ -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.

View File

@@ -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_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_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(uint32_t icounter, uint32_t level, bool remaining) {
return (DOLPHIN_LVL_THRESHOLD * level * (level + 1) / 2) - (remaining ? icounter : 0);
}

View File

@@ -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_xp_to_levelup(DolphinState* dolphin_state, uint32_t level, bool remaining);
uint32_t dolphin_state_get_level(uint32_t icounter);
uint32_t dolphin_state_xp_to_levelup(uint32_t icounter, uint32_t level, bool remaining);