add Replay (#251)
This commit is contained in:
parent
106be6255f
commit
c61602e9f8
21 changed files with 586 additions and 241 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -8,4 +8,5 @@ wbindgen/
|
|||
*.result.json
|
||||
failure*.json
|
||||
game.json
|
||||
escaped-game.json
|
||||
node_modules/
|
||||
|
|
|
|||
|
|
@ -8,8 +8,9 @@ use macroquad::window::screen_height;
|
|||
use server::action::execute_action;
|
||||
use server::advance::{Advance, do_advance};
|
||||
use server::city::City;
|
||||
use server::game::{Game, GameContext, GameData, GameOptions, UndoOption};
|
||||
use server::game_setup::setup_game;
|
||||
use server::game::{Game, GameContext, GameOptions, UndoOption};
|
||||
use server::game_data::GameData;
|
||||
use server::game_setup::{GameSetupBuilder, setup_game};
|
||||
use server::map::Terrain;
|
||||
use server::player::add_unit;
|
||||
use server::position::Position;
|
||||
|
|
@ -58,12 +59,12 @@ async fn main() {
|
|||
"a".repeat(32)
|
||||
};
|
||||
setup_game(
|
||||
players,
|
||||
seed,
|
||||
true,
|
||||
GameOptions {
|
||||
undo: UndoOption::SamePlayer,
|
||||
},
|
||||
GameSetupBuilder::new(players)
|
||||
.seed(seed)
|
||||
.options(GameOptions {
|
||||
undo: UndoOption::SamePlayer,
|
||||
})
|
||||
.build(),
|
||||
) // todo make configurable
|
||||
};
|
||||
|
||||
|
|
@ -163,7 +164,7 @@ fn ai_autoplay(mut game: Game, f: &mut Features, state: &mut State) -> Game {
|
|||
|
||||
#[must_use]
|
||||
fn setup_local_game() -> Game {
|
||||
let mut game = setup_game(2, "0".to_string(), false, GameOptions::default());
|
||||
let mut game = setup_game(GameSetupBuilder::new(2).skip_random_map().build());
|
||||
game.round = 1;
|
||||
game.dice_roll_outcomes = vec![1, 1, 10, 10, 10, 10, 10, 10, 10, 10];
|
||||
let add_unit = |game: &mut Game, pos: &str, player_index: usize, unit_type: UnitType| {
|
||||
|
|
@ -339,7 +340,7 @@ fn import(game: Game) -> Game {
|
|||
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, game.cache, GameContext::Server)
|
||||
Game::from_data(data, game.cache, GameContext::Play)
|
||||
}
|
||||
|
||||
fn export(game: &Game) {
|
||||
|
|
|
|||
|
|
@ -136,7 +136,7 @@ impl RemoteClient {
|
|||
let g = Game::from_data(
|
||||
serde_json::from_str(&state).expect("game should be of type game data"),
|
||||
cache,
|
||||
GameContext::Client,
|
||||
GameContext::Play,
|
||||
);
|
||||
self.state.show_player = g.active_player();
|
||||
self.game = Some(g);
|
||||
|
|
|
|||
|
|
@ -11,6 +11,10 @@ description = "A server for Clash of Cultures"
|
|||
name = "server"
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[[bin]]
|
||||
name = "api_tool"
|
||||
path = "src/api_tool/bin/main.rs"
|
||||
|
||||
[dependencies]
|
||||
hex2d = "1.1.0"
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
|
|
|
|||
|
|
@ -68,26 +68,39 @@ pub enum ActionType {
|
|||
///
|
||||
/// Panics if the action is illegal
|
||||
#[must_use]
|
||||
pub fn execute_action(mut game: Game, action: Action, player_index: usize) -> Game {
|
||||
assert_eq!(player_index, game.active_player(), "Not your turn");
|
||||
pub fn execute_action(game: Game, action: Action, player_index: usize) -> Game {
|
||||
try_execute_action(game, action, player_index).expect("could not execute action")
|
||||
}
|
||||
|
||||
///
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if the action is not valid
|
||||
pub fn try_execute_action(
|
||||
mut game: Game,
|
||||
action: Action,
|
||||
player_index: usize,
|
||||
) -> Result<Game, String> {
|
||||
if player_index != game.active_player() {
|
||||
return Err(format!("Player {player_index} is not the active player"));
|
||||
}
|
||||
|
||||
if game.context == GameContext::AI {
|
||||
execute_without_undo(&mut game, action, player_index).expect("action should be executed");
|
||||
return game;
|
||||
return execute_without_undo(&mut game, action, player_index).map(|()| game);
|
||||
}
|
||||
|
||||
if let Action::Undo = action {
|
||||
assert!(
|
||||
game.can_undo(),
|
||||
"actions revealing new information can't be undone"
|
||||
);
|
||||
return undo(game).expect("cannot undo");
|
||||
if !game.can_undo() {
|
||||
return Err("action can't be undone".to_string());
|
||||
}
|
||||
return undo(game);
|
||||
}
|
||||
|
||||
let add_undo = !matches!(&action, Action::Undo);
|
||||
let old = to_serde_value(&game);
|
||||
let old_player = game.active_player();
|
||||
execute_without_undo(&mut game, action, player_index).expect("action should be executed");
|
||||
execute_without_undo(&mut game, action, player_index)?;
|
||||
let new = to_serde_value(&game);
|
||||
let new_player = game.active_player();
|
||||
let patch = json_patch::diff(&new, &old);
|
||||
|
|
@ -97,7 +110,7 @@ pub fn execute_action(mut game: Game, action: Action, player_index: usize) -> Ga
|
|||
let i = game.action_log_index - 1;
|
||||
current_player_turn_log_mut(&mut game).items[i].undo = clean_patch(patch.0);
|
||||
}
|
||||
game
|
||||
Ok(game)
|
||||
}
|
||||
|
||||
///
|
||||
|
|
@ -120,12 +133,22 @@ pub fn execute_without_undo(
|
|||
add_log_item_from_action(game, &action);
|
||||
add_action_log_item(game, action.clone());
|
||||
|
||||
if game.context == GameContext::Replay
|
||||
&& !game.events.is_empty()
|
||||
&& !matches!(action, Action::Response(_))
|
||||
{
|
||||
// ignore missing response in replay
|
||||
game.events.clear();
|
||||
}
|
||||
|
||||
match game.current_event_handler_mut() {
|
||||
Some(s) => {
|
||||
s.response = if let Action::Response(v) = action {
|
||||
Some(v)
|
||||
} else {
|
||||
return Err(format!("action should be a response: {action:?}"));
|
||||
return Err(format!(
|
||||
"action should be a response: {action:?} - event: {s:?}"
|
||||
));
|
||||
};
|
||||
let details = game.current_event().event_type.clone();
|
||||
execute_custom_phase_action(game, player_index, details)
|
||||
|
|
@ -337,9 +360,17 @@ fn execute_move_action(game: &mut Game, player_index: usize, m: &MoveUnits) -> R
|
|||
)?;
|
||||
|
||||
let (dest, result) = destinations
|
||||
.into_iter()
|
||||
.iter()
|
||||
.find(|(route, _)| route.destination == m.destination)
|
||||
.expect("destination should be a valid destination");
|
||||
.map_or_else(
|
||||
|| {
|
||||
Err(format!(
|
||||
"destination {} not found in {:?}",
|
||||
m.destination, destinations
|
||||
))
|
||||
},
|
||||
|(dest, r)| Ok((dest, r.clone())),
|
||||
)?;
|
||||
result?;
|
||||
|
||||
let c = &dest.cost;
|
||||
|
|
|
|||
|
|
@ -9,11 +9,12 @@ use tokio::runtime::Runtime;
|
|||
use crate::ai_actions::AiActions;
|
||||
use crate::cache::Cache;
|
||||
use crate::game::GameContext;
|
||||
use crate::game_data::GameData;
|
||||
use crate::movement::MovementAction;
|
||||
use crate::{
|
||||
action::{self, Action, ActionType},
|
||||
ai_missions::ActiveMissions,
|
||||
game::{Game, GameData, GameState},
|
||||
game::{Game, GameState},
|
||||
playing_actions::{PlayingAction, PlayingActionType},
|
||||
utils::{self, Rng},
|
||||
};
|
||||
|
|
|
|||
49
server/src/api_tool/bin/main.rs
Normal file
49
server/src/api_tool/bin/main.rs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#![allow(clippy::missing_panics_doc)]
|
||||
|
||||
use server::game::Game;
|
||||
use server::replay;
|
||||
use server::replay::ReplayGameData;
|
||||
use std::fs::File;
|
||||
use std::{env, fs};
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
if args.len() != 2 {
|
||||
println!("Usage: {} command", args[0]);
|
||||
return;
|
||||
}
|
||||
let command = &args[1];
|
||||
match command.as_str() {
|
||||
"replay" => {
|
||||
replay();
|
||||
}
|
||||
_ => {
|
||||
println!("Unknown command: {}", command);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn replay() {
|
||||
let data: ReplayGameData =
|
||||
serde_json::from_str(&read_game_str()).expect("Failed to read export file");
|
||||
let game = replay::replay(data, None);
|
||||
export(game)
|
||||
}
|
||||
|
||||
const EXPORT_FILE: &str = "escaped-game.json";
|
||||
|
||||
fn read_game_str() -> String {
|
||||
let escaped = fs::read_to_string(EXPORT_FILE).expect("Failed to read export file");
|
||||
let val: String = serde_json::from_str(&escaped).expect("Failed to parse export file");
|
||||
val
|
||||
}
|
||||
|
||||
fn export(game: Game) {
|
||||
let string = serde_json::to_string(&game.data()).expect("Failed to serialize game");
|
||||
serde_json::to_writer_pretty(
|
||||
File::create(EXPORT_FILE).expect("Failed to create export file"),
|
||||
&string,
|
||||
)
|
||||
.expect("Failed to write export file");
|
||||
}
|
||||
|
|
@ -529,7 +529,9 @@ pub mod tests {
|
|||
round.players.push(log);
|
||||
age.rounds.push(round);
|
||||
Game {
|
||||
context: GameContext::Server,
|
||||
context: GameContext::Play,
|
||||
version: 0,
|
||||
seed: String::new(),
|
||||
options: GameOptions::default(),
|
||||
cache: Cache::new(),
|
||||
state: GameState::Playing,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
use crate::advance::Advance;
|
||||
use crate::{resource_pile::ResourcePile, unit::Units};
|
||||
|
||||
pub const JSON_SCHEMA_VERSION: u16 = 2;
|
||||
pub const MAX_CITY_PIECES: usize = 4; // i.e. city size 5
|
||||
pub const AGES: u32 = 6;
|
||||
pub const ADVANCE_COST: u8 = 2;
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ use crate::content::persistent_events::{
|
|||
PersistentEventHandler, PersistentEventPlayer, PersistentEventState, PersistentEventType,
|
||||
};
|
||||
use crate::events::{Event, EventOrigin};
|
||||
use crate::game_data::GameData;
|
||||
use crate::log::{
|
||||
ActionLogAge, ActionLogPlayer, ActionLogRound, current_player_turn_log,
|
||||
current_player_turn_log_mut,
|
||||
|
|
@ -20,14 +21,10 @@ use crate::player_events::{
|
|||
};
|
||||
use crate::resource::check_for_waste;
|
||||
use crate::status_phase::enter_status_phase;
|
||||
use crate::utils;
|
||||
use crate::utils::Rng;
|
||||
use crate::wonder::Wonder;
|
||||
use crate::{
|
||||
city::City,
|
||||
content::custom_actions::CustomActionType,
|
||||
map::{Map, MapData},
|
||||
player::{Player, PlayerData},
|
||||
city::City, content::custom_actions::CustomActionType, game_data, map::Map, player::Player,
|
||||
position::Position,
|
||||
};
|
||||
use itertools::Itertools;
|
||||
|
|
@ -59,15 +56,16 @@ pub struct GameOptions {
|
|||
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
pub enum GameContext {
|
||||
Client,
|
||||
Server,
|
||||
Play,
|
||||
AI,
|
||||
Replay,
|
||||
}
|
||||
|
||||
pub struct Game {
|
||||
pub cache: Cache,
|
||||
pub context: GameContext, // trasient
|
||||
pub options: GameOptions,
|
||||
pub cache: Cache,
|
||||
pub version: u16, // JSON schema version
|
||||
pub state: GameState,
|
||||
pub events: Vec<PersistentEventState>,
|
||||
// in turn order starting from starting_player_index and wrapping around
|
||||
|
|
@ -85,6 +83,7 @@ pub struct Game {
|
|||
pub round: u32, // starts at 1
|
||||
pub age: u32, // starts at 1
|
||||
pub messages: Vec<String>,
|
||||
pub seed: String,
|
||||
pub rng: Rng,
|
||||
pub dice_roll_outcomes: Vec<u8>, // for testing
|
||||
pub dice_roll_log: Vec<u8>,
|
||||
|
|
@ -111,115 +110,19 @@ impl PartialEq for Game {
|
|||
}
|
||||
|
||||
impl Game {
|
||||
///
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if any wonder does not exist
|
||||
#[must_use]
|
||||
pub fn from_data(data: GameData, cache: Cache, context: GameContext) -> Self {
|
||||
let mut game = Self {
|
||||
context,
|
||||
cache,
|
||||
options: data.options,
|
||||
state: data.state,
|
||||
players: Vec::new(),
|
||||
map: Map::from_data(data.map),
|
||||
starting_player_index: data.starting_player_index,
|
||||
current_player_index: data.current_player_index,
|
||||
actions_left: data.actions_left,
|
||||
successful_cultural_influence: data.successful_cultural_influence,
|
||||
action_log: data.action_log,
|
||||
action_log_index: data.action_log_index,
|
||||
log: data.log,
|
||||
undo_limit: data.undo_limit,
|
||||
round: data.round,
|
||||
age: data.age,
|
||||
messages: data.messages,
|
||||
rng: Rng::from_seed_string(&data.rng),
|
||||
dice_roll_outcomes: data.dice_roll_outcomes,
|
||||
dice_roll_log: data.dice_roll_log,
|
||||
dropped_players: data.dropped_players,
|
||||
wonders_left: data.wonders_left,
|
||||
action_cards_left: data.action_cards_left,
|
||||
action_cards_discarded: data.action_cards_discarded,
|
||||
objective_cards_left: data.objective_cards_left,
|
||||
incidents_left: data.incidents_left,
|
||||
incidents_discarded: data.incidents_discarded,
|
||||
permanent_effects: data.permanent_effects,
|
||||
events: data.events,
|
||||
};
|
||||
let all = game.cache.get_builtins().clone();
|
||||
for player in data.players {
|
||||
Player::initialize_player(player, &mut game, &all);
|
||||
}
|
||||
game
|
||||
game_data::from_data(data, cache, context)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn data(self) -> GameData {
|
||||
GameData {
|
||||
state: self.state,
|
||||
options: self.options,
|
||||
events: self.events,
|
||||
players: self.players.into_iter().map(Player::data).collect(),
|
||||
map: self.map.data(),
|
||||
starting_player_index: self.starting_player_index,
|
||||
current_player_index: self.current_player_index,
|
||||
action_log: self.action_log,
|
||||
action_log_index: self.action_log_index,
|
||||
log: self.log,
|
||||
undo_limit: self.undo_limit,
|
||||
actions_left: self.actions_left,
|
||||
successful_cultural_influence: self.successful_cultural_influence,
|
||||
round: self.round,
|
||||
age: self.age,
|
||||
messages: self.messages,
|
||||
rng: self.rng.seed.to_string(),
|
||||
dice_roll_outcomes: self.dice_roll_outcomes,
|
||||
dice_roll_log: self.dice_roll_log,
|
||||
dropped_players: self.dropped_players,
|
||||
wonders_left: self.wonders_left,
|
||||
action_cards_left: self.action_cards_left,
|
||||
action_cards_discarded: self.action_cards_discarded,
|
||||
objective_cards_left: self.objective_cards_left,
|
||||
incidents_left: self.incidents_left,
|
||||
incidents_discarded: self.incidents_discarded,
|
||||
permanent_effects: self.permanent_effects,
|
||||
}
|
||||
game_data::data(self)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn cloned_data(&self) -> GameData {
|
||||
GameData {
|
||||
state: self.state.clone(),
|
||||
options: self.options.clone(),
|
||||
events: self.events.clone(),
|
||||
players: self.players.iter().map(Player::cloned_data).collect(),
|
||||
map: self.map.cloned_data(),
|
||||
starting_player_index: self.starting_player_index,
|
||||
current_player_index: self.current_player_index,
|
||||
action_log: self.action_log.clone(),
|
||||
action_log_index: self.action_log_index,
|
||||
log: self.log.clone(),
|
||||
undo_limit: self.undo_limit,
|
||||
actions_left: self.actions_left,
|
||||
successful_cultural_influence: self.successful_cultural_influence,
|
||||
round: self.round,
|
||||
age: self.age,
|
||||
messages: self.messages.clone(),
|
||||
rng: self.rng.seed.to_string(),
|
||||
dice_roll_outcomes: self.dice_roll_outcomes.clone(),
|
||||
dice_roll_log: self.dice_roll_log.clone(),
|
||||
dropped_players: self.dropped_players.clone(),
|
||||
wonders_left: self.wonders_left.clone(),
|
||||
action_cards_left: self.action_cards_left.clone(),
|
||||
action_cards_discarded: self.action_cards_discarded.clone(),
|
||||
objective_cards_left: self.objective_cards_left.clone(),
|
||||
incidents_left: self.incidents_left.clone(),
|
||||
incidents_discarded: self.incidents_discarded.clone(),
|
||||
permanent_effects: self.permanent_effects.clone(),
|
||||
}
|
||||
game_data::cloned_data(self)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
|
@ -741,70 +644,6 @@ impl Game {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq)]
|
||||
pub struct GameData {
|
||||
#[serde(default)]
|
||||
options: GameOptions,
|
||||
state: GameState,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
events: Vec<PersistentEventState>,
|
||||
players: Vec<PlayerData>,
|
||||
map: MapData,
|
||||
starting_player_index: usize,
|
||||
current_player_index: usize,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
action_log: Vec<ActionLogAge>,
|
||||
action_log_index: usize,
|
||||
log: Vec<Vec<String>>,
|
||||
undo_limit: usize,
|
||||
actions_left: u32,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "utils::is_false")]
|
||||
successful_cultural_influence: bool,
|
||||
round: u32,
|
||||
age: u32,
|
||||
messages: Vec<String>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
dice_roll_outcomes: Vec<u8>, // for testing purposes
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "is_string_zero")]
|
||||
rng: String,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
dice_roll_log: Vec<u8>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
dropped_players: Vec<usize>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
wonders_left: Vec<Wonder>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
action_cards_left: Vec<u8>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
action_cards_discarded: Vec<u8>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
objective_cards_left: Vec<u8>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
incidents_left: Vec<u8>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
incidents_discarded: Vec<u8>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
permanent_effects: Vec<PermanentEffect>,
|
||||
}
|
||||
|
||||
fn is_string_zero(s: &String) -> bool {
|
||||
s == "0"
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
|
||||
pub enum GameState {
|
||||
Playing,
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ use crate::content::persistent_events::{
|
|||
EventResponse, PersistentEventRequest, PersistentEventType,
|
||||
};
|
||||
use crate::game::GameOptions;
|
||||
use crate::game_setup::setup_game;
|
||||
use crate::log::current_player_turn_log_mut;
|
||||
use crate::game_setup::{GameSetupBuilder, setup_game};
|
||||
use crate::log::{current_player_turn_log_mut, linear_action_log};
|
||||
use crate::utils::Shuffle;
|
||||
use crate::{
|
||||
action::Action,
|
||||
|
|
@ -19,7 +19,12 @@ use std::cmp::Ordering::*;
|
|||
|
||||
#[must_use]
|
||||
pub fn init(player_amount: usize, seed: String, options: GameOptions) -> Game {
|
||||
setup_game(player_amount, seed, true, options)
|
||||
setup_game(
|
||||
GameSetupBuilder::new(player_amount)
|
||||
.seed(seed)
|
||||
.options(options)
|
||||
.build(),
|
||||
)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
|
@ -49,14 +54,15 @@ pub fn drop_player(mut game: Game, player_index: usize) -> Game {
|
|||
|
||||
#[must_use]
|
||||
pub fn log_length(game: &Game) -> usize {
|
||||
game.log.len()
|
||||
linear_action_log(game).len()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn log_slice(game: &Game, options: &LogSliceOptions) -> Vec<Vec<String>> {
|
||||
pub fn log_slice(game: &Game, options: &LogSliceOptions) -> Vec<Action> {
|
||||
let l = linear_action_log(game);
|
||||
match options.end {
|
||||
Some(end) => &game.log[options.start..=end],
|
||||
None => &game.log[options.start..],
|
||||
Some(end) => &l[options.start..=end],
|
||||
None => &l[options.start..],
|
||||
}
|
||||
.to_vec()
|
||||
}
|
||||
|
|
@ -115,6 +121,7 @@ pub fn strip_secret(mut game: Game, player_index: Option<usize>) -> Game {
|
|||
game.wonders_left.shuffle(&mut game.rng);
|
||||
game.action_cards_left.shuffle(&mut game.rng);
|
||||
game.objective_cards_left.shuffle(&mut game.rng);
|
||||
game.seed = String::new();
|
||||
game.rng = Rng::default();
|
||||
for (i, player) in game.players.iter_mut().enumerate() {
|
||||
if player_index != Some(i) {
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
#![allow(clippy::pedantic)]
|
||||
|
||||
extern crate console_error_panic_hook;
|
||||
use crate::cache::Cache;
|
||||
use crate::game::{GameContext, GameOptions};
|
||||
use crate::{game::Game, game_api};
|
||||
use crate::replay::ReplayGameData;
|
||||
use crate::{game::Game, game_api, replay};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::mem;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
extern crate console_error_panic_hook;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct PlayerMetaData {
|
||||
name: String,
|
||||
|
|
@ -19,7 +19,7 @@ fn get_game(data: String) -> Game {
|
|||
Game::from_data(
|
||||
serde_json::from_str(&data).expect("Could not deserialize game data"),
|
||||
Cache::new(),
|
||||
GameContext::Server,
|
||||
GameContext::Play,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -90,6 +90,23 @@ pub fn log_slice(game: String, options: JsValue) -> JsValue {
|
|||
serde_wasm_bindgen::to_value(&log).expect("log should be serializable")
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq)]
|
||||
struct ReplayOptions {
|
||||
to: Option<usize>,
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = "replay")]
|
||||
pub fn replay(game: String, options: JsValue) -> String {
|
||||
console_error_panic_hook::set_once();
|
||||
|
||||
let r: ReplayGameData = serde_json::from_str(&game).expect("Could not deserialize game data");
|
||||
let to = serde_wasm_bindgen::from_value::<ReplayOptions>(options)
|
||||
.ok()
|
||||
.and_then(|o| o.to);
|
||||
let game = replay::replay(r, to);
|
||||
from_game(game)
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = "setPlayerMetaData")]
|
||||
pub fn set_player_meta_data(game: String, player_index: usize, meta_data: JsValue) -> String {
|
||||
let game = get_game(game);
|
||||
|
|
|
|||
199
server/src/game_data.rs
Normal file
199
server/src/game_data.rs
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
use crate::cache::Cache;
|
||||
use crate::content::effects::PermanentEffect;
|
||||
use crate::content::persistent_events::PersistentEventState;
|
||||
use crate::game::{Game, GameContext, GameOptions, GameState};
|
||||
use crate::log::ActionLogAge;
|
||||
use crate::map::{Map, MapData};
|
||||
use crate::player::{Player, PlayerData};
|
||||
use crate::utils;
|
||||
use crate::utils::Rng;
|
||||
use crate::wonder::Wonder;
|
||||
use num::Zero;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq)]
|
||||
pub struct GameData {
|
||||
#[serde(default)]
|
||||
options: GameOptions,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "u16::is_zero")]
|
||||
version: u16,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "String::is_empty")]
|
||||
seed: String,
|
||||
state: GameState,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
events: Vec<PersistentEventState>,
|
||||
players: Vec<PlayerData>,
|
||||
map: MapData,
|
||||
starting_player_index: usize,
|
||||
current_player_index: usize,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
action_log: Vec<ActionLogAge>,
|
||||
action_log_index: usize,
|
||||
log: Vec<Vec<String>>,
|
||||
undo_limit: usize,
|
||||
actions_left: u32,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "utils::is_false")]
|
||||
successful_cultural_influence: bool,
|
||||
round: u32,
|
||||
age: u32,
|
||||
messages: Vec<String>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
dice_roll_outcomes: Vec<u8>, // for testing purposes
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "is_string_zero")]
|
||||
rng: String,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
dice_roll_log: Vec<u8>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
dropped_players: Vec<usize>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
wonders_left: Vec<Wonder>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
action_cards_left: Vec<u8>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
action_cards_discarded: Vec<u8>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
objective_cards_left: Vec<u8>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
incidents_left: Vec<u8>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
incidents_discarded: Vec<u8>,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
permanent_effects: Vec<PermanentEffect>,
|
||||
}
|
||||
|
||||
///
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if any wonder does not exist
|
||||
#[must_use]
|
||||
pub fn from_data(data: GameData, cache: Cache, context: GameContext) -> Game {
|
||||
let mut game = Game {
|
||||
context,
|
||||
cache,
|
||||
options: data.options,
|
||||
version: data.version,
|
||||
state: data.state,
|
||||
players: Vec::new(),
|
||||
map: Map::from_data(data.map),
|
||||
starting_player_index: data.starting_player_index,
|
||||
current_player_index: data.current_player_index,
|
||||
actions_left: data.actions_left,
|
||||
successful_cultural_influence: data.successful_cultural_influence,
|
||||
action_log: data.action_log,
|
||||
action_log_index: data.action_log_index,
|
||||
log: data.log,
|
||||
undo_limit: data.undo_limit,
|
||||
round: data.round,
|
||||
age: data.age,
|
||||
messages: data.messages,
|
||||
seed: data.seed,
|
||||
rng: Rng::from_seed_string(&data.rng),
|
||||
dice_roll_outcomes: data.dice_roll_outcomes,
|
||||
dice_roll_log: data.dice_roll_log,
|
||||
dropped_players: data.dropped_players,
|
||||
wonders_left: data.wonders_left,
|
||||
action_cards_left: data.action_cards_left,
|
||||
action_cards_discarded: data.action_cards_discarded,
|
||||
objective_cards_left: data.objective_cards_left,
|
||||
incidents_left: data.incidents_left,
|
||||
incidents_discarded: data.incidents_discarded,
|
||||
permanent_effects: data.permanent_effects,
|
||||
events: data.events,
|
||||
};
|
||||
let all = game.cache.get_builtins().clone();
|
||||
for player in data.players {
|
||||
Player::initialize_player(player, &mut game, &all);
|
||||
}
|
||||
game
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn data(game: Game) -> GameData {
|
||||
GameData {
|
||||
options: game.options,
|
||||
version: game.version,
|
||||
state: game.state,
|
||||
events: game.events,
|
||||
players: game.players.into_iter().map(Player::data).collect(),
|
||||
map: game.map.data(),
|
||||
starting_player_index: game.starting_player_index,
|
||||
current_player_index: game.current_player_index,
|
||||
action_log: game.action_log,
|
||||
action_log_index: game.action_log_index,
|
||||
log: game.log,
|
||||
undo_limit: game.undo_limit,
|
||||
actions_left: game.actions_left,
|
||||
successful_cultural_influence: game.successful_cultural_influence,
|
||||
round: game.round,
|
||||
age: game.age,
|
||||
messages: game.messages,
|
||||
seed: game.seed,
|
||||
rng: game.rng.seed.to_string(),
|
||||
dice_roll_outcomes: game.dice_roll_outcomes,
|
||||
dice_roll_log: game.dice_roll_log,
|
||||
dropped_players: game.dropped_players,
|
||||
wonders_left: game.wonders_left,
|
||||
action_cards_left: game.action_cards_left,
|
||||
action_cards_discarded: game.action_cards_discarded,
|
||||
objective_cards_left: game.objective_cards_left,
|
||||
incidents_left: game.incidents_left,
|
||||
incidents_discarded: game.incidents_discarded,
|
||||
permanent_effects: game.permanent_effects,
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn cloned_data(game: &Game) -> GameData {
|
||||
GameData {
|
||||
options: game.options.clone(),
|
||||
version: game.version,
|
||||
state: game.state.clone(),
|
||||
events: game.events.clone(),
|
||||
players: game.players.iter().map(Player::cloned_data).collect(),
|
||||
map: game.map.cloned_data(),
|
||||
starting_player_index: game.starting_player_index,
|
||||
current_player_index: game.current_player_index,
|
||||
action_log: game.action_log.clone(),
|
||||
action_log_index: game.action_log_index,
|
||||
log: game.log.clone(),
|
||||
undo_limit: game.undo_limit,
|
||||
actions_left: game.actions_left,
|
||||
successful_cultural_influence: game.successful_cultural_influence,
|
||||
round: game.round,
|
||||
age: game.age,
|
||||
messages: game.messages.clone(),
|
||||
seed: game.seed.clone(),
|
||||
rng: game.rng.seed.to_string(),
|
||||
dice_roll_outcomes: game.dice_roll_outcomes.clone(),
|
||||
dice_roll_log: game.dice_roll_log.clone(),
|
||||
dropped_players: game.dropped_players.clone(),
|
||||
wonders_left: game.wonders_left.clone(),
|
||||
action_cards_left: game.action_cards_left.clone(),
|
||||
action_cards_discarded: game.action_cards_discarded.clone(),
|
||||
objective_cards_left: game.objective_cards_left.clone(),
|
||||
incidents_left: game.incidents_left.clone(),
|
||||
incidents_discarded: game.incidents_discarded.clone(),
|
||||
permanent_effects: game.permanent_effects.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_string_zero(s: &String) -> bool {
|
||||
s == "0"
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
use crate::action_card::gain_action_card_from_pile;
|
||||
use crate::advance::Advance;
|
||||
use crate::cache::Cache;
|
||||
use crate::consts::{ACTIONS, NON_HUMAN_PLAYERS};
|
||||
use crate::consts::{ACTIONS, JSON_SCHEMA_VERSION, NON_HUMAN_PLAYERS};
|
||||
use crate::content::civilizations::{BARBARIANS, PIRATES};
|
||||
use crate::content::{builtin, civilizations};
|
||||
use crate::game::{Game, GameContext, GameOptions, GameState};
|
||||
|
|
@ -14,14 +14,64 @@ use crate::utils::{Rng, Shuffle};
|
|||
use itertools::Itertools;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Creates a new [`Game`].
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics only if there is an internal bug
|
||||
#[must_use]
|
||||
pub fn setup_game(player_amount: usize, seed: String, setup: bool, options: GameOptions) -> Game {
|
||||
setup_game_with_cache(player_amount, seed, setup, options, Cache::new())
|
||||
pub struct GameSetup {
|
||||
player_amount: usize,
|
||||
seed: String,
|
||||
random_map: bool,
|
||||
options: GameOptions,
|
||||
civilizations: Vec<String>,
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub struct GameSetupBuilder {
|
||||
player_amount: usize,
|
||||
seed: String,
|
||||
random_map: bool,
|
||||
options: GameOptions,
|
||||
civilizations: Vec<String>,
|
||||
}
|
||||
|
||||
impl GameSetupBuilder {
|
||||
pub fn new(player_amount: usize) -> Self {
|
||||
GameSetupBuilder {
|
||||
player_amount,
|
||||
seed: String::new(),
|
||||
random_map: true,
|
||||
options: GameOptions::default(),
|
||||
civilizations: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn seed(mut self, seed: String) -> Self {
|
||||
self.seed = seed;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn skip_random_map(mut self) -> Self {
|
||||
self.random_map = false;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn options(mut self, options: GameOptions) -> Self {
|
||||
self.options = options;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn civilizations(mut self, civilizations: Vec<String>) -> Self {
|
||||
self.civilizations = civilizations;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> GameSetup {
|
||||
GameSetup {
|
||||
player_amount: self.player_amount,
|
||||
seed: self.seed,
|
||||
random_map: self.random_map,
|
||||
options: self.options,
|
||||
civilizations: self.civilizations,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new [`Game`].
|
||||
|
|
@ -30,15 +80,20 @@ pub fn setup_game(player_amount: usize, seed: String, setup: bool, options: Game
|
|||
///
|
||||
/// Panics only if there is an internal bug
|
||||
#[must_use]
|
||||
pub fn setup_game_with_cache(
|
||||
player_amount: usize,
|
||||
seed: String,
|
||||
setup: bool,
|
||||
options: GameOptions,
|
||||
cache: Cache,
|
||||
) -> Game {
|
||||
let mut rng = init_rng(seed);
|
||||
let mut players = init_human_players(player_amount, &mut rng);
|
||||
pub fn setup_game(setup: GameSetup) -> Game {
|
||||
setup_game_with_cache(setup, Cache::new())
|
||||
}
|
||||
|
||||
/// Creates a new [`Game`].
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics only if there is an internal bug
|
||||
#[must_use]
|
||||
pub fn setup_game_with_cache(setup: GameSetup, cache: Cache) -> Game {
|
||||
let mut rng = init_rng(setup.seed.clone());
|
||||
|
||||
let mut players = init_human_players(&setup, &mut rng);
|
||||
|
||||
let starting_player = rng.range(0, players.len());
|
||||
|
||||
|
|
@ -51,7 +106,7 @@ pub fn setup_game_with_cache(
|
|||
players.len(),
|
||||
));
|
||||
|
||||
let map = if setup {
|
||||
let map = if setup.random_map {
|
||||
Map::random_map(&mut players, &mut rng)
|
||||
} else {
|
||||
Map::new(HashMap::new())
|
||||
|
|
@ -83,8 +138,10 @@ pub fn setup_game_with_cache(
|
|||
.shuffled(&mut rng);
|
||||
let all = &cache.get_builtins().clone();
|
||||
let mut game = Game {
|
||||
context: GameContext::Server,
|
||||
options,
|
||||
seed: setup.seed.clone(),
|
||||
context: GameContext::Play,
|
||||
version: JSON_SCHEMA_VERSION,
|
||||
options: setup.options,
|
||||
cache,
|
||||
state: GameState::Playing,
|
||||
events: Vec::new(),
|
||||
|
|
@ -120,13 +177,13 @@ pub fn setup_game_with_cache(
|
|||
builtin::init_player(&mut game, i, all);
|
||||
}
|
||||
|
||||
for player_index in 0..player_amount {
|
||||
for player_index in 0..setup.player_amount {
|
||||
let p = game.player(player_index);
|
||||
game.add_info_log_group(format!("{p} is playing as {}", p.civilization.name));
|
||||
gain_action_card_from_pile(&mut game, player_index);
|
||||
gain_objective_card_from_pile(&mut game, player_index);
|
||||
let p = game.player(player_index);
|
||||
if setup {
|
||||
if setup.random_map {
|
||||
add_unit(p.index, p.cities[0].position, UnitType::Settler, &mut game);
|
||||
}
|
||||
}
|
||||
|
|
@ -153,12 +210,18 @@ fn init_rng(seed: String) -> Rng {
|
|||
Rng::from_seed(seed)
|
||||
}
|
||||
|
||||
fn init_human_players(player_amount: usize, rng: &mut Rng) -> Vec<Player> {
|
||||
fn init_human_players(setup: &GameSetup, rng: &mut Rng) -> Vec<Player> {
|
||||
let mut players = Vec::new();
|
||||
let mut civilizations = civilizations::get_all();
|
||||
for player_index in 0..player_amount {
|
||||
let civilization = rng.range(NON_HUMAN_PLAYERS, civilizations.len());
|
||||
let mut player = Player::new(civilizations.remove(civilization), player_index);
|
||||
for player_index in 0..setup.player_amount {
|
||||
let civilization = if setup.civilizations.is_empty() {
|
||||
civilizations.remove(rng.range(NON_HUMAN_PLAYERS, civilizations.len()))
|
||||
} else {
|
||||
rng.next_seed(); // need to call next_seed to have the same number of calls to rng
|
||||
civilizations::get_civilization(&setup.civilizations[player_index])
|
||||
.expect("civilization not found")
|
||||
};
|
||||
let mut player = Player::new(civilization, player_index);
|
||||
player.resource_limit = ResourcePile::new(2, 7, 7, 7, 7, 0, 0);
|
||||
player.gain_resources(ResourcePile::food(2));
|
||||
player.advances.insert(Advance::Farming);
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@ mod explore;
|
|||
pub mod game;
|
||||
pub mod game_api;
|
||||
pub mod game_api_wrapper;
|
||||
pub mod game_data;
|
||||
pub mod game_setup;
|
||||
pub mod happiness;
|
||||
pub mod incident;
|
||||
|
|
@ -58,6 +59,7 @@ pub mod playing_actions;
|
|||
pub mod position;
|
||||
pub mod profiling;
|
||||
pub mod recruit;
|
||||
pub mod replay;
|
||||
pub mod resource;
|
||||
pub mod resource_pile;
|
||||
mod special_advance;
|
||||
|
|
|
|||
|
|
@ -104,6 +104,20 @@ pub struct LogSliceOptions {
|
|||
pub end: Option<usize>,
|
||||
}
|
||||
|
||||
pub(crate) fn linear_action_log(game: &Game) -> Vec<Action> {
|
||||
game.action_log
|
||||
.iter()
|
||||
.flat_map(|age| {
|
||||
age.rounds.iter().flat_map(|round| {
|
||||
round
|
||||
.players
|
||||
.iter()
|
||||
.flat_map(|player| player.items.iter().map(|item| item.action.clone()))
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
///
|
||||
///
|
||||
/// # Panics
|
||||
|
|
@ -112,7 +126,7 @@ pub struct LogSliceOptions {
|
|||
///
|
||||
/// this is called before the action is executed
|
||||
#[must_use]
|
||||
pub fn format_action_log_item(action: &Action, game: &Game) -> Vec<String> {
|
||||
pub(crate) fn format_action_log_item(action: &Action, game: &Game) -> Vec<String> {
|
||||
match action {
|
||||
Action::Playing(action) => vec![format_playing_action_log_item(action, game)],
|
||||
Action::Movement(action) => vec![format_movement_action_log_item(action, game)],
|
||||
|
|
|
|||
114
server/src/replay.rs
Normal file
114
server/src/replay.rs
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
use crate::action::{Action, try_execute_action};
|
||||
use crate::consts::NON_HUMAN_PLAYERS;
|
||||
use crate::game::{Game, GameContext, GameOptions};
|
||||
use crate::game_setup::{GameSetupBuilder, setup_game};
|
||||
use itertools::Itertools;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::mem;
|
||||
|
||||
///
|
||||
/// Minimal data for replay - try to avoid breaking changes as much as possible
|
||||
///
|
||||
#[derive(Serialize, Deserialize, PartialEq)]
|
||||
pub struct ReplayGameData {
|
||||
#[serde(default)]
|
||||
options: GameOptions,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "String::is_empty")]
|
||||
seed: String,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
action_log: Vec<ReplayActionLogAge>,
|
||||
players: Vec<ReplayPlayerData>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq)]
|
||||
pub struct ReplayActionLogAge {
|
||||
pub rounds: Vec<ReplayActionLogRound>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq)]
|
||||
pub struct ReplayActionLogRound {
|
||||
pub players: Vec<ReplayActionLogPlayer>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq)]
|
||||
pub struct ReplayActionLogPlayer {
|
||||
pub index: usize,
|
||||
pub items: Vec<ReplayActionLogItem>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq)]
|
||||
pub struct ReplayActionLogItem {
|
||||
pub action: Action,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq)]
|
||||
pub struct ReplayPlayerData {
|
||||
id: usize,
|
||||
#[serde(default)]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
name: Option<String>,
|
||||
civilization: String,
|
||||
}
|
||||
|
||||
/// replay is used to store the game data for replay
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the game data cannot be replayed
|
||||
#[must_use]
|
||||
pub fn replay(mut data: ReplayGameData, to: Option<usize>) -> Game {
|
||||
let log = linear_action_log(mem::take(&mut data.action_log));
|
||||
let to = to.unwrap_or(log.len() - 1);
|
||||
let mut game = setup_game(
|
||||
GameSetupBuilder::new(data.players.len() - NON_HUMAN_PLAYERS)
|
||||
.seed(data.seed)
|
||||
.options(data.options)
|
||||
.civilizations(
|
||||
data.players
|
||||
.iter()
|
||||
.map(|player| player.civilization.clone())
|
||||
.collect_vec(),
|
||||
)
|
||||
.build(),
|
||||
);
|
||||
for player in &data.players {
|
||||
if let Some(name) = &player.name {
|
||||
game.players[player.id].set_name(name.clone());
|
||||
}
|
||||
}
|
||||
game.context = GameContext::Replay;
|
||||
|
||||
for (i, (id, a)) in log.into_iter().enumerate() {
|
||||
if i > to {
|
||||
break;
|
||||
}
|
||||
let player_index = game.active_player();
|
||||
match try_execute_action(game, a.clone(), player_index) {
|
||||
Ok(g) => game = g,
|
||||
Err(e) => {
|
||||
panic!("Failed to execute action {id}, {a:?}: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
game
|
||||
}
|
||||
|
||||
pub(crate) fn linear_action_log(log: Vec<ReplayActionLogAge>) -> Vec<(String, Action)> {
|
||||
log.into_iter()
|
||||
.enumerate()
|
||||
.flat_map(move |(age_num, age)| {
|
||||
age.rounds
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.flat_map(move |(round_num, round)| {
|
||||
round.players.into_iter().flat_map(move |player| {
|
||||
player.items.into_iter().map(move |item| {
|
||||
(format!("{age_num}{round_num}{}", player.index), item.action)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
|
@ -419,7 +419,7 @@ pub fn load_game(path: &GamePath) -> Game {
|
|||
serde_json::from_str(&read_game_str(path))
|
||||
.unwrap_or_else(|e| panic!("the game file should be deserializable {path}: {e}",)),
|
||||
Cache::new(),
|
||||
GameContext::Server,
|
||||
GameContext::Play,
|
||||
);
|
||||
if update_expected() {
|
||||
write_result(&to_json(&game), path);
|
||||
|
|
|
|||
|
|
@ -4,8 +4,7 @@ use playing_actions::PlayingActionType;
|
|||
use server::card::HandCard;
|
||||
use server::collect::PositionCollection;
|
||||
use server::content::persistent_events::{EventResponse, SelectedStructure, Structure};
|
||||
use server::game::GameOptions;
|
||||
use server::game_setup::setup_game;
|
||||
use server::game_setup::{GameSetupBuilder, setup_game};
|
||||
use server::log::current_player_turn_log;
|
||||
use server::unit::Units;
|
||||
use server::wonder::Wonder;
|
||||
|
|
@ -31,15 +30,13 @@ const JSON: JsonTest = JsonTest::new("base");
|
|||
|
||||
#[test]
|
||||
fn new_game() {
|
||||
let seed = String::new();
|
||||
let game = setup_game(2, seed, true, GameOptions::default());
|
||||
let game = setup_game(GameSetupBuilder::new(2).build());
|
||||
JSON.compare_game("new_game", &game);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_actions() {
|
||||
let seed = String::new();
|
||||
let mut game = setup_game(1, seed, false, GameOptions::default());
|
||||
let mut game = setup_game(GameSetupBuilder::new(1).skip_random_map().build());
|
||||
|
||||
game.wonders_left.retain(|w| *w == Wonder::Pyramids);
|
||||
let founded_city_position = Position::new(0, 1);
|
||||
|
|
@ -216,7 +213,7 @@ fn increase_happiness(game: Game) -> Game {
|
|||
|
||||
#[test]
|
||||
fn undo() {
|
||||
let mut game = setup_game(1, String::new(), false, GameOptions::default());
|
||||
let mut game = setup_game(GameSetupBuilder::new(1).skip_random_map().build());
|
||||
game.players[0]
|
||||
.cities
|
||||
.push(City::new(0, Position::new(0, 0)));
|
||||
|
|
|
|||
|
|
@ -4,7 +4,8 @@ use itertools::Itertools;
|
|||
use server::action::ActionType;
|
||||
use server::ai_actions::AiActions;
|
||||
use server::cache::Cache;
|
||||
use server::game::{Game, GameContext, GameOptions};
|
||||
use server::game::{Game, GameContext};
|
||||
use server::game_setup::GameSetupBuilder;
|
||||
use server::movement::{MoveUnits, MovementAction, possible_move_units_destinations};
|
||||
use server::playing_actions::PlayingActionType;
|
||||
use server::profiling::start_profiling;
|
||||
|
|
@ -60,7 +61,8 @@ async fn test_random_actions() {
|
|||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn random_actions_iteration(mut rng: Rng, cache: Cache) {
|
||||
let seed = rng.range(0, 10_usize.pow(15)).to_string();
|
||||
let mut game = game_setup::setup_game_with_cache(2, seed, true, GameOptions::default(), cache);
|
||||
let mut game =
|
||||
game_setup::setup_game_with_cache(GameSetupBuilder::new(2).seed(seed).build(), cache);
|
||||
game.context = GameContext::AI;
|
||||
let mut ai_actions = AiActions::new();
|
||||
loop {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"options": {},
|
||||
"version": 2,
|
||||
"state": "Playing",
|
||||
"players": [
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue