PuzzleController.cs 1.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using NPuzzle.Core;
using NPuzzle.Entity;
using NPuzzle.Models;
using NPuzzle.Service;

namespace NPuzzle.Controllers
{
    public class PuzzleController : Controller
    {
        IScoreService _scoreService = new ScoreServiceEF();

        public IActionResult Index()
        {
            var field = new Field(4, 4);
            HttpContext.Session.SetObject("field", field);

            var model = PrepareModel("New field created");
            return View(model);
        }

        public IActionResult Move(int tile)
        {
            var field = (Field) HttpContext.Session.GetObject("field");
            field.Move(tile);
            HttpContext.Session.SetObject("field", field);

            if (field.IsSolved())
                _scoreService.AddScore(new Score { Player = "Janko", Points = field.GetScore()});

            var model = PrepareModel("Moved");
            return View("Index", model);
        }

        private PuzzleModel PrepareModel(string message)
        {
            return new PuzzleModel
            {
                Field = (Field) HttpContext.Session.GetObject("field"),
                Message = message,
                Scores = _scoreService.GetTopScores()
            };
        }
    }
}