Skip to content

Commit 9a0e991

Browse files
Added level downloading
It doesn't show the name or desc or anything but hahahha
1 parent d5aa780 commit 9a0e991

5 files changed

Lines changed: 199 additions & 3 deletions

File tree

src/funcs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ impl GameState {
455455
GameState::LevelComplete => return "LevelComplete".to_string(),
456456
GameState::LevelSettings => return "LevelSettings".to_string(),
457457
GameState::SearchPage => return "SearchPage".to_string(),
458+
GameState::LevelPage => return "LevelPage".to_string(),
458459
}
459460
}
460461
}

src/game/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ pub mod playing;
22
pub mod editor;
33
pub mod object_types;
44
pub mod saving;
5-
pub mod loading;
5+
pub mod loading;
6+
pub mod parsing;

src/game/parsing.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// This is the function for parsing the level download response from the servers
2+
pub fn parse_level_download_response(
3+
level_download_result: String,
4+
online_level_name: &mut String,
5+
online_level_desc: &mut String,
6+
online_level_diff: &mut u8,
7+
online_level_rated: &mut bool,
8+
online_level_creator: &mut String,
9+
online_level_data: &mut String
10+
) {
11+
let level_download_result_parts: Vec<&str> = level_download_result.split(";;;;;").collect();
12+
let level_download_result_parts_empty_user: Vec<&str> = level_download_result.split(";;;;;;").collect();
13+
let name_desc: Vec<&str> = level_download_result_parts[0].split(";").collect();
14+
15+
*online_level_name = name_desc[0].to_string();
16+
*online_level_desc = name_desc[1].to_string();
17+
*online_level_diff = name_desc[2].parse().unwrap();
18+
if name_desc.len() > 4 {
19+
*online_level_creator = name_desc[4].parse().unwrap();
20+
} else {
21+
*online_level_creator = "".to_string();
22+
}
23+
24+
if name_desc.len() > 4 {
25+
*online_level_data = level_download_result_parts[1].to_string();
26+
} else {
27+
*online_level_data = level_download_result_parts_empty_user[1].to_string();
28+
}
29+
30+
if name_desc[3] == "0" {
31+
*online_level_rated = false
32+
} else {
33+
*online_level_rated = true
34+
}
35+
}

src/main.rs

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{cell::Cell, collections::HashMap, rc::Rc};
22

3-
use game::loading::load_level;
3+
use game::{loading::load_level, parsing::parse_level_download_response};
44
use macroquad::prelude::*;
55
use miniquad::conf::Icon;
66
use image::ImageReader;
@@ -256,9 +256,46 @@ async fn main() {
256256
active: false
257257
};
258258

259+
let mut level_download_button = Button::new(
260+
|| screen_width() - 140.0,
261+
|| 75.0,
262+
|| 130.0,
263+
|| 75.0,
264+
"Download",
265+
15,
266+
false
267+
);
268+
269+
let mut level_id_textbox = TextBox {
270+
rect: Rect {
271+
x: screen_width() - 140.0,
272+
y: 10.0,
273+
w: 130.0,
274+
h: 55.0
275+
},
276+
text: "Level ID".to_string(),
277+
text_size: 18,
278+
max_length: 6,
279+
spaces_allowed: false,
280+
active: false
281+
};
282+
283+
284+
285+
let mut level_play_button = Button::new(
286+
|| screen_width() as f32 / 2.0 - 100.0,
287+
|| screen_height() as f32 / 2.0 - 50.0,
288+
|| 200.0,
289+
|| 100.0,
290+
"Play",
291+
20,
292+
false
293+
);
294+
259295
// Url's for server requests
260296
let main_url = "http://georays.puppet57.xyz/php-code/".to_string();
261297
let latest_version_url: String = format!("{}get-latest-version.php", main_url).to_string();
298+
let download_url: String = format!("{}download-level.php", main_url);
262299

