main.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. pub fn holding(&self) -> bool {
  162. if self.stash_fill() == 0 {
  163. return false;
  164. } else {
  165. return true;
  166. }
  167. }
  168. }
  169. ////////////////////////////////////////////////////////
  170. // "Low-level" Fuctions, called by "Top-level" functions
  171. ////////////////////////////////////////////////////////
  172. fn prompt(prompt_text: String) -> io::Result<String> {
  173. println!("$ {}",prompt_text);
  174. let mut input = String::new();
  175. std::io::stdin().read_line(&mut input);
  176. let value: String = input.trim().parse().unwrap();
  177. Ok(value)
  178. }
  179. // This hurts portability but works for now
  180. fn clear_term() {
  181. print!("{}[2J", 27 as char);
  182. }
  183. fn get_drug_market_value(drug_to_buy: &String, market: &Market) -> io::Result<u32> {
  184. let market_value: u32;
  185. match drug_to_buy.as_ref() {
  186. "weed" => market_value = market.weed,
  187. "cocaine" => market_value = market.cocaine,
  188. "heroin" => market_value = market.heroin,
  189. "acid" => market_value = market.acid,
  190. "xtc" => market_value = market.xtc,
  191. "ludes" => market_value = market.ludes,
  192. _ => panic!("get_drug_market_value got a drug_to_buy value that doesn't make sense!"),
  193. }
  194. Ok(market_value)
  195. }
  196. // This is maybe stupid? Or could be more idomatic ?? TODO?
  197. // May should use match inside of let ?
  198. fn get_drug_as_string(drug: &String) -> io::Result<String> {
  199. let mut drug_as_string: String = String::new();
  200. match drug.as_ref() {
  201. "weed" => drug_as_string = "weed".to_string(),
  202. "cocaine" => drug_as_string = "cocaine".to_string(),
  203. "heroin" => drug_as_string = "heroin".to_string(),
  204. "acid" => drug_as_string = "acid".to_string(),
  205. "xtc" => drug_as_string = "xtc".to_string(),
  206. "ludes" => drug_as_string = "ludes".to_string(),
  207. _ => drug_as_string = "null".to_string(),
  208. }
  209. Ok(drug_as_string)
  210. }
  211. ////////////////////////////////////
  212. // Events -- happen randomly in main
  213. ////////////////////////////////////
  214. fn trenchcoat_event(player: &mut Player) {
  215. let mut relative_cost = ( player.money as f64 * 0.25 ) as u32;
  216. if relative_cost < 200 {
  217. relative_cost = 200;
  218. }
  219. println!("A guy is selling his trench count, it'll allow you to carry 25 more units, want to buy it for ${} ?", relative_cost);
  220. println!("You have ${}", player.money);
  221. let response = prompt("Yes/no?".to_string()).unwrap();
  222. if response.contains("yes") {
  223. if player.money < relative_cost {
  224. println!("You cant afford this!");
  225. } else {
  226. player.buy_trenchcoat(relative_cost);
  227. }
  228. }
  229. }
  230. fn body_armor_event(player: &mut Player) {
  231. let mut relative_cost = ( player.money as f64 * 0.3 ) as u32;
  232. if relative_cost < 300 {
  233. relative_cost = 300;
  234. }
  235. println!("A guy is selling some body armor, it'll increase your health by 2, want to buy it for ${} ?", relative_cost);
  236. println!("You have ${}", player.money);
  237. let response = prompt("Yes/no?".to_string()).unwrap();
  238. if response.contains("yes") {
  239. if player.money < relative_cost {
  240. println!("You cant afford this!");
  241. } else {
  242. player.buy_body_armor(relative_cost);
  243. }
  244. }
  245. }
  246. fn cops_event(player: &mut Player) {
  247. let one_second = time::Duration::from_secs(1);
  248. if ! player.holding() {
  249. println!("The cops found you, but you're not holding so it's chill.");
  250. thread::sleep(one_second);
  251. thread::sleep(one_second);
  252. thread::sleep(one_second);
  253. } else {
  254. println!("The cops found you! Run!");
  255. thread::sleep(one_second);
  256. let mut rng = rand::thread_rng();
  257. let mut escape: u32;
  258. let mut hit: u32;
  259. let mut done = false;
  260. while ! done {
  261. escape = rng.gen_range(0,5);
  262. hit = rng.gen_range(0,10);
  263. if escape == 0 {
  264. println!("You got away!");
  265. thread::sleep(one_second);
  266. done = true;
  267. } else {
  268. println!("You cant get away, the cops are firing!");
  269. thread::sleep(one_second);
  270. if hit == 0 {
  271. println!("You're hit for 2 damage!");
  272. player.take_damage(4);
  273. thread::sleep(one_second);
  274. } else {
  275. println!("They miss and you keep running!");
  276. thread::sleep(one_second);
  277. }
  278. }
  279. }
  280. }
  281. }
  282. ////////////////////////////////////////
  283. // "Top-level" Functions, called by main
  284. ////////////////////////////////////////
  285. fn go_to_location(player: &mut Player, market: &mut Market) {
  286. let mut loc_loop = false;
  287. while ! loc_loop {
  288. let mut new_location = prompt("Select a new location, locations are\nBrooklyn Midtown Harlem CentralPark".to_string()).unwrap();
  289. match new_location.as_ref() {
  290. "Brooklyn" => println!("Going to Brooklyn"),
  291. "Midtown" => println!("Going to Midtown"),
  292. "Harlem" => println!("Going to Harlem"),
  293. "CentralPark" => println!("Going to CentralPark"),
  294. _ => new_location = "null".to_string(),
  295. }
  296. if new_location != "null".to_string() {
  297. if new_location == player.location {
  298. println!("Youre already in {}", player.location);
  299. } else {
  300. player.change_location(new_location);
  301. market.change_prices();
  302. loc_loop = true;
  303. }
  304. } else {
  305. println!("Not a valid location, try again");
  306. }
  307. }
  308. }
  309. fn buy_drugs(player: &mut Player, market: &mut Market) {
  310. let mut done = false;
  311. let mut drug_to_buy: String = String::new();
  312. while ! done {
  313. drug_to_buy = prompt("What drug will you buy?".to_string()).unwrap();
  314. drug_to_buy = get_drug_as_string(&drug_to_buy).unwrap();
  315. if drug_to_buy != "null".to_string() {
  316. done = true;
  317. } else {
  318. println!("Try again");
  319. }
  320. }
  321. let mut done1 = false;
  322. while ! done1 {
  323. let drug_market_value: u32 = get_drug_market_value(&drug_to_buy, &market).unwrap();
  324. let affordable_amount: u32 = (player.money / drug_market_value) as u32;
  325. println!("You can afford {} {}", affordable_amount, drug_to_buy);
  326. let amount_to_buy = prompt("How many will you buy?".to_string()).unwrap();
  327. let amount_to_buy_int: u32 = amount_to_buy.parse().unwrap();
  328. let current_stash_fill = player.stash_fill();
  329. if amount_to_buy_int > affordable_amount {
  330. println!("You cant afford that many");
  331. } else if amount_to_buy_int > player.stash_size {
  332. println!("You cant hold that much!");
  333. } else if (amount_to_buy_int + current_stash_fill) > player.stash_size {
  334. println!("You cant hold that much!");
  335. } else {
  336. println!("Buying {} {}", amount_to_buy_int, drug_to_buy);
  337. player.buy_transaction(&drug_to_buy,&amount_to_buy_int,&drug_market_value);
  338. done1 = true;
  339. }
  340. }
  341. }
  342. fn sell_drugs(player: &mut Player, market: &mut Market) {
  343. let mut done = false;
  344. let mut drug_to_sell: String = String::new();
  345. while ! done {
  346. drug_to_sell = prompt("What drug will you sell?".to_string()).unwrap();
  347. drug_to_sell = get_drug_as_string(&drug_to_sell).unwrap();
  348. if drug_to_sell != "null".to_string() {
  349. done = true;
  350. } else {
  351. println!("Try again");
  352. }
  353. }
  354. let mut done1 = false;
  355. let mut amount_to_sell: String = String::new();
  356. while ! done1 {
  357. amount_to_sell = prompt("How many to sell?".to_string()).unwrap();
  358. let amount_to_sell_int: u32 = amount_to_sell.parse().unwrap();
  359. let drug_market_value: u32 = get_drug_market_value(&drug_to_sell, &market).unwrap();
  360. let amount_possible_to_sell: u32 = player.get_stash_amount(&drug_to_sell);
  361. if amount_to_sell_int > amount_possible_to_sell {
  362. println!("You don't have that many {}, try again", drug_to_sell);
  363. } else {
  364. println!("Selling {} {}", amount_to_sell_int, drug_to_sell);
  365. player.sell_transaction(&drug_to_sell,&amount_to_sell_int,&drug_market_value);
  366. done1 = true;
  367. }
  368. }
  369. }
  370. fn loan_shark(player: &mut Player) {
  371. if player.location != "Brooklyn".to_string() {
  372. println!("Can only talk to the loanshark in Brooklyn");
  373. } else {
  374. let mut loan_done = false;
  375. while ! loan_done {
  376. let action = prompt("What do you want to do? (borrow,repay)".to_string()).unwrap();
  377. if action.contains("repay") {
  378. let payback_amount: u32 = prompt("How much to pay back?".to_string()).unwrap().parse().unwrap();
  379. if payback_amount > player.money {
  380. println!("You dont have that much money");
  381. } else {
  382. player.remove_debt(payback_amount);
  383. println!("You now owe the loanshark: ${}", player.debt);
  384. loan_done = true;
  385. }
  386. } else if action.contains("borrow") {
  387. let borrow_amount = prompt("How much do you want to borrow? (Limit 20000)".to_string()).unwrap().parse().unwrap();
  388. if borrow_amount > 20000 {
  389. println!("You cant borrow that much");
  390. } else {
  391. player.borrow_from_shark(borrow_amount);
  392. loan_done = true;
  393. }
  394. }
  395. }
  396. }
  397. }
  398. ///////
  399. // Main
  400. ///////
  401. fn main() {
  402. let mut player = Player::new(10,2000,2050,0,0,0,0,0,0,"Brooklyn".to_string(),10,50);
  403. let mut market = Market::new(0,0,0,0,0,0);
  404. Market::change_prices(&mut market);
  405. // let mut objects = [player,market];
  406. let mut done = false;
  407. while ! done {
  408. let input = prompt("Type start, quit, or help".to_string()).unwrap();
  409. if input == "quit".to_string() {
  410. done = true;
  411. } else if input == "start".to_string() {
  412. println!("Starting the game");
  413. let mut main_loop = false;
  414. while ! main_loop {
  415. clear_term();
  416. let mut rng = rand::thread_rng();
  417. let event_chance = rng.gen_range(0,9);
  418. if event_chance == 2 {
  419. trenchcoat_event(&mut player);
  420. }
  421. if event_chance == 1 {
  422. body_armor_event(&mut player);
  423. }
  424. if event_chance == 0 {
  425. cops_event(&mut player);
  426. }
  427. if player.health == 0 {
  428. println!("You died! Game over!");
  429. main_loop = true;
  430. }
  431. if player.debt != 0 {
  432. if player.loan_timer == 0 {
  433. println!("** The loan shark wants his money! He beats you to a pulp to remind you! **");
  434. player.take_damage(2);
  435. player.increase_loan_timer(5);
  436. println!("** You take 2 damage! **");
  437. }
  438. }
  439. player.dump();
  440. market.dump();
  441. if player.location == "Brooklyn".to_string() {
  442. println!("You're currently in {}, what will you do? (buy,sell,jet,loanshark)",player.location);
  443. } else {
  444. println!("You're currently in {}, what will you do? (buy,sell,jet)",player.location);
  445. }
  446. let action = prompt("".to_string()).unwrap();
  447. if action.contains("buy") {
  448. buy_drugs(&mut player, &mut market);
  449. } else if action.contains("sell") {
  450. sell_drugs(&mut player, &mut market);
  451. } else if action.contains("jet") {
  452. go_to_location(&mut player, &mut market);
  453. player.debt_interest();
  454. player.decrease_loan_timer(1);
  455. } else if action.contains("loanshark") {
  456. loan_shark(&mut player);
  457. } else if action == "quit".to_string() {
  458. std::process::exit(0);
  459. }
  460. }
  461. }
  462. }
  463. }