/*
         *
         * public int Id;
         * public int Surplus;
         * public int TurksDisValue;
         * public int MachineDisValue;
         * public int TimeOut;
         * public double Stubborn;
         * public bool MachineStarts;
         */
        public async Task SaveGameParameters(GameParametersModel gp)
        {
            string sql;

            if (gp.Id != 0)
            {
                sql = @"UPDATE [dbo].[GameParameters] 
                        SET 
                            Surplus = @Surplus,
                            TurksDisValue = @TurksDisValue,
                            MachineDisValue = @MachineDisValue,
                            TimeOut = @TimeOut,
                            Stubborn = @Stubborn,
                            MachineStarts = @MachineStarts,
                            ShowMachinesDisValue = @ShowMachinesDisValue
                        WHERE Id = @Id";
            }
            else
            {
                sql = @"INSERT INTO [dbo].[GameParameters](
                            Surplus,
                            TurksDisValue,
                            MachineDisValue,
                            TimeOut,
                            Stubborn,
                            MachineStarts,
                            ShowMachinesDisValue
                            ) 
                        VALUES (
                            @Surplus,
                            @TurksDisValue,
                            @MachineDisValue,
                            @TimeOut,
                            @Stubborn,
                            @MachineStarts,
                            @ShowMachinesDisValue
                        )";
            }

            await _db.SaveDataAsync <GameParametersModel>(sql, gp);
        }
예제 #2
0
        /// <summary>
        /// Starts new game using first not used GameParameter
        /// </summary>
        /// <param name="workerId"></param>
        /// <returns>new game or null if there are no more unused GameParameters</returns>
        public async Task <GameInfo> StartNewGame(string workerId, string algoVersion)
        {
            string sql =
                @"select Top 1 gp.* from GameParameters gp
                  left join (
                     select Games.Id, Games.GameParameterId, Games.SessionId 
                     from Games
                     left join Sessions
                     on Sessions.Id = Games.SessionId
                     where Sessions.WorkerId = @WorkerId) AS G
                  on gp.Id = G.GameParameterId
                  where G.SessionId is null;";

            GameParametersModel gameParameter = null;

            try
            {
                gameParameter = await _db.LoadDataSingleAsync <dynamic, GameParametersModel>(sql, new { WorkerId = workerId });
            }
            catch (SqlException e)
            {
                Debug.WriteLine(e);
                throw;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                throw;
            }


            if (gameParameter is null)
            {
                return(null);
            }

            var g = new
            {
                WorkerId             = workerId,
                GameParameterId      = gameParameter.Id,
                StartTime            = DateTime.UtcNow,
                Surplus              = gameParameter.Surplus,
                TurksDisValue        = gameParameter.TurksDisValue,
                MachineDisValue      = gameParameter.MachineDisValue,
                TimeOut              = gameParameter.TimeOut,
                Stubborn             = gameParameter.Stubborn,
                MachineStarts        = gameParameter.MachineStarts,
                ShowMachinesDisValue = gameParameter.ShowMachinesDisValue,
                AlgoVersion          = algoVersion,
            };

            sql = @"insert into dbo.Games 
                              (SessionId,
                               GameParameterId, StartTime, Surplus, TurksDisValue, MachineDisValue, TimeOut, Stubborn, MachineStarts, ShowMachinesDisValue, AlgoVersion)
                           output inserted.*
                           values 
                              ((select Id from Sessions where WorkerId = @WorkerId), 
                               @GameParameterId, @StartTime, @Surplus, @TurksDisValue, @MachineDisValue, @TimeOut, @Stubborn, @MachineStarts, @ShowMachinesDisValue, @AlgoVersion)";
            try
            {
                var res = await _db.SaveData <dynamic, GameModel>(sql, g);

                return(new GameInfo()
                {
                    Game = res,
                    Moves = new List <MoveModel>(),
                });
            }
            catch (SqlException)
            {
                return(null);
            }
        }