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