From e4e31cecd4e5032c0b2d8ef8f71f24cef9212cd2 Mon Sep 17 00:00:00 2001 From: necrashter Date: Sat, 23 Apr 2022 22:45:12 +0300 Subject: Load bots from config file instead of hardcoding --- src/db.rs | 62 +++++++++++++++++--------------------------------------------- 1 file changed, 17 insertions(+), 45 deletions(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index 64be0c1..6befa2d 100644 --- a/src/db.rs +++ b/src/db.rs @@ -9,7 +9,7 @@ //! [`Db::users`] is the in memory representation of the users, //! with their public keys, `metu_ids` and gradecoin balances. use crate::block::{Block, Fingerprint, Id, Transaction}; -use crate::config::Config; +use crate::config::{BotConfig, Config}; use crate::student::{MetuId, User, UserAtRest}; use log::info; use parking_lot::RwLock; @@ -30,7 +30,7 @@ impl Db { fs::create_dir_all(format!("users/{}", config.name)).unwrap(); // Load bots - let users: HashMap = get_friendly_users(); + let users: HashMap = get_bots(&config.bots); // Load the list of users who can register let preapproved_users = read_approved_users(&config.preapproved_users); @@ -138,49 +138,21 @@ fn read_users(config_name: &str) -> io::Result> { Ok(entries) } -fn get_friendly_users() -> HashMap { - let mut users: HashMap = HashMap::new(); - - users.insert( - "cde48537ca2c28084ff560826d0e6388b7c57a51497a6cb56f397289e52ff41b".to_owned(), - User { - user_id: MetuId::new("friend_1".to_owned(), "not_used".to_owned()), - public_key: "not_used".to_owned(), - balance: 70, - is_bot: true, - }, - ); - - users.insert( - "a1a38b5bae5866d7d998a9834229ec2f9db7a4fc8fb6f58b1115a96a446875ff".to_owned(), - User { - user_id: MetuId::new("friend_2".to_owned(), "not_used".to_owned()), - public_key: "not_used".to_owned(), - balance: 20, - is_bot: true, - }, - ); - - users.insert( - "4e048fd2a62f1307866086e803e9be43f78a702d5df10831fbf434e7663ae0e7".to_owned(), - User { - user_id: MetuId::new("friend_4".to_owned(), "not_used".to_owned()), - public_key: "not_used".to_owned(), - balance: 120, - is_bot: true, - }, - ); - - users.insert( - "60e77101e76950a9b1830fa107fd2f8fc545255b3e0f14b6a7797cf9ee005f07".to_owned(), - User { - user_id: MetuId::new("friend_4".to_owned(), "not_used".to_owned()), - public_key: "not_used".to_owned(), - balance: 40, - is_bot: true, - }, - ); - users +/// Build bots from the given set of bot configurations. +fn get_bots(bot_configs: &HashMap) -> HashMap { + let mut index = 0; + + bot_configs.iter() + .map(|(fingerprint, config)| { + index += 1; + (fingerprint.to_string(), User { + user_id: MetuId::new(format!("friend_{}", index), "not_used".to_owned()), + public_key: "not_used".to_owned(), + balance: config.starting_balance, + is_bot: true, + }) + }) + .collect() } fn read_approved_users(filename: &str) -> Vec { -- cgit v1.2.3-70-g09d2