コード例 #1
0
ファイル: Program.cs プロジェクト: zerowind168/Referee
        private static void Main(string[] args)
        {
            // Get gRPC client
            Console.WriteLine("This project is still in early stage");
            var blueChannel   = new Channel("127.0.0.1:50052", ChannelCredentials.Insecure);
            var yellowChannel = new Channel("127.0.0.1:50053", ChannelCredentials.Insecure);
            var blueClient    = new FiraMessage.RefToCli.Referee.RefereeClient(blueChannel);
            var yellowClient  = new FiraMessage.RefToCli.Referee.RefereeClient(yellowChannel);

            //var simulationChannel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
            //var simulationClient = new GrpcSimulateClient(new Simulate.SimulateClient(simulationChannel));
            using var simulationClient = new SimulateClient("127.0.0.1", 50051, 50054);

            try
            {
                // Register strategy
                var blueTeamInfo = new FiraMessage.RefToCli.TeamInfo {
                    Color = Color.B
                };
                var blueName       = blueClient.Register(blueTeamInfo);
                var yellowTeamInfo = new FiraMessage.RefToCli.TeamInfo {
                    Color = Color.Y
                };
                var yellowName = yellowClient.Register(yellowTeamInfo);

                var matchInfo = MainLoop(blueClient, yellowClient, simulationClient);

                if (matchInfo.Score.BlueScore > matchInfo.Score.YellowScore)
                {
                    blueName.Name += " Win";
                    Console.WriteLine(blueName.Name);
                }
                else if (matchInfo.Score.BlueScore < matchInfo.Score.YellowScore)
                {
                    yellowName.Name += " Win";
                    Console.WriteLine(yellowName.Name);
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
            }
            catch (RpcException ex)
            {
                if (ex.Status.StatusCode == StatusCode.Unavailable)
                {
                    Console.WriteLine("An agent client has closed.");
                }
            }
            finally
            {
                blueChannel.ShutdownAsync().Wait();
                yellowChannel.ShutdownAsync().Wait();
                //simulationChannel.ShutdownAsync().Wait();
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: zerowind168/Referee
        private static MatchInfo MainLoop(FiraMessage.RefToCli.Referee.RefereeClient blueClient,
                                          FiraMessage.RefToCli.Referee.RefereeClient yellowClient, ISimulateClient simulationClient)
        {
            var  matchInfo    = new MatchInfo();
            var  environment  = InitSimEnvironment();
            bool isSecondHalf = false;

            while (true)
            {
                try
                {
                    //TODO: just for test
                    Console.Out.WriteLine("MatchPhase = {0}", matchInfo.MatchPhase);
                    Console.Out.WriteLine("matchInfo.TickPhase = {0}", matchInfo.TickPhase);
                    Console.Out.WriteLine("matchInfo.TickMatch = {0}", matchInfo.TickMatch);
                    if (matchInfo.MatchPhase == MatchPhase.Penalty)
                    {
                        Console.Out.WriteLine("BlueScore = {0}", matchInfo.Score.BlueScore);
                        Console.Out.WriteLine("YellowScore = {0}", matchInfo.Score.YellowScore);
                    }

                    // Get JudgeResult from the judge
                    var      judgeResult = matchInfo.Referee.Judge(matchInfo);
                    FoulInfo info        = ExtractFoulInfo(judgeResult, matchInfo);
                    switch (judgeResult.ResultType)
                    {
                    case ResultType.NormalMatch:
                    {
                        // The game continues, move to next frame
                        var blueCliEnvironment     = EnvironmentSimToCli(environment, info);
                        var blueClientCommandReply = blueClient.RunStrategy(blueCliEnvironment);

                        var yellowCliEnvironment     = ConvertToRight(EnvironmentSimToCli(environment, info));
                        var yellowClientCommandReply = yellowClient.RunStrategy(yellowCliEnvironment);

                        environment = simulationClient.Simulate(CommandCliToSim(blueClientCommandReply,
                                                                                yellowClientCommandReply, isSecondHalf));
                        break;
                    }

                    case ResultType.NextPhase:
                    {
                        // This Phase is over, start next phase
                        switch (matchInfo.MatchPhase)
                        {
                        case MatchPhase.FirstHalf:
                            matchInfo.MatchPhase = MatchPhase.SecondHalf;
                            isSecondHalf         = true;
                            break;

                        case MatchPhase.SecondHalf:
                            matchInfo.MatchPhase = MatchPhase.OverTime;
                            isSecondHalf         = false;
                            break;

                        case MatchPhase.OverTime:
                            matchInfo.MatchPhase = MatchPhase.Penalty;
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }

                        matchInfo.TickPhase = 0;
                        continue;
                    }

                    case ResultType.GameOver:
                    {
                        return(matchInfo);
                    }

                    case ResultType.PlaceKick:
                    case ResultType.GoalKick:
                    case ResultType.PenaltyKick:
                    case ResultType.FreeKickRightTop:
                    case ResultType.FreeKickRightBot:
                    case ResultType.FreeKickLeftTop:
                    case ResultType.FreeKickLeftBot:
                    {
                        // A foul happened

                        // Get ball position
                        var cliEnvironment = EnvironmentSimToCli(environment, info);
                        var ballPosition   = judgeResult.WhosBall switch
                        {
                            Simuro5v5.Side.Nobody => FoulBallPosition(info, judgeResult),
                            Simuro5v5.Side.Blue => blueClient.SetBall(cliEnvironment),
                            Simuro5v5.Side.Yellow => ConvertFromRight(
                                yellowClient.SetBall(ConvertToRight(cliEnvironment))),
                            _ => throw new ArgumentOutOfRangeException()
                        };
                        Robots blueClientRobotsReply;
                        Robots yellowClientRobotsReply;

                        cliEnvironment.Frame.Ball = ballPosition;
                        switch (judgeResult.WhoisFirst)
                        {
                        case Simuro5v5.Side.Blue:
                            blueClientRobotsReply         = blueClient.SetFormerRobots(cliEnvironment);
                            cliEnvironment.FoulInfo.Actor = Side.Opponent;
                            cliEnvironment.Frame.RobotsBlue.SetRobots(blueClientRobotsReply);
                            matchInfo.UpdateByCliEnvironment(cliEnvironment);

                            matchInfo.Referee.JudgeAutoPlacement(matchInfo, judgeResult, Simuro5v5.Side.Blue);
                            MatchInfo2CliEnvironment(matchInfo, ref cliEnvironment, false);
                            yellowClientRobotsReply =
                                yellowClient.SetLaterRobots(ConvertToRight(cliEnvironment));
                            ConvertFromRight(ref yellowClientRobotsReply);
                            cliEnvironment.Frame.RobotsYellow.SetRobots(yellowClientRobotsReply);
                            matchInfo.UpdateByCliEnvironment(cliEnvironment);
                            matchInfo.Referee.JudgeAutoPlacement(matchInfo, judgeResult, Simuro5v5.Side.Yellow);
                            MatchInfo2CliEnvironment(matchInfo, ref cliEnvironment, true);
                            break;

                        case Simuro5v5.Side.Yellow:
                            yellowClientRobotsReply =
                                yellowClient.SetFormerRobots(ConvertToRight(cliEnvironment));
                            ConvertFromRight(ref yellowClientRobotsReply);
                            cliEnvironment.FoulInfo.Actor = Side.Opponent;
                            cliEnvironment.Frame.RobotsYellow.SetRobots(yellowClientRobotsReply);
                            matchInfo.UpdateByCliEnvironment(cliEnvironment);
                            matchInfo.Referee.JudgeAutoPlacement(matchInfo, judgeResult, Simuro5v5.Side.Yellow);
                            MatchInfo2CliEnvironment(matchInfo, ref cliEnvironment, true);
                            blueClientRobotsReply = blueClient.SetLaterRobots(cliEnvironment);
                            cliEnvironment.Frame.RobotsBlue.SetRobots(blueClientRobotsReply);
                            matchInfo.UpdateByCliEnvironment(cliEnvironment);
                            matchInfo.Referee.JudgeAutoPlacement(matchInfo, judgeResult, Simuro5v5.Side.Blue);
                            MatchInfo2CliEnvironment(matchInfo, ref cliEnvironment, false);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }

                        environment = simulationClient.Simulate(MatchInfo2Packet(matchInfo, isSecondHalf));
                        break;
                    }

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    if (isSecondHalf)
                    {
                        SecondHalfTransform(ref environment);
                    }

                    SimEnvironment2MatchInfo(environment, ref matchInfo);
                    matchInfo.TickMatch++;
                    matchInfo.TickPhase++;
                }
                catch (RpcException ex)
                {
                    Console.WriteLine($"RPC exception occured: {ex.Message}");
                    Console.WriteLine("Press ENTER to retry...");
                    Console.ReadLine();
                }
            }
        }