Browse Source

+ f2b_matches_score()

master
Alex 'AdUser' Z 3 years ago
parent
commit
316df08b05
  1. 14
      src/matches.c
  2. 8
      src/matches.h
  3. 7
      t/t_matches.c

14
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;
}

8
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_ */

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

Loading…
Cancel
Save