dolphin.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "dolphin/dolphin.h"
  2. #include "desktop/desktop.h"
  3. #include "dolphin/helpers/dolphin_state.h"
  4. #include "dolphin_i.h"
  5. #include "furi/pubsub.h"
  6. #include "sys/_stdint.h"
  7. #include <furi.h>
  8. #define DOLPHIN_TIMEGATE 86400 // one day
  9. #define DOLPHIN_LOCK_EVENT_FLAG (0x1)
  10. void dolphin_deed(Dolphin* dolphin, DolphinDeed deed) {
  11. furi_assert(dolphin);
  12. DolphinEvent event;
  13. event.type = DolphinEventTypeDeed;
  14. event.deed = deed;
  15. dolphin_event_send_async(dolphin, &event);
  16. }
  17. DolphinStats dolphin_stats(Dolphin* dolphin) {
  18. furi_assert(dolphin);
  19. DolphinStats stats;
  20. DolphinEvent event;
  21. event.type = DolphinEventTypeStats;
  22. event.stats = &stats;
  23. dolphin_event_send_wait(dolphin, &event);
  24. return stats;
  25. }
  26. void dolphin_flush(Dolphin* dolphin) {
  27. furi_assert(dolphin);
  28. DolphinEvent event;
  29. event.type = DolphinEventTypeFlush;
  30. dolphin_event_send_wait(dolphin, &event);
  31. }
  32. Dolphin* dolphin_alloc() {
  33. Dolphin* dolphin = furi_alloc(sizeof(Dolphin));
  34. dolphin->state = dolphin_state_alloc();
  35. dolphin->event_queue = osMessageQueueNew(8, sizeof(DolphinEvent), NULL);
  36. dolphin->pubsub = furi_pubsub_alloc();
  37. return dolphin;
  38. }
  39. void dolphin_free(Dolphin* dolphin) {
  40. furi_assert(dolphin);
  41. dolphin_state_free(dolphin->state);
  42. osMessageQueueDelete(dolphin->event_queue);
  43. free(dolphin);
  44. }
  45. void dolphin_event_send_async(Dolphin* dolphin, DolphinEvent* event) {
  46. furi_assert(dolphin);
  47. furi_assert(event);
  48. event->flag = NULL;
  49. furi_check(osMessageQueuePut(dolphin->event_queue, event, 0, osWaitForever) == osOK);
  50. }
  51. void dolphin_event_send_wait(Dolphin* dolphin, DolphinEvent* event) {
  52. furi_assert(dolphin);
  53. furi_assert(event);
  54. event->flag = osEventFlagsNew(NULL);
  55. furi_check(event->flag);
  56. furi_check(osMessageQueuePut(dolphin->event_queue, event, 0, osWaitForever) == osOK);
  57. furi_check(
  58. osEventFlagsWait(event->flag, DOLPHIN_LOCK_EVENT_FLAG, osFlagsWaitAny, osWaitForever) ==
  59. DOLPHIN_LOCK_EVENT_FLAG);
  60. furi_check(osEventFlagsDelete(event->flag) == osOK);
  61. }
  62. void dolphin_event_release(Dolphin* dolphin, DolphinEvent* event) {
  63. if(event->flag) {
  64. osEventFlagsSet(event->flag, DOLPHIN_LOCK_EVENT_FLAG);
  65. }
  66. }
  67. static void dolphin_check_butthurt(DolphinState* state) {
  68. furi_assert(state);
  69. float diff_time = difftime(state->data.timestamp, dolphin_state_timestamp());
  70. if((fabs(diff_time)) > DOLPHIN_TIMEGATE) {
  71. dolphin_state_butthurted(state);
  72. }
  73. }
  74. FuriPubSub* dolphin_get_pubsub(Dolphin* dolphin) {
  75. return dolphin->pubsub;
  76. }
  77. int32_t dolphin_srv(void* p) {
  78. Dolphin* dolphin = dolphin_alloc();
  79. furi_record_create("dolphin", dolphin);
  80. dolphin_state_load(dolphin->state);
  81. DolphinEvent event;
  82. while(1) {
  83. if(osMessageQueueGet(dolphin->event_queue, &event, NULL, 60000) == osOK) {
  84. if(event.type == DolphinEventTypeDeed) {
  85. if(dolphin_state_on_deed(dolphin->state, event.deed)) {
  86. DolphinPubsubEvent event = DolphinPubsubEventUpdate;
  87. furi_pubsub_publish(dolphin->pubsub, &event);
  88. }
  89. } else if(event.type == DolphinEventTypeStats) {
  90. event.stats->icounter = dolphin->state->data.icounter;
  91. event.stats->butthurt = dolphin->state->data.butthurt;
  92. event.stats->timestamp = dolphin->state->data.timestamp;
  93. event.stats->level = dolphin_get_level(dolphin->state->data.icounter);
  94. event.stats->level_up_is_pending =
  95. !dolphin_state_xp_to_levelup(dolphin->state->data.icounter);
  96. } else if(event.type == DolphinEventTypeFlush) {
  97. dolphin_state_save(dolphin->state);
  98. }
  99. dolphin_event_release(dolphin, &event);
  100. } else {
  101. dolphin_check_butthurt(dolphin->state);
  102. dolphin_state_save(dolphin->state);
  103. }
  104. }
  105. dolphin_free(dolphin);
  106. return 0;
  107. }
  108. void dolphin_upgrade_level(Dolphin* dolphin) {
  109. dolphin_state_increase_level(dolphin->state);
  110. dolphin_flush(dolphin);
  111. }