Skip to content

Commit 73844a2

Browse files
committed
feat:update multi
1 parent a86e4c0 commit 73844a2

File tree

11 files changed

+54
-28
lines changed

11 files changed

+54
-28
lines changed

gdrust/Cargo.lock

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gdrust/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ derive = { path = "./derive" }
1111
proto = { path = "proto" }
1212
tokio = "1"
1313
anyhow = "1"
14+
base = { path = "base" }
15+
prost = "0"
16+
prost-types = { version = "0", optional = true }
17+
1418

1519
[lib]
1620
crate-type = ["cdylib"]
@@ -21,4 +25,4 @@ codegen-units = 1
2125
strip = true
2226

2327
[workspace]
24-
members = [".", "derive", "proto", "server"]
28+
members = [".", "base", "derive", "proto", "server"]

gdrust/base/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "base"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
shadow-rs = "0"
8+
9+
[build-dependencies]
10+
shadow-rs = "0"
File renamed without changes.

gdrust/base/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
shadow_rs::shadow!(build);

gdrust/proto/proto/connect.proto

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package connect;
33

44
message Join {
55
string player_name = 1;
6-
int32 version = 2;
6+
// Commit Hash as the version
7+
string version = 2;
78
}
89

910
enum Status {
@@ -13,5 +14,5 @@ enum Status {
1314

1415
message Response {
1516
Status status = 1;
16-
string message = 2;
17+
string message = 2;
1718
}

gdrust/server/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ edition = "2021"
77
tokio = { version = "1", features = ["full"] }
88
bytes = "1"
99
clap = { version = "4", features = ["derive"] }
10-
shadow-rs = "0"
1110
log = "0"
1211
env_logger = "0"
1312
anyhow = "1"
1413
prost = "0"
1514
prost-types = { version = "0", optional = true }
1615
proto = { path = "../proto" }
17-
18-
[build-dependencies]
19-
shadow-rs = "0"
16+
base = { path = "../base" }

gdrust/server/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl Connection {
5858
#[derive(Debug, Parser)]
5959
#[command(
6060
author = "SkyUOI",
61-
version = crate::build::VERSION,
61+
version = base::build::VERSION,
6262
about = "The Server Of Zenith",
6363
long_about = "Multi-player Game Server Of Zenith"
6464
)]
@@ -70,8 +70,6 @@ struct ArgsParser {
7070
ip: String,
7171
}
7272

73-
shadow_rs::shadow!(build);
74-
7573
async fn process_request(mut connect: Connection) -> anyhow::Result<()> {
7674
// 首先获取连接请求
7775
let join_data = connect.read_join().await?;

gdrust/src/multi.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
use anyhow::Ok;
22
use godot::engine::{INode, Node};
33
use godot::prelude::*;
4+
use proto::connect::Join;
45
use std::collections::HashMap;
56
use std::sync::{Arc, Mutex};
67
use tokio::net::TcpStream;
78
use tokio::runtime::{Builder, Runtime};
89

910
use crate::get_multi_single;
1011

11-
pub struct MultiPlayer {}
12+
pub struct MultiPlayerConnection {}
13+
14+
impl MultiPlayerConnection {}
1215

1316
pub struct MultiManagerImpl {
14-
clients: HashMap<usize, MultiPlayer>,
17+
clients: HashMap<usize, MultiPlayerConnection>,
1518
socket: Option<TcpStream>,
1619
runtime: Runtime,
1720
}
1821

1922
impl MultiManagerImpl {
20-
fn set_connection(&mut self, socket: TcpStream) {
23+
pub fn connect_to_server(&mut self, ip: String) -> anyhow::Result<()> {
2124
if self.socket.is_some() {
2225
godot_warn!("Socket has value,but reset")
2326
}
24-
self.socket = Some(socket)
27+
self.socket = Some(TcpStream::from_std(std::net::TcpStream::connect(&ip)?)?);
28+
Ok(())
2529
}
2630

27-
pub fn connect_to_server(&mut self, ip: &str) -> anyhow::Result<()> {
31+
pub fn join_to_server(&mut self, player_name: String) -> anyhow::Result<()> {
32+
let mut data = Join {
33+
player_name,
34+
version: base::build::COMMIT_HASH.to_string(),
35+
};
2836
Ok(())
2937
}
3038
}

gdrust/src/ui/multi_enter.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::debug_check;
1+
use crate::{debug_check, get_multi_single};
22
use derive::gen_debug;
33
use godot::engine::{AcceptDialog, Button, IButton};
44
use godot::obj::WithBaseField;
@@ -28,16 +28,15 @@ impl IButton for MultiEnter {
2828
impl MultiEnter {
2929
#[func]
3030
fn connect_to_server(&mut self, ip: String) -> bool {
31-
let global = self.base().get_node_as::<Node>("/root/Global");
32-
let ret = true;
33-
// (self.socket, ret) = match TcpStream::connect(&ip) {
34-
// Ok(socket) => (Some(socket), true),
35-
// Err(error) => {
36-
// self.show_dialog(format!("Connect failed:{}", error));
37-
// (None, false)
38-
// }
39-
// };
40-
ret
31+
let mult_manager = get_multi_single();
32+
let mut lock = mult_manager.lock().unwrap();
33+
match lock.connect_to_server(ip) {
34+
Ok(_) => true,
35+
Err(err) => {
36+
self.show_dialog(err.to_string());
37+
false
38+
}
39+
}
4140
}
4241

4342
#[debug]

project.godot

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ slow_down={
5757

5858
[internationalization]
5959

60-
locale/translations=PackedStringArray("res://locales/zh_cn.po")
6160
locale/translations_pot_files=PackedStringArray("res://scenes/multi_game.tscn")
6261

6362
[layer_names]

0 commit comments

Comments
 (0)