Exemple #1
0
        public static IgsMatchRequest ParseMatchRequest(IgsLine line)
        {
            Match match = regexMatchRequest.Match(line.EntireLine);

            if (match.Success)
            {
                return(IgsMatchRequest.FromOldStyleResponse(match.Groups[1].Value,
                                                            match.Groups[2].Value == "B" ? StoneColor.Black : StoneColor.White,
                                                            match.Groups[3].Value.AsInteger(),
                                                            match.Groups[4].Value.AsInteger(),
                                                            match.Groups[5].Value.AsInteger()));
            }
            return(null);
        }
Exemple #2
0
        private async void bRejectRequest_Click(object sender, EventArgs e)
        {
            IgsMatchRequest selectedItem = this.lbMatchRequests.SelectedItem as IgsMatchRequest;

            if (selectedItem != null)
            {
                if (await igs.Commands.DeclineMatchRequestAsync(selectedItem))
                {
                    this.lbMatchRequests.Items.Remove(selectedItem);
                }
                else
                {
                    Fail("Match request cannot be declined.");
                }
            }
        }
Exemple #3
0
        public async Task <IgsGame> AcceptMatchRequestAsync(IgsMatchRequest matchRequest)
        {
            // We are accepting a match and it begins.
            var lines = await MakeRequestAsync(matchRequest.AcceptCommand);

            if (lines.IsError)
            {
                return(null);
            }
            if (lines.Any(ln => ln.Code == IgsCode.Info && ln.PureLine.Contains("Requesting")))
            {
                this.igsConnection.Events.OnErrorMessageReceived("Requesting " + matchRequest.OpponentName +
                                                                 " to confirm match.");
                return(null);
            }
            var heading = this.igsConnection.Data.LastReceivedGameHeading;
            var ogi     = await GetGameByIdAsync(heading.GameNumber);

            var  builder     = GameBuilder.CreateOnlineGame(ogi).Connection(this.igsConnection);
            bool youAreBlack = heading.BlackName == this.igsConnection.Username;
            var  humanPlayer =
                new HumanPlayerBuilder(youAreBlack ? StoneColor.Black : StoneColor.White).Name(youAreBlack
                    ? heading.BlackName
                    : heading.WhiteName)
                .Rank(youAreBlack ? ogi.Black.Rank : ogi.White.Rank)
                .Clock(new CanadianTimeControl(TimeSpan.Zero, 25, TimeSpan.FromMinutes(ogi.ByoyomiPeriod)))
                .Build();
            var onlinePlayer =
                new IgsPlayerBuilder(youAreBlack ? StoneColor.White : StoneColor.Black, this.igsConnection).Name(
                    youAreBlack
                        ? heading.WhiteName
                        : heading.BlackName)
                .Rank(youAreBlack ? ogi.White.Rank : ogi.Black.Rank)
                .Clock(new CanadianTimeControl(TimeSpan.Zero, 25, TimeSpan.FromMinutes(ogi.ByoyomiPeriod)))
                .Build();

            builder.BlackPlayer(youAreBlack ? humanPlayer : onlinePlayer)
            .WhitePlayer(youAreBlack ? onlinePlayer : humanPlayer);
            var game = builder.Build();

            this.igsConnection.GamesYouHaveOpened.Add(game);
            return(game);
        }
Exemple #4
0
        private async void bAcceptRequest_Click(object sender, EventArgs e)
        {
            IgsMatchRequest selectedItem = this.lbMatchRequests.SelectedItem as IgsMatchRequest;

            if (selectedItem != null)
            {
                IgsGame game = await igs.Commands.AcceptMatchRequestAsync(selectedItem);

                if (game != null)
                {
                    InGameForm ingameForm = new FormsPrototype.InGameForm(game.Info, game.Controller, igs);
                    ingameForm.LoadGame(game);
                    ingameForm.Show();
                }
                else
                {
                    Fail("Match request cannot be accepted.");
                }
            }
        }
Exemple #5
0
 internal void OnIncomingMatchRequest(IgsMatchRequest matchRequest)
 {
     IncomingMatchRequest?.Invoke(matchRequest);
 }
 private void OnIncomingMatchRequest(IgsMatchRequest matchRequest)
 {
     IncomingMatchRequest?.Invoke(matchRequest);
 }
        private async Task HandleIncomingData(StreamReader sr)
        {
            bool           thisIsNotAMove           = false;
            bool           weAreHandlingAnInterrupt = false;
            bool           interruptIsImpossible    = false;
            List <IgsLine> currentLineBatch         = new List <IgsLine>();

            while (true)
            {
                string line;
                try
                {
                    line = await sr.ReadLineAsync();
                }
                catch (Exception)
                {
                    line = null;
                }
                if (line == null)
                {
                    ConnectionLost();
                    return;
                }
                line = line.Trim();


                IgsCode code    = ExtractCodeFromLine(line);
                IgsLine igsLine = new IgsLine(code, line);
                Events.OnIncomingLine((weAreHandlingAnInterrupt ? "(INTERRUPT) " : "") + (interruptIsImpossible ? "(INTERRUPT IMPOSSIBLE) " : "") + line);

                // IGS occasionally sends blank lines, I don't know why. They serve no reason.
                if (line == "")
                {
                    continue;
                }

                switch (this.Composure)
                {
                case IgsComposure.Confused:
                case IgsComposure.Ok:
                case IgsComposure.Disconnected:
                    // No special mode.
                    break;

                case IgsComposure.InitialHandshake:
                    if (igsLine.EntireLine.Trim() == "1 5")
                    {
                        this.Composure = IgsComposure.Ok;
                        continue;
                    }
                    else
                    {
                        // Ignore.
                        continue;
                    }

                case IgsComposure.LoggingIn:
                    if (igsLine.EntireLine.Contains("Invalid password."))
                    {
                        this.Composure   = IgsComposure.Confused;
                        this._loginError = "The password is incorrect.";
                        continue;
                    }
                    if (igsLine.EntireLine.Contains("Sorry, names can be"))
                    {
                        this.Composure   = IgsComposure.Confused;
                        this._loginError = "Your name is too long.";
                        continue;
                    }
                    if (igsLine.EntireLine.Contains("This is a guest account."))
                    {
                        this.Composure   = IgsComposure.Confused;
                        this._loginError = "The username does not exist.";
                        continue;
                    }
                    if (igsLine.EntireLine.Contains("1 5"))
                    {
                        this.Composure = IgsComposure.Ok;
                        continue;
                    }
                    break;
                }

                if (igsLine.Code == IgsCode.Error)
                {
                    Events.OnErrorMessageReceived(igsLine.PureLine);
                }
                currentLineBatch.Add(igsLine);

                if (weAreHandlingAnInterrupt && code == IgsCode.Prompt)
                {
                    // Interrupt message is over, let's wait for a new message
                    weAreHandlingAnInterrupt = false;
                    HandleFullInterrupt(currentLineBatch);
                    thisIsNotAMove        = false;
                    interruptIsImpossible = false;
                    currentLineBatch      = new List <IgsLine>();
                    continue;
                }
                if (code == IgsCode.Prompt)
                {
                    thisIsNotAMove        = false;
                    currentLineBatch      = new List <IgsLine>();
                    interruptIsImpossible = false;
                    if (this._ignoreNextPrompt)
                    {
                        this._ignoreNextPrompt = false;
                        continue;
                    }
                }
                if (code == IgsCode.Kibitz)
                {
                    weAreHandlingAnInterrupt = true;
                    continue;
                }
                if (code == IgsCode.Beep)
                {
                    Events.OnBeep();
                    continue;
                }

                if (!interruptIsImpossible)
                {
                    if (code == IgsCode.Tell)
                    {
                        if (igsLine.PureLine.StartsWith("*SYSTEM*"))
                        {
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }
                        HandleIncomingChatMessage(line);
                        weAreHandlingAnInterrupt = true;
                        continue;
                    }
                    if (code == IgsCode.SayInformation)
                    {
                        weAreHandlingAnInterrupt = true;
                        continue;
                    }
                    if (code == IgsCode.Status)
                    {
                        weAreHandlingAnInterrupt = true;
                        continue;
                    }
                    if (code == IgsCode.Shout)
                    {
                        HandleIncomingShoutMessage(line);
                        weAreHandlingAnInterrupt = true;
                        continue;
                    }
                    if (code == IgsCode.StoneRemoval)
                    {
                        Tuple <int, Position> removedStone = IgsRegex.ParseStoneRemoval(igsLine);
                        OnIncomingStoneRemoval(removedStone.Item1, removedStone.Item2);
                        continue;
                    }
                    if (code == IgsCode.Move)
                    {
                        var heading = IgsRegex.ParseGameHeading(igsLine);
                        if (heading != null)
                        {
                            this.Data.LastReceivedGameHeading = heading;
                        }
                        if (!thisIsNotAMove)
                        {
                            HandleIncomingMove(igsLine);
                            weAreHandlingAnInterrupt = true;
                        }
                        continue;
                    }
                    if (code == IgsCode.Undo)
                    {
                        thisIsNotAMove           = true;
                        weAreHandlingAnInterrupt = true;
                        continue;
                    }
                }
                if (code == IgsCode.Info)
                {
                    // 9 Adding game to observation list.
                    if (igsLine.EntireLine.Contains("9 Adding game to observation list."))
                    {
                        interruptIsImpossible = true;
                    }
                    if (!interruptIsImpossible)
                    {
                        if (igsLine.PureLine == "yes")
                        {
                            // This is "ayt" response, ignore it.
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }

                        if (igsLine.EntireLine ==
                            "9 You can check your score with the score command, type 'done' when finished.")
                        {
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }
                        if (igsLine.PureLine.Contains("accepted."))
                        {
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }
                        if (igsLine.PureLine.Contains("Removing @"))
                        {
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }
                        if (igsLine.PureLine.Contains("has run out of time"))
                        {
                            weAreHandlingAnInterrupt = true;
                            string whoRanOutOfTime = IgsRegex.WhoRanOutOfTime(igsLine);
                            foreach (var game in GetGamesIncluding(whoRanOutOfTime).ToList())
                            {
                                game.Controller.IgsConnector.EndTheGame(
                                    GameEndInformation.CreateTimeout(
                                        game.Controller.Players.First(pl => pl.Info.Name == whoRanOutOfTime),
                                        game.Controller.Players)
                                    );
                            }
                            continue;
                        }
                        if (igsLine.PureLine.Contains("White resigns.}"))
                        {
                            int gameInWhichSomebodyResigned = IgsRegex.WhatObservedGameWasResigned(igsLine);
                            ResignObservedGame(gameInWhichSomebodyResigned, StoneColor.White);
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }
                        if (igsLine.PureLine.Contains("Black resigns.}"))
                        {
                            int gameInWhichSomebodyResigned = IgsRegex.WhatObservedGameWasResigned(igsLine);
                            ResignObservedGame(gameInWhichSomebodyResigned, StoneColor.Black);
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }
                        if (igsLine.PureLine.Contains("has resigned the game"))
                        {
                            string whoResigned = IgsRegex.WhoResignedTheGame(igsLine);
                            if (whoResigned != this._username)
                            {
                                // .ToList() is used because the collection may be modified
                                foreach (var game in GetGamesIncluding(whoResigned).ToList())
                                {
                                    HandleIncomingResignation(game.Info, whoResigned);
                                }
                            }
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }
                        if (igsLine.PureLine.Contains("has typed done."))
                        {
                            string username = IgsRegex.GetFirstWord(igsLine);
                            weAreHandlingAnInterrupt = true;
                            foreach (var game in GetGamesIncluding(username))
                            {
                                var player = game.Controller.Players.First(pl => pl.Info.Name == username);
                                game.Controller.IgsConnector.RaiseServerSaidDone(player);
                            }
                            continue;
                        }
                        if (igsLine.PureLine.Contains("Board is restored to what it was when you started scoring"))
                        {
                            foreach (
                                var game in
                                this.GamesYouHaveOpened.Where(
                                    gi =>
                                    gi.Controller.Phase.Type ==
                                    GamePhaseType.LifeDeathDetermination))
                            {
                                GetConnector(game.Info).ForceLifeDeathUndoDeathMarks();
                            }
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }

                        if (igsLine.PureLine.Contains("Removed game file"))
                        {
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }
                        if (igsLine.PureLine.Contains("game completed."))
                        {
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }
                        if (igsLine.PureLine.StartsWith("!!*Pandanet*!!:"))
                        {
                            // Advertisement
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }
                        if (IgsRegex.IsIrrelevantInterruptLine(igsLine))
                        {
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }
                        if (igsLine.PureLine.StartsWith("Increase "))
                        {
                            weAreHandlingAnInterrupt = true;
                            string person = IgsRegex.ParseIncreaseXTimeByYMinute(igsLine);
                            foreach (var game in this.GamesYouHaveOpened)
                            {
                                if (game.Info.Black.Name == person ||
                                    game.Info.White.Name == person)
                                {
                                    MakeUnattendedRequest("refresh " + game.Info.IgsIndex);
                                }
                            }
                        }

                        if (igsLine.PureLine.EndsWith("declines undo."))
                        {
                            string username = IgsRegex.WhoDeclinesUndo(igsLine);
                            foreach (var game in GetGamesIncluding(username))
                            {
                                Events.OnUndoDeclined(game.Info);
                            }
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }

                        if (igsLine.PureLine.EndsWith("declines your request for a match."))
                        {
                            Events.OnMatchRequestDeclined(igsLine.PureLine.Substring(0, igsLine.PureLine.IndexOf(' ')));
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }

                        IgsMatchRequest matchRequest = IgsRegex.ParseMatchRequest(igsLine);
                        if (matchRequest != null)
                        {
                            this._incomingMatchRequests.Add(matchRequest);
                            Events.OnIncomingMatchRequest(matchRequest);
                            weAreHandlingAnInterrupt = true;
                            continue;
                        }
                    }
                }

                if (!weAreHandlingAnInterrupt)
                {
                    // We cannot handle this generally - let's hand it off to whoever made the request for this information.
                    lock (this._mutex)
                    {
                        if (this._requestInProgress != null)
                        {
                            this._requestInProgress.IncomingLines.Post(igsLine);
                        }
                        else
                        {
                            if (this.Composure == IgsComposure.Ok)
                            {
                                Events.OnUnhandledLine(igsLine.EntireLine);
                            }
                        }
                    }
                }
            }
        }
Exemple #8
0
        public async Task <bool> DeclineMatchRequestAsync(IgsMatchRequest matchRequest)
        {
            var response = await MakeRequestAsync(matchRequest.RejectCommand);

            return(!response.IsError);
        }
Exemple #9
0
 public IgsIncomingMatchRequestBundle(IgsMatchRequest igsMatchRequest)
 {
     _igsMatchRequest = igsMatchRequest;
 }