예제 #1
0
        static void Main(string[] args)
        {
            //色々初期化
            GameMaster.Initialize();

            //サーバ開始
            GameServer.StartGameServer();

            Console.WriteLine("Enterで競技開始");
            Console.ReadLine();
            GameServer.StartedAtUnixTime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;

            //0ターン目開始
            Console.WriteLine("デュエル!");
            GameServer.IsIntervall = false;
            GameMaster.TurnTimer.Start();


            Console.ReadLine();
            JsonInOut.NGO();
            while (true)
            {
                Console.ReadLine();
            }
        } //End_Main
예제 #2
0
        }         //End_Method

        //-----------------------publicメソッド-----------------------
        /// <summary>
        /// 静的コンストラクタ
        /// </summary>
        public static void Initialize()
        {
            //試合事前情報取得API準備
            using (var sr = new StreamReader(@"..\..\JsonFiles\GameInfo.json", Encoding.UTF8)) {
                JsonInOut.GameInfoJson = sr.ReadToEnd();
                //GameMasterの設定
                _GameInfo[] info;
                using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(JsonInOut.GameInfoJson))) {
                    var serializer = new DataContractJsonSerializer(typeof(_GameInfo[]));
                    info = (_GameInfo[])serializer.ReadObject(ms);
                }                 //End_Using
                GameMaster.GameID         = info[0].ID;
                GameMaster.TurnMills      = info[0].TurnMillis;
                GameMaster.IntervalMillis = info[0].IntervalMillis;
                GameMaster.TurnOfGameEnd  = info[0].Turns;
                GameMaster.TurnTimer      = new Timer(GameMaster.TurnMills);
                GameMaster.IntervalTimer  = new Timer(GameMaster.IntervalMillis);
            }             //End_Using

            //試合状態取得API準備,Turn0の状態を取得する
            using (var sr = new StreamReader(@"..\..\JsonFiles\FieldInfo.json", Encoding.UTF8)) {
                var fieldJson = sr.ReadToEnd();
                var table     = JsonInOut.FieldInfoToMyGameTable(fieldJson, 1, 2);
                for (int i = 0; i < table.AgentNum; ++i)
                {
                    table.MyTeam.Agents[i].X -= 1;
                    table.MyTeam.Agents[i].Y -= 1;
                    table.OpTeam.Agents[i].X -= 1;
                    table.OpTeam.Agents[i].Y -= 1;
                }
                GameMaster.Table = table;
            }             //End_Using

            Console.WriteLine("JsonInOut.cs 静的コンストラクタ終了");
        }         //End_Constructor
