Beispiel #1
0
        public async Task <IActionResult> StartGame(Room room)
        {
            var wordCount = await _wordRepository.CountAll();

            Random rnd        = new Random();
            var    randomWord = rnd.Next(0, wordCount);
            var    data       = await _wordRepository.GetAll();

            var           word      = data.ToList()[randomWord];
            StringBuilder firstHint = new StringBuilder();

            for (int i = 0; i < word.WordName.Length; i++)
            {
                firstHint.Append("_");
            }

            var           secondHintNumber = rnd.Next(0, word.WordName.Length);
            StringBuilder secondHint       = new StringBuilder();

            for (int i = 0; i < word.WordName.Length; i++)
            {
                if (i != secondHintNumber)
                {
                    secondHint.Append("_");
                }
                else
                {
                    secondHint.Append($"{word.WordName[secondHintNumber]} ");
                }
            }

            var thirdHintNumber = rnd.Next(0, word.WordName.Length);
            var thirdHint       = secondHint.ToString();

            if (thirdHint.ToString()[thirdHintNumber] != '_')
            {
                thirdHint.Remove(thirdHintNumber, 1);
                thirdHint.Insert(thirdHintNumber, word.WordName[thirdHintNumber].ToString());
            }
            else
            {//PROBLEM
                thirdHintNumber = rnd.Next(0, word.WordName.Length);
                thirdHint.Remove(thirdHintNumber, 1);
                thirdHint.Insert(thirdHintNumber, word.WordName[thirdHintNumber].ToString());
            }

            var roomPlayerList = JsonConvert.DeserializeObject <List <ConnectedUser> >(await _redisHandler.GetFromCache("Userlist:ConnectedUser")).Where(x => x.ConnectedRoomName == room.roomName).ToList();
            var shuffledList   = ShuffleList(roomPlayerList);

            await _redisHandler.AddToCache($"Room:StartedGame:{room.roomName}", TimeSpan.FromMinutes(10), JsonConvert.SerializeObject(new StartedGame
            {
                word       = word,
                firstHint  = firstHint.ToString(),
                secondHint = secondHint.ToString(),
                thirdHint  = thirdHint,
                userList   = shuffledList,
                point      = 30,
                turn       = 1
            }));

            await _hubContext.Clients.Group(room.roomName).SendAsync("GameTurn", new { wordName = word.WordName, turn = 1, firstHint = firstHint.ToString(), secondHint = secondHint.ToString() });

            await _hubContext.Clients.Client(shuffledList[0].ConnectionId).SendAsync("YourTurn", true);

            await _hubContext.Clients.Group(room.roomName).SendAsync("StartTurnTimer", true);

            return(await Task.FromResult(Ok()));
        }