263300
println!("Defining important game variables..");
264301
let game_state: Shared<GameState> = Shared::<GameState>(Rc::new(Cell::new(GameState::Menu)));
@@ -425,6 +462,18 @@ async fn main() {
425462
let mut grnd_green: String = "".to_string();
426463
let mut grnd_blue: String = "".to_string();
427464

465+
let mut level_id: String = "".to_string();
466+
467+
// Values for server responses
468+
let mut level_download_response: String = "".to_string();
469+
let mut online_level_name = "".to_string();
470+
let mut online_level_desc = "".to_string();
471+
let mut online_level_data = "".to_string();
472+
let mut online_level_diff: u8 = 0;
473+
let mut online_level_rated: bool = false;
474+
let mut online_level_creator = "".to_string();
475+
let mut show_level_not_found: bool = false;
476+
428477
println!("Loading mods...");
429478
let mod_paths_kinda = std::fs::read_dir("./mods").unwrap();
430479
let mut mod_contents: Vec<String> = vec![];
@@ -1035,10 +1084,83 @@ async fn main() {
10351084

10361085
GameState::SearchPage => {
10371086
back_button.update(delta_time);
1087+
level_download_button.update(delta_time);
10381088

10391089
if back_button.is_clicked() {
10401090
game_state.0.set(GameState::CreatorMenu);
10411091
}
1092+
1093+
if level_download_button.is_clicked() {
1094+
level_download_response = ureq::get(download_url.clone())
1095+
.query("id", &level_id)
1096+
.call()
1097+
.unwrap()
1098+
.into_body()
1099+
.read_to_string()
1100+
.unwrap();
1101+
1102+
1103+
if level_download_response.clone().contains(";;;;;") {
1104+
parse_level_download_response(
1105+
level_download_response.clone(),
1106+
&mut online_level_name,
1107+
&mut online_level_desc,
1108+
&mut online_level_diff,
1109+
&mut online_level_rated,
1110+
&mut online_level_creator,
1111+
&mut online_level_data
1112+
);
1113+
1114+
game_state.0.set(GameState::LevelPage);
1115+
} else {
1116+
show_level_not_found = true;
1117+
}
1118+
}
1119+
1120+
if level_id_textbox.is_clicked() {
1121+
level_id_textbox.active = true
1122+
}
1123+
1124+
if level_id_textbox.is_not_clicked() {
1125+
level_id_textbox.active = false
1126+
}
1127+
1128+
level_id_textbox.input(&mut level_id);
1129+
}
1130+
1131+
GameState::LevelPage => {
1132+
back_button.update(delta_time);
1133+
level_play_button.update(delta_time);
1134+
1135+
if back_button.is_clicked() {
1136+
game_state.0.set(GameState::SearchPage);
1137+
}
1138+
1139+
if level_play_button.is_clicked() {
1140+
let load_level_result = load_level(
1141+
online_level_data.clone(),
1142+
&mut obj_grid,
1143+
&mut cc_1001,
1144+
&mut cc_1002,
1145+
&mut current_song,
1146+
true,
1147+
main_levels.clone()
1148+
);
1149+
1150+
stop_audio(&sink);
1151+
play_audio_path(
1152+
&current_song,
1153+
master_volume,
1154+
false,
1155+
&sink
1156+
);
1157+
1158+
if load_level_result == "ok" {
1159+
game_state.0.set(GameState::Playing);
1160+
} else {
1161+
println!("Problem loading level: {}", load_level_result);
1162+
}
1163+
}
10421164
}
10431165
}
10441166

@@ -1615,7 +1737,43 @@ async fn main() {
16151737
}
16161738
);
16171739

1740+
if show_level_not_found {
1741+
draw_text_pro(
1742+
&level_download_response,
1743+
screen_width() / 2.0 - measure_text_ex(&level_download_response, 30, &font) / 2.0,
1744+
screen_height() / 2.0,
1745+
30,
1746+
RED,
1747+
&font
1748+
);
1749+
}
1750+
1751+
back_button.draw(false, None, 1.0, false, &font);
1752+
level_download_button.draw(false, None, 1.0, false, &font);
1753+
level_id_textbox.draw(level_id.clone(), &font);
1754+
}
1755+
1756+
GameState::LevelPage => {
1757+
draw_texture_ex(
1758+
&default_bg_no_gradient,
1759+
-50.0,
1760+
-75.0,
1761+
Color::from_rgba(20, 20, 20, 255),
1762+
DrawTextureParams {
1763+
dest_size: Some(Vec2 {
1764+
x: default_bg_no_gradient.width() * screen_width() as f32 * 0.0008,
1765+
y: default_bg_no_gradient.height() * screen_width() as f32 * 0.0008
1766+
}),
1767+
source: None,
1768+
rotation: 0.0,
1769+
flip_x: false,
1770+
flip_y: false,
1771+
pivot: None
1772+
}
1773+
);
1774+
16181775
back_button.draw(false, None, 1.0, false, &font);
1776+
level_play_button.draw(false, None, 1.0, false, &font);
16191777
}
16201778
}
16211779

src/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ pub enum GameState {
1414
Editor,
1515
LevelComplete,
1616
LevelSettings,
17-
SearchPage
17+
SearchPage,
18+
LevelPage
1819
}
1920

2021
pub struct Button {

0 commit comments

Comments
 (0)