Skip to content

Commit 2071f3e

Browse files
authored
Updated Gessing Game to 3.0
3.0 Release Merge
2 parents 6f32a01 + 7272e99 commit 2071f3e

File tree

7 files changed

+79
-145
lines changed

7 files changed

+79
-145
lines changed

Cargo.lock

+9-132
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "gessing_game"
3-
version = "2.1.0"
3+
version = "3.0.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -11,5 +11,5 @@ opt-level = 0
1111
opt-level = 3
1212

1313
[dependencies]
14-
rand = "0.9.0-beta.1"
14+
rand = "0.9.0"
1515
base64 = "0.22.1"

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
The goal of the game is to open one of the 2 doors that does not kill you.
33

44
# Test Build
5-
In the test build I always have the most up to date code that maybe not even work.
5+
In the test build I always have the most up-to-date code that maybe not even work.
66

77
# Goals
88
1. [x] Make Code better
99
2. [ ] storing
1010
3. [ ] on a server
11-
4. [ ] lokal
11+
4. [ ] local
1212
5. [ ] Ui with [bevy](https://bevyengine.org/)

src/gamefiles/game.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::gamefiles::store;
22
use crate::lib::input;
3-
use rand::Rng;
43
use crate::menu::menu;
4+
use rand::Rng;
55

66
pub fn game_loop(name: String) {
77
let mut score: i8 = 0;
@@ -23,6 +23,7 @@ pub fn game_loop(name: String) {
2323
}
2424
if score > 0 {
2525
store::store(name, score);
26-
}else {
27-
menu(name);
28-
}}
26+
} else {
27+
menu(name);
28+
}
29+
}

src/gamefiles/store.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::lib::{check_for_scoreboard, decode};
1+
use crate::lib::remove_access_scores::ScoreboardState;
2+
use crate::lib::{check_for_scoreboard, decode, remove_access_scores};
23
use crate::menu;
34
use base64::prelude::*;
45
use std::fs::*;
56
use std::io::Write;
6-
77
const SCOREBOARD_PATH: &str = "src/gamefiles/scoreboard.dat";
88
pub fn store(name: String, score: i8) {
99
if !check_for_scoreboard::check_if_path_exist() {
@@ -17,8 +17,21 @@ pub fn store(name: String, score: i8) {
1717
} else {
1818
println!("Found scoreboard");
1919
}
20-
// remove_access_scores::remove_scores(name.clone(), score);
2120
let existing_scoreboard = decode::decode();
21+
let remove_score: (ScoreboardState, i8) =
22+
remove_access_scores::remove_scores(name.clone(), score);
23+
if remove_score.0 == ScoreboardState::NoAdding {
24+
menu::menu(name)
25+
} else if remove_score.0 == ScoreboardState::ReplaceAdding {
26+
let replacement_string: String = format!("\n {name} has score: {}", remove_score.1);
27+
let new_scoreboard = existing_scoreboard.replace(replacement_string.as_str(), "");
28+
println!("Replaced score");
29+
write_to_scoreboard(new_scoreboard, name, score)
30+
} else if remove_score.0 == ScoreboardState::JustAdding {
31+
write_to_scoreboard(decode::decode(), name, score)
32+
}
33+
}
34+
fn write_to_scoreboard(scoreboard: String, name: String, score: i8) {
2235
let mut file = match OpenOptions::new()
2336
.write(true)
2437
.create(true)
@@ -28,7 +41,7 @@ pub fn store(name: String, score: i8) {
2841
Ok(file) => file,
2942
Err(e) => panic!("File error. {}", e),
3043
};
31-
let add_score: String = format!("{existing_scoreboard} \n {name} has score: {score}\n");
44+
let add_score: String = format!("{scoreboard} \n {name} has score: {score}\n");
3245
println!("Adding score...");
3346
match file.write_all(BASE64_STANDARD.encode(add_score).as_bytes()) {
3447
Ok(f) => {

src/lib/remove_access_scores.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crate::lib::decode;
2+
use std::cmp::*;
3+
#[derive(PartialEq)]
4+
pub enum ScoreboardState {
5+
ReplaceAdding,
6+
NoAdding,
7+
JustAdding,
8+
}
9+
pub fn remove_scores(name: String, current_score: i8) -> (ScoreboardState, i8) {
10+
let mut scores = Vec::new();
11+
let mut lines: Vec<String> = Vec::new();
12+
let scoreboard = decode::decode();
13+
let mut number_scores: Vec<i8> = Vec::new();
14+
let string_remove = format!("{name} has score: ");
15+
let mut counter = 0;
16+
for l in scoreboard.lines() {
17+
if l.contains(&name) {
18+
lines.push(l.to_string());
19+
}
20+
}
21+
for s in lines.clone() {
22+
scores.push(s.replace(&string_remove, ""));
23+
counter += 1;
24+
}
25+
if counter < 3 {
26+
println!("Less than 3 scores of {name} found.");
27+
(ScoreboardState::JustAdding, 0)
28+
} else {
29+
for s in scores {
30+
match s.trim().parse::<i8>() {
31+
Ok(t) => number_scores.push(t),
32+
Err(e) => panic!("Scoreboard has invalid data: ({e})"),
33+
};
34+
}
35+
let smallest = number_scores.iter().min();
36+
println!("smallest is {}", *smallest.unwrap());
37+
if *smallest.unwrap() > current_score {
38+
(ScoreboardState::NoAdding, 0)
39+
} else {
40+
(ScoreboardState::ReplaceAdding, *smallest.unwrap())
41+
}
42+
}
43+
}

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod lib {
77
pub mod check_for_scoreboard;
88
pub mod decode;
99
pub mod input;
10-
// pub mod remove_access_scores;
10+
pub mod remove_access_scores;
1111
}
1212
fn main() {
1313
menu::start_menu();

0 commit comments

Comments
 (0)