chiark / gitweb /
apitest: Set positions of library load markers
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 26 Feb 2021 23:19:26 +0000 (23:19 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 26 Feb 2021 23:19:26 +0000 (23:19 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
apitest.rs
apitest/at-otter.rs
src/updates.rs

index 4fbd060ba9ed40b05c4f66724a4c3a2a865eb1c8..a367ddc29e62a1bc96471f7ddc3f0ee94d8ebe48 100644 (file)
@@ -19,6 +19,7 @@ pub use arrayvec::ArrayVec;
 pub use boolinator::Boolinator;
 pub use fehler::{throw, throws};
 pub use if_chain::if_chain;
+pub use itertools::izip;
 pub use log::{debug, error, info, trace, warn};
 pub use log::{log, log_enabled};
 pub use nix::unistd::LinkatFlags;
@@ -61,14 +62,15 @@ pub use otter::commands::{MgmtGameInstruction, MgmtGameResponse};
 pub use otter::commands::{MgmtGameUpdateMode};
 pub use otter::config::*;
 pub use otter::{deref_to_field, deref_to_field_mut};
-pub use otter::gamestate::{self, Generation, PlayerId};
+pub use otter::gamestate::{self, Generation, PieceId, PlayerId};
 pub use otter::global::InstanceName;
 pub use otter::mgmtchannel::MgmtChannel;
 pub use otter::slotmap_slot_idx::KeyDataExt;
-pub use otter::spec::{Coord, GameSpec, Pos, PosC};
+pub use otter::spec::{Coord, GameSpec, Pos, PosC, RawToken};
 pub use otter::toml_de;
 pub use otter::ui::{AbbrevPresentationLayout, PresentationLayout};
 pub use otter::ui::player_num_dasharray;
+pub use otter::updates::RawClientSequence;
 pub use otter::utils::*;
 
 pub const MS: time::Duration = time::Duration::from_millis(1);
index 62a470be356d69e09b0d01dd6b0618a14bf352eb..cd61d337fb9eed5fb858637289d81c3461111f8e 100644 (file)
@@ -22,8 +22,12 @@ struct Player {
 }
 
 struct Session {
+  pub ctoken: RawToken,
+  pub gen: Generation,
+  pub cseq: RawClientSequence,
   pub dom: scraper::Html,
   pub updates: UnixStream,
+  pub client: reqwest::blocking::Client,
 }
 
 mod scraper_ext {
@@ -123,7 +127,13 @@ impl Ctx {
       eprintln!("copy_to'd!"); 
     });
 
-    Session { dom: session, updates: reader }
+    Session {
+      client, gen,
+      cseq: 42,
+      ctoken: RawToken(ctoken.to_string()),
+      dom: session,
+      updates: reader,
+    }
   }
 }
 
@@ -156,6 +166,25 @@ impl Session {
       })
       .collect()
   }
+
+  #[throws(AE)]
+  fn api_piece_op(&mut self, su: &SetupCore, piece: &str,
+                  opname: &str, op: serde_json::Value) {
+    self.cseq += 1;
+    let cseq = self.cseq;
+    
+    let resp = self.client.post(&su.ds.also(&[("opname",opname)])
+                                .subst("@url@/_/api/@opname@")?)
+      .json(&json!({
+        "ctoken": self.ctoken,
+        "piece": piece,
+        "gen": self.gen,
+        "cseq": cseq,
+        "op": op,
+      }))
+      .send()?;
+    ensure_eq!(resp.status(), 200);
+  }
 }
 
 impl Ctx {
@@ -179,7 +208,7 @@ impl Ctx {
     ensure_eq!(add_err.downcast::<ExitStatusError>()?.0.code(),
                Some(EXIT_NOTFOUND));
 
-    let session = self.connect_player(&self.alice)?;
+    let mut session = self.connect_player(&self.alice)?;
     let pieces = session.pieces()?;
     dbg!(&pieces);
     let llm = pieces.into_iter()
@@ -187,9 +216,12 @@ impl Ctx {
       .collect::<ArrayVec<_>>();
     let llm: [_;2] = llm.into_inner().unwrap();
     dbg!(&llm);
-    // xxx find load markers ids
 
-    // xxx find load markers' locations
+    for (llm, pos) in izip!(&llm, [PosC([5,5]), PosC([50,25])].iter()) {
+      session.api_piece_op(&self.su, &llm.id, "grab", json!({}))?;
+      session.api_piece_op(&self.su, &llm.id, "m", json![pos.0])?;
+    }
+
     // xxx send api requests to move markers
     // run library-add again
   }
index 43f54a29bfcab1fe58f75a1752b793aa3d664644..7774153ef3164aabff2dbdbe24530bf18577d4f3 100644 (file)
@@ -11,9 +11,11 @@ use crate::prelude::*;
 
 // ---------- newtypes, type aliases, basic definitions ----------
 
+pub type RawClientSequence = u64;
+
 #[derive(Debug,Copy,Clone,Eq,PartialEq,Deserialize,Serialize)]
 #[serde(transparent)]
-pub struct ClientSequence(u64);
+pub struct ClientSequence(RawClientSequence);
 
 // ---------- from manamgenet operations ----------