示例#1
0
文件: Game.cs 项目: Giska/TheorieJeux
        /// <summary>
        /// Select all the strategies that will be used during the game
        /// </summary>
        /// <param name="Request">A request representing the strategies that must be selected. The sum must be equal to 100.</param>
        public void Select(Request Request)
        {
            if (_Step != Action.Select)
            {
                Console.WriteLine("[ERROR] you can use select only at the beining of the game");
                throw new Exception("Invalid Action");
            }
            if (Request.Count != 100)
            {
                Console.WriteLine("[ERROR] You must select 100 startegies");
                throw new Exception("Invalid command");
            }
            JSON.Object obj = new JSON.Object();
            obj["type"] = "select";
            JSON.Object strategies = new JSON.Object();
            strategies["N"] = Request.N;
            strategies["T"] = Request.T;
            strategies["M"] = Request.M;
            strategies["E"] = Request.E;
            strategies["G"] = Request.G;
            strategies["S"] = Request.S;
            strategies["W"] = Request.W;
            obj["strategies"] = strategies;
            _Parent._Write(obj);
            JSON.Object result = _Parent._ReadObject();
            {
                try
                {
                    JSON.Object you = result["you"] as JSON.Object;
                    _UpdateStock(_You, you["strategies"] as JSON.Object);
                }
                catch { }

                try
                {
                    JSON.Object challenger = result["challenger"] as JSON.Object;
                    _UpdateStock(_Challenger, challenger["strategies"] as JSON.Object);
                }
                catch { }
            }
            _NextStep();
        }
示例#2
0
文件: Game.cs 项目: Giska/TheorieJeux
        /// <summary>
        /// Purchase some strategy using your points
        /// </summary>
        /// <param name="Request">The strategies that must be purchase. Check if Cost is lower or equal to your score before using it.</param>
        public void Purchase(Request Request)
        {
            if (_Step != Action.Buy)
            {
                Console.WriteLine("[ERROR] you can use purchase only after the action");
                throw new Exception("Invalid Action");
            }

            JSON.Object buy = new JSON.Object();
            buy["type"] = "purchase";

            if (Request == null)
            {

            }
            else
            {
                if (_You.Score < Request.Cost)
                {
                    Console.WriteLine("[ERROR] you are trying to buy more than you can");
                    throw new Exception("Invalid Action");
                }
                JSON.Object strategies = new JSON.Object();
                strategies["N"] = Request.N;
                strategies["T"] = Request.T;
                strategies["M"] = Request.M;
                strategies["E"] = Request.E;
                strategies["G"] = Request.G;
                strategies["S"] = Request.S;
                strategies["W"] = Request.W;
                buy["strategies"] = strategies;
            }

            _Parent._Write(buy);
            JSON.Object result = _Parent._ReadObject();
            try
            {
                try
                {
                    JSON.Object you = result["you"] as JSON.Object;
                    _UpdateStock(_You, you["strategies"] as JSON.Object);
                    try { _You._Score = (int)(you["score"] as JSON.Number).Value; }
                    catch { }
                }
                catch
                {
                    Console.WriteLine("[ERROR] Unexpected server response, data have not been updated");
                }

                try
                {
                    JSON.Object challenger = result["challenger"] as JSON.Object;
                    _UpdateStock(_Challenger, challenger["strategies"] as JSON.Object);
                    try { _Challenger._Score = (int)(challenger["score"] as JSON.Number).Value; }
                    catch { }
                }
                catch
                {
                    Console.WriteLine("[ERROR] Unexpected server response, data have not been updated");
                }
            }
            catch { }
            _NextStep();
        }