Пример #1
0
        private async Task <GameSession> GameCreationHandler(GameRoom room, GamerAccount createdByAccount)
        {
            // trying to find existing session
            var sessionExists = _dataContext.GameSessions
                                .Any(i =>
                                     i.RoomId == room.Id &&
                                     new[] { GameSessionStates.Playing, GameSessionStates.Registration }.Contains(i.State));

            if (sessionExists)
            {
                throw new Exception("Нельзя создавать игру там где она уже есть :)");
            }

            var session = new GameSession
            {
                RoomId = room.Id,
                Room   = room,
                State  = GameSessionStates.Registration,
                CreatedByGamerAccountId = createdByAccount.Id,
                CreatedByGamerAccount   = createdByAccount
            };
            await _dataContext.AddAsync(session);

            await _dataContext.SaveChangesAsync();

            return(session);
        }
Пример #2
0
        private async Task <GameSession> RegistrationHandler(GameRoom room, GamerAccount account)
        {
            var session = await _dataContext.GameSessions
                          .Include(r => r.Room)
                          .Include("GameMembers.GamerAccount")
                          .FirstOrDefaultAsync(g => g.RoomId == room.Id && g.State != GameSessionStates.GameOver);

            if (session == null)
            {
                throw new Exception("Игры в данной комнате не существует. Необходимо создать ее.");
            }

            if (session.State != GameSessionStates.Registration)
            {
                throw new Exception("Нельзя зарегистрироваться, игра уже идет");
            }

            if (session.GameMembers.Any(gm => gm.GamerAccountId == account.Id))
            {
                throw new Exception("Да ты уже в игре! Жди :)");
            }

            session.GameMembers.Add(new GameSessionMember
            {
                GamerAccountId = account.Id,
                GameSessionId  = session.Id
            });
            await _dataContext.SaveChangesAsync();

            await _frontend.SendMessageToRoom(session.Room, $"{account.NickName}, красавчик, в деле!");

            return(session);
        }
Пример #3
0
        private async Task <GameSession> StopGameHandler(GameRoom room, GamerAccount callerAccount)
        {
            var session =
                await _dataContext.GameSessions.FirstOrDefaultAsync(s =>
                                                                    s.RoomId == room.Id && s.State != GameSessionStates.GameOver);

            if (session == null)
            {
                throw new Exception("Игры нет, сначала создай ее, а потом закрывай)");
            }

            if (session.State == GameSessionStates.Playing)
            {
                throw new Exception("Нельзя так! Народ играет!");
            }

            if (session.CreatedByGamerAccountId != callerAccount.Id)
            {
                throw new Exception("Игру может удалить только ее создатель!");
            }

            _dataContext.Remove(session);
            await _dataContext.SaveChangesAsync();

            return(session);
        }
        public async Task SendMessageToGamer(GamerAccount gamer, string message)
        {
            if (gamer.PersonalRoomId == null || gamer.PersonalRoomId == "0")
            {
                return;
            }

            await _bot.LockAndDo(() => _bot.SendTextMessageAsync(gamer.PersonalRoomId, message, ParseMode.Html));
        }
Пример #5
0
 private Task MessageHandler(GamerAccount gamer, string message)
 {
     throw new System.NotImplementedException();
 }