Skip to content

Commit b320b49

Browse files
yoshdmarkmandel
andauthored
Supports Rust Alpha SDK (agones-dev#1717)
* Supports Rust Alpha SDK Co-authored-by: Mark Mandel <markmandel@google.com>
1 parent f678d49 commit b320b49

10 files changed

Lines changed: 1464 additions & 4 deletions

File tree

‎build/build-sdk-images/rust/gen.sh‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@ cd /go/src/agones.dev/agones
2424
protoc \
2525
-I ${googleapis} -I ${sdk} sdk.proto \
2626
--rust_out=sdks/rust/src/grpc --grpc_out=sdks/rust/src/grpc \
27-
--plugin=protoc-gen-grpc=`which grpc_rust_plugin` \
27+
--plugin=protoc-gen-grpc=`which grpc_rust_plugin`
28+
protoc \
29+
-I ${googleapis} -I ${sdk}/alpha alpha.proto \
30+
--rust_out=sdks/rust/src/grpc --grpc_out=sdks/rust/src/grpc \
31+
--plugin=protoc-gen-grpc=`which grpc_rust_plugin`
2832

2933
cat ./build/boilerplate.go.txt ./sdks/rust/src/grpc/sdk.rs >> ./sdk.rs
3034
cat ./build/boilerplate.go.txt ./sdks/rust/src/grpc/sdk_grpc.rs >> ./sdk_grpc.rs
35+
cat ./build/boilerplate.go.txt ./sdks/rust/src/grpc/alpha.rs >> ./alpha.rs
36+
cat ./build/boilerplate.go.txt ./sdks/rust/src/grpc/alpha_grpc.rs >> ./alpha_grpc.rs
3137
mv ./sdk.rs ./sdks/rust/src/grpc/
32-
mv ./sdk_grpc.rs ./sdks/rust/src/grpc/
38+
mv ./sdk_grpc.rs ./sdks/rust/src/grpc/
39+
mv ./alpha.rs ./sdks/rust/src/grpc/
40+
mv ./alpha_grpc.rs ./sdks/rust/src/grpc/

‎build/includes/sdk.mk‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ run-sdk-conformance-test-go:
165165
$(MAKE) run-sdk-conformance-no-build SDK_FOLDER=go GRPC_PORT=9001 HTTP_PORT=9101 FEATURE_GATES=PlayerTracking=true TESTS=$(DEFAULT_CONFORMANCE_TESTS),$(ALPHA_CONFORMANCE_TESTS)
166166

167167
run-sdk-conformance-test-rust:
168-
$(MAKE) run-sdk-conformance-test SDK_FOLDER=rust
168+
# run without feature flags
169+
$(MAKE) run-sdk-conformance-test SDK_FOLDER=rust GRPC_PORT=9004 HTTP_PORT=9104
170+
# run with feature flags enabled
171+
$(MAKE) run-sdk-conformance-test SDK_FOLDER=rust GRPC_PORT=9004 HTTP_PORT=9104 FEATURE_GATES=PlayerTracking=true TESTS=$(DEFAULT_CONFORMANCE_TESTS),$(ALPHA_CONFORMANCE_TESTS)
169172

170173
run-sdk-conformance-test-rest:
171174
# run without feature flags

‎sdks/rust/src/alpha.rs‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright 2020 Google LLC All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::sync::Arc;
16+
17+
use grpcio;
18+
19+
use errors::*;
20+
use grpc::alpha;
21+
use grpc::alpha_grpc;
22+
23+
/// Alpha is an instance of the Agones Alpha SDK
24+
pub struct Alpha {
25+
client: Arc<alpha_grpc::SdkClient>,
26+
}
27+
28+
impl Alpha {
29+
/// new creates a new instance of the Alpha SDK
30+
pub fn new(ch: grpcio::Channel) -> Alpha {
31+
let cli = alpha_grpc::SdkClient::new(ch);
32+
Alpha {
33+
client: Arc::new(cli),
34+
}
35+
}
36+
37+
/// This returns the last player capacity that was set through the SDK.
38+
/// If the player capacity is set from outside the SDK, use sdk.get_gameserver() instead.
39+
pub fn get_player_capacity(&self) -> Result<i64> {
40+
let req = alpha::Empty::new();
41+
let count = self.client.get_player_capacity(&req).map(|c| c.count)?;
42+
Ok(count)
43+
}
44+
45+
/// This changes the player capacity to a new value.
46+
pub fn set_player_capacity(&self, capacity: i64) -> Result<()> {
47+
let mut c = alpha::Count::new();
48+
c.set_count(capacity);
49+
let res = self.client.set_player_capacity(&c).map(|_| ())?;
50+
Ok(res)
51+
}
52+
53+
/// This function increases the SDK’s stored player count by one, and appends this playerID to GameServer.status.players.ids.
54+
/// Returns true and adds the playerID to the list of playerIDs if the playerIDs was not already in the list of connected playerIDs.
55+
pub fn player_connect<S>(&self, id: S) -> Result<bool>
56+
where
57+
S: Into<String>,
58+
{
59+
let mut p = alpha::PlayerID::new();
60+
p.set_playerID(id.into());
61+
let res = self.client.player_connect(&p).map(|b| b.bool)?;
62+
Ok(res)
63+
}
64+
65+
/// This function decreases the SDK’s stored player count by one, and removes the playerID from GameServer.status.players.ids.
66+
/// Will return true and remove the supplied playerID from the list of connected playerIDs if the playerID value exists within the list.
67+
pub fn player_disconnect<S>(&self, id: S) -> Result<bool>
68+
where
69+
S: Into<String>,
70+
{
71+
let mut p = alpha::PlayerID::new();
72+
p.set_playerID(id.into());
73+
let res = self.client.player_disconnect(&p).map(|b| b.bool)?;
74+
Ok(res)
75+
}
76+
77+
/// Returns the current player count.
78+
pub fn get_player_count(&self) -> Result<i64> {
79+
let req = alpha::Empty::new();
80+
let count = self.client.get_player_count(&req).map(|c| c.count)?;
81+
Ok(count)
82+
}
83+
84+
/// This returns if the playerID is currently connected to the GameServer.
85+
/// This is always accurate, even if the value hasn’t been updated to the GameServer status yet.
86+
pub fn is_player_connected<S>(&self, id: S) -> Result<bool>
87+
where
88+
S: Into<String>,
89+
{
90+
let mut p = alpha::PlayerID::new();
91+
p.set_playerID(id.into());
92+
let res = self.client.is_player_connected(&p).map(|b| b.bool)?;
93+
Ok(res)
94+
}
95+
96+
/// This returns the list of the currently connected player ids.
97+
/// This is always accurate, even if the value has not been updated to the Game Server status yet.
98+
pub fn get_connected_players(&self) -> Result<Vec<String>> {
99+
let req = alpha::Empty::new();
100+
let res = self
101+
.client
102+
.get_connected_players(&req)
103+
.map(|pl| pl.list.into())?;
104+
Ok(res)
105+
}
106+
}
107+
108+
impl Clone for Alpha {
109+
fn clone(&self) -> Self {
110+
Self {
111+
client: Arc::clone(&self.client),
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)