예제 #1
0
        internal static ProcessingResult?StartGameByUrl(ReceivedMessage message)
        {
            //only from waiting list
            ProcessingResult?result = null;

            if (message.Text.Contains("igra.lv"))
            {
                var activeGame = GameManager.CreateGame(message.ChatId, GameType.IgraLv);
                result = GameSetup(message);
            }
            else if (message.Text.Contains("en.cx"))
            {
                var activeGame = GameManager.CreateGame(message.ChatId, GameType.CustomEnCx);
                var parts      = message.Text.Split(new char[] { '\\', '/', '?', '&' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                if (parts[0].Trim().StartsWith("http"))
                {
                    parts.RemoveAt(0);
                }
                activeGame.CustomEnCxDomain = $"http://{parts[0]}";
                var gidPart = parts.Where(x => x.Contains("=") && x.StartsWith("gid"))?.ToList();
                if (gidPart == null || !gidPart.Any())
                {
                    gidPart = parts.SkipWhile(x => x.ToLower() != "play")?.ToList();
                    if (gidPart != null && gidPart.Count > 1)
                    {
                        gidPart.RemoveAt(0);
                    }
                    if (gidPart == null || gidPart.Count > 1 || !long.TryParse(gidPart[0], out var _))
                    {
                        gidPart = null;
                    }
                }

                if (gidPart != null && gidPart.Any())
                {
                    activeGame.EnCxId = gidPart.First().Split('=').Last();
                }

                GameManager.Update(activeGame);


                result = GameSetup(message);
            }


            if (result != null)
            {
                ConversationManager.WaitingList.RemoveAll(x => x.chatId == message.ChatId && x.sender == message.SenderId);
            }
            return(result);
        }
예제 #2
0
 public static ProcessingResult?CreateText(ReceivedMessage message, string response)
 {
     if (String.IsNullOrEmpty(response))
     {
         return(null);
     }
     return(new ProcessingResult()
     {
         Text = response,
         ChatId = message.ChatId,
         Markup = new ReplyKeyboardRemove(),
         DisableWebPagePreview = true,
         IsHtml = false
     });
 }
예제 #3
0
        internal static ProcessingResult?JoinGame(ReceivedMessage message)
        {
            if (message.Parameter == null)
            {
                return(null);
            }
            GameManager.JoinGame(null, message.Parameter, message.ChatId, true);
            var result = GameSetup(message);

            if (result == null)
            {
                return(null);
            }
            result.Delete    = true;
            result.MessageId = message.Id;
            return(result);
        }
예제 #4
0
        internal static ProcessingResult?FoundEncxGame(ReceivedMessage message)
        {
            if (String.IsNullOrEmpty(message.Parameter))
            {
                return(null);
            }

            var activeGame = GameManager.GetActiveGameByChatId(message.ChatId);

            if (activeGame == null)
            {
                return(null);
            }
            activeGame.EnCxId = message.Parameter;
            GameManager.Update(activeGame);

            return(GameSetup(message));
        }
예제 #5
0
        internal static ProcessingResult?MirrorLink(ReceivedMessage message)
        {
            var player = message.SenderId;

            if (Config == null || Config.GetValue <string>("MirrorLink") == null)
            {
                return(null);
            }
            var game           = GameManager.GetActiveGameByChatId(message.ChatId);
            var mirrorLink     = $"{Config.GetValue<string>("MirrorLink").TrimEnd('/','\\')}/{game.Guid}-{player}";
            var mirrorPassword = GetSecretForMirror(game, player);

            return(new ProcessingResult()
            {
                ChatId = player,
                Text = String.Format(Constants.Replies.MIRROR_REPLY_FORMAT, mirrorLink, mirrorPassword)
            });
        }
예제 #6
0
        public static ProcessingResult?ProcessAddress(ReceivedMessage message)
        {
            var input = message.Parameter;

            if (String.IsNullOrEmpty(input))
            {
                return(null);
            }

            var coordinates = Coordinates.ParseCoordinates(input);

            if (coordinates != null)
            {
                var address = LocationsHelper.GetAddress(coordinates);
                if (address != null)
                {
                    return(ProcessingResult.CreateReply(message, address));
                }
            }
            else
            {
                try
                {
                    if (message.Parameter == null)
                    {
                        return(null);
                    }
                    var cords = LocationsHelper.GetCoordinates(message.Parameter);
                    if (cords != null)
                    {
                        return(ProcessingResult.CreateHtml(message, cords));
                    }
                }
                catch (Exception ex)
                {
                    Log.New(ex);
                }
            }

            return(null);
        }
예제 #7
0
        internal static ProcessingResult?SetPrefix(ReceivedMessage message)
        {
            if (message.Parameters == null || !message.Parameters.Any())
            {
                return(null);
            }

            if (message.Parameters?.Count < 2)
            {
                return(new ProcessingResult
                {
                    Text = String.Format(Constants.Replies.SET_PREFIX_FORMAT, message.Parameters[0]),
                    ChatId = message.SenderId,
                    Markup = new ForceReplyMarkup(),
                    MessageId = message.Id
                });
            }
            else
            {
                if (message?.Parameters?.Count > 0 && long.TryParse(message.Parameters[0], out var gameId))
                {
                    var activeGame = GameManager.GetById(gameId);
                    if (activeGame != null)
                    {
                        activeGame.Prefix = message.Parameters[1];
                        GameManager.Update(activeGame);
                        return(new ProcessingResult
                        {
                            Delete = true,
                            MessageId = message.Id,
                            ChatId = message.ChatId
                        });
                    }
                }
            }
            return(null);
        }
예제 #8
0
        internal static ProcessingResult?ShowGameSettings(ReceivedMessage message)
        {
            //var activeGame = GameManager.GetActiveGameByChatId(message.ChatId);
            var activePlayer = GameManager.GetActivePlayerAndGameByChatId(message.ChatId);

            if (activePlayer == null)
            {
                return(null);
            }

            return(new ProcessingResult
            {
                EditMessages = new List <ProcessingResult>
                {
                    new ProcessingResult
                    {
                        ChatId = message.ChatId,
                        Text = Constants.Replies.GAME_FULL,
                        Markup = Constants.Keyboards.GameSettingsAndCommands(activePlayer.Game, activePlayer),
                        MessageId = message.Id
                    }
                }
            });
        }
예제 #9
0
 internal static ProcessingResult?SaveCoordinates(ReceivedMessage message)
 {
     //todo[vg]
     return(null);
 }
예제 #10
0
 internal static ProcessingResult?ExitFromGame(ReceivedMessage message)
 {
     GameManager.ExitFromGame(message.ChatId);
     return(GameSetup(message));
 }
예제 #11
0
        internal static ProcessingResult?StartSetingAuth(ReceivedMessage message)
        {
            var activeGame = GameManager.GetActiveGameByChatId(message.ChatId);

            if (activeGame == null)
            {
                return(null);//todo:ex
            }
            if (String.IsNullOrEmpty(message.Parameter))
            {
                //has nothing
                if (!message.PrivateChat)
                {
                    //send to private
                    GameManager.JoinGame(activeGame.Id, activeGame.Guid, message.ChatId, true);
                    return(new ProcessingResult()
                    {
                        ChatId = message.SenderId,
                        Text = String.Format(Constants.Replies.AUTH_GET_LOGIN_FROM_PUBLIC, activeGame.Guid),
                    });
                }
                else
                {
                    return(new ProcessingResult()
                    {
                        ChatId = message.ChatId,
                        Text = String.Format(Constants.Replies.AUTH_GET_LOGIN, activeGame.Guid),
                        Markup = new ForceReplyMarkup()
                    });
                }
            }
            else if (message.Parameters != null && message.Parameters.Count == 1)
            {
                var loginsConf = Config.GetSection("LOGINS")
                                 ?.GetSection(message.ChatId.ToString())
                                 ?.GetSection(activeGame.Type.ToString());
                var logins = loginsConf?.GetChildren();
                if (logins != null && logins.ToList().Any(x => x.Key == message.Parameter))
                {
                    message.Parameters.Add(logins.Where(x => x.Key == message.Parameter).First().Value);
                }
                //has login
                else
                {
                    return new ProcessingResult()
                           {
                               ChatId    = message.ChatId,
                               Text      = String.Format(Constants.Replies.AUTH_GET_PASSWORD, activeGame.Guid, message.Parameter),
                               Markup    = new ForceReplyMarkup(),
                               Delete    = true,
                               MessageId = message.Id
                           }
                };
            }
            if (message.Parameters != null && message.Parameters.Count == 2)
            {
                //has login and pass
                activeGame.Login    = message.Parameters[0];
                activeGame.Password = message.Parameters[1];
                var engine = IGameEngine.Get(activeGame);
                try
                {
                    if (engine.Login(activeGame))
                    {
                        //activeGame.Update();
                        GameManager.Update(activeGame);
                    }
                }
                catch (Exception ex)
                {
                    Log.New(ex);
                }

                var result = GameSetup(message);
                result.Delete    = true;
                result.MessageId = message.Id;
                return(result);
            }
            return(null);
        }
예제 #12
0
        internal static ProcessingResult?GameSetup(ReceivedMessage message)
        {
            ProcessingResult?result = null;
            var activeGame          = GameManager.GetActiveGameByChatId(message.ChatId);

            if (activeGame == null)
            {
                if (ConversationManager.WaitingList.Any(x => x.chatId == message.ChatId && x.sender == message.SenderId))
                {
                    ConversationManager.WaitingList.RemoveAll(x => x.chatId == message.ChatId && x.sender == message.SenderId);
                }
                ConversationManager.WaitingList.Add((message.ChatId, message.SenderId, ConversationManager.WaitingReason.GameUrl));

                result = new ProcessingResult()
                {
                    ChatId = message.ChatId,
                    Text   = Constants.Replies.NO_ACTIVE_GAME_CREATE,
                    Markup = Constants.Keyboards.CreateNewGame,
                };
            }
            else if (activeGame.Type == GameType.CustomEnCx && String.IsNullOrEmpty(activeGame.CustomEnCxDomain))
            {
                if (message.Parameter == null)
                {
                    return(ProcessingResult.CreateText(message, String.Format(Constants.Replies.EN_CX_NO_DOMAIN, activeGame.Guid)));
                }
                else
                {
                    var parts = message.Parameter.Replace("/", "\\").Split("\\").ToList();
                    if (parts.Count() > 1)
                    {
                        parts = parts.Where(x => x.Contains("en.cx")).Take(1).ToList();
                    }

                    if (parts.Count() == 1)
                    {
                        var domain = parts.First();
                        if (!domain.Contains("en.cx"))
                        {
                            domain += ".en.cx";
                        }
                        activeGame.CustomEnCxDomain = $"http://{domain}";
                        GameManager.Update(activeGame);
                        return(GameSetup(message));
                    }
                }
            }
            else if (activeGame.Login == null || activeGame.Password == null)
            {
                if (IGameEngine.Get(activeGame) is IEnCxGameEngine && activeGame.EnCxId == null)
                {
                    result = FindEnCxGame(message);
                }
                else
                {
                    var _markup    = Constants.Keyboards.GameWithoutAuth(activeGame);
                    var loginsConf = Config.GetSection("LOGINS")
                                     ?.GetSection(message.ChatId.ToString())
                                     ?.GetSection(activeGame.Type.ToString());
                    if (loginsConf != null)
                    {
                        var logins = loginsConf.GetChildren();
                        if (logins != null && logins.ToList().Any())
                        {
                            var buttons = _markup.InlineKeyboard.ToList();
                            buttons.AddRange(
                                logins.Select(x => new List <InlineKeyboardButton> {
                                new InlineKeyboardButton {
                                    Text = $"Login as: {x.Key}", CallbackData = $"/{Constants.Commands.SetAuth} {x.Key}"
                                }
                            }).ToList()
                                );
                            _markup = new InlineKeyboardMarkup(buttons);
                        }
                    }
                    result = new ProcessingResult()
                    {
                        ChatId = message.ChatId,
                        Text   = String.Format(Constants.Replies.GAME_NO_AUTH, activeGame.Guid, activeGame.Type.ToString()),
                        Markup = _markup
                    };
                }
            }
            else
            {
                result = new ProcessingResult()
                {
                    ChatId = message.ChatId,
                    Text   = Constants.Replies.GAME_FULL,
                    Markup = Constants.Keyboards.GameCommands(activeGame)
                };
            }

            if (result != null && message.IsCallback)
            {
                result.MessageId = message.Id;
                result           = new ProcessingResult()
                {
                    EditMessages = new List <ProcessingResult>
                    {
                        result
                    }
                };
            }

            if (result != null && message.Command == Constants.Commands.GameSetup)
            {
                result.Delete    = true;
                result.MessageId = message.Id;
            }

            return(result);
        }
예제 #13
0
        public static ProcessingResult?ProcessMessage(ReceivedMessage message)
        {
            try
            {
                string?resultString = null;
                if (!String.IsNullOrWhiteSpace(message.Command))
                {
                    switch (message.Command)
                    {
                    case "u":
                        if (message.Parameter == "happy?")
                        {
                            return(ProcessingResult.CreateText(message, (new Random(DateTime.Now.Second).Next(0, 2) == 1) ? "Yes" : "No"));
                        }
                        return(null);

                    case "foo":
                        return(ProcessingManager.Foo(message));

                    case Constants.Commands.Start:
                    case Constants.Commands.Help:
                    case Constants.Commands.HelpShort:
                        return(ProcessingResult.CreateText(message, Constants.Replies.HELP));

                    case Constants.Commands.Address:
                        return(ProcessingManager.ProcessAddress(message));

                    case Constants.Commands.List:
                    case Constants.Commands.ListShort:
                        resultString = ListManager.GetList(message.ChatId);
                        if (resultString != null)
                        {
                            return(ProcessingResult.CreateText(message, resultString));
                        }
                        return(null);

                    case Constants.Commands.ListSorted:
                    case Constants.Commands.ListSortedShort:
                        resultString = ListManager.GetList(message.ChatId, true);
                        if (resultString != null)
                        {
                            return(ProcessingResult.CreateText(message, resultString));
                        }
                        return(null);

                    case Constants.Commands.ClearList:
                    case Constants.Commands.ClearListShort:
                        ListManager.ClearList(message.ChatId);
                        return(null);

                    case Constants.Commands.SaveCoordinates:
                        return(ProcessingManager.SaveCoordinates(message));

                    case Constants.Commands.GetChatId:
                        return(ProcessingResult.CreateText(message, message.ChatId.ToString()));

                    case Constants.Commands.DeleteMessage:
                        return(new ProcessingResult()
                        {
                            Delete = true, ChatId = message.ChatId, MessageId = message.Id
                        });

                    case Constants.Commands.GameSetup:
                        return(ProcessingManager.GameSetup(message));

                    case Constants.Commands.CreateDemoGame:
                        return(ProcessingManager.CreateGame(message, GameType.Demo));

                    case Constants.Commands.CreateQuestGame:
                        return(ProcessingManager.CreateGame(message, GameType.Quest));

                    case Constants.Commands.CreateEncxGame:
                        return(ProcessingManager.CreateGame(message, GameType.CustomEnCx));

                    case Constants.Commands.CreateIgraGame:
                        return(ProcessingManager.CreateGame(message, GameType.IgraLv));

                    case Constants.Commands.SetAuth:
                        return(ProcessingManager.StartSetingAuth(message));

                    case Constants.Commands.JoinGameLink:
                        return(ProcessingManager.JoinGame(message));

                    case Constants.Commands.ExitGame:
                        return(ProcessingManager.ExitFromGame(message));

                    case Constants.Commands.ShowGameSettings:
                        return(ProcessingManager.ShowGameSettings(message));

                    case Constants.Commands.GetTask:
                        return(ProcessingManager.GetTask(message));

                    case Constants.Commands.FindEncxGame:
                        return(ProcessingManager.FindEnCxGame(message));

                    case Constants.Commands.FoundEncxGame:
                        return(ProcessingManager.FoundEncxGame(message));

                    case Constants.Commands.SetPrefix:
                        return(ProcessingManager.SetPrefix(message));

                    case Constants.Commands.SetRadius:
                        return(ProcessingManager.SetRadius(message));

                    case Constants.Commands.GameTaskNoUpdates:
                    case Constants.Commands.GameTaskUpdateStatus:
                    case Constants.Commands.GameTaskUpdateText:
                        return(ProcessingManager.SetTaskUpdate(message));

                    case Constants.Commands.EnterCode:
                        return(ProcessingManager.EnterCode(message));

                    case Constants.Commands.MirrorLink:
                        return(ProcessingManager.MirrorLink(message));

                    default:
                        break;
                    }
                }
                if (message.Text != null && message.Text.StartsWith(Constants.SpecialCommands.AddListValue))
                {
                    ListManager.AddValue(message.ChatId, message.Text.Substring(Constants.SpecialCommands.AddListValue.Length));
                    return(null);
                }
                if (message.Text != null && message.Text.StartsWith(Constants.SpecialCommands.EnterCode))
                {
                    message.Parameter = message.Text.Substring(Constants.SpecialCommands.EnterCode.Length);
                    return(ProcessingManager.EnterCode(message));
                }
                var waiting4List = ConversationManager.WaitingList.Where(x => x.chatId == message.ChatId && x.sender == message.SenderId)?.ToList();
                var waiting4     = (waiting4List == null || !waiting4List.Any()) ? ConversationManager.WaitingReason.None : waiting4List.First().waitingFor;
                switch (waiting4)
                {
                case ConversationManager.WaitingReason.None:
                    break;

                case ConversationManager.WaitingReason.GameUrl:
                    var result = ProcessingManager.StartGameByUrl(message);
                    if (result != null)
                    {
                        return(result);
                    }
                    break;

                default:
                    break;
                }
                var chatPrefix = StaticData.Prefixes.Get(message.ChatId);
                if (chatPrefix != null &&
                    message.Text != null &&
                    message.Text.Length > chatPrefix.Length)
                {
                    if (chatPrefix == message.Text.Substring(0, chatPrefix.Length))
                    {
                        message.Parameter = message.Text;
                        return(ProcessingManager.EnterCode(message));
                    }
                }
                if (message.Coordinates != null)
                {
                    return(new ProcessingResult()
                    {
                        ChatId = message.ChatId,
                        ReplyTo = message.Id,
                        DisableWebPagePreview = true,
                        Markup = Constants.Keyboards.CoordinatesKeyboard(message.Coordinates),
                        Text = $"{message.Coordinates.Lat},{message.Coordinates.Lon}"
                    });
                }
                var location = Coordinates.ParseCoordinates(message.Text);
                if (location != null)
                {
                    return(ProcessingResult.CreateCoordinates(message, location));
                }
                if (message.ReplyToBot)
                {
                    return(ConversationManager.Process(message));
                }
            }
            catch (Exception ex)
            {
                //switch (ex.Level)
                //{
                //    case GCException.LevelType.Fatal:
                //        throw new Exception(ex.Message);
                //    case GCException.LevelType.Chat:
                //        return ProcessingResult.CreateText(message, ex.Message);
                //    case GCException.LevelType.ChatFull:
                //        return ProcessingResult.CreateText(message, $"Code:{ex.Code}, Message:{ex.Message}, Stack:{ex.StackTrace}");
                //    case GCException.LevelType.Log:
                //        Log.New(ex, message);
                //        break;
                //    case GCException.LevelType.Quiet:
                //        break;
                //}
                //return ProcessingResult.CreateText(message, $"Message: {ex.Message??""}; StackTrace: {ex.StackTrace??""};");
            }
            //return ProcessingResult.CreateText(message, "Nothing to see here");
            return(null);
        }