diff options
author | necrashter | 2022-04-23 22:45:12 +0300 |
---|---|---|
committer | Yigit Sever | 2022-04-23 23:12:09 +0300 |
commit | e4e31cecd4e5032c0b2d8ef8f71f24cef9212cd2 (patch) | |
tree | b763bb4178df350aeab46f51e7991717b9aa4d20 /src/db.rs | |
parent | 6737ce1650dab4819a33ac03ff3e9c8bb09d3645 (diff) | |
download | gradecoin-e4e31cecd4e5032c0b2d8ef8f71f24cef9212cd2.tar.gz gradecoin-e4e31cecd4e5032c0b2d8ef8f71f24cef9212cd2.tar.bz2 gradecoin-e4e31cecd4e5032c0b2d8ef8f71f24cef9212cd2.zip |
Load bots from config file instead of hardcoding
Diffstat (limited to 'src/db.rs')
-rw-r--r-- | src/db.rs | 62 |
1 files changed, 17 insertions, 45 deletions
@@ -9,7 +9,7 @@ | |||
9 | //! [`Db::users`] is the in memory representation of the users, | 9 | //! [`Db::users`] is the in memory representation of the users, |
10 | //! with their public keys, `metu_ids` and gradecoin balances. | 10 | //! with their public keys, `metu_ids` and gradecoin balances. |
11 | use crate::block::{Block, Fingerprint, Id, Transaction}; | 11 | use crate::block::{Block, Fingerprint, Id, Transaction}; |
12 | use crate::config::Config; | 12 | use crate::config::{BotConfig, Config}; |
13 | use crate::student::{MetuId, User, UserAtRest}; | 13 | use crate::student::{MetuId, User, UserAtRest}; |
14 | use log::info; | 14 | use log::info; |
15 | use parking_lot::RwLock; | 15 | use parking_lot::RwLock; |
@@ -30,7 +30,7 @@ impl Db { | |||
30 | fs::create_dir_all(format!("users/{}", config.name)).unwrap(); | 30 | fs::create_dir_all(format!("users/{}", config.name)).unwrap(); |
31 | 31 | ||
32 | // Load bots | 32 | // Load bots |
33 | let users: HashMap<Fingerprint, User> = get_friendly_users(); | 33 | let users: HashMap<Fingerprint, User> = get_bots(&config.bots); |
34 | 34 | ||
35 | // Load the list of users who can register | 35 | // Load the list of users who can register |
36 | let preapproved_users = read_approved_users(&config.preapproved_users); | 36 | let preapproved_users = read_approved_users(&config.preapproved_users); |
@@ -138,49 +138,21 @@ fn read_users(config_name: &str) -> io::Result<Vec<PathBuf>> { | |||
138 | Ok(entries) | 138 | Ok(entries) |
139 | } | 139 | } |
140 | 140 | ||
141 | fn get_friendly_users() -> HashMap<Fingerprint, User> { | 141 | /// Build bots from the given set of bot configurations. |
142 | let mut users: HashMap<Fingerprint, User> = HashMap::new(); | 142 | fn get_bots(bot_configs: &HashMap<Fingerprint, BotConfig>) -> HashMap<Fingerprint, User> { |
143 | 143 | let mut index = 0; | |
144 | users.insert( | 144 | |
145 | "cde48537ca2c28084ff560826d0e6388b7c57a51497a6cb56f397289e52ff41b".to_owned(), | 145 | bot_configs.iter() |
146 | User { | 146 | .map(|(fingerprint, config)| { |
147 | user_id: MetuId::new("friend_1".to_owned(), "not_used".to_owned()), | 147 | index += 1; |
148 | public_key: "not_used".to_owned(), | 148 | (fingerprint.to_string(), User { |
149 | balance: 70, | 149 | user_id: MetuId::new(format!("friend_{}", index), "not_used".to_owned()), |
150 | is_bot: true, | 150 | public_key: "not_used".to_owned(), |
151 | }, | 151 | balance: config.starting_balance, |
152 | ); | 152 | is_bot: true, |
153 | 153 | }) | |
154 | users.insert( | 154 | }) |
155 | "a1a38b5bae5866d7d998a9834229ec2f9db7a4fc8fb6f58b1115a96a446875ff".to_owned(), | 155 | .collect() |
156 | User { | ||
157 | user_id: MetuId::new("friend_2".to_owned(), "not_used".to_owned()), | ||
158 | public_key: "not_used".to_owned(), | ||
159 | balance: 20, | ||
160 | is_bot: true, | ||
161 | }, | ||
162 | ); | ||
163 | |||
164 | users.insert( | ||
165 | "4e048fd2a62f1307866086e803e9be43f78a702d5df10831fbf434e7663ae0e7".to_owned(), | ||
166 | User { | ||
167 | user_id: MetuId::new("friend_4".to_owned(), "not_used".to_owned()), | ||
168 | public_key: "not_used".to_owned(), | ||
169 | balance: 120, | ||
170 | is_bot: true, | ||
171 | }, | ||
172 | ); | ||
173 | |||
174 | users.insert( | ||
175 | "60e77101e76950a9b1830fa107fd2f8fc545255b3e0f14b6a7797cf9ee005f07".to_owned(), | ||
176 | User { | ||
177 | user_id: MetuId::new("friend_4".to_owned(), "not_used".to_owned()), | ||
178 | public_key: "not_used".to_owned(), | ||
179 | balance: 40, | ||
180 | is_bot: true, | ||
181 | }, | ||
182 | ); | ||
183 | users | ||
184 | } | 156 | } |
185 | 157 | ||
186 | fn read_approved_users(filename: &str) -> Vec<MetuId> { | 158 | fn read_approved_users(filename: &str) -> Vec<MetuId> { |