コード例 #1
0
        public static bool HTTPPostStartGame(string gameName, string session, gameSettings gameSettings)
        {
            MyPacket p = new MyPacket()
            {
                gameName = gameName, session = session, gameSettings = gameSettings
            };
            string serialized = JsonConvert.SerializeObject(p);

            Log.TraceMessage(Log.Nav.NavIn, "Serialized data: " + serialized, Log.LogType.Info);
            using (var client = new HttpClient())
            {
                try
                {
                    List <Task> allGames = new List <Task>();
                    //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IklMaWtlU29ja3NPblN1bmRheXMiLCJpZCI6Mywicm9sZSI6InVzZXIiLCJpYXQiOjE1NDE4NjY1NTcsImV4cCI6MTU0MjI5ODU1N30.XWaWB_cWhUFEC1m0GxFJ4ln8uq5h086gXGxRmOLVXA0");
                    allGames.Add(Task.Run(async() =>
                    {
                        var x = await client.PostAsync(
                            "http://127.0.0.1:3080/setup",
                            new StringContent(serialized, Encoding.UTF8, "application/json"));
                        Console.WriteLine(await x.Content.ReadAsStringAsync());
                        Log.TraceMessage(Log.Nav.NavIn, "Server Response Content: " + await x.Content.ReadAsStringAsync(), Log.LogType.Info);
                        Log.TraceMessage(Log.Nav.NavIn, "Server Response: " + x, Log.LogType.Info);
                    }));
                    Task.WaitAll(allGames.ToArray());
                    return(true);
                }
                catch (Exception ex)
                {
                    string idk = ex.Message;
                    Log.TraceMessage(Log.Nav.NavIn, "Except: " + idk, Log.LogType.Info);
                    return(false);
                }
            }
        }
