コード例 #1
0
        public MazeModel CreateMazeCharacters(Guid mazeId)
        {
            try
            {
                if (mazeId != null && mazeId != Guid.Empty)
                {
                    Random randomGenerator = new Random();

                    Maze maze = MemoryCacher.GetMazeFromCache(mazeId);
                    MemoryCacher.DeleteDomokunMovesFromCache(mazeId);

                    //clear characters if new game is choosed
                    MazeHelper.ClearMazeCharacters(maze);
                    int mazeHeight = maze.GetMazeHeight();
                    int mazeWidth  = maze.GetMazeWidth();

                    int randomPonyId     = 0;
                    int randomDomokunId  = 0;
                    int randomEndpointId = 0;
                    do
                    {
                        randomPonyId     = randomGenerator.Next(mazeHeight * mazeWidth);
                        randomDomokunId  = randomGenerator.Next(mazeHeight * mazeWidth);
                        randomEndpointId = randomGenerator.Next(mazeHeight * mazeWidth);
                    }while (CheckIfTwoSameRandoms(randomPonyId, randomDomokunId, randomEndpointId));

                    maze.SetDomokunId(randomDomokunId);
                    maze.SetPonyId(randomPonyId);
                    maze.SetEndpointId(randomEndpointId);

                    MemoryCacher.UpdateMazeInCache(mazeId, maze);
                    DifficultyManager.DifficultyCheckForPrecalculation(maze);

                    return(new MazeModel
                    {
                        Pony = randomPonyId,
                        Domokun = randomDomokunId,
                        Endpoint = randomEndpointId,
                        Maze = maze,
                        Difficulty = maze.GetDifficulty(),
                        Size = new List <int> {
                            mazeHeight, mazeWidth
                        },
                        GameState = new GameState {
                            State = State.Active, StateResult = StateResult.SuccesfullyCreated
                        },
                        MazeId = mazeId
                    });
                }
                else
                {
                    HttpResponseException exception = CreateResponseException(HttpStatusCode.BadRequest, ERRORMESSAGE_MAZEID_INVALID);
                    throw exception;
                }
            }
            catch (Exception ex)
            {
                HttpResponseException exception = CreateResponseException(HttpStatusCode.InternalServerError, ex.Message);
                throw exception;
            }
        }