Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testout ai level6 up #336

Merged
merged 34 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
ad23358
fix: all leveling up to level 4
Aluxray Jun 22, 2024
2129083
feat: leveling up to level 8
Aluxray Jun 22, 2024
ec94311
Merge remote-tracking branch 'origin/main' into testout-ai-level6-up
MartinFillon Jun 22, 2024
1830b2d
fix: missing to check if client is unset and not only gui to be skipped
MartinFillon Jun 22, 2024
ee8bc7a
refactor(ai): make Alexandre requested changes to AI roles
louonezime Jun 22, 2024
ebddb38
fix(ai-incantationers): handle err in loops to not block when server …
louonezime Jun 22, 2024
18defcf
fix(ai): add more control flow for no Zombie tasks
louonezime Jun 22, 2024
2e89bf9
Merge branch 'main' into testout-ai-level6-up
louonezime Jun 22, 2024
fb842f6
fix(ai): error as no response to detect end of connect
louonezime Jun 22, 2024
0be7173
style(ai-queen): remove uneeded commenting
louonezime Jun 22, 2024
dfab043
fix(ai): Queens are now moving towards broadcasts with a 0 to increas…
Aluxray Jun 23, 2024
706a3b8
fix(level_up): Level requirement are requested for nb_players
RahulCHANDER25 Jun 23, 2024
7809960
fix(incantation): Level specifications for ais
RahulCHANDER25 Jun 23, 2024
6db2b32
feat(ai): update from new branch changes
louonezime Jun 23, 2024
6896822
Merge branch 'testout-ai-level6-up' of github.com:MartinFillon/Zappy …
louonezime Jun 23, 2024
05c18cd
style(ai): apply clippy and fmt to code
louonezime Jun 23, 2024
e424961
Merge branch 'main' into testout-ai-level6-up
louonezime Jun 23, 2024
dba698c
chore: remove unuseful debug
MartinFillon Jun 23, 2024
bb6dbb4
fix(Incantation): Ko when 2 incantation at a time
RahulCHANDER25 Jun 23, 2024
502e1fd
refactor: removed dead code and lightened code
Aluxray Jun 23, 2024
63a527f
refactor(ai): handle check response in case server quits
louonezime Jun 23, 2024
079710c
Merge branch 'main' into fix-tcp-res
louonezime Jun 23, 2024
29c92bc
fix(queen): now leveling up consistantly to level 6 and some times le…
Aluxray Jun 23, 2024
cead972
Merge branch 'testout-ai-level6-up' of github.com:MartinFillon/Zappy …
louonezime Jun 23, 2024
9ba65b7
merge(ai): from testout-level6-up to fix-tcp-res
louonezime Jun 23, 2024
30a1fae
Merge branch 'main' of github.com:MartinFillon/Zappy into testout-ai-…
Aluxray Jun 23, 2024
58978e2
fix(ai): queen is not spawned
louonezime Jun 23, 2024
6a0e917
feat(queen): more level up logs
Aluxray Jun 23, 2024
af397d8
Merge branch 'testout-ai-level6-up' of github.com:MartinFillon/Zappy …
Aluxray Jun 23, 2024
d397586
feat(queen): leveling up to level 8 most of the times
Aluxray Jun 23, 2024
edb8f09
fix(ai): wait more frames after fork
louonezime Jun 23, 2024
b731f90
style(ai): apply cargo fmt
louonezime Jun 23, 2024
b796af8
refactor(ai): logging is more relevant
louonezime Jun 23, 2024
ec8404a
style(server): fix L-2 coding style error
louonezime Jun 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ Broadcast <command>\n
| Command | Definition | Sent by |
| -------- | ---------- | ------- |
| `<ID> assign <role> <pID>` | assign role to a new AI (by **default** considered a `Queen`) | `Queen`\|`Knight` |
| `<ID> waiting` | Wait for AI `Queen` to join at same tile | `Queen` |
| `<ID> inc` | Incantating | `Queen` |
| `<ID> mv` | Moved | `Queen` |
| `<ID> lvl <level>` | Notify level | `Queen` |
| `<ID> mv` | Moved | `Queen` |
| `<ID> nf` | Need `food` | `Queen` |
| `<ID> nl` | Need `linemate` | `Queen` |
| `<ID> nd` | Need `deraumere` | `Queen` |
Expand Down
85 changes: 42 additions & 43 deletions ai/src/ai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ fn init_from_broadcast(info: &AI, role: String) -> Result<Box<dyn AIHandler>, St
})
}

async fn init_and_update(ai: AI, role: String) -> io::Result<()> {
let mut rle =
init_from_broadcast(&ai, role).map_err(|e| std::io::Error::new(ErrorKind::NotFound, e))?;

let ai_status = rle.update().await;
if let Err(CommandError::DeadReceived) = ai_status {
info!("~[{}] AI is dead, stopping the process...", ai.cli_id);
return Err(Error::new(ErrorKind::ConnectionAborted, "AI is now dead."));
} else if let Err(e) = ai_status {
error!("~[{}] Error: {}", ai.cli_id, e);
return Err(Error::new(
ErrorKind::ConnectionAborted,
"AI received error from server.",
));
}
Ok(())
}

