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

Supports Rust Alpha SDK #1717

Merged
merged 5 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
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/
2 changes: 1 addition & 1 deletion build/includes/sdk.mk
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ 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
$(MAKE) run-sdk-conformance-test SDK_FOLDER=rust FEATURE_GATES=PlayerTracking=true TESTS=$(DEFAULT_CONFORMANCE_TESTS),$(ALPHA_CONFORMANCE_TESTS)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably follow what Go is doing - which is running one without the flag, and then one with:

$(MAKE) run-sdk-conformance-test SDK_FOLDER=go GRPC_PORT=9001 HTTP_PORT=9101

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review.
OK. As far as I can see, it seems better not to duplicate ports between tests in each language.
I'll fix it like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it.


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)
markmandel marked this conversation as resolved.
Show resolved Hide resolved
}

/// 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