status phase actions
This commit is contained in:
parent
80ebad4c88
commit
cd023c2fd7
4 changed files with 48 additions and 31 deletions
|
|
@ -22,6 +22,7 @@ pub enum PlayingAction {
|
|||
city: CityData,
|
||||
city_piece: BuildingData,
|
||||
payment: ResourcePile,
|
||||
temple_bonus: Option<ResourcePile>,
|
||||
},
|
||||
IncreaseHappiness {
|
||||
cities: Vec<(CityData, u32)>,
|
||||
|
|
@ -42,7 +43,7 @@ pub enum PlayingAction {
|
|||
}
|
||||
|
||||
impl PlayingAction {
|
||||
pub fn execute(self, player: &mut Player, user_specification: Option<String>, game: &mut Game) {
|
||||
pub fn execute(self, player: &mut Player, game: &mut Game) {
|
||||
let player_name = player.name();
|
||||
match self {
|
||||
Advance {
|
||||
|
|
@ -61,6 +62,7 @@ impl PlayingAction {
|
|||
city,
|
||||
city_piece,
|
||||
payment,
|
||||
temple_bonus,
|
||||
} => {
|
||||
let city = City::from_data(city);
|
||||
let building = Building::from_data(&city_piece);
|
||||
|
|
@ -76,10 +78,7 @@ impl PlayingAction {
|
|||
panic!("Illegal action");
|
||||
}
|
||||
if matches!(building, Temple) {
|
||||
let building_bonus = serde_json::from_str(
|
||||
&user_specification.expect("user should have specified the building bonus"),
|
||||
)
|
||||
.expect("use should have specified the building bonus");
|
||||
let building_bonus = temple_bonus.expect("build data should contain temple bonus");
|
||||
if building_bonus != ResourcePile::mood_tokens(1)
|
||||
&& building_bonus != ResourcePile::culture_tokens(1)
|
||||
{
|
||||
|
|
@ -144,7 +143,7 @@ impl PlayingAction {
|
|||
target_city.influence_culture(player, &building);
|
||||
}
|
||||
Custom { name, contents } => custom_actions::get_custom_action(&name, &contents)
|
||||
.execute(player, user_specification),
|
||||
.execute(player),
|
||||
EndTurn => unreachable!("end turn should be returned before executing the action"),
|
||||
}
|
||||
}
|
||||
|
|
@ -160,7 +159,7 @@ impl PlayingAction {
|
|||
}
|
||||
|
||||
pub trait CustomAction {
|
||||
fn execute(&self, player: &mut Player, user_specification: Option<String>);
|
||||
fn execute(&self, player: &mut Player);
|
||||
fn action_type(&self) -> ActionType;
|
||||
fn name(&self) -> String;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ impl<T> Event<T> {
|
|||
}
|
||||
|
||||
pub fn remove_listener(&mut self, id: usize) {
|
||||
self.listeners.remove(
|
||||
let _ = self.listeners.remove(
|
||||
self.listeners
|
||||
.iter()
|
||||
.position(|(_, _, value)| value == &id)
|
||||
|
|
@ -63,7 +63,7 @@ impl<T, U, V> EventMut<T, U, V> {
|
|||
}
|
||||
|
||||
pub fn remove_listener(&mut self, id: usize) {
|
||||
self.listeners.remove(
|
||||
let _ = self.listeners.remove(
|
||||
self.listeners
|
||||
.iter()
|
||||
.position(|(_, _, value)| value == &id)
|
||||
|
|
@ -85,7 +85,7 @@ impl<T, U, V> EventMut<T, U, V> {
|
|||
}
|
||||
|
||||
pub fn remove_listener_mut(&mut self, id: usize) {
|
||||
self.listeners_mut.remove(
|
||||
let _ = self.listeners_mut.remove(
|
||||
self.listeners_mut
|
||||
.iter()
|
||||
.position(|(_, _, value)| value == &id)
|
||||
|
|
@ -93,8 +93,8 @@ impl<T, U, V> EventMut<T, U, V> {
|
|||
);
|
||||
}
|
||||
|
||||
pub fn trigger(&mut self, value: &mut T, info: &U, details: &V) {
|
||||
for (listener, _, _) in self.listeners_mut.iter_mut() {
|
||||
pub fn trigger(&self, value: &mut T, info: &U, details: &V) {
|
||||
for (listener, _, _) in self.listeners_mut.iter() {
|
||||
listener(value, info, details);
|
||||
}
|
||||
for (listener, _, _) in self.listeners.iter() {
|
||||
|
|
@ -132,7 +132,7 @@ impl StaticEvent {
|
|||
}
|
||||
|
||||
pub fn remove_listener(&mut self, id: usize) {
|
||||
self.listeners.remove(
|
||||
let _ = self.listeners.remove(
|
||||
self.listeners
|
||||
.iter()
|
||||
.position(|(_, _, value)| value == &id)
|
||||
|
|
@ -146,11 +146,3 @@ impl StaticEvent {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for StaticEvent {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
listeners: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,11 +5,10 @@ use crate::{
|
|||
action::PlayingAction::*,
|
||||
content::civilizations,
|
||||
game_api::UserAction,
|
||||
player::{Player, PlayerData},
|
||||
player::{Player, PlayerData}, city::CityData,
|
||||
};
|
||||
|
||||
use GameState::*;
|
||||
use StatusPhaseState::*;
|
||||
|
||||
const DICE_ROLL_BUFFER: u32 = 200;
|
||||
const AGES: u32 = 6;
|
||||
|
|
@ -126,9 +125,13 @@ impl Game {
|
|||
.expect("dice roll outcomes should not be empty")
|
||||
}
|
||||
|
||||
pub fn execute_playing_action(&mut self, user_action: UserAction, player_index: usize) {
|
||||
pub fn execute_action(&mut self, user_action: UserAction, player_index: usize) {
|
||||
let action = user_action.action;
|
||||
let user_specification = user_action.specification;
|
||||
if let StatusPhase(phase) = &self.state {
|
||||
return;
|
||||
}
|
||||
let action = serde_json::from_str(&action).expect("action should be valid playing action json");
|
||||
if matches!(action, EndTurn) {
|
||||
self.next_turn();
|
||||
return;
|
||||
|
|
@ -147,7 +150,7 @@ impl Game {
|
|||
}
|
||||
}
|
||||
let mut player = self.players.remove(player_index);
|
||||
action.execute(&mut player, user_specification, self);
|
||||
action.execute(&mut player, self);
|
||||
self.players.insert(player_index, player);
|
||||
if !free_action {
|
||||
self.actions_left -= 1;
|
||||
|
|
@ -176,7 +179,7 @@ impl Game {
|
|||
if self.players.iter().any(|player| player.cities.is_empty()) {
|
||||
self.end_game();
|
||||
}
|
||||
self.state = StatusPhase(ChangeGovernmentType);
|
||||
self.state = StatusPhase(StatusPhaseState::ChangeGovernmentType);
|
||||
}
|
||||
|
||||
fn next_age(&mut self) {
|
||||
|
|
@ -225,8 +228,32 @@ pub enum GameState {
|
|||
pub enum StatusPhaseState {
|
||||
CompleteObjectives,
|
||||
FreeAdvance,
|
||||
DrawNewCards,
|
||||
RaseSize1City,
|
||||
ChangeGovernmentType,
|
||||
DetermineFirstPlayer,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CompleteObjectives {
|
||||
objectives: Vec<usize>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct FreeAdvance {
|
||||
technology: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct RaseSize1City {
|
||||
city: Option<CityData>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ChangeGovernmentType {
|
||||
new_government_technology: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct DetermineFirstPlayer {
|
||||
first_player: usize,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,18 +2,17 @@ use std::{cmp::Ordering::*, mem};
|
|||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::action::PlayingAction;
|
||||
use crate::game::Game;
|
||||
use crate::game::GameState::*;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct UserAction {
|
||||
pub action: PlayingAction,
|
||||
pub action: String,
|
||||
pub specification: Option<String>,
|
||||
}
|
||||
|
||||
impl UserAction {
|
||||
pub fn new(action: PlayingAction, specification: Option<String>) -> Self {
|
||||
pub fn new(action: String, specification: Option<String>) -> Self {
|
||||
Self {
|
||||
action,
|
||||
specification,
|
||||
|
|
@ -60,7 +59,7 @@ pub extern "C" fn execute_action(game: String, r#move: String, player: usize) ->
|
|||
let user_action =
|
||||
serde_json::from_str(&r#move).expect("API call should receive valid move json");
|
||||
game.log.push(r#move);
|
||||
game.execute_playing_action(user_action, player);
|
||||
game.execute_action(user_action, player);
|
||||
game.json()
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue