Skip to content

Commit c52d27a

Browse files
committed
Adds summarization of invoked actions
* when calling p_move consecutively, the steps are summed up while logging * when calling p_turn consecutively, only the last step is recorded
1 parent e28ce7e commit c52d27a

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

contracts/logging_engine/src/engine.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,26 @@ impl LoggingEngine {
3030
}
3131
fn log_action(env: &Env, action: &ActionItem) {
3232
let mut actions = Self::actions(env.clone());
33-
actions.push_back(*action);
33+
let add_action: ActionItem;
34+
35+
if let Some(Ok(last_action_item)) = actions.last() {
36+
match last_action_item {
37+
ActionItem(Action::Turn, _) if action.0 == Action::Turn => {
38+
actions.pop_back();
39+
add_action = action.clone();
40+
}
41+
ActionItem(Action::Move, _) if action.0 == Action::Move => {
42+
actions.pop_back();
43+
add_action = ActionItem(last_action_item.0, last_action_item.1 + (action.1 as u32));
44+
45+
}
46+
_ => add_action = *action,
47+
}
48+
} else {
49+
add_action = *action;
50+
}
51+
52+
actions.push_back(add_action);
3453
env.storage().set(&ACTIONS, &actions);
3554
}
3655

@@ -73,7 +92,7 @@ impl LoggingEngine {
7392
if let Err(Ok(e)) = Self::get_engine(&env).try_p_move(&times) {
7493
return Err(e);
7594
}
76-
Self::log_action(&env, &ActionItem(Action::Move, 1));
95+
Self::log_action(&env, &ActionItem(Action::Move, times.unwrap_or(1)));
7796
Ok(())
7897
}
7998
pub fn p_shoot(env: Env) -> Result<(), game_engine::Error> {

contracts/solution/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![no_std]
22

3-
use engine::Client as GameEngine;
3+
use engine::{Client as GameEngine, Direction};
44
use soroban_sdk::{contractimpl, BytesN, Env};
55

66
pub struct Solution;
@@ -19,6 +19,12 @@ impl Solution {
1919
// YOUR CODE START
2020

2121
engine.p_shoot();
22+
engine.p_move(&None);
23+
engine.p_move(&Some(2));
24+
engine.p_turn(&Direction::UpLeft);
25+
engine.p_turn(&Direction::Left);
26+
engine.p_move(&Some(1));
27+
engine.p_turn(&Direction::Down);
2228
// YOUR CODE END
2329
}
2430
}

0 commit comments

Comments
 (0)