Skip to content

Commit 998cd3e

Browse files
committed
feat:add monitor
1 parent 31cfea4 commit 998cd3e

File tree

10 files changed

+98
-33
lines changed

10 files changed

+98
-33
lines changed

gdrust/proto/proto/connect.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ message Response {
1616
Status status = 1;
1717
string message = 2;
1818
}
19+
20+
message CreateObj { string path = 1; }

gdrust/proto/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
pub mod proto;
22

33
pub use proto::connect;
4+
use proto::connect::{CreateObj, Join};
5+
6+
pub enum ProtoRequest {
7+
Join(Join),
8+
CreateObj(CreateObj),
9+
}

gdrust/server/src/lib.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bytes::BytesMut;
55
use cfg::{DEFAULT_IP, DEFAULT_PORT};
66
use clap::Parser;
77
use prost::Message;
8-
use proto::connect::Join;
8+
use proto::{connect::Join, ProtoRequest};
99
use std::{
1010
error::Error,
1111
io::{Cursor, Write},
@@ -39,6 +39,10 @@ impl Connection {
3939
log::info!("Exit");
4040
return Err(anyhow!("Exit"));
4141
}
42+
if let Some(request) = self.parse_join()? {
43+
log::info!("Join exiting...");
44+
return Ok(Some(request));
45+
}
4246
let sz = self.stream.read_buf(&mut self.buffer).await?;
4347
log::info!("Received:{}", sz);
4448
if 0 == sz {
@@ -51,10 +55,6 @@ impl Connection {
5155
return Err(anyhow!(msg));
5256
}
5357
}
54-
if let Some(request) = self.parse_join()? {
55-
log::info!("Join exiting...");
56-
return Ok(Some(request));
57-
}
5858
}
5959
}
6060

@@ -65,6 +65,33 @@ impl Connection {
6565
Err(_) => Err(anyhow!("Not a join message")),
6666
}
6767
}
68+
69+
pub async fn process_request(&mut self) -> anyhow::Result<()> {
70+
loop {
71+
if self.shutdown.try_recv().is_ok() {
72+
return Err(anyhow!("Exit"));
73+
}
74+
if let Some(request) = self.parse_request()? {}
75+
let sz = self.stream.read_buf(&mut self.buffer).await?;
76+
if 0 == sz {
77+
if self.buffer.is_empty() {
78+
return Ok(());
79+
} else {
80+
let msg = "Connection reset by peer";
81+
log::info!("{}", msg);
82+
return Err(anyhow!(msg));
83+
}
84+
}
85+
}
86+
}
87+
88+
pub fn parse_request(&mut self) -> anyhow::Result<Option<ProtoRequest>> {
89+
let buf = Cursor::new(&self.buffer[..]);
90+
match proto::connect::CreateObj::decode(buf) {
91+
Ok(data) => Ok(Some(ProtoRequest::CreateObj(data))),
92+
Err(_) => Err(anyhow!("Not a message")),
93+
}
94+
}
6895
}
6996

7097
#[derive(Debug, Parser)]
@@ -87,13 +114,7 @@ async fn process_request(mut connect: Connection) -> anyhow::Result<()> {
87114
log::info!("start joining");
88115
let join_data = connect.read_join().await?;
89116
log::info!("joined");
90-
let request = async { loop {} };
91-
select! {
92-
_ = request => {},
93-
_ = connect.shutdown.recv() => {
94-
log::info!("Player exited")
95-
}
96-
}
117+
connect.process_request().await?;
97118
Ok(())
98119
}
99120

gdrust/src/multi.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use ::proto::ProtoRequest;
12
use anyhow::anyhow;
23
use bytes::{Bytes, BytesMut};
34
use godot::engine::{INode, Node};
@@ -18,7 +19,7 @@ pub struct MultiPlayerConnection {}
1819
impl MultiPlayerConnection {}
1920

