添加天梯(暂时还不是)

This commit is contained in:
shenjack 2024-03-24 00:25:11 +08:00
parent df7ba9484d
commit 41d22824a9
Signed by: shenjack
GPG Key ID: 7B1134A979775551
6 changed files with 1604 additions and 0 deletions

1
rust-namer/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target

1457
rust-namer/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

12
rust-namer/Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "rust-namer"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = { version = "1.0", features = ["backtrace"] }
serde = { version = "1.0", features = ["serde_derive"] }
serde_json = "1.0"
tokio = { version = "1.36.0", features = ["full"] }
thirtyfour = "0.31.0"

21
rust-namer/insert.js Normal file
View File

@ -0,0 +1,21 @@
let has_done_target = document.getElementById('done_target');
if (has_done_target) {
has_done_target.display = 'none';
has_done_target.done = 'false';
} else {
let target = document.createElement("div", style = "display: none;");
target.id = "done_target";
target.style.display = "none";
document.body.appendChild(target);
}
// 监听消息
window.addEventListener('message', function (event) {
console.log('Received message:', event.data, event);
if (event.data.hasOwnProperty("all")) {
// 为 done_target 添加 done=true 属性
let done_target = document.getElementById('done_target');
done_target.setAttribute('done', 'true');
done_target.win_data = event.data;
console.log('done_target:', done_target);
}
});

16
rust-namer/src/main.rs Normal file
View File

@ -0,0 +1,16 @@
use anyhow::Result;
mod runner;
#[tokio::main]
async fn main() -> Result<()> {
let web_runner = runner::WebDriverRunner::init("https://shenjack.top:82/md5").await?;
let result = web_runner.raw_flight("aaaaaaa\nnnnnn".to_string()).await?;
println!("{}", result.str_without_pic());
web_runner.quit().await?;
Ok(())
}

97
rust-namer/src/runner.rs Normal file
View File

@ -0,0 +1,97 @@
use std::time::Duration;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use thirtyfour::prelude::*;
const INSERT_JS: &str = include_str!("../insert.js");
#[derive(Debug, Serialize, Deserialize)]
pub struct WinData {
pub all: Vec<Vec<Vec<String>>>,
pub winners: Vec<String>,
pub pic: String, // base64 img
#[serde(rename = "firstKill")]
pub first_kill: Option<String>,
}
impl WinData {
pub fn str_without_pic(&self) -> String {
format!(
"Winners: {:?}\nFirst Kill: {:?}\nAll: {:?}",
self.winners, self.first_kill, self.all
)
}
}
#[derive(Debug)]
pub struct TeamRunner {
pub time_out: Duration,
/// 每个队伍的成员, 队伍名
pub teams: Vec<(String, Vec<String>)>,
}
impl TeamRunner {
// pub fn builder
}
pub struct WebDriverRunner {
pub driver: WebDriver,
}
impl std::fmt::Display for WebDriverRunner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Runner")
}
}
impl WebDriverRunner {
pub async fn init(target_url: impl AsRef<str>) -> Result<Self> {
let caps = DesiredCapabilities::edge();
let driver = WebDriver::new("http://localhost:9515", caps).await?;
driver.goto(target_url.as_ref()).await?;
driver.execute(INSERT_JS, vec![]).await?;
// insert.js
// 预备环境
Ok(Self { driver })
}
pub async fn raw_flight(&self, teams: String) -> Result<WinData> {
let done_target = self.driver.find(By::Id("done_target")).await?;
let go_btn = self.driver.find(By::ClassName("goBtn")).await?;
let fast_forward_btn = self.driver.find(By::Id("fastBtn")).await?;
let name_input = self.driver.find(By::Id("input_name")).await?;
name_input.send_keys(teams).await?;
go_btn.click().await?;
tokio::time::sleep(std::time::Duration::from_millis(100)).await; // 等一会
fast_forward_btn.click().await?;
done_target
.wait_until()
.has_attribute("done", "true")
.await?;
let win_data = self
.driver
.execute("return arguments[0].win_data", vec![done_target.to_json()?])
.await?;
let win_data: WinData = serde_json::from_value(win_data.json().to_owned())?;
Ok(win_data)
}
pub async fn flight(&self, teams: Vec<Vec<String>>) -> Result<WinData> {
let done_target = self.driver.find(By::Id("done_target")).await?;
let go_btn = self.driver.find(By::ClassName("goBtn")).await?;
let fast_forward_btn = self.driver.find(By::Id("fastBtn")).await?;
let name_input = self.driver.find(By::Id("input_name")).await?;
todo!("flight")
}
pub async fn quit(self) -> Result<()> {
self.driver.quit().await?;
Ok(())
}
}