agones.dev/agones@v1.54.0/sdks/unity/AgonesAlphaSdk.cs (about) 1 // Copyright 2022 Google LLC 2 // All Rights Reserved. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 using System; 17 using System.Collections.Generic; 18 using System.Linq; 19 using System.Net; 20 using System.Runtime.CompilerServices; 21 using System.Text; 22 using System.Threading; 23 using System.Threading.Tasks; 24 using Agones.Model; 25 using MiniJSON; 26 using UnityEngine; 27 using UnityEngine.Networking; 28 29 namespace Agones 30 { 31 /// <summary> 32 /// Agones Alpha SDK for Unity. 33 /// </summary> 34 public class AgonesAlphaSdk : AgonesSdk 35 { 36 #region AgonesRestClient Public Methods 37 38 private struct Player 39 { 40 public string playerID; 41 42 public Player(string playerId) 43 { 44 this.playerID = playerId; 45 } 46 } 47 48 /// <summary> 49 /// This function increases the SDK’s stored player count by one, and appends this playerID to GameServer.Status.Players.IDs. 50 /// Returns true and adds the playerID to the list of playerIDs if the playerIDs was not already in the list of connected playerIDs. 51 /// </summary> 52 /// <returns>True if the playerID was added to the list of playerIDs</returns> 53 public async Task<bool> PlayerConnect(string id) 54 { 55 string json = JsonUtility.ToJson(new Player(playerId: id)); 56 return await SendRequestAsync("/alpha/player/connect", json).ContinueWith(task => task.Result.ok); 57 } 58 59 /// <summary> 60 /// This function decreases the SDK’s stored player count by one, and removes the playerID from GameServer.Status.Players.IDs. 61 /// Will return true and remove the supplied playerID from the list of connected playerIDs if the playerID value exists within the list. 62 /// </summary> 63 /// <returns>True if the playerID was removed from the list of playerIDs</returns> 64 public async Task<bool> PlayerDisconnect(string id) 65 { 66 string json = JsonUtility.ToJson(new Player(playerId: id)); 67 return await SendRequestAsync("/alpha/player/disconnect", json).ContinueWith(task => task.Result.ok); 68 } 69 70 private struct Capacity 71 { 72 public long count; 73 74 public Capacity(long count) 75 { 76 this.count = count; 77 } 78 } 79 80 /// <summary> 81 /// This changes the player capacity to a new value. 82 /// </summary> 83 /// <returns>gRPC Status of the request</returns> 84 public async Task<bool> SetPlayerCapacity(long count) 85 { 86 string json = JsonUtility.ToJson(new Capacity(count: count)); 87 return await SendRequestAsync("/alpha/player/capacity", json, UnityWebRequest.kHttpVerbPUT).ContinueWith(task => task.Result.ok); 88 } 89 90 91 /// <summary> 92 /// This returns the last player capacity that was set through the SDK. 93 /// If the player capacity is set from outside the SDK, use SDK.GameServer() instead. 94 /// </summary> 95 /// <returns>Player capacity</returns> 96 public async Task<long> GetPlayerCapacity() 97 { 98 var result = await SendRequestAsync("/alpha/player/capacity", "{}", UnityWebRequest.kHttpVerbGET); 99 100 if (!result.ok) 101 { 102 return 0; 103 } 104 105 if (Json.Deserialize(result.json) is not Dictionary<string, object> data 106 || !data.TryGetValue("count", out object countObject) 107 || countObject is not string countString 108 || !long.TryParse(countString, out long count)) 109 { 110 return 0; 111 } 112 113 return count; 114 } 115 116 /// <summary> 117 /// Returns the current player count. 118 /// </summary> 119 /// <returns>Player count</returns> 120 public async Task<long> GetPlayerCount() 121 { 122 var result = await SendRequestAsync("/alpha/player/count", "{}", UnityWebRequest.kHttpVerbGET); 123 124 if (!result.ok) 125 { 126 return 0; 127 } 128 129 if (Json.Deserialize(result.json) is not Dictionary<string, object> data 130 || !data.TryGetValue("count", out object countObject) 131 || countObject is not string countString 132 || !long.TryParse(countString, out long count)) 133 { 134 return 0; 135 } 136 137 return count; 138 } 139 140 /// <summary> 141 /// This returns if the playerID is currently connected to the GameServer. 142 /// This is always accurate, even if the value hasn’t been updated to the GameServer status yet. 143 /// </summary> 144 /// <returns>True if the playerID is currently connected</returns> 145 public async Task<bool> IsPlayerConnected(string id) 146 { 147 var result = await SendRequestAsync($"/alpha/player/connected/{id}", "{}", UnityWebRequest.kHttpVerbGET); 148 149 if (!result.ok) 150 { 151 return false; 152 } 153 154 if (Json.Deserialize(result.json) is not Dictionary<string, object> data 155 || !data.TryGetValue("bool", out object boolObject) 156 || boolObject is not bool resultBool) 157 { 158 return false; 159 } 160 161 return resultBool; 162 } 163 164 /// <summary> 165 /// This returns the list of the currently connected player ids. 166 /// This is always accurate, even if the value has not been updated to the Game Server status yet. 167 /// </summary> 168 /// <returns>The list of the currently connected player ids</returns> 169 public async Task<List<string>> GetConnectedPlayers() 170 { 171 var result = await SendRequestAsync("/alpha/player/connected", "{}", UnityWebRequest.kHttpVerbGET); 172 173 if (!result.ok) 174 { 175 return new List<string>(); 176 } 177 178 if (Json.Deserialize(result.json) is not Dictionary<string, object> data 179 || !data.TryGetValue("list", out object listObject) 180 || listObject is not List<object> list) 181 { 182 return new List<string>(); 183 } 184 185 return list.Where(l => l is string).Select(l => l.ToString()).ToList();; 186 } 187 188 #endregion 189 190 } 191 }