コード例 #2
0
ファイル: Client.cs プロジェクト: siggame/TheArena
        static List <string> BuildAndRunGame()
        {
            try
            {
                List <string> answers = new List <string>();
                Log.TraceMessage(Log.Nav.NavIn, "Building and Running Game ", Log.LogType.Info);
                if (!Directory.Exists(ARENA_FILES_PATH))
                {
                    Directory.CreateDirectory(ARENA_FILES_PATH);
                }
                var files = Directory.GetFiles(ARENA_FILES_PATH);
                Log.TraceMessage(Log.Nav.NavIn, "ARENA FILES Directory Contains " + files.Count() + " files.", Log.LogType.Info);
                List <Tuple <Task <bool>, string> > allGames = new List <Tuple <Task <bool>, string> >(); //First parameter is the thread, second parameter is the file being run (which has the player name)

                List <string> playerNames = new List <string>();
                //Call SETUP GameServer API and create random game session
                foreach (var f in files)
                {
                    var      fs         = f.Substring(f.LastIndexOf('/') + 1);     // From C:\Users\Me\Documents\team1_1_csharp.zip to team_one_1_cs.zip
                    var      withoutZip = fs.Substring(0, fs.LastIndexOf(".zip")); //To team_one_1_cs
                    string[] split      = withoutZip.Split('_');                   // ["team","one","1","cs"]
                    var      reversed   = split.Reverse().ToArray();               // ["cs","1","one","team"]
                    string   lang       = reversed[0];                             //"cs"
                    string   submission = reversed[1];                             //"1"
                    string   teamName   = "";
                    for (int j = reversed.Length - 1; j > 1; j--)
                    {
                        teamName += reversed[j] + "_";                     // "team_one_"
                    }
                    teamName = teamName.Substring(0, teamName.Length - 1); //"team_one"
                    playerNames.Add(teamName);
                }
                string       gameSession = "seth" + DateTime.Now.Ticks;
                gameSettings gSettings   = new gameSettings()
                {
                    playerNames = playerNames.ToArray()
                };

                while (true)
                {
                    if (HTTP.HTTPPostStartGame(DA_GAME, gameSession, gSettings))
                    {
                        Log.TraceMessage(Log.Nav.NavIn, "Posted.", Log.LogType.Info);
                        break;
                    }
                    Thread.Sleep(100);
                    Log.TraceMessage(Log.Nav.NavIn, "Connecting...", Log.LogType.Info);
                }

                foreach (var file in files)
                {
                    Log.TraceMessage(Log.Nav.NavIn, "Creating Thread for file " + file, Log.LogType.Info);
                    Task <bool> t          = Task.Run(() => RunGame(file, gameSession));
                    var         fs         = file.Substring(file.LastIndexOf('/') + 1); // From C:\Users\Me\Documents\team1_1_csharp.zip to team_one_1_cs.zip
                    var         withoutZip = fs.Substring(0, fs.LastIndexOf(".zip"));   //To team_one_1_cs
                    allGames.Add(new Tuple <Task <bool>, string>(t, withoutZip));
                }
                Log.TraceMessage(Log.Nav.NavIn, "Starting WaitAny ", Log.LogType.Info);
                Task.WaitAny(allGames.Select(_ => _.Item1).ToArray()); //Wait for all the threads to finish
                Log.TraceMessage(Log.Nav.NavIn, "Finished WaitAny", Log.LogType.Info);

                for (int i = 0; i < allGames.Count; i++)
                {
                    if (allGames[i].Item1.IsCompleted && allGames[i].Item1.Result == false) //Error on compilation
                    {
                        for (int j = 0; j < allGames.Count; j++)
                        {
                            try
                            {
                                allGames[j].Item1.Dispose();
                            }
                            catch (Exception exA)
                            {
                                Log.TraceMessage(Log.Nav.NavOut, "Failed to abort thread..." + exA.Message, Log.LogType.Error);
                            }
                        }
                        Log.TraceMessage(Log.Nav.NavIn, "At least one client failed to compile.", Log.LogType.Info);
                        answers.Add("Other player did not compile"); //winReason
                        answers.Add("You did not compile");          // loseReason

                        if (i == 0)                                  //If the first thread errored, set the winner as the second thread
                        {
                            Log.TraceMessage(Log.Nav.NavIn, "First Thread did not compile.", Log.LogType.Info);
                            string[] reversed = allGames[1].Item2.Split('_').Reverse().ToArray(); // ["cs","1","one","team"]
                            string   teamName = "";
                            for (int j = reversed.Length - 1; j > 1; j--)
                            {
                                teamName += reversed[j] + "_";                           // "team_one_"
                            }
                            teamName = teamName.Substring(0, teamName.Length - 1);       //"team_one"
                            answers.Add(teamName);                                       //winnerName --- May not be 0 -- its in the file somewhere
                            answers.Add(reversed[1]);                                    //winnerSubmissionNumber --- May not be 1 -- its in the file somewhere

                            reversed = allGames[0].Item2.Split('_').Reverse().ToArray(); // ["cs","1","one","team"]
                            teamName = "";
                            for (int j = reversed.Length - 1; j > 1; j--)
                            {
                                teamName += reversed[j] + "_";                     // "team_one_"
                            }
                            teamName = teamName.Substring(0, teamName.Length - 1); //"team_one"
                            answers.Add(teamName);                                 //loserName --- May not be 0 -- its in the file somewhere
                            answers.Add(reversed[1]);                              //loserSubmissionNumber --- May not be 1 -- its in the file somewhere
                        }
                        else //If any other thread errored, set the winner as the first thread
                        {
                            Log.TraceMessage(Log.Nav.NavIn, "Other Thread did not compile.", Log.LogType.Info);
                            string[] reversed = allGames[0].Item2.Split('_').Reverse().ToArray(); // ["cs","1","one","team"]
                            string   teamName = "";
                            for (int j = reversed.Length - 1; j > 1; j--)
                            {
                                teamName += reversed[j] + "_";                           // "team_one_"
                            }
                            teamName = teamName.Substring(0, teamName.Length - 1);       //"team_one"
                            answers.Add(teamName);                                       //winnerName --- May not be 0 -- its in the file somewhere
                            answers.Add(reversed[1]);                                    //winnerSubmissionNumber --- May not be 1 -- its in the file somewhere

                            reversed = allGames[1].Item2.Split('_').Reverse().ToArray(); // ["cs","1","one","team"]
                            teamName = "";
                            for (int j = reversed.Length - 1; j > 1; j--)
                            {
                                teamName += reversed[j] + "_";                     // "team_one_"
                            }
                            teamName = teamName.Substring(0, teamName.Length - 1); //"team_one"
                            answers.Add(teamName);                                 //loserName --- May not be 0 -- its in the file somewhere
                            answers.Add(reversed[1]);                              //loserSubmissionNumber --- May not be 1 -- its in the file somewhere
                        }

                        answers.Add("not created -- compilation failed"); //logURL
                        return(answers);                                  //return early
                    }
                }
                Log.TraceMessage(Log.Nav.NavIn, "All threads successfully compiled.", Log.LogType.Info);

                //Get Final Status from GameServer API
                Result status = HTTP.HTTPPostGetStatus(DA_GAME, gameSession);

                string winReason = status.clients[0].won ? status.clients[0].reason : status.clients[1].reason;
                answers.Add(winReason);
                string loseReason = status.clients[0].lost ? status.clients[0].reason : status.clients[1].reason;
                answers.Add(loseReason);
                string winnerName = status.clients[0].won ? status.clients[0].name : status.clients[1].name;
                string loserName  = status.clients[0].lost ? status.clients[0].name : status.clients[1].name;
                answers.Add(winnerName);
                string winnerSubmissionNumber = "";
                string loserSubmissionNumber  = "";
                foreach (var file in files)
                {
                    Log.TraceMessage(Log.Nav.NavIn, "checking file - " + file, Log.LogType.Info);
                    if (file.Contains(winnerName))
                    {
                        winnerSubmissionNumber = file.Split('_').Reverse().ToArray()[1]; //Maybe not 1 -- its in the file somewhere
                    }
                    if (file.Contains(loserName))
                    {
                        loserSubmissionNumber = file.Split('_').Reverse().ToArray()[1]; //Maybe not 1 -- its in the file somewhere
                    }
                }
                answers.Add(winnerSubmissionNumber);
                answers.Add(loserName);
                answers.Add(loserSubmissionNumber);
                string logURL = status.gamelogFilename;
                answers.Add(logURL);
                return(answers);
            }
            catch (Exception ex)
            {
                Log.TraceMessage(Log.Nav.NavIn, ex);
                return(new List <string>());
            }
        }