pub async fn fork_ai(info: AI) -> io::Result<()> {
let client = match handle_tcp(info.address.clone(), info.team.clone(), info.cli_id).await {
Ok(client) => {
Expand Down Expand Up @@ -129,48 +147,31 @@ pub async fn fork_ai(info: AI) -> io::Result<()> {
"~[{}] Handling assignment of role {} with id {}",
info.cli_id, role, p_id
);
let mut rle = init_from_broadcast(&ai, role)
.map_err(|e| std::io::Error::new(ErrorKind::NotFound, e))?;
if let Err(e) = rle.update().await {
println!("~[{}] Error: {}", info.cli_id, e);
}
let ai_clone = ai.clone();
tokio::spawn(async move {
if init_and_update(ai_clone, role).await.is_ok() {
info!(
"~[{}] AI successfully initialized and updated.",
info.cli_id
)
}
});
return Ok(());
}
warn!(
"~[{}] No role assignment detected, turning to idle...",
info.cli_id
);
return Err(Error::new(
ErrorKind::NotFound,
"No role detected, AI is slowly dying.",
));
}
Err(e) => error!("=[{}] {}", info.cli_id, e),
}
Ok(())
}

pub async fn connect_ai(mut ai: AI) -> io::Result<()> {
debug!("~[{}] AI is being checked in...", ai.cli_id);
if let Some((c_id, role, p_id)) = ai.clone().wait_assignment().await {
ai.set_p_id(p_id);
ai.set_cli_id(c_id + 1);
info!(
"~[{}] Handling assignment of role {} with connection id {}",
ai.cli_id,
role,
c_id + 1
);
let mut rle = init_from_broadcast(&ai, role)
.map_err(|e| std::io::Error::new(ErrorKind::NotFound, e))?;
if let Err(e) = rle.update().await {
println!("~[{}] Error: {}", ai.cli_id, e);
}
return Ok(());
}
warn!(
"~[{}] No role assignment detected, turning to idle...",
ai.cli_id
);
Ok(())
}

#[async_trait]
pub trait AIHandler: Send {
fn init(info: AI) -> Self
Expand Down Expand Up @@ -223,7 +224,7 @@ impl AI {
async fn wait_assignment(&mut self) -> Option<(usize, String, usize)> {
let mut client = self.client().lock().await;
let start_time = time::Instant::now();
let overall_timeout = Duration::from_secs(5);
let overall_timeout = Duration::from_secs(1);

loop {
if start_time.elapsed() >= overall_timeout {
Expand Down Expand Up @@ -335,8 +336,7 @@ async fn checkout_ai_info(
1,
client_number,
);
println!("~[{}] New! >> {}", c_id, ai);
debug!("~[{}] AI is initialized.", ai.cli_id);
println!("~[{}] New AI! >> {}", c_id, ai);
ai
})
.map_err(|e: Error| {
Expand All @@ -355,8 +355,8 @@ async fn init_ai(
let ai = checkout_ai_info(client, response, team, address, (c_id, p_id)).await?;
let mut queen = queen::Queen::init(ai.clone());
if let Err(e) = queen.update().await {
println!("QUEEN received error {}", e);
error!("[{}] !!!!Error: {}", queen.info().cli_id, e);
error!("[{}] Error: {}", queen.info().cli_id, e);
println!("[{}] QUEEN received error.", c_id);
}
Ok(ai)
}
Expand All @@ -380,7 +380,7 @@ async fn start_ai(
println!("~[{}] server> {}", c_id, response);
match response.trim_end() {
"ko" => {
debug!("~[{}] Server doesn't handle any more connection.", c_id);
warn!("~[{}] Server doesn't handle any more connection.", c_id);
Err(Error::new(
ErrorKind::ConnectionRefused,
"No room for player.",
Expand All @@ -399,7 +399,7 @@ async fn start_ai(
}
}
} else {
debug!("=[{}] Host not reachable.", c_id);
error!("=[{}] Host not reachable.", c_id);
Err(Error::new(
ErrorKind::ConnectionRefused,
"Couldn't reach host.",
Expand All @@ -416,7 +416,7 @@ pub async fn launch(address: String, team: String) -> io::Result<()> {

loop {
if stop_flag.load(Ordering::SeqCst) {
println!(
warn!(
"=[AT {:?}] Stop flag is set, breaking the loop.",
connection_id
);
Expand All @@ -429,7 +429,6 @@ pub async fn launch(address: String, team: String) -> io::Result<()> {
let stop_flag = Arc::clone(&stop_flag);

let curr_id = connection_id.load(Ordering::SeqCst);
println!("=[{}] Attempting connection...", curr_id);

match handle_tcp(address.to_string(), team.to_string(), curr_id).await {
Ok(client) => {
Expand All @@ -448,11 +447,11 @@ pub async fn launch(address: String, team: String) -> io::Result<()> {

match result {
Ok(_) => {
println!("=[{}] Connection handled successfully", id);
info!("=[{}] Connection handled successfully", id);
Ok(())
}
Err(e) => {
println!("=[{}] Connection failed: {}", id, e);
warn!("=[{}] Connection failed: {}", id, e);
stop_flag.store(true, Ordering::SeqCst);
Err(e)
}
Expand All @@ -468,7 +467,7 @@ pub async fn launch(address: String, team: String) -> io::Result<()> {
}

if handles.is_empty() {
warn!("Connection refused, handles is empty.");
eprintln!("Connection refused, handles is empty.");
return Err(Error::new(
ErrorKind::ConnectionRefused,
"Couldn't reach host.",
Expand All @@ -477,7 +476,7 @@ pub async fn launch(address: String, team: String) -> io::Result<()> {

for (id, handle) in handles.into_iter().enumerate() {
if let Err(e) = handle.await {
println!("=[{}] Task failed: {:?}", id, e);
error!("=[{}] Task failed: {:?}", id, e);
}
}

Expand Down
Loading