Example #1
0
        public void RelayOriginalAndMirroredAttack(int srcX, int srcY, int destX, int destY,
                                                   string attackerPowerLevel)
        {
            this.Locker.WaitOne();

            if (this.CurrentMatch == null)
            {
                this.Locker.Release();
                return;
            }

            int mirroredSrcX  = Math.Abs(srcX - 9);
            int mirroredSrcY  = Math.Abs(srcY - 9);
            int mirroredDestX = Math.Abs(destX - 9);
            int mirroredDestY = Math.Abs(destY - 9);

            string defenderPowerLevel = null;

            Player opponent = this.CurrentMatch.GetOpponent(this);

            var tryAction = new Action(async() =>
            {
                defenderPowerLevel = opponent.Client.ShowPowerLevel(mirroredDestX, mirroredDestY);

                await Task.WhenAll(
                    this.Client.AttackBoardPieceAsync(srcX, srcY, destX, destY,
                                                      attackerPowerLevel, defenderPowerLevel, true),
                    opponent.Client.AttackBoardPieceAsync(mirroredSrcX, mirroredSrcY, mirroredDestX, mirroredDestY,
                                                          attackerPowerLevel, defenderPowerLevel, false));
            });

            TryToRunIt(tryAction);

            this.CurrentMatch.MoveCount++;

            if (defenderPowerLevel == "*")
            {
                opponent.Locker.WaitOne();

                lock (this.CurrentMatch.Winner)
                {
                    this.CurrentMatch.SetWinner(this);
                }

                opponent.InvokeEndMatch();
                opponent.CurrentMatch = null;

                opponent.Locker.Release();

                this.InvokeEndMatch();
                this.CurrentMatch = null;
            }

            this.Locker.Release();
        }
Example #2
0
        private void PingPlayer(object param)
        {
            this.Locker.WaitOne();

            if (this.Client == null)
            {
                this.Locker.Release();
                return;
            }

            try
            {
                this.Client.Ping();
            }
            catch (Exception)
            {
                Console.WriteLine("Player " + this.ClientId + " couldn't be reached");
                Console.WriteLine();

                this.Pinger.Dispose();

                if (this.CurrentMatch != null)
                {
                    Player opponent = this.CurrentMatch.GetOpponent(this);

                    opponent.Locker.WaitOne();

                    lock (this.CurrentMatch.Winner)
                    {
                        if (this.CurrentMatch.Winner == "0")
                        {
                            this.CurrentMatch.SetWinner(opponent);

                            opponent.InvokeEndMatch();
                        }
                    }

                    opponent.Locker.Release();

                    this.CurrentMatch = null;
                }

                this.Client.Abort();

                this.Locker.Release();

                Task.Run(() => this.GameSession.LeaveGame());
            }

            this.Locker.Release();
        }
Example #3
0
        public void CancelCurrentMatch()
        {
            lock (WaitingPlayers)
            {
                this.Locker.WaitOne();

                if (this.CurrentMatch == null)
                {
                    if (WaitingPlayers.Count == 0)
                    {
                        this.Locker.Release();
                        return;
                    }

                    Console.WriteLine("Player " + this.ClientId + " is not waiting anymore");
                    Console.WriteLine();

                    WaitingPlayers.Dequeue();
                }
                else
                {
                    Console.WriteLine("Player " + this.ClientId + " has quit the match");
                    Console.WriteLine();

                    Player opponent = this.CurrentMatch.GetOpponent(this);

                    opponent.Locker.WaitOne();

                    opponent.InvokeEndMatch();
                    opponent.CurrentMatch = null;

                    opponent.Locker.Release();

                    this.InvokeEndMatch();
                    this.CurrentMatch = null;
                }

                this.Locker.Release();
            }
        }