예제 #3
0
        //-----------------------コンストラクタ-----------------------

        //-----------------------privateメソッド-----------------------

        //-----------------------publicメソッド-----------------------

        public static async void StartGameServer()
        {
            //HttpListenerで接続待ち
            var httpListener = new HttpListener();

            httpListener.Prefixes.Add(GameServer.URI);
            httpListener.Start();

            while (true)
            {
                var listenerContext = await httpListener.GetContextAsync();

                var response = listenerContext.Response;
                var headers  = listenerContext.Request.Headers;
                Console.WriteLine("New Access!!  token : " + listenerContext.Request.Headers.GetValues("Authorization").First());

                //試合情報取得APIの要求
                if (listenerContext.Request.RawUrl == "/matches")
                {
                    //HttpMethodが違う
                    if (listenerContext.Request.HttpMethod != "GET")
                    {
                        response.StatusCode = 405;
                        var c = Encoding.UTF8.GetBytes("Method Not Allowed");
                        response.OutputStream.Write(c, 0, c.Length);
                        //デバッグ
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("試合事前情報取得API  StatusCode_405 : Method_Not_Allowd");
                        Console.ForegroundColor = ConsoleColor.White;
                    }                     //Invalid Token
                    else if (!GameServer.AuthorizationTokens.ContainsKey(headers.GetValues("Authorization").First()))
                    {
                        response.StatusCode = 401;
                        var c = Encoding.UTF8.GetBytes("Invalied Token");
                        response.OutputStream.Write(c, 0, c.Length);
                        //デバッグ
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("試合事前情報取得API  StatusCode_401 : Invalied_Token");
                        Console.ForegroundColor = ConsoleColor.White;
                    }                     //正常なリクエスト
                    else
                    {
                        //正常なアクセス
                        response.StatusCode = 200;
                        //トークンによって返すJSONを整形
                        var c = Encoding.UTF8.GetBytes(JsonInOut.GetGameInfo(GameServer.AuthorizationTokens[headers.GetValues("Authorization").First()]));
                        response.OutputStream.Write(c, 0, c.Length);
                        //デバッグ
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("試合事前情報取得API  StatusCode_200 : Accept");
                        Console.ForegroundColor = ConsoleColor.White;
                    }             //End_IfElse
                }                 //End_If

                //Intervall時間,試合開始前ならうんち返す
                else if (GameServer.IsIntervall)
                {
                    response.StatusCode = 400;
                    var c = Encoding.UTF8.GetBytes("TooEarly");
                    response.OutputStream.Write(c, 0, c.Length);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("TooEarcy or Intervall\r\n");
                    Console.ForegroundColor = ConsoleColor.White;
                }                 //End_If

                //試合状態取得APIの要求
                else if (listenerContext.Request.RawUrl == "/matches/1919")
                {
                    //HttpMethodが違う
                    if (listenerContext.Request.HttpMethod != "GET")
                    {
                        response.StatusCode = 405;
                        var c = Encoding.UTF8.GetBytes("Method Not Allowed");
                        response.OutputStream.Write(c, 0, c.Length);
                        //デバッグ
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("試合事前情報取得API  StatusCode_405 : Method_Not_Allowd");
                        Console.ForegroundColor = ConsoleColor.White;
                    }                     //Invalid Token
                    else if (!GameServer.AuthorizationTokens.ContainsKey(headers.GetValues("Authorization").First()))
                    {
                        response.StatusCode = 401;
                        var c = Encoding.UTF8.GetBytes("Invalied Token");
                        response.OutputStream.Write(c, 0, c.Length);
                        //デバッグ
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("試合事前情報取得API  StatusCode_401 : Invalied_Token");
                        Console.ForegroundColor = ConsoleColor.White;
                    }                     //正常なリクエスト
                    else
                    {
                        //正常なアクセス
                        response.StatusCode = 200;
                        //現在のフィールド状態を返す
                        string fieldInfoJson = JsonInOut.FieldInfoToJson();
                        var    c             = Encoding.UTF8.GetBytes(fieldInfoJson);
                        response.OutputStream.Write(c, 0, c.Length);
                        //デバッグ
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("試合事前情報取得API  StatusCode_200 : Accept");
                        Console.ForegroundColor = ConsoleColor.White;
                    }             //End_IfElse
                }                 //End_ElseIf

                //行動更新API送信の要求
                else if (listenerContext.Request.RawUrl == "/matches/1919/action")
                {
                    //HttpMethodが違う
                    if (listenerContext.Request.HttpMethod != "POST")
                    {
                        response.StatusCode = 405;
                        var c = Encoding.UTF8.GetBytes("Method Not Allowed");
                        response.OutputStream.Write(c, 0, c.Length);
                        //デバッグ
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("試合事前情報取得API  StatusCode_405 : Method_Not_Allowd");
                        Console.ForegroundColor = ConsoleColor.White;
                    }                     //Invalid Token
                    else if (!GameServer.AuthorizationTokens.ContainsKey(headers.GetValues("Authorization").First()))
                    {
                        response.StatusCode = 401;
                        var c = Encoding.UTF8.GetBytes("Invalied Token");
                        response.OutputStream.Write(c, 0, c.Length);
                        //デバッグ
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("試合事前情報取得API  StatusCode_401 : Invalied_Token");
                        Console.ForegroundColor = ConsoleColor.White;
                    }                     //Jsonでないとき
                    else if (listenerContext.Request.ContentType != "application/json")
                    {
                        response.StatusCode = 415;
                        var c = Encoding.UTF8.GetBytes("Content-Type Error");
                        response.OutputStream.Write(c, 0, c.Length);
                        //デバッグ
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("試合事前情報取得API  StatusCode_415 : Content-Type_Error");
                        Console.ForegroundColor = ConsoleColor.White;
                    }                     //正常なリクエスト
                    else
                    {
                        //リクエストからJSONデータを取得
                        string jsonData;
                        using (var reader = new StreamReader(listenerContext.Request.InputStream)) {
                            jsonData = reader.ReadToEnd();
                        }                         //End_Using

                        Console.WriteLine("行動送信リクエスト");
                        Console.WriteLine(jsonData);

                        //取得したJSONデータをデシリアライズする
                        var actions = JsonInOut.TurnActions;
                        var result  = JsonInOut.TeamAgentsActionsToMyActions(jsonData, GameServer.AuthorizationTokens[headers.GetValues("Authorization").First()]);

                        var c = Encoding.UTF8.GetBytes(jsonData);
                        response.OutputStream.Write(c, 0, c.Length);

                        //デバッグ
                        Console.ForegroundColor = result ? ConsoleColor.Green: ConsoleColor.Red;
                        Console.WriteLine("行動更新API  StatusCode_200 : Accepted  Result:" + result);
                        Console.ForegroundColor = ConsoleColor.White;
                    }             //End_Else
                }                 //End_ElseIf

                Console.WriteLine();
                response.Close();
            } //End_While
        }     //End_Method