Skip to content

Commit

Permalink
Supports Rust Alpha SDK (googleforgames#1717)
Browse files Browse the repository at this point in the history
* Supports Rust Alpha SDK

Co-authored-by: Mark Mandel <[email protected]>
  • Loading branch information
2 people authored and ilkercelikyilmaz committed Oct 23, 2020
1 parent 14cf9be commit 5a0fc51
Show file tree
Hide file tree
Showing 10 changed files with 1,464 additions and 4 deletions.
12 changes: 10 additions & 2 deletions build/build-sdk-images/rust/gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ cd /go/src/agones.dev/agones
protoc \
-I ${googleapis} -I ${sdk} sdk.proto \
--rust_out=sdks/rust/src/grpc --grpc_out=sdks/rust/src/grpc \
--plugin=protoc-gen-grpc=`which grpc_rust_plugin` \
--plugin=protoc-gen-grpc=`which grpc_rust_plugin`
protoc \
-I ${googleapis} -I ${sdk}/alpha alpha.proto \
--rust_out=sdks/rust/src/grpc --grpc_out=sdks/rust/src/grpc \
--plugin=protoc-gen-grpc=`which grpc_rust_plugin`

cat ./build/boilerplate.go.txt ./sdks/rust/src/grpc/sdk.rs >> ./sdk.rs
cat ./build/boilerplate.go.txt ./sdks/rust/src/grpc/sdk_grpc.rs >> ./sdk_grpc.rs
cat ./build/boilerplate.go.txt ./sdks/rust/src/grpc/alpha.rs >> ./alpha.rs
cat ./build/boilerplate.go.txt ./sdks/rust/src/grpc/alpha_grpc.rs >> ./alpha_grpc.rs
mv ./sdk.rs ./sdks/rust/src/grpc/
mv ./sdk_grpc.rs ./sdks/rust/src/grpc/
mv ./sdk_grpc.rs ./sdks/rust/src/grpc/
mv ./alpha.rs ./sdks/rust/src/grpc/
mv ./alpha_grpc.rs ./sdks/rust/src/grpc/
5 changes: 4 additions & 1 deletion build/includes/sdk.mk
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ run-sdk-conformance-test-go:
$(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)

run-sdk-conformance-test-rust:
$(MAKE) run-sdk-conformance-test SDK_FOLDER=rust
# run without feature flags
$(MAKE) run-sdk-conformance-test SDK_FOLDER=rust GRPC_PORT=9004 HTTP_PORT=9104
# run with feature flags enabled
$(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)

run-sdk-conformance-test-rest:
# run without feature flags
Expand Down
114 changes: 114 additions & 0 deletions sdks/rust/src/alpha.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright 2020 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::Arc;

use grpcio;

use errors::*;
use grpc::alpha;
use grpc::alpha_grpc;

/// Alpha is an instance of the Agones Alpha SDK
pub struct Alpha {
client: Arc<alpha_grpc::SdkClient>,
}

impl Alpha {
/// new creates a new instance of the Alpha SDK
pub fn new(ch: grpcio::Channel) -> Alpha {
let cli = alpha_grpc::SdkClient::new(ch);
Alpha {
client: Arc::new(cli),
}
}

/// This returns the last player capacity that was set through the SDK.
/// If the player capacity is set from outside the SDK, use sdk.get_gameserver() instead.
pub fn get_player_capacity(&self) -> Result<i64> {
let req = alpha::Empty::new();
let count = self.client.get_player_capacity(&req).map(|c| c.count)?;
Ok(count)
}

/// This changes the player capacity to a new value.
pub fn set_player_capacity(&self, capacity: i64) -> Result<()> {
let mut c = alpha::Count::new();
c.set_count(capacity);
let res = self.client.set_player_capacity(&c).map(|_| ())?;
Ok(res)
}

/// This function increases the SDK’s stored player count by one, and appends this playerID to GameServer.status.players.ids.
/// Returns true and adds the playerID to the list of playerIDs if the playerIDs was not already in the list of connected playerIDs.
pub fn player_connect<S>(&self, id: S) -> Result<bool>
where
S: Into<String>,
{
let mut p = alpha::PlayerID::new();
p.set_playerID(id.into());
let res = self.client.player_connect(&p).map(|b| b.bool)?;
Ok(res)
}

/// This function decreases the SDK’s stored player count by one, and removes the playerID from GameServer.status.players.ids.
/// Will return true and remove the supplied playerID from the list of connected playerIDs if the playerID value exists within the list.
pub fn player_disconnect<S>(&self, id: S) -> Result<bool>
where
S: Into<String>,
{
let mut p = alpha::PlayerID::new();
p.set_playerID(id.into());
let res = self.client.player_disconnect(&p).map(|b| b.bool)?;
Ok(res)
}

/// Returns the current player count.
pub fn get_player_count(&self) -> Result<i64> {
let req = alpha::Empty::new();
let count = self.client.get_player_count(&req).map(|c| c.count)?;
Ok(count)
}

/// This returns if the playerID is currently connected to the GameServer.
/// This is always accurate, even if the value hasn’t been updated to the GameServer status yet.
pub fn is_player_connected<S>(&self, id: S) -> Result<bool>
where
S: Into<String>,
{
let mut p = alpha::PlayerID::new();
p.set_playerID(id.into());
let res = self.client.is_player_connected(&p).map(|b| b.bool)?;
Ok(res)
}

/// This returns the list of the currently connected player ids.
/// This is always accurate, even if the value has not been updated to the Game Server status yet.
pub fn get_connected_players(&self) -> Result<Vec<String>> {
let req = alpha::Empty::new();
let res = self
.client
.get_connected_players(&req)
.map(|pl| pl.list.into())?;
Ok(res)
}
}

impl Clone for Alpha {
fn clone(&self) -> Self {
Self {
client: Arc::clone(&self.client),
}
}
}
Loading

0 comments on commit 5a0fc51

Please sign in to comment.