Updated main.rs adding a fix for multithreading
I added a fix for multithreading {I am actively in pain send help aaaaaaaaaaaa} Once activated autoclicker now doesn't kill the program instead making a background thread that adds X amount to the current players score. The thread Is currently not killed upon program close and you should probably not run It, since It leaves a dangling thread. Cheers ~ Dan
This commit is contained in:
parent
7a94b86b08
commit
4adc7e1651
1 changed files with 32 additions and 29 deletions
59
src/main.rs
59
src/main.rs
|
@ -1,6 +1,5 @@
|
||||||
use eframe::egui;
|
use eframe::egui;
|
||||||
//use rand::Rng;
|
use std::{sync::{Arc, Mutex}, thread, time::Duration};
|
||||||
use std::{thread, time::Duration};
|
|
||||||
|
|
||||||
fn main() -> Result<(), eframe::Error> {
|
fn main() -> Result<(), eframe::Error> {
|
||||||
let options = eframe::NativeOptions::default();
|
let options = eframe::NativeOptions::default();
|
||||||
|
@ -12,20 +11,22 @@ fn main() -> Result<(), eframe::Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MyApp {
|
struct MyApp {
|
||||||
score: i32,
|
score: Arc<Mutex<i32>>,
|
||||||
x: i32,
|
x: i32,
|
||||||
upgd_cost: i32,
|
upgd_cost: i32,
|
||||||
proc: i32,
|
proc: i32,
|
||||||
constt: i32,
|
autoclicker_running: Arc<Mutex<bool>>,
|
||||||
// rand_number: i32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for MyApp {
|
impl Default for MyApp {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
//let mut rng = rand::thread_rng();
|
Self {
|
||||||
//let rand_number = rng.gen_range(1..=50);
|
score: Arc::new(Mutex::new(0)),
|
||||||
Self { score: 0, x: 1, upgd_cost: 10, proc: 0, constt: 0}
|
x: 1,
|
||||||
|
upgd_cost: 10,
|
||||||
|
proc: 0,
|
||||||
|
autoclicker_running: Arc::new(Mutex::new(false)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,39 +42,41 @@ impl eframe::App for MyApp {
|
||||||
ctx.set_style(style);
|
ctx.set_style(style);
|
||||||
|
|
||||||
// Display the score
|
// Display the score
|
||||||
ui.heading(format!("Score: {}", self.score));
|
ui.heading(format!("Score: {}", *self.score.lock().unwrap()));
|
||||||
ui.heading(format!("Upgrade cost: {}", self.upgd_cost));
|
ui.heading(format!("Upgrade cost: {}", self.upgd_cost));
|
||||||
ui.heading(format!("Autoclicker cost: {}", self.upgd_cost));
|
ui.heading(format!("Autoclicker cost: {}", self.upgd_cost));
|
||||||
|
|
||||||
// Buttons
|
// Buttons
|
||||||
if ui.button("Click").clicked() {
|
if ui.button("Click").clicked() {
|
||||||
self.score += self.x;
|
*self.score.lock().unwrap() += self.x;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ui.button("Upgrade").clicked() {
|
if ui.button("Upgrade").clicked() {
|
||||||
if self.score >= self.upgd_cost {
|
let mut score = self.score.lock().unwrap();
|
||||||
self.score = self.score - self.upgd_cost;
|
if *score >= self.upgd_cost {
|
||||||
self.x = self.x + 1;
|
*score -= self.upgd_cost;
|
||||||
self.proc = self.score * 40/100 + self.upgd_cost * 10/100;
|
self.x += 1;
|
||||||
self.upgd_cost = self.upgd_cost + self.proc;
|
self.proc = *score * 40 / 100 + self.upgd_cost * 10 / 100;
|
||||||
|
self.upgd_cost += self.proc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ui.button("Autoclick").clicked() {
|
if ui.button("Autoclick").clicked() {
|
||||||
if self.score >= self.upgd_cost {
|
let mut autoclicker_running = self.autoclicker_running.lock().unwrap();
|
||||||
while self.constt == self.constt {
|
if *self.score.lock().unwrap() >= self.upgd_cost && !*autoclicker_running {
|
||||||
thread::sleep(Duration::from_millis(4000));
|
*autoclicker_running = true;
|
||||||
self.score = self.score + self.x
|
let score_arc = Arc::clone(&self.score);
|
||||||
|
let x = self.x;
|
||||||
|
thread::spawn(move || {
|
||||||
|
loop {
|
||||||
|
{
|
||||||
|
let mut score = score_arc.lock().unwrap();
|
||||||
|
*score += x;
|
||||||
}
|
}
|
||||||
|
thread::sleep(Duration::from_secs(4));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue