/// <summary> /// Matches the users. /// </summary> /// <param name="connection">The connection.</param> /// <param name="gameOwnerId">The game owner ID.</param> private void JoinBattle(IConnection connection, int gameOwnerId) { ConnectionArgs args = (ConnectionArgs)connection.ConnectionData; if (args.ClientState != ClientState.Inactive) { // Cant join, user has an open game. return; } try { IConnection gameOwner = this.UsersInLobby.Where(user => ((ConnectionArgs)user.ConnectionData).Id == gameOwnerId).First(); if (gameOwner != connection) { if (gameOwner.ConnectionData == null) { gameOwner.ConnectionData = new ConnectionArgs(); } ConnectionArgs gameOwnerArgs = (ConnectionArgs)gameOwner.ConnectionData; gameOwnerArgs.Challenger = connection; gameOwner.SendMessage(new Message(MessageType.JoinBattleRequest, new ClientDummy(args.UserName, connection.IPAddress, false, args.Id))); } } catch { // TODO: not found. } }
/// <summary> /// Sends The battle declined message. /// </summary> /// <param name="gameOwner">The game owner.</param> private void BattleDeclined(IConnection gameOwner) { ConnectionArgs args = (ConnectionArgs)gameOwner.ConnectionData; IConnection challenger = args.Challenger; ConnectionArgs challengerArgs = (ConnectionArgs)challenger.ConnectionData; args.Challenger = null; challenger.SendMessage(new Message(MessageType.DeclineBattle)); }
/// <summary> /// Sets the arguments of the connection, when a connection is accepted. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="args">The event arguments.</param> private void ConnectionManager_ConnectionAccepted(object sender, ClientAcceptedEventArgs args) { ConnectionArgs connectionData = new ConnectionArgs(); // TODO: Implement user name. connectionData.UserName = "******"; connectionData.ClientState = ClientState.Inactive; IConnection connection = args.Connection; connection.ConnectionData = connectionData; }
/// <summary> /// Initiates a battle. /// </summary> /// <param name="competitorA">The first competitor.</param> /// <param name="competitorB">The second competitor.</param> public void InitiateBattle(IConnection competitorA, IConnection competitorB) { ConnectionArgs argsA = (ConnectionArgs)competitorA.ConnectionData; ConnectionArgs argsB = (ConnectionArgs)competitorB.ConnectionData; argsA.ClientState = ClientState.InGame; argsB.ClientState = ClientState.InGame; Battle battle = new Battle(competitorA, competitorB); battle.Finished += this.Battle_Finished; this.Battles.Add(battle); }
/// <summary> /// Handles the BattleConfirmed message. /// </summary> /// <param name="gameOwner">The game owner.</param> private void BattleConfirmed(IConnection gameOwner) { ConnectionArgs args = (ConnectionArgs)gameOwner.ConnectionData; IConnection challenger = args.Challenger; ConnectionArgs challengerArgs = (ConnectionArgs)challenger.ConnectionData; if (args.ClientState != ClientState.WaitingForOpponent || challengerArgs.ClientState != ClientState.Inactive) { // Client is not available. return; } this.UsersInLobby.Add(challenger); this.FireBattleReady(this, new BattleReadyEventArgs(gameOwner, challenger)); }
/// <summary> /// Adds a pending user. /// </summary> /// <param name="connection">The connection.</param> private void NewBattle(IConnection connection) { ConnectionArgs args = (ConnectionArgs)connection.ConnectionData; if (args.ClientState != ClientState.Inactive) { // Client has already an open game. return; } args.Id = DateTime.Now.Millisecond; args.ClientState = ClientState.WaitingForOpponent; connection.ConnectionLost += this.Connection_Disconnected; connection.TimedOut += this.Connection_Disconnected; this.UsersInLobby.Add(connection); }