No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 4 horas
hace 4 horas
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 4 horas
hace 2 semanas
hace 3 días
hace 2 semanas
hace 3 días
hace 2 semanas
hace 1 semana
hace 1 semana
hace 1 semana
hace 4 horas
hace 1 semana
hace 4 horas
hace 1 semana
hace 4 horas
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 1 semana
hace 2 semanas
hace 1 semana
hace 4 horas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 3 días
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 1 semana
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 1 semana
hace 1 semana
hace 2 semanas
hace 4 horas
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 4 horas
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 4 horas
hace 2 semanas
hace 4 horas
hace 3 días
hace 2 semanas
hace 4 horas
hace 2 semanas
hace 4 horas
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 4 horas
hace 2 semanas
hace 1 semana
hace 4 horas
hace 1 semana
hace 1 semana
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 3 días
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 3 días
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
hace 1 semana
hace 2 semanas
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #include <math.h>
  2. #include <memory.h>
  3. #include <time.h>
  4. #include "./djstdlib/core.cpp"
  5. const string LOG_FILE_LOCATION = "./log.gtl"_s;
  6. const string DB_FILE_LOCATION = "./db.gtd"_s;
  7. struct GymLogDbHeader {
  8. uint32 nextId;
  9. };
  10. struct GymLogDbEntry {
  11. uint32 id;
  12. uint32 nameLength;
  13. };
  14. struct GymLogDbParsedEntry {
  15. uint32 id;
  16. string name;
  17. };
  18. typedef GymLogDbParsedEntry Exercise;
  19. struct GymLogDbParsed {
  20. GymLogDbHeader header;
  21. list<GymLogDbParsedEntry> entries;
  22. };
  23. struct WeightRepsInfo {
  24. uint8 reps;
  25. real32 weight;
  26. };
  27. struct GymLogEntry {
  28. uint64 timestamp;
  29. uint32 exerciseId;
  30. union {
  31. WeightRepsInfo weightRepsInfo;
  32. };
  33. };
  34. struct WorkSummary {
  35. real32 totalWork;
  36. uint32 restTime;
  37. };
  38. GymLogDbParsed *parseDb(Arena *arena, string database) {
  39. GymLogDbParsed *dbParsed = PushStruct(arena, GymLogDbParsed);
  40. dbParsed->header = *((GymLogDbHeader *)database.str);
  41. size_t head = sizeof(GymLogDbHeader);
  42. uint32 entriesLeft = dbParsed->header.nextId - 1;
  43. dbParsed->entries = PushList(arena, GymLogDbParsedEntry, entriesLeft);
  44. while (entriesLeft > 0 && head < database.length) {
  45. GymLogDbEntry *currentEntry = (GymLogDbEntry *)((byte *)database.str + head);
  46. GymLogDbParsedEntry parsedEntry = { currentEntry->id, PushString(arena, currentEntry->nameLength) };
  47. head += sizeof(GymLogDbEntry);
  48. memcpy(parsedEntry.name.str, database.str + head, currentEntry->nameLength);
  49. appendList(&dbParsed->entries, parsedEntry);
  50. head += currentEntry->nameLength;
  51. }
  52. return dbParsed;
  53. }
  54. list<GymLogEntry> loadEntryLog(Arena *arena, string fileLocation) {
  55. list<GymLogEntry> result = {0};
  56. string logfile = os_readEntireFile(arena, LOG_FILE_LOCATION);
  57. if (logfile.length % sizeof(GymLogEntry) != 0) {
  58. log("Log file corrupted.\n");
  59. } else {
  60. size_t entryCount = logfile.length / sizeof(GymLogEntry);
  61. result = { (GymLogEntry *)logfile.str, entryCount, entryCount };
  62. }
  63. return result;
  64. }
  65. WorkSummary workSummaryForExercise(list<GymLogEntry> entries, Exercise exercise) {
  66. WorkSummary result = {0};
  67. UnixTimestamp lastTimestamp = 0;
  68. for (EachInReversed(entries, i)) {
  69. if (entries.data[i].exerciseId == exercise.id) {
  70. GymLogEntry logEntry = entries.data[i];
  71. result.totalWork += logEntry.weightRepsInfo.weight * logEntry.weightRepsInfo.reps;
  72. if (lastTimestamp > 0) {
  73. result.restTime += (uint32)(lastTimestamp - logEntry.timestamp);
  74. }
  75. lastTimestamp = logEntry.timestamp;
  76. }
  77. }
  78. return result;
  79. }
  80. int gymTrackerLogWorkToday(Arena *arena, Exercise exercise) {
  81. int statusCode = 0;
  82. string logfile = os_readEntireFile(arena, LOG_FILE_LOCATION);
  83. if (logfile.length % sizeof(GymLogEntry) != 0) {
  84. log("Log file corrupted.\n");
  85. statusCode = 1;
  86. } else {
  87. size_t entryCount = logfile.length / sizeof(GymLogEntry);
  88. list<GymLogEntry> logEntries = { (GymLogEntry *)logfile.str, entryCount, entryCount };
  89. UnixTimestamp todayUnix = getSystemUnixTime();
  90. Timestamp todayTs = timestampFromUnixTime(&todayUnix);
  91. list<GymLogEntry> todaysEntries = {0};
  92. todaysEntries.data = logEntries.data;
  93. for (EachInReversed(logEntries, i)) {
  94. GymLogEntry logEntry = logEntries.data[i];
  95. Timestamp logTs = timestampFromUnixTime(&logEntry.timestamp);
  96. if (logTs.tm_yday == todayTs.tm_yday && todayTs.tm_year == logTs.tm_year) {
  97. todaysEntries.head += 1;
  98. todaysEntries.data = &logEntries.data[i];
  99. }
  100. }
  101. if (todaysEntries.data) {
  102. todaysEntries.length = todaysEntries.head;
  103. WorkSummary summary = workSummaryForExercise(todaysEntries, exercise);
  104. log("Total work today for %S:\n%.2fkg in ~%.2fmin.\n", exercise.name, summary.totalWork, (real32)summary.restTime / 60.0f);
  105. }
  106. }
  107. return statusCode;
  108. }
  109. int gymTrackerStatus(Arena *arena, list<string> args) {
  110. int statusCode = 0;
  111. string file = os_readEntireFile(arena, LOG_FILE_LOCATION);
  112. if (file.length % sizeof(GymLogEntry) != 0) {
  113. puts("Log file corrupted.");
  114. statusCode = 1;
  115. } else {
  116. GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION));
  117. Timestamp startTs = {0};
  118. int numDays = 1;
  119. bool showAll = args.length == 1 && strEql(args.data[0], "--all"_s);
  120. if (!showAll) {
  121. if (args.length == 2 && (strEql(args.data[0], "--days"_s) || strEql(args.data[0], "-d"_s))) {
  122. size_t l;
  123. numDays = parsePositiveInt(args.data[1], &l);
  124. }
  125. if (numDays == -1) {
  126. puts("Bad argument for --days (-d) parameter.");
  127. statusCode = 1;
  128. } else {
  129. uint64 todayUnix = getSystemUnixTime();
  130. UnixTimestamp startUnix = todayUnix - numDays * 24 * 60 * 60;
  131. startTs = timestampFromUnixTime(&startUnix);
  132. }
  133. }
  134. if (statusCode == 0) {
  135. int lastDay = -1;
  136. int lastYear = -1;
  137. size_t entryCount = file.length / sizeof(GymLogEntry);
  138. list<GymLogEntry> logEntries = { (GymLogEntry *)file.str, entryCount, entryCount };
  139. list<string> nameByExercise = PushFullListZero(arena, string, db->header.nextId);
  140. list<real32> workPerExerciseByDay = PushFullListZero(arena, real32, db->header.nextId);
  141. list<uint32> restPerExerciseByDay = PushFullListZero(arena, uint32, db->header.nextId);
  142. list<uint32> lastTsPerExerciseByDay = PushFullListZero(arena, uint32, db->header.nextId);
  143. int dayCount = 0;
  144. Timestamp timestamp = {0};
  145. GymLogEntry *prevEntry = 0;
  146. GymLogEntry *entry = 0;
  147. for (EachIn(logEntries, i)) {
  148. prevEntry = entry;
  149. entry = &logEntries.data[i];
  150. timestamp = timestampFromUnixTime(&entry->timestamp);
  151. if (timestamp.tm_year < startTs.tm_year || timestamp.tm_yday < startTs.tm_yday) {
  152. continue;
  153. }
  154. if (timestamp.tm_yday != lastDay || timestamp.tm_year != lastYear) {
  155. if (dayCount > 0) {
  156. log("\n");
  157. }
  158. log("================ %S =================================\n", formatTimeYmd(arena, &timestamp));
  159. lastDay = timestamp.tm_yday;
  160. lastYear = timestamp.tm_year;
  161. dayCount++;
  162. }
  163. workPerExerciseByDay.data[entry->exerciseId] += entry->weightRepsInfo.reps * entry->weightRepsInfo.weight;
  164. uint32 lastTsForExercise = lastTsPerExerciseByDay.data[entry->exerciseId];
  165. if (lastTsForExercise > 0) {
  166. restPerExerciseByDay.data[entry->exerciseId] += (uint32)(entry->timestamp - lastTsForExercise);
  167. }
  168. lastTsPerExerciseByDay.data[entry->exerciseId] = (uint32)entry->timestamp;
  169. const char *format;
  170. if (entry->weightRepsInfo.weight == (int32)entry->weightRepsInfo.weight) {
  171. format = "%S: %S %.0fkg X %i\n";
  172. } else {
  173. format = "%S: %S %.2fkg X %i\n";
  174. }
  175. string *exerciseName = &(nameByExercise.data[entry->exerciseId]);
  176. if (exerciseName->str == 0) {
  177. for (EachIn(db->entries, j)) {
  178. GymLogDbParsedEntry dbEntry = db->entries.data[j];
  179. if (dbEntry.id == entry->exerciseId) {
  180. *exerciseName = dbEntry.name;
  181. }
  182. }
  183. }
  184. string nameToPrint = {0};
  185. if (prevEntry && entry->exerciseId == prevEntry->exerciseId) {
  186. nameToPrint = PushStringFill(arena, exerciseName->length, '.');
  187. } else {
  188. nameToPrint = exerciseName->str ? *exerciseName : "unknown-exercise"_s;
  189. log("\n");
  190. }
  191. log(format,
  192. formatTimeHms(arena, &timestamp),
  193. nameToPrint,
  194. entry->weightRepsInfo.weight,
  195. entry->weightRepsInfo.reps);
  196. Timestamp nextTimestamp = {0};
  197. if (i < logEntries.head - 1) {
  198. nextTimestamp = timestampFromUnixTime(&logEntries.data[i + 1].timestamp);
  199. }
  200. if (i == logEntries.head + 1 || nextTimestamp.tm_yday != lastDay || nextTimestamp.tm_year != lastYear) {
  201. log("\n");
  202. log("Work summary:\n");
  203. for (size_t j = 0; j < workPerExerciseByDay.length; j++) {
  204. if (workPerExerciseByDay.data[j] != 0.0f) {
  205. log("%S: %.2fkg in %.2fmin\n", nameByExercise.data[j], workPerExerciseByDay.data[j], (real32)restPerExerciseByDay.data[j] / 60.0f);
  206. }
  207. }
  208. zeroListFull(&workPerExerciseByDay);
  209. zeroListFull(&restPerExerciseByDay);
  210. zeroListFull(&lastTsPerExerciseByDay);
  211. }
  212. }
  213. }
  214. }
  215. return statusCode;
  216. }
  217. int gymTrackerDeleteEntries(Arena *arena, list<string> args) {
  218. int statusCode = 0;
  219. if (args.length == 0) {
  220. log("Please pass the number of entries to delete starting from the most recent.");
  221. statusCode = 1;
  222. } else {
  223. size_t position = 0;
  224. int numToDelete = parsePositiveInt(args.data[0], &position);
  225. if (numToDelete != -1) {
  226. list<GymLogEntry> logEntries = loadEntryLog(arena, LOG_FILE_LOCATION);
  227. if (numToDelete > logEntries.length) {
  228. log("%i is more than the current number of log entries (%i). Aborting.", numToDelete, logEntries.length);
  229. statusCode = 1;
  230. } else {
  231. os_writeEntireFile(arena, LOG_FILE_LOCATION, (byte *)logEntries.data, (logEntries.length - numToDelete) * sizeof(GymLogEntry));
  232. }
  233. } else {
  234. log("Invalid number to delete.\n");
  235. statusCode = 0;
  236. }
  237. }
  238. return statusCode;
  239. }
  240. // Syntax: do <exercise-name> weightKg reps
  241. int gymTrackerDo(Arena *arena, list<string> args) {
  242. int statusCode = 0;
  243. Exercise exercise = {};
  244. if (args.length < 3 || args.data[0].length == 0) {
  245. log("Invalid exercise name and/or number of arguments.\n");
  246. statusCode = 1;
  247. } else {
  248. exercise.name = args.data[0];
  249. }
  250. GymLogDbParsedEntry *existingEntry = 0;
  251. if (statusCode == 0) {
  252. GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION));
  253. for (EachIn(db->entries, i)) {
  254. GymLogDbParsedEntry entry = db->entries.data[i];
  255. if (strStartsWith(entry.name, exercise.name)) {
  256. existingEntry = &entry;
  257. if (entry.name.length != exercise.name.length) {
  258. exercise.name = entry.name;
  259. log("Assuming exercise \"%S\".\n\n", entry.name);
  260. }
  261. break;
  262. }
  263. }
  264. if (!existingEntry) {
  265. log("The exercise \"%S\" hasn't been registered.", exercise.name);
  266. statusCode = 1;
  267. }
  268. }
  269. if (statusCode == 0) {
  270. exercise.id = existingEntry->id;
  271. size_t parsedCount = 0;
  272. real32 kg = parsePositiveReal32(args.data[1], &parsedCount);
  273. uint8 reps = parsePositiveInt(args.data[2], &parsedCount);
  274. if (parsedCount == 0 || kg == NAN || reps == 0 || kg == 0) {
  275. log("Invalid reps or weight input.\n");
  276. statusCode = 1;
  277. } else {
  278. GymLogEntry entry = {
  279. getSystemUnixTime(),
  280. exercise.id,
  281. reps,
  282. kg,
  283. };
  284. os_fileAppend(arena, LOG_FILE_LOCATION, (byte *)&entry, sizeof(entry));
  285. statusCode = gymTrackerLogWorkToday(arena, exercise);
  286. }
  287. }
  288. return statusCode;
  289. }
  290. int gymTrackerListExercises(Arena *arena, list<string> args) {
  291. int statusCode = 0;
  292. GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION));
  293. if (db->entries.length == 0) {
  294. log("No entries currently registered in the exercise database.");
  295. } else {
  296. log("%i entries currently registered in the exercise database:\n\n", db->entries.length);
  297. for (EachIn(db->entries, i)) {
  298. log("#%i: %S\n", i + 1, db->entries.data[i].name);
  299. }
  300. }
  301. return statusCode;
  302. }
  303. int gymTrackerAddExercise(Arena *arena, list<string> args) {
  304. int statusCode = 0;
  305. string newExerciseName = args.data[0];
  306. if (newExerciseName.length == 0) {
  307. log("No exercise name provided.\n");
  308. statusCode = 1;
  309. }
  310. if (statusCode != 1) {
  311. string database = os_readEntireFile(arena, DB_FILE_LOCATION);
  312. byte *buf = 0;
  313. size_t newEntryStartIndex = 0;
  314. if (database.length == 0) {
  315. // Initialise DB
  316. newEntryStartIndex = sizeof(GymLogDbHeader);
  317. buf = PushArray(arena, byte, sizeof(GymLogDbHeader) + sizeof(GymLogDbEntry) + newExerciseName.length);
  318. GymLogDbHeader *header = (GymLogDbHeader *)buf;
  319. header->nextId = 1;
  320. } else {
  321. // Validate entry not already present
  322. bool invalid = false;
  323. GymLogDbHeader *header = (GymLogDbHeader *)database.str;
  324. size_t head = sizeof(GymLogDbHeader);
  325. uint32 entriesLeft = header->nextId - 1;
  326. while (entriesLeft > 0 && head < database.length) {
  327. GymLogDbEntry *currentEntry = (GymLogDbEntry *)((byte *)database.str + head);
  328. head += sizeof(GymLogDbEntry);
  329. string entryName = {(char *)((byte *)database.str + head), currentEntry->nameLength };
  330. if (strEql(entryName, newExerciseName)) {
  331. invalid = true;
  332. log("Exercise \"%S\" already registered (entry #%i)\n", entryName, currentEntry->id);
  333. break;
  334. }
  335. head += currentEntry->nameLength;
  336. }
  337. if (!invalid) {
  338. newEntryStartIndex = database.length;
  339. buf = PushArray(arena, byte, database.length + sizeof(GymLogDbEntry) + newExerciseName.length);
  340. memcpy(buf, database.str, database.length);
  341. } else {
  342. statusCode = 1;
  343. }
  344. }
  345. if (statusCode != 1) {
  346. // Add entry
  347. GymLogDbHeader *header = (GymLogDbHeader *)buf;
  348. GymLogDbEntry *entry = (GymLogDbEntry *)(buf + newEntryStartIndex);
  349. entry->id = header->nextId;
  350. entry->nameLength = (uint32)newExerciseName.length;
  351. header->nextId++;
  352. byte *newExerciseNameDb = buf + newEntryStartIndex + sizeof(GymLogDbEntry);
  353. memcpy(newExerciseNameDb, newExerciseName.str, newExerciseName.length);
  354. size_t bufSize = newEntryStartIndex + sizeof(GymLogDbEntry) + newExerciseName.length;
  355. os_writeEntireFile(arena, DB_FILE_LOCATION, buf, bufSize);
  356. }
  357. }
  358. return statusCode;
  359. }
  360. int main(int argc, char **argv) {
  361. initialiseCore();
  362. Arena *arena = arenaAlloc(Megabytes(64));
  363. list<string> args = getArgs(arena, argc, argv);
  364. int statusCode = 0;
  365. if (args.length < 1) {
  366. log("At least one arg is required.\n");
  367. statusCode = 1;
  368. }
  369. if (statusCode == 0) {
  370. string cmd = args.data[0];
  371. list<string> argsRest = listSlice(args, 1);
  372. if (strEql("status"_s, cmd)) {
  373. statusCode = gymTrackerStatus(arena, argsRest);
  374. } else if (strEql("do"_s, cmd)) {
  375. statusCode = gymTrackerDo(arena, argsRest);
  376. } else if (strEql("delete"_s, cmd)) {
  377. statusCode = gymTrackerDeleteEntries(arena, argsRest);
  378. } else if (strEql("list"_s, cmd)) {
  379. statusCode = gymTrackerListExercises(arena, argsRest);
  380. } else if (strEql("add"_s, cmd)) {
  381. statusCode = gymTrackerAddExercise(arena, argsRest);
  382. } else {
  383. log("Unknown command \"%S\"\n", args.data[0]);
  384. statusCode = 1;
  385. }
  386. }
  387. return statusCode;
  388. }