2021
enum Requests {
21-
Join(Join),
22+
Proto(ProtoRequest),
2223
Wrong(String),
2324
ExitSuccess,
2425
}
@@ -75,7 +76,7 @@ async fn read_loop(
7576

7677
fn parse_request(buf: &BytesMut) -> Option<Requests> {
7778
match proto::connect::Join::decode(&buf[..]) {
78-
Ok(v) => Some(Requests::Join(v)),
79+
Ok(v) => Some(Requests::Proto(ProtoRequest::Join(v))),
7980
Err(_) => None,
8081
}
8182
}

gdrust/src/weapons.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
mod enchanted_sword;
12
mod star_wrath;
3+

gdrust/src/weapons/enchanted_sword.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use godot::{
2+
engine::{ISprite2D, Sprite2D},
3+
prelude::*,
4+
};
5+
use std::collections::HashMap;
6+
7+
#[derive(GodotClass)]
8+
#[class(init, base = Sprite2D)]
9+
struct EnchantedSword {
10+
properties: HashMap<String, Callable>,
11+
base: Base<Sprite2D>,
12+
}
13+
14+
#[godot_api]
15+
impl ISprite2D for EnchantedSword {
16+
fn get_property(&self, name: StringName) -> Option<Variant> {
17+
match self.properties.get(&name.to_string()) {
18+
None => {}
19+
Some(val) => {
20+
val.callv((&[self.base().to_variant()]).into());
21+
}
22+
}
23+
None
24+
}
25+
}
26+
27+
#[godot_api]
28+
impl EnchantedSword {
29+
#[func]
30+
/// 设置要检测的属性
31+
fn set_monitor(&mut self, property_name: String, func: Callable) {
32+
self.properties.insert(property_name, func);
33+
}
34+
}

scenes/weapons/enchanted_sword.tscn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[ext_resource type="Script" path="res://scripts/weapons/enchanted_sword.gd" id="2_h56vq"]
55
[ext_resource type="PackedScene" uid="uid://bi0hwjyalpjn6" path="res://scenes/bullets/enchanted_beam.tscn" id="3_ndj1h"]
66

7-
[node name="EnchantedSword" type="Sprite2D"]
7+
[node name="EnchantedSword" type="EnchantedSword"]
88
z_index = 1
99
position = Vector2(574, 291)
1010
rotation = -5.57284

scenes/weapons/star_wrath.tscn

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,21 @@ libraries = {
116116
[node name="AnimationTree" type="AnimationTree" parent="."]
117117
tree_root = SubResource("AnimationNodeBlendTree_rpkys")
118118
anim_player = NodePath("../AnimationPlayer")
119-
parameters/current_length = null
120-
parameters/current_position = null
121-
parameters/current_delta = null
122-
parameters/Animation/current_length = null
123-
parameters/Animation/current_position = null
124-
parameters/Animation/current_delta = null
125-
"parameters/Animation 2/current_length" = null
126-
"parameters/Animation 2/current_position" = null
127-
"parameters/Animation 2/current_delta" = null
128-
parameters/Blend2/current_length = 0
129-
parameters/Blend2/current_position = 0
130-
parameters/Blend2/current_delta = 0
119+
parameters/current_length = 1.5
120+
parameters/current_position = 0.449277
121+
parameters/current_delta = 0.0166667
122+
parameters/Animation/current_length = 1.5
123+
parameters/Animation/current_position = 0.449277
124+
parameters/Animation/current_delta = 0.0166667
125+
"parameters/Animation 2/current_length" = 1.0
126+
"parameters/Animation 2/current_position" = 1.0
127+
"parameters/Animation 2/current_delta" = 0.0
128+
parameters/Blend2/current_length = 1.5
129+
parameters/Blend2/current_position = 0.449277
130+
parameters/Blend2/current_delta = 0.0166667
131131
parameters/Blend2/blend_amount = 0.5
132-
parameters/output/current_length = null
133-
parameters/output/current_position = null
134-
parameters/output/current_delta = null
132+
parameters/output/current_length = 1.5
133+
parameters/output/current_position = 0.449277
134+
parameters/output/current_delta = 0.0166667
135135

136136
[connection signal="timeout" from="FightTimer" to="." method="on_fight_timer_timeout"]

scripts/global.gd

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
#extends GlobalClass
2-
extends Node
1+
extends GlobalClass

scripts/weapons/enchanted_sword.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
extends Sprite2D
1+
extends EnchantedSword
22

33
var finished: bool
44

0 commit comments

Comments
 (0)