diff --git a/src/matches.c b/src/matches.c index d30a9c1..c2d021e 100644 --- a/src/matches.c +++ b/src/matches.c @@ -69,3 +69,17 @@ f2b_matches_expire(f2b_matches_t *ms, time_t before) { } ms->last = ms->list ? ms->list->time : 0; } + +int +f2b_matches_score(f2b_matches_t *ms, time_t after) { + int score = 0; + + assert(ms != NULL); + for (f2b_match_t *match = ms->list; match != NULL; match = match->next) { + if (after > match->time) + break; /* speedhack: consider list is sorted from newest to oldest matches */ + score += match->score; + } + + return score; +} diff --git a/src/matches.h b/src/matches.h index 717e5ab..a65a192 100644 --- a/src/matches.h +++ b/src/matches.h @@ -54,4 +54,12 @@ void f2b_matches_prepend (f2b_matches_t *ms, f2b_match_t *m); */ void f2b_matches_expire (f2b_matches_t *m, time_t before); +/** + * @brief Get sum of scores after specified time + * @param m Pointer to struct + * @param after Only check matches after this time + * @returns Sum of scores + */ +int f2b_matches_score(f2b_matches_t *ms, time_t after); + #endif /* F2B_MATCHES_H_ */ diff --git a/t/t_matches.c b/t/t_matches.c index 12300fc..9b74fdc 100644 --- a/t/t_matches.c +++ b/t/t_matches.c @@ -7,6 +7,7 @@ int main() { bool result = false; size_t size = 15; time_t now = time(NULL); + int score = 0; UNUSED(result); @@ -28,6 +29,7 @@ int main() { for (size_t i = 1; i < size; i++) { match = f2b_match_create(now - 60 * (size - i)); + match->score = i * 10; f2b_matches_prepend(&matches, match); assert(matches.count == i); assert(matches.last == now - (time_t) (60 * (size - i))); @@ -36,6 +38,11 @@ int main() { f2b_matches_expire(&matches, now - 60 * 4); assert(matches.count == 3); + score = f2b_matches_score(&matches, 0); + assert(score == 140 + 130 + 120); + score = f2b_matches_score(&matches, now - 60); + assert(score == 140); + f2b_matches_flush(&matches); assert(matches.count == 0); assert(matches.last == 0);