Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable import/export in native client #53

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ name = "client"
version = "0.1.0"
edition = "2021"

[features]
# disable file import and export
wasm = []

# may be needed for remote client
#[lib]
#name = "client"
Expand Down
37 changes: 16 additions & 21 deletions client/src/local_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@

use crate::client;
use macroquad::prelude::next_frame;
use std::fs::File;
use std::io::BufReader;

use crate::client::{Features, GameSyncRequest, GameSyncResult};
use server::city::City;
use server::game::Game;
use server::game::{Game, GameData};
use server::map::Terrain;
use server::position::Position;
use server::resource_pile::ResourcePile;
use server::unit::UnitType;

pub async fn run(mut game: Game) {
pub async fn run(mut game: Game, features: &Features) {
let mut state = client::init().await;
let features = Features {
import_export: true,
};

let mut sync_result = GameSyncResult::None;
loop {
let message = client::render_and_update(&game, &mut state, &sync_result, &features);
let message = client::render_and_update(&game, &mut state, &sync_result, features);
sync_result = GameSyncResult::None;
match message {
GameSyncRequest::None => {}
Expand Down Expand Up @@ -110,23 +109,19 @@ fn add_terrain(game: &mut Game, pos: &str, terrain: Terrain) {
game.map.tiles.insert(Position::from_offset(pos), terrain);
}

// const EXPORT_FILE: &str = "game.json";
const EXPORT_FILE: &str = "game.json";

fn import() -> Game {
// todo only works with native client
// let file = File::open(EXPORT_FILE).expect("Failed to open export file");
// let reader = BufReader::new(file);
// let data: GameData = serde_json::from_reader(reader).expect("Failed to read export file");
// Game::from_data(data)
panic!()
let file = File::open(EXPORT_FILE).expect("Failed to open export file");
let reader = BufReader::new(file);
let data: GameData = serde_json::from_reader(reader).expect("Failed to read export file");
Game::from_data(data)
}

fn export(_game: &Game) {
// todo only works with native client
// serde_json::to_writer_pretty(
// File::create(EXPORT_FILE).expect("Failed to create export file"),
// &game.cloned_data(),
// )
// .expect("Failed to write export file");
panic!()
fn export(game: &Game) {
serde_json::to_writer_pretty(
File::create(EXPORT_FILE).expect("Failed to create export file"),
&game.cloned_data(),
)
.expect("Failed to write export file");
}
9 changes: 8 additions & 1 deletion client/src/local_client/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
use client::client::Features;
use client::local_client;
use server::game::Game;

#[macroquad::main("Clash")]
async fn main() {
let wasm = cfg!(feature = "wasm");

let features = Features {
import_export: !wasm,
};

//todo add button to decide random or fixed game
let game = if false {
Game::new(2, "a".repeat(32), true)
} else {
local_client::setup_local_game()
};

local_client::run(game).await;
local_client::run(game, &features).await;
}
4 changes: 2 additions & 2 deletions client/wasm-bindgen-macroquad.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ TARGET_DIR="target/wasm32-unknown-unknown"
# Build
echo "Building $PROJECT_NAME..."
if [ -n "$RELEASE" ]; then
cargo build --release --target wasm32-unknown-unknown
cargo build --release --target wasm32-unknown-unknown --features "wasm"
TARGET_DIR="$TARGET_DIR/release"
else
cargo build --target wasm32-unknown-unknown
cargo build --target wasm32-unknown-unknown --features "wasm"
TARGET_DIR="$TARGET_DIR/debug"
fi

Expand Down
Loading