Skip to content

Commit

Permalink
add CallBack TaskStateType to maa-types, too
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackhr-arch committed Mar 7, 2025
1 parent 62af87c commit 069f318
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 189 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"protoc": {
"options": ["-I","crates/maa-types"]
"options": ["-I","${workspaceRoot}/crates/maa-types"]
}
}
29 changes: 1 addition & 28 deletions crates/maa-server/protos/task.proto
Original file line number Diff line number Diff line change
Expand Up @@ -60,34 +60,7 @@ message TaskId { int32 id = 1; }

message TaskState {
string content = 1;
State state = 2;

enum State {
/* Global Info */
InternalError = 0;
InitFailed = 1;
ConnectionInfo = 2;
AllTasksCompleted = 3;
AsyncCallInfo = 4;
Destroyed = 5;

/* TaskChain Info */
TaskChainError = 6;
TaskChainStart = 7;
TaskChainCompleted = 8;
TaskChainExtraInfo = 9;
TaskChainStopped = 10;

/* SubTask Info */
SubTaskError = 11;
SubTaskStart = 12;
SubTaskCompleted = 13;
SubTaskExtraInfo = 14;
SubTaskStopped = 15;

/* Unknown */
Unknown = 16;
}
types.TaskStateType state = 2;
}

message LogArray {
Expand Down
95 changes: 7 additions & 88 deletions crates/maa-server/src/callback.rs
Original file line number Diff line number Diff line change
@@ -1,85 +1,4 @@
use maa_types::primitive::{AsstMsgId, AsstTaskId as TaskId};

#[repr(i32)]
#[derive(Debug, Clone, Copy)]
pub enum AsstMsg {
/* Global Info */
InternalError = 0,
InitFailed = 1,
ConnectionInfo = 2,
AllTasksCompleted = 3,
AsyncCallInfo = 4,
Destroyed = 5,

/* TaskChain Info */
TaskChainError = 10000,
TaskChainStart = 10001,
TaskChainCompleted = 10002,
TaskChainExtraInfo = 10003,
TaskChainStopped = 10004,

/* SubTask Info */
SubTaskError = 20000,
SubTaskStart = 20001,
SubTaskCompleted = 20002,
SubTaskExtraInfo = 20003,
SubTaskStopped = 20004,

/* Unknown */
Unknown = -1,
}

impl From<AsstMsgId> for AsstMsg {
fn from(msg: AsstMsgId) -> Self {
match msg {
0 => AsstMsg::InternalError,
1 => AsstMsg::InitFailed,
2 => AsstMsg::ConnectionInfo,
3 => AsstMsg::AllTasksCompleted,
4 => AsstMsg::AsyncCallInfo,
5 => AsstMsg::Destroyed,

10000 => AsstMsg::TaskChainError,
10001 => AsstMsg::TaskChainStart,
10002 => AsstMsg::TaskChainCompleted,
10003 => AsstMsg::TaskChainExtraInfo,
10004 => AsstMsg::TaskChainStopped,

20000 => AsstMsg::SubTaskError,
20001 => AsstMsg::SubTaskStart,
20002 => AsstMsg::SubTaskCompleted,
20003 => AsstMsg::SubTaskExtraInfo,
20004 => AsstMsg::SubTaskStopped,

_ => AsstMsg::Unknown,
}
}
}

impl From<AsstMsg> for crate::task::task_state::State {
fn from(value: AsstMsg) -> Self {
use crate::task::task_state::State::*;
match value {
AsstMsg::InternalError => InternalError,
AsstMsg::InitFailed => InitFailed,
AsstMsg::ConnectionInfo => ConnectionInfo,
AsstMsg::AllTasksCompleted => AllTasksCompleted,
AsstMsg::AsyncCallInfo => AsyncCallInfo,
AsstMsg::Destroyed => Destroyed,
AsstMsg::TaskChainError => TaskChainError,
AsstMsg::TaskChainStart => TaskChainStart,
AsstMsg::TaskChainCompleted => TaskChainCompleted,
AsstMsg::TaskChainExtraInfo => TaskChainExtraInfo,
AsstMsg::TaskChainStopped => TaskChainStopped,
AsstMsg::SubTaskError => SubTaskError,
AsstMsg::SubTaskStart => SubTaskStart,
AsstMsg::SubTaskCompleted => SubTaskCompleted,
AsstMsg::SubTaskExtraInfo => SubTaskExtraInfo,
AsstMsg::SubTaskStopped => SubTaskStopped,
AsstMsg::Unknown => Unknown,
}
}
}
use maa_types::{primitive::AsstTaskId as TaskId, TaskStateType};

use tracing::{debug, error, info, trace, warn};

Expand All @@ -91,7 +10,7 @@ use crate::{
type Map = serde_json::Map<String, serde_json::Value>;

#[tracing::instrument("C CallBack", skip_all)]
pub fn main(code: AsstMsg, json_str: &str, session_id: SessionID) {
pub fn main(code: TaskStateType, json_str: &str, session_id: SessionID) {
trace!("Session ID: {:?}", session_id);

Session::log(session_id).log((code, json_str.to_string()));
Expand All @@ -108,8 +27,8 @@ pub fn main(code: AsstMsg, json_str: &str, session_id: SessionID) {
}
}

fn process_message(code: AsstMsg, message: Map, session_id: SessionID) -> Option<()> {
use AsstMsg::*;
fn process_message(code: TaskStateType, message: Map, session_id: SessionID) -> Option<()> {
use TaskStateType::*;

match code {
InternalError => Some(()),
Expand Down Expand Up @@ -211,7 +130,7 @@ fn process_connection_info(message: Map, session_id: SessionID) -> Option<()> {
Some(())
}

fn process_taskchain(code: AsstMsg, message: Map, session_id: SessionID) -> Option<()> {
fn process_taskchain(code: TaskStateType, message: Map, session_id: SessionID) -> Option<()> {
#[derive(serde::Deserialize)]
struct TaskChain {
taskchain: maa_types::TaskType,
Expand All @@ -222,7 +141,7 @@ fn process_taskchain(code: AsstMsg, message: Map, session_id: SessionID) -> Opti
serde_json::from_value(serde_json::Value::Object(message)).unwrap();
Session::tasks(session_id).update(taskid, (code, msg));

use AsstMsg::*;
use TaskStateType::*;
match code {
TaskChainStart => {
info!("{} {}", taskchain, "Start");
Expand Down Expand Up @@ -251,7 +170,7 @@ fn process_taskchain(code: AsstMsg, message: Map, session_id: SessionID) -> Opti
mod subtask {
use super::*;

pub fn process_subtask(code: AsstMsg, message: Map, session_id: SessionID) -> Option<()> {
pub fn process_subtask(code: TaskStateType, message: Map, session_id: SessionID) -> Option<()> {
let msg = serde_json::to_string_pretty(&message).unwrap();
let taskid = message.get("taskid")?.as_i64()? as TaskId;
Session::tasks(session_id).update(taskid, (code, msg));
Expand Down
58 changes: 0 additions & 58 deletions crates/maa-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,70 +21,12 @@ pub mod task {
Self { id }
}
}

// impl From<TaskType> for MaaTaskType {
// fn from(value: TaskType) -> Self {
// match value {
// TaskType::StartUp => MaaTaskType::StartUp,
// TaskType::CloseDown => MaaTaskType::CloseDown,
// TaskType::Fight => MaaTaskType::Fight,
// TaskType::Recruit => MaaTaskType::Recruit,
// TaskType::Infrast => MaaTaskType::Infrast,
// TaskType::Mall => MaaTaskType::Mall,
// TaskType::Award => MaaTaskType::Award,
// TaskType::Roguelike => MaaTaskType::Roguelike,
// TaskType::Copilot => MaaTaskType::Copilot,
// TaskType::SssCopilot => MaaTaskType::SSSCopilot,
// TaskType::Depot => MaaTaskType::Depot,
// TaskType::OperBox => MaaTaskType::OperBox,
// TaskType::Reclamation => MaaTaskType::Reclamation,
// TaskType::Custom => MaaTaskType::Custom,
// TaskType::SingleStep => MaaTaskType::SingleStep,
// TaskType::VideoRecognition => MaaTaskType::VideoRecognition,
// }
// }
// }

// impl From<MaaTaskType> for TaskType {
// fn from(value: MaaTaskType) -> Self {
// match value {
// MaaTaskType::StartUp => TaskType::StartUp,
// MaaTaskType::CloseDown => TaskType::CloseDown,
// MaaTaskType::Fight => TaskType::Fight,
// MaaTaskType::Recruit => TaskType::Recruit,
// MaaTaskType::Infrast => TaskType::Infrast,
// MaaTaskType::Mall => TaskType::Mall,
// MaaTaskType::Award => TaskType::Award,
// MaaTaskType::Roguelike => TaskType::Roguelike,
// MaaTaskType::Copilot => TaskType::Copilot,
// MaaTaskType::SSSCopilot => TaskType::SssCopilot,
// MaaTaskType::Depot => TaskType::Depot,
// MaaTaskType::OperBox => TaskType::OperBox,
// MaaTaskType::Reclamation => TaskType::Reclamation,
// MaaTaskType::Custom => TaskType::Custom,
// MaaTaskType::SingleStep => TaskType::SingleStep,
// MaaTaskType::VideoRecognition => TaskType::VideoRecognition,
// }
// }
// }
}

mod utils {
use super::*;
use maa_types::TouchMode;

// impl TouchMode {
// /// Convert TouchMode to a static string slice
// pub const fn to_str(self) -> &'static str {
// match self {
// TouchMode::Adb => "adb",
// TouchMode::MiniTouch => "minitouch",
// TouchMode::MaaTouch => "maatouch",
// TouchMode::MacPlayTools => "MacPlayTools",
// }
// }
// }

impl new_connection_request::InstanceOptions {
pub fn apply_to(self, asst: &maa_sys::Assistant) -> Result<(), String> {
use maa_sys::InstanceOptionKey;
Expand Down
9 changes: 4 additions & 5 deletions crates/maa-server/src/server_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ unsafe extern "C" fn default_callback(
json_str: *const std::ffi::c_char,
session_id: *mut std::ffi::c_void,
) {
use crate::{
callback::{main, AsstMsg},
types::SessionID,
};
let code: AsstMsg = code.into();
use crate::{callback::main, types::SessionID};
use maa_types::TaskStateType;

let code: TaskStateType = code.try_into().unwrap();
let json_str = unsafe { std::ffi::CStr::from_ptr(json_str).to_str().unwrap() };
let session_id: SessionID = unsafe {
let mut raw = [0u8; 16];
Expand Down
7 changes: 3 additions & 4 deletions crates/maa-server/src/server_impl/task.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
session::Session,
task::{task_server::TaskServer, *,},
task::{task_server::TaskServer, *},
tonic::{self, Request, Response},
types::SessionID,
};
Expand Down Expand Up @@ -186,7 +186,6 @@ impl task_server::Task for TaskImpl {
let session_id = meta.get_session_id()?;

let task_type: TaskType = task_type.try_into().unwrap();
let task_type: maa_types::TaskType = task_type.into();

let ret = func_with(session_id, |handler| {
handler.append_task(task_type, task_params.as_str())
Expand Down Expand Up @@ -305,7 +304,7 @@ impl task_server::Task for TaskImpl {
tokio_stream::wrappers::UnboundedReceiverStream::new(rx).map(|(state, log)| {
Ok(TaskState {
content: log,
state: task_state::State::from(state).into(),
state: state.into(),
})
});

Expand All @@ -325,7 +324,7 @@ impl task_server::Task for TaskImpl {
.into_iter()
.map(|(state, log)| TaskState {
content: log,
state: task_state::State::from(state).into(),
state: state.into(),
})
.collect(),
}))
Expand Down
8 changes: 3 additions & 5 deletions crates/maa-server/src/session.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use crate::{
callback::AsstMsg,
types::{SessionID, TaskId},
};
use crate::types::{SessionID, TaskId};
use maa_types::TaskStateType;
use parking_lot::RwLock;
use std::collections::BTreeMap;
use tokio::sync::oneshot::Sender;

static SESSION_POOL: RwLock<BTreeMap<SessionID, _Session>> = RwLock::new(BTreeMap::new());

type LogContent = (AsstMsg, String);
type LogContent = (TaskStateType, String);
type CallBackContent = String;

// re-export
Expand Down
31 changes: 31 additions & 0 deletions crates/maa-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,37 @@ impl std::fmt::Display for TaskType {
}
}

/// CallBack: Todo
#[repr(i32)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "prost", derive(prost::Enumeration))]
pub enum TaskStateType {
/* Global Info */
InternalError = 0,
InitFailed = 1,
ConnectionInfo = 2,
AllTasksCompleted = 3,
AsyncCallInfo = 4,
Destroyed = 5,

/* TaskChain Info */
TaskChainError = 10000,
TaskChainStart = 10001,
TaskChainCompleted = 10002,
TaskChainExtraInfo = 10003,
TaskChainStopped = 10004,

/* SubTask Info */
SubTaskError = 20000,
SubTaskStart = 20001,
SubTaskCompleted = 20002,
SubTaskExtraInfo = 20003,
SubTaskStopped = 20004,

/* Unknown */
Unknown = -1,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
27 changes: 27 additions & 0 deletions crates/maa-types/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,31 @@ enum TaskType {
Custom = 13;
SingleStep = 14;
VideoRecognition = 15;
}

enum TaskStateType {
/* Global Info */
InternalError = 0;
InitFailed = 1;
ConnectionInfo = 2;
AllTasksCompleted = 3;
AsyncCallInfo = 4;
Destroyed = 5;

/* TaskChain Info */
TaskChainError = 6;
TaskChainStart = 7;
TaskChainCompleted = 8;
TaskChainExtraInfo = 9;
TaskChainStopped = 10;

/* SubTask Info */
SubTaskError = 11;
SubTaskStart = 12;
SubTaskCompleted = 13;
SubTaskExtraInfo = 14;
SubTaskStopped = 15;

/* Unknown */
Unknown = 16;
}

0 comments on commit 069f318

Please sign in to comment.