| 1 |
/***************************************************************************
|
| 2 |
user_list.c - description
|
| 3 |
-------------------
|
| 4 |
Copyright : (C) 2004-2025 by Leaflet
|
| 5 |
Email : leaflet@leafok.com
|
| 6 |
***************************************************************************/
|
| 7 |
|
| 8 |
/***************************************************************************
|
| 9 |
* *
|
| 10 |
* This program is free software; you can redistribute it and/or modify *
|
| 11 |
* it under the terms of the GNU General Public License as published by *
|
| 12 |
* the Free Software Foundation; either version 3 of the License, or *
|
| 13 |
* (at your option) any later version. *
|
| 14 |
* *
|
| 15 |
***************************************************************************/
|
| 16 |
|
| 17 |
#include "log.h"
|
| 18 |
#include "section_list.h"
|
| 19 |
#include "user_list.h"
|
| 20 |
#include "user_stat.h"
|
| 21 |
|
| 22 |
int user_stat_map_init(USER_STAT_MAP *p_map)
|
| 23 |
{
|
| 24 |
if (p_map == NULL)
|
| 25 |
{
|
| 26 |
log_error("NULL pointer error\n");
|
| 27 |
return -1;
|
| 28 |
}
|
| 29 |
|
| 30 |
p_map->user_count = 0;
|
| 31 |
p_map->last_uid = -1;
|
| 32 |
p_map->last_article_index = -1;
|
| 33 |
|
| 34 |
return 0;
|
| 35 |
}
|
| 36 |
|
| 37 |
int user_stat_map_update(USER_STAT_MAP *p_map)
|
| 38 |
{
|
| 39 |
int32_t uid_list[BBS_max_user_count];
|
| 40 |
int user_cnt = BBS_max_user_count;
|
| 41 |
int article_cnt;
|
| 42 |
int i;
|
| 43 |
ARTICLE *p_article;
|
| 44 |
|
| 45 |
if (p_map == NULL)
|
| 46 |
{
|
| 47 |
log_error("NULL pointer error\n");
|
| 48 |
return -1;
|
| 49 |
}
|
| 50 |
|
| 51 |
// Load uid_list
|
| 52 |
if (get_user_id_list(uid_list, &user_cnt, p_map->last_uid + 1) < 0)
|
| 53 |
{
|
| 54 |
log_error("get_user_id_list(%d, 0) error\n", user_cnt);
|
| 55 |
return -2;
|
| 56 |
}
|
| 57 |
|
| 58 |
for (i = 0; i < user_cnt && p_map->user_count + i < BBS_max_user_count; i++)
|
| 59 |
{
|
| 60 |
p_map->stat_list[p_map->user_count + i].uid = uid_list[i];
|
| 61 |
p_map->stat_list[p_map->user_count + i].article_count = 0;
|
| 62 |
}
|
| 63 |
|
| 64 |
p_map->user_count += i;
|
| 65 |
p_map->last_uid = uid_list[i - 1];
|
| 66 |
|
| 67 |
// Scan articles
|
| 68 |
article_cnt = article_block_article_count();
|
| 69 |
for (i = p_map->last_article_index + 1; i < article_cnt; i++)
|
| 70 |
{
|
| 71 |
p_article = article_block_find_by_index(i);
|
| 72 |
if (p_article == NULL)
|
| 73 |
{
|
| 74 |
log_error("article_block_find_by_index(index=%d) error\n", i);
|
| 75 |
break;
|
| 76 |
}
|
| 77 |
|
| 78 |
if (p_article->visible == 0)
|
| 79 |
{
|
| 80 |
continue;
|
| 81 |
}
|
| 82 |
|
| 83 |
if (p_article->uid > p_map->last_uid)
|
| 84 |
{
|
| 85 |
#ifdef _DEBUG
|
| 86 |
log_error("uid=%d of article(aid=%d) is greater than last_uid=%d\n",
|
| 87 |
p_article->uid, p_article->aid, p_map->last_uid);
|
| 88 |
#endif
|
| 89 |
continue;
|
| 90 |
}
|
| 91 |
|
| 92 |
if (user_stat_article_cnt_inc(p_map, p_article->uid, 1) < 0)
|
| 93 |
{
|
| 94 |
log_error("user_stat_article_cnt_inc(uid=%d, 1) error\n", p_article->uid);
|
| 95 |
break;
|
| 96 |
}
|
| 97 |
}
|
| 98 |
|
| 99 |
p_map->last_article_index = i - 1;
|
| 100 |
|
| 101 |
return 0;
|
| 102 |
}
|
| 103 |
|
| 104 |
int user_stat_article_cnt_inc(USER_STAT_MAP *p_map, int32_t uid, int n)
|
| 105 |
{
|
| 106 |
int left;
|
| 107 |
int right;
|
| 108 |
int mid;
|
| 109 |
|
| 110 |
if (p_map == NULL)
|
| 111 |
{
|
| 112 |
log_error("NULL pointer error\n");
|
| 113 |
return -1;
|
| 114 |
}
|
| 115 |
|
| 116 |
left = 0;
|
| 117 |
right = p_map->user_count - 1;
|
| 118 |
|
| 119 |
while (left < right)
|
| 120 |
{
|
| 121 |
mid = (left + right) / 2;
|
| 122 |
if (uid < p_map->stat_list[mid].uid)
|
| 123 |
{
|
| 124 |
right = mid - 1;
|
| 125 |
}
|
| 126 |
else if (uid > p_map->stat_list[mid].uid)
|
| 127 |
{
|
| 128 |
left = mid + 1;
|
| 129 |
}
|
| 130 |
else // if (uid == p_map->stat_list[mid].uid)
|
| 131 |
{
|
| 132 |
left = mid;
|
| 133 |
break;
|
| 134 |
}
|
| 135 |
}
|
| 136 |
|
| 137 |
if (uid == p_map->stat_list[left].uid) // found
|
| 138 |
{
|
| 139 |
p_map->stat_list[left].article_count += n;
|
| 140 |
return 1;
|
| 141 |
}
|
| 142 |
|
| 143 |
return 0; // not found
|
| 144 |
}
|
| 145 |
|
| 146 |
int user_stat_get(USER_STAT_MAP *p_map, int32_t uid, const USER_STAT **pp_stat)
|
| 147 |
{
|
| 148 |
int left;
|
| 149 |
int right;
|
| 150 |
int mid;
|
| 151 |
|
| 152 |
if (p_map == NULL)
|
| 153 |
{
|
| 154 |
log_error("NULL pointer error\n");
|
| 155 |
return -1;
|
| 156 |
}
|
| 157 |
|
| 158 |
left = 0;
|
| 159 |
right = p_map->user_count - 1;
|
| 160 |
|
| 161 |
while (left < right)
|
| 162 |
{
|
| 163 |
mid = (left + right) / 2;
|
| 164 |
if (uid < p_map->stat_list[mid].uid)
|
| 165 |
{
|
| 166 |
right = mid - 1;
|
| 167 |
}
|
| 168 |
else if (uid > p_map->stat_list[mid].uid)
|
| 169 |
{
|
| 170 |
left = mid + 1;
|
| 171 |
}
|
| 172 |
else // if (uid == p_map->stat_list[mid].uid)
|
| 173 |
{
|
| 174 |
left = mid;
|
| 175 |
break;
|
| 176 |
}
|
| 177 |
}
|
| 178 |
|
| 179 |
if (uid == p_map->stat_list[left].uid) // found
|
| 180 |
{
|
| 181 |
*pp_stat = &(p_map->stat_list[left]);
|
| 182 |
return 1;
|
| 183 |
}
|
| 184 |
|
| 185 |
// not found
|
| 186 |
*pp_stat = NULL;
|
| 187 |
return 0;
|
| 188 |
}
|