private IEnumerator startSetup() { int playerCount = ServerManager.Instance.ConnectedUsers.Count; // give each player a turn to pick a region until all regions are picked for (int i = 0; i < MapManager.Instance.MapRegions.Keys.Count; i++) { CredentialToken player = ServerManager.Instance.ConnectedUsers[i % playerCount]; BoltConnection nextPlayer = ServerManager.Instance.Connections[player]; PickRegion evnt = PickRegion.Create(nextPlayer); evnt.Send(); pickingRegion = true; yield return(new WaitUntil(() => pickingRegion == false)); } EndSetup es = EndSetup.Create(Bolt.GlobalTargets.AllClients); es.Send(); StartTurns(); yield return(null); }
/// <summary> /// add user to this game session /// </summary> /// <param name="user"></param> public void AddToSession(CredentialToken user) { if (WasUserInSession(user.UserName) == true) { // this user was previously connected to this session, reconnect them and re-attach shit ConnectedUsers.Add(user); // also, only ONE user in the session may have this username // so we need to remove that user and replace with this one, if anything is different CredentialToken previousUser = SessionUsers.Find(x => x.UserName == user.UserName); if (user != previousUser) { SessionUsers.Remove(previousUser); SessionUsers.Add(user); } return; } if (ConnectedUsers.Contains(user) == false) { Debug.Log("adding to lobby"); ConnectedUsers.Add(user); SessionUsers.Add(user); } }
private IEnumerator startTurns() { int playerCount = 0; int players = ServerManager.Instance.ConnectedUsers.Count; // go til end while (true) { // assign turn // turn order is currently in order of who connected CredentialToken player = ServerManager.Instance.ConnectedUsers[playerCount % players]; BoltConnection nextPlayer = ServerManager.Instance.Connections[player]; TakeTurn tt = TakeTurn.Create(nextPlayer); tt.Send(); takingTurn = true; yield return(new WaitUntil(() => takingTurn == false)); // resolve turn } yield return(null); }
/// <summary> /// called when someone connects to server /// </summary> /// <param name="connection"></param> public override void Connected(BoltConnection connection) { IProtocolToken acceptToken = connection.AcceptToken; IProtocolToken connectToken = connection.ConnectToken; var log = LogEvent.Create(); //log.Message = string.Format("{0} connected", connection.RemoteEndPoint); // connected to server CredentialToken token = (CredentialToken)connectToken; if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name != GameManager.Instance.MenuSceneName) { Debug.Log("client tried to connect during gameplay: " + token.UserName); return; } Debug.Log("token connected: " + token.UserName); log.message += "connected: " + token.UserName; log.Send(); // maintain credential/connection database ServerManager.Instance.Connections.Add(token, connection); ServerManager.Instance.Credentials.Add(connection, token); ServerManager.Instance.AddToSession(token); // grab all connections to the server, get their credentials // serialize that list, and send it as an event List <CredentialToken> connectedCredentials = new List <CredentialToken>(); foreach (BoltConnection conn in BoltNetwork.connections) { if (conn != connection) { connectedCredentials.Add((CredentialToken)conn.ConnectToken); Debug.Log(((CredentialToken)conn.ConnectToken).DisplayName); // tell all clients except for this one that we're joining var userJoined = UserJoinedLobby.Create(conn); userJoined.UserToken = token; userJoined.Send(); } } BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, connectedCredentials); // send the list of connected clients to the newly-connected client SendConnectedClients sendClients = SendConnectedClients.Create(connection); sendClients.BinaryData = ms.ToArray(); sendClients.Send(); }
/// <summary> /// Sent by: Client who is currently picking a region /// Received by: Server /// Sent after client has picked their region during setup /// </summary> /// <param name="evnt"></param> public override void OnEvent(RegionPicked evnt) { int regionID = evnt.RegionID; // find new owner of region ID CredentialToken newOwner = (CredentialToken)evnt.NewOwner; Debug.Log("new owner info: " + newOwner.DisplayName); // send regionchange event to all clients ChangeRegionOwner cro = ChangeRegionOwner.Create(Bolt.GlobalTargets.AllClients); cro.RegionID = regionID; cro.NewOwner = newOwner; cro.Send(); // update owner MapManager.Instance.MapOwners[regionID] = newOwner; // change color Color newColor = GameManager.Instance.PlayerColors[newOwner].GetColor; GameObject mapRegion = MapManager.Instance.MapRegions[regionID]; mapRegion.GetComponent <Image>().color = newColor; GameManager.Instance.PickRegionOver(); }
public void ProvideOAuth2Token(string oauth2Token) { var appToken = new Token() { AccessToken = oauth2Token, RefreshToken = null, ExpiresIn = 20 }; CredentialToken = new CredentialToken(appToken, clock); }
/// <summary> /// remove user from game session /// </summary> /// <param name="user"></param> public void RemoveFromSession(CredentialToken user) { if (ConnectedUsers.Contains(user) == true) { Debug.Log("removing from lobby"); ConnectedUsers.Remove(user); } //Messenger.Broadcast("UpdateLobby"); }
private void Refresh(string credentialId, CredentialToken token) { var cache = this.Cache.Value ?? throw new InvalidOperationException($"Missing the required cache."); //顺延当前用户及场景对应凭证号的缓存项的过期时长 cache.SetExpiry(this.GetCacheKeyOfUser(token.Principal.Identity.GetIdentifier(), token.Principal.Scenario), token.Principal.Expiration); //顺延当前凭证缓存项的过期时长 cache.SetExpiry(this.GetCacheKeyOfCredential(credentialId), token.Principal.Expiration); //重置本地缓存的时间信息 token.Reset(); }
public async Task RefreshToken() { // TODO : Handle not success status codes (rate limit exceeded, api down, ect) var token = await baseUrl.AppendPathSegment("/oauth2/token").PostUrlEncodedAsync(new { grant_type = "refresh_token", client_id = clientId, client_secret = clientSecret, refresh_token = CredentialToken.RefreshToken }).ReceiveJson <Token>(); CredentialToken = new CredentialToken(token, clock); }
public bool Equals(CredentialToken other) { if (other == null) { return(false); } if ((object)other == null) { return(false); } return(other.DisplayName == DisplayName); }
/// <summary> /// Called when a local scene change finishes. /// </summary> /// <param name="map"></param> /// <param name="token"></param> public override void SceneLoadLocalDone(string map, Bolt.IProtocolToken token) { SceneChangeToken sceneChange = (SceneChangeToken)token; if (sceneChange != null) { Debug.Log("loading from scene: " + sceneChange.SceneFrom); Debug.Log("loading to scene: " + sceneChange.SceneTo); Debug.Log("reason: " + sceneChange.Reason); if (sceneChange.Reason == "StartGame") { if (BoltNetwork.isClient) { GameplayStartedEvent evnt = GameplayStartedEvent.Create(Bolt.GlobalTargets.OnlyServer); CredentialToken credentials = ClientManager.Instance.Credentials; evnt.CredentialsToken = credentials; evnt.Send(); } else { // we're the server /* * BoltEntity wizard = GameManager.Instance.SpawnWizard(ClientManager.Instance.Credentials); * wizard.GetComponent<WizardController>().Init(); * wizard.TakeControl(); * * BoltEntity entropyManager = GameManager.Instance.SpawnEntropyManager(); * * entropyManager.TakeControl(); */ // enable the start of setup for the game GameManager.Instance.StartSetup(); } ServerManager.Instance.InitUserStats(); //GameTypeManager.Instance.RoundStart(); //StartMenuGUIManager.Instance.FSM.Transition("Init"); GameGUIManager.Instance.BoltLoadGameScene(); } } }
public async Task GenerateToken(string username, string password, Scope[] scopes = null) { var scope = string.Join(" ", scopes?.Select(s => s.Value) ?? new string[0]); // TODO : Handle not success status codes (rate limit exceeded, api down, ect) var token = await baseUrl.AppendPathSegment("/oauth2/token").PostUrlEncodedAsync(new { grant_type = "password", client_id = clientId, client_secret = clientSecret, username, password, scope }).ReceiveJson <Token>(); CredentialToken = new CredentialToken(token, clock); }
public override void Disconnected(BoltConnection connection) { var log = LogEvent.Create(); log.message = string.Format("{0} disconnected", connection.RemoteEndPoint.Address.ToString()); log.Send(); Debug.Log("token disconnected: " + connection.RemoteEndPoint.Address.ToString()); // get token connected with connection's ip //CredentialToken disconnectToken = ServerManager.Instance.GetConnectedTokenByIP(connection.RemoteEndPoint.Address.ToString()); CredentialToken disconnectToken = (CredentialToken)connection.ConnectToken; // tell all clients that a user is leaving the lobby so that they may update their GUIs and such var userDisconnected = UserDisconnectedLobby.Create(); userDisconnected.UserToken = disconnectToken; userDisconnected.Send(); }
/// <summary> /// Sent by: Server /// Received by: All clients /// Update client data when a region changes hands from one client to another /// </summary> /// <param name="evnt"></param> public override void OnEvent(ChangeRegionOwner evnt) { int regionID = evnt.RegionID; CredentialToken newOwner = (CredentialToken)evnt.NewOwner; // update owner MapManager.Instance.MapOwners[regionID] = newOwner; // change color Color newColor = GameManager.Instance.PlayerColors[newOwner].GetColor; GameObject mapRegion = MapManager.Instance.MapRegions[regionID]; mapRegion.GetComponent <Image>().color = newColor; }
public async Task <string> GetCredentialAsync(Options.IAppSetting app) { if (app == null) { throw new ArgumentNullException(nameof(app)); } var key = GetCredentalKey(app.Id); //首先从本地内存缓存中获取凭证标记,如果获取成功并且凭证未过期则返回该凭证号 if (_localCache.TryGetValue(key, out var token) && !token.IsExpired) { return(token.Key); } var credentialId = this.Cache.GetValue <string>(key); if (string.IsNullOrEmpty(credentialId)) { token = await this.GetRemoteCredentialAsync(app.Id, app.Secret); if (this.Cache.SetValue(key, token.Key, token.Expiry)) { _localCache[key] = token; } return(token.Key); } else { var expiry = this.Cache.GetExpiry(key); if (expiry.HasValue) { _localCache[key] = new CredentialToken(credentialId, DateTime.UtcNow.Add(expiry.Value)); } return(credentialId); } }
public override bool Equals(object obj) { /* * // existence is merely a social construct. nothing is equal. * if(obj != null) * { * return false; * } */ if (obj == null) { return(false); } CredentialToken other = obj as CredentialToken; if ((object)obj == null) { return(false); } return(other.DisplayName == DisplayName); }