main.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. use std::io;
  2. use std::io::Read;
  3. use rand::Rng;
  4. use std::{thread, time};
  5. //////////
  6. // Objects
  7. //////////
  8. struct Market {
  9. // Store current values of drugs on the market
  10. weed: u32,
  11. cocaine: u32,
  12. heroin: u32,
  13. acid: u32,
  14. xtc: u32,
  15. ludes: u32,
  16. }
  17. impl Market {
  18. pub fn new(weed: u32, cocaine: u32, heroin: u32, acid: u32, xtc: u32, ludes: u32) -> Self {
  19. Market { weed, cocaine, heroin, acid, xtc, ludes }
  20. }
  21. // This is normal market fluctuation when moving locations, as opposed to an event that drives prices up/down
  22. pub fn change_prices(&mut self) {
  23. let mut rng = rand::thread_rng();
  24. let weed_diff = rng.gen_range(90,600);
  25. let cocaine_diff = rng.gen_range(19000,40000);
  26. let heroin_diff = rng.gen_range(7000,20000);
  27. let acid_diff = rng.gen_range(300,1500);
  28. let xtc_diff = rng.gen_range(50,500);
  29. let ludes_diff = rng.gen_range(15,190);
  30. self.weed = weed_diff;
  31. self.cocaine = cocaine_diff;
  32. self.heroin = heroin_diff;
  33. self.acid = acid_diff;
  34. self.xtc = xtc_diff;
  35. self.ludes = ludes_diff;
  36. }
  37. pub fn dump(&self) {
  38. println!("Prices\nWeed: ${}\tCocaine: ${}\nHeroin: ${}\tAcid: ${}\nXTC: ${}\tLudes: ${}\n", self.weed, self.cocaine, self.heroin, self.acid, self.xtc, self.ludes);
  39. }
  40. }
  41. struct Player {
  42. health: u32,
  43. money: u32,
  44. debt: u32,
  45. weed: u32,
  46. cocaine: u32,
  47. heroin: u32,
  48. acid: u32,
  49. xtc: u32,
  50. ludes: u32,
  51. location: String,
  52. loan_timer: u32,
  53. stash_size: u32,
  54. }
  55. impl Player {
  56. pub fn new(health: u32, money: u32, debt: u32, weed: u32, cocaine: u32, heroin: u32, acid: u32, xtc: u32, ludes: u32, location: String, loan_timer: u32, stash_size: u32 ) -> Self {
  57. Player { health, money, debt, weed, cocaine, heroin, acid, xtc, ludes, location, loan_timer, stash_size }
  58. }
  59. pub fn dump(&self) {
  60. println!("Health: {}\nMoney: ${}\tDebt: ${}", self.health, self.money, self.debt);
  61. println!("You're in {}\n", self.location);
  62. println!("You can hold {} units in your stash",self.stash_size);
  63. println!("You're holding:\nWeed: {} Cocaine: {}\nHeroin: {} Acid: {}\nXTC: {} Ludes: {}\n", self.weed, self.cocaine, self.heroin, self.acid, self.xtc, self.ludes);
  64. }
  65. pub fn change_location(&mut self, new_location: String) {
  66. self.location = new_location;
  67. }
  68. pub fn get_stash_amount(&self, drug: &String) -> u32 {
  69. let mut amount = 0;
  70. match drug.as_ref() {
  71. "weed" => amount = self.weed,
  72. "cocaine" => amount = self.cocaine,
  73. "heroin" => amount = self.heroin,
  74. "acid" => amount = self.acid,
  75. "xtc" => amount = self.xtc,
  76. "ludes" => amount = self.ludes,
  77. _ => amount = 0,
  78. }
  79. return amount;
  80. }
  81. pub fn buy_transaction(&mut self, drug_to_buy: &String, amount_to_buy: &u32, market_price: &u32) {
  82. let cost = market_price * amount_to_buy;
  83. self.money -= cost;
  84. let add_amount = amount_to_buy;
  85. match drug_to_buy.as_ref() {
  86. "weed" => self.weed += add_amount,
  87. "cocaine" => self.cocaine += add_amount,
  88. "heroin" => self.heroin += add_amount,
  89. "acid" => self.acid += add_amount,
  90. "xtc" => self.xtc += add_amount,
  91. "ludes" => self.ludes += add_amount,
  92. _ => panic!("buy_transaction got a drug_to_buy value that doesn't make sense"),
  93. }
  94. }
  95. pub fn sell_transaction(&mut self, drug_to_sell: &String, amount_to_sell: &u32, market_price: &u32) {
  96. let revenue = market_price * amount_to_sell;
  97. match drug_to_sell.as_ref() {
  98. "weed" => self.weed -= amount_to_sell,
  99. "cocaine" => self.cocaine -= amount_to_sell,
  100. "heroin" => self.heroin -= amount_to_sell,
  101. "acid" => self.acid -= amount_to_sell,
  102. "xtc" => self.xtc -= amount_to_sell,
  103. "ludes" => self.ludes -= amount_to_sell,
  104. _ => panic!("sell_transaction got a drug_to_sell value that doesn't make sense"),
  105. }
  106. self.money += revenue;
  107. }
  108. pub fn debt_interest(&mut self) {
  109. let mut interest_amount: u32;
  110. match self.debt {
  111. 0 => interest_amount = 0,
  112. _ => interest_amount = ( self.debt as f32 * 0.2 ) as u32,
  113. }
  114. self.debt += interest_amount;
  115. }
  116. pub fn remove_debt(&mut self, payback_amount: u32) {
  117. if (self.debt as i32 - payback_amount as i32) < 0 {
  118. self.money -= payback_amount;
  119. self.debt = 0;
  120. } else {
  121. self.money -= payback_amount;
  122. self.debt -= payback_amount;
  123. }
  124. }
  125. pub fn borrow_from_shark(&mut self, borrow_amount: u32) {
  126. self.money += borrow_amount;
  127. self.debt += borrow_amount;
  128. }
  129. pub fn take_damage(&mut self, damage_amount: u32) {
  130. if (self.health as i32 - damage_amount as i32) < 0 {
  131. self.health = 0;
  132. } else {
  133. self.health -= damage_amount;
  134. }
  135. }
  136. pub fn take_heal(&mut self, heal_amount: u32) {
  137. self.health += heal_amount;
  138. }
  139. pub fn decrease_loan_timer(&mut self, timer_amount: u32) {
  140. if (self.loan_timer as i32 - timer_amount as i32) < 0 {
  141. self.loan_timer = 0;
  142. } else {
  143. self.loan_timer -= timer_amount;
  144. }
  145. }
  146. pub fn increase_loan_timer(&mut self, timer_amount: u32) {
  147. self.loan_timer += timer_amount;
  148. }
  149. pub fn buy_trenchcoat(&mut self, cost: u32) {
  150. self.stash_size += 25;
  151. self.money -= cost;
  152. }
  153. pub fn buy_body_armor(&mut self, cost: u32) {
  154. self.health += 2;
  155. self.money -= cost;
  156. }
  157. pub fn stash_fill(&self) -> u32 {
  158. let stash_fill = self.weed + self.cocaine + self.heroin + self.xtc + self.acid + self.ludes;
  159. return stash_fill;
  160. }
  161. }
  162. ////////////////////////////////////////////////////////
  163. // "Low-level" Fuctions, called by "Top-level" functions
  164. ////////////////////////////////////////////////////////
  165. fn prompt(prompt_text: String) -> io::Result<String> {
  166. println!("$ {}",prompt_text);
  167. let mut input = String::new();
  168. std::io::stdin().read_line(&mut input);
  169. let value: String = input.trim().parse().unwrap();
  170. Ok(value)
  171. }
  172. // This hurts portability but works for now
  173. fn clear_term() {
  174. print!("{}[2J", 27 as char);
  175. }
  176. fn get_drug_market_value(drug_to_buy: &String, market: &Market) -> io::Result<u32> {
  177. let market_value: u32;
  178. match drug_to_buy.as_ref() {
  179. "weed" => market_value = market.weed,
  180. "cocaine" => market_value = market.cocaine,
  181. "heroin" => market_value = market.heroin,
  182. "acid" => market_value = market.acid,
  183. "xtc" => market_value = market.xtc,
  184. "ludes" => market_value = market.ludes,
  185. _ => panic!("get_drug_market_value got a drug_to_buy value that doesn't make sense!"),
  186. }
  187. Ok(market_value)
  188. }
  189. // This is maybe stupid? Or could be more idomatic ?? TODO?
  190. // May should use match inside of let ?
  191. fn get_drug_as_string(drug: &String) -> io::Result<String> {
  192. let mut drug_as_string: String = String::new();
  193. match drug.as_ref() {
  194. "weed" => drug_as_string = "weed".to_string(),
  195. "cocaine" => drug_as_string = "cocaine".to_string(),
  196. "heroin" => drug_as_string = "heroin".to_string(),
  197. "acid" => drug_as_string = "acid".to_string(),
  198. "xtc" => drug_as_string = "xtc".to_string(),
  199. "ludes" => drug_as_string = "ludes".to_string(),
  200. _ => drug_as_string = "null".to_string(),
  201. }
  202. Ok(drug_as_string)
  203. }
  204. ////////////////////////////////////
  205. // Events -- happen randomly in main
  206. ////////////////////////////////////
  207. fn trenchcoat_event(player: &mut Player) {
  208. let mut relative_cost = ( player.money as f64 * 0.25 ) as u32;
  209. if relative_cost < 200 {
  210. relative_cost = 200;
  211. }
  212. println!("A guy is selling his trench count, it'll allow you to carry 25 more units, want to buy it for ${} ?", relative_cost);
  213. println!("You have ${}", player.money);
  214. let response = prompt("Yes/no?".to_string()).unwrap();
  215. if response.contains("yes") {
  216. if player.money < relative_cost {
  217. println!("You cant afford this!");
  218. } else {
  219. player.buy_trenchcoat(relative_cost);
  220. }
  221. }
  222. }
  223. fn body_armor_event(player: &mut Player) {
  224. let mut relative_cost = ( player.money as f64 * 0.3 ) as u32;
  225. if relative_cost < 300 {
  226. relative_cost = 300;
  227. }
  228. println!("A guy is selling some body armor, it'll increase your health by 2, want to buy it for ${} ?", relative_cost);
  229. println!("You have ${}", player.money);
  230. let response = prompt("Yes/no?".to_string()).unwrap();
  231. if response.contains("yes") {
  232. if player.money < relative_cost {
  233. println!("You cant afford this!");
  234. } else {
  235. player.buy_body_armor(relative_cost);
  236. }
  237. }
  238. }
  239. fn cops_event(player: &mut Player) {
  240. let one_second = time::Duration::from_secs(1);
  241. println!("The cops found you! Run!");
  242. thread::sleep(one_second);
  243. let mut rng = rand::thread_rng();
  244. let mut escape: u32;
  245. let mut hit: u32;
  246. let mut done = false;
  247. while ! done {
  248. escape = rng.gen_range(0,5);
  249. hit = rng.gen_range(0,10);
  250. if escape == 0 {
  251. println!("You got away!");
  252. thread::sleep(one_second);
  253. done = true;
  254. } else {
  255. println!("You cant get away, the cops are firing!");
  256. thread::sleep(one_second);
  257. if hit == 0 {
  258. println!("You're hit for 2 damage!");
  259. player.take_damage(2);
  260. thread::sleep(one_second);
  261. } else {
  262. println!("They miss and you keep running!");
  263. thread::sleep(one_second);
  264. }
  265. }
  266. }
  267. }
  268. ////////////////////////////////////////
  269. // "Top-level" Functions, called by main
  270. ////////////////////////////////////////
  271. fn go_to_location(player: &mut Player, market: &mut Market) {
  272. let mut loc_loop = false;
  273. while ! loc_loop {
  274. let mut new_location = prompt("Select a new location, locations are\nBrooklyn Midtown Harlem CentralPark".to_string()).unwrap();
  275. match new_location.as_ref() {
  276. "Brooklyn" => println!("Going to Brooklyn"),
  277. "Midtown" => println!("Going to Midtown"),
  278. "Harlem" => println!("Going to Harlem"),
  279. "CentralPark" => println!("Going to CentralPark"),
  280. _ => new_location = "null".to_string(),
  281. }
  282. if new_location != "null".to_string() {
  283. if new_location == player.location {
  284. println!("Youre already in {}", player.location);
  285. } else {
  286. player.change_location(new_location);
  287. market.change_prices();
  288. loc_loop = true;
  289. }
  290. } else {
  291. println!("Not a valid location, try again");
  292. }
  293. }
  294. }
  295. fn buy_drugs(player: &mut Player, market: &mut Market) {
  296. let mut done = false;
  297. let mut drug_to_buy: String = String::new();
  298. while ! done {
  299. drug_to_buy = prompt("What drug will you buy?".to_string()).unwrap();
  300. drug_to_buy = get_drug_as_string(&drug_to_buy).unwrap();
  301. if drug_to_buy != "null".to_string() {
  302. done = true;
  303. } else {
  304. println!("Try again");
  305. }
  306. }
  307. let mut done1 = false;
  308. while ! done1 {
  309. let drug_market_value: u32 = get_drug_market_value(&drug_to_buy, &market).unwrap();
  310. let affordable_amount: u32 = (player.money / drug_market_value) as u32;
  311. println!("You can afford {} {}", affordable_amount, drug_to_buy);
  312. let amount_to_buy = prompt("How many will you buy?".to_string()).unwrap();
  313. let amount_to_buy_int: u32 = amount_to_buy.parse().unwrap();
  314. let current_stash_fill = player.stash_fill();
  315. if amount_to_buy_int > affordable_amount {
  316. println!("You cant afford that many");
  317. } else if amount_to_buy_int > player.stash_size {
  318. println!("You cant hold that much!");
  319. } else if (amount_to_buy_int + current_stash_fill) > player.stash_size {
  320. println!("You cant hold that much!");
  321. } else {
  322. println!("Buying {} {}", amount_to_buy_int, drug_to_buy);
  323. player.buy_transaction(&drug_to_buy,&amount_to_buy_int,&drug_market_value);
  324. done1 = true;
  325. }
  326. }
  327. }
  328. fn sell_drugs(player: &mut Player, market: &mut Market) {
  329. let mut done = false;
  330. let mut drug_to_sell: String = String::new();
  331. while ! done {
  332. drug_to_sell = prompt("What drug will you sell?".to_string()).unwrap();
  333. drug_to_sell = get_drug_as_string(&drug_to_sell).unwrap();
  334. if drug_to_sell != "null".to_string() {
  335. done = true;
  336. } else {
  337. println!("Try again");
  338. }
  339. }
  340. let mut done1 = false;
  341. let mut amount_to_sell: String = String::new();
  342. while ! done1 {
  343. amount_to_sell = prompt("How many to sell?".to_string()).unwrap();
  344. let amount_to_sell_int: u32 = amount_to_sell.parse().unwrap();
  345. let drug_market_value: u32 = get_drug_market_value(&drug_to_sell, &market).unwrap();
  346. let amount_possible_to_sell: u32 = player.get_stash_amount(&drug_to_sell);
  347. if amount_to_sell_int > amount_possible_to_sell {
  348. println!("You don't have that many {}, try again", drug_to_sell);
  349. } else {
  350. println!("Selling {} {}", amount_to_sell_int, drug_to_sell);
  351. player.sell_transaction(&drug_to_sell,&amount_to_sell_int,&drug_market_value);
  352. done1 = true;
  353. }
  354. }
  355. }
  356. fn loan_shark(player: &mut Player) {
  357. if player.location != "Brooklyn".to_string() {
  358. println!("Can only talk to the loanshark in Brooklyn");
  359. } else {
  360. let mut loan_done = false;
  361. while ! loan_done {
  362. let action = prompt("What do you want to do? (borrow,repay)".to_string()).unwrap();
  363. if action.contains("repay") {
  364. let payback_amount: u32 = prompt("How much to pay back?".to_string()).unwrap().parse().unwrap();
  365. if payback_amount > player.money {
  366. println!("You dont have that much money");
  367. } else {
  368. player.remove_debt(payback_amount);
  369. println!("You now owe the loanshark: ${}", player.debt);
  370. loan_done = true;
  371. }
  372. } else if action.contains("borrow") {
  373. let borrow_amount = prompt("How much do you want to borrow? (Limit 20000)".to_string()).unwrap().parse().unwrap();
  374. if borrow_amount > 20000 {
  375. println!("You cant borrow that much");
  376. } else {
  377. player.borrow_from_shark(borrow_amount);
  378. loan_done = true;
  379. }
  380. }
  381. }
  382. }
  383. }
  384. ///////
  385. // Main
  386. ///////
  387. fn main() {
  388. let mut player = Player::new(10,2000,2050,0,0,0,0,0,0,"Brooklyn".to_string(),10,50);
  389. let mut market = Market::new(0,0,0,0,0,0);
  390. Market::change_prices(&mut market);
  391. // let mut objects = [player,market];
  392. let mut done = false;
  393. while ! done {
  394. let input = prompt("Type start, quit, or help".to_string()).unwrap();
  395. if input == "quit".to_string() {
  396. done = true;
  397. } else if input == "start".to_string() {
  398. println!("Starting the game");
  399. let mut main_loop = false;
  400. while ! main_loop {
  401. clear_term();
  402. let mut rng = rand::thread_rng();
  403. let event_chance = rng.gen_range(0,9);
  404. println!("Event chance is: {}",event_chance);
  405. if event_chance == 2 {
  406. trenchcoat_event(&mut player);
  407. }
  408. if event_chance == 1 {
  409. body_armor_event(&mut player);
  410. }
  411. if event_chance == 0 {
  412. cops_event(&mut player);
  413. }
  414. if player.health == 0 {
  415. println!("You died! Game over!");
  416. main_loop = true;
  417. }
  418. if player.debt != 0 {
  419. if player.loan_timer == 0 {
  420. println!("** The loan shark wants his money! He beats you to a pulp to remind you! **");
  421. player.take_damage(2);
  422. player.increase_loan_timer(5);
  423. println!("** You take 2 damage! **");
  424. }
  425. }
  426. player.dump();
  427. market.dump();
  428. if player.location == "Brooklyn".to_string() {
  429. println!("You're currently in {}, what will you do? (buy,sell,jet,loanshark)",player.location);
  430. } else {
  431. println!("You're currently in {}, what will you do? (buy,sell,jet)",player.location);
  432. }
  433. let action = prompt("".to_string()).unwrap();
  434. if action.contains("buy") {
  435. buy_drugs(&mut player, &mut market);
  436. } else if action.contains("sell") {
  437. sell_drugs(&mut player, &mut market);
  438. } else if action.contains("jet") {
  439. go_to_location(&mut player, &mut market);
  440. player.debt_interest();
  441. player.decrease_loan_timer(1);
  442. } else if action.contains("loanshark") {
  443. loan_shark(&mut player);
  444. } else if action == "quit".to_string() {
  445. std::process::exit(0);
  446. }
  447. }
  448. }
  449. }
  450. }