Beispiel #1
0
        public static async Task PlaceSign(int x, int y, int id, string text, int morph)
        {
            await Con.SendAsync(MessageType.PlaceBlock, 1, x, y, id, text, morph);

            World[1, x, y] = new Sign(id, text, morph);
        }
Beispiel #2
0
        static async Task Main3(Message m)
        {
            Player player;
            Player playerInGame;


            /*if (m.Type != MessageType.PlayerMove)
             *   if (m.Type != MessageType.Chat)
             *   {
             *       Console.WriteLine(m.Type);
             *       Console.WriteLine(m);
             *   }*/

            switch (m.Type)
            {
            case MessageType.Init:
            {
                Console.WriteLine("Logged in!");
                Console.WriteLine();
                await Say($"Connected!");

                World = new Block[2, m.GetInt(9), m.GetInt(10)];

                Width  = m.GetInt(9);
                Height = m.GetInt(10);
                BotId  = m.GetInt(0);

                int index = 11;
                for (int _y = 0; _y < Width; _y++)
                {
                    for (int _x = 0; _x < Height; _x++)
                    {
                        int value = 0;
                        if (m[index++] is int iValue)
                        {
                            value = iValue;
                        }

                        var backgroundId = value >> 16;
                        var foregroundId = 65535 & value;

                        World[1, _x, _y] = new Block(foregroundId);
                        World[0, _x, _y] = new Block(backgroundId);
                        switch (foregroundId)
                        {
                        case 55:
                        case 56:
                        case 57:
                        case 58:
                            string text  = m.GetString(index++);
                            int    morph = m.GetInt(index++);
                            World[1, _x, _y] = new Sign(foregroundId, text, morph);
                            break;

                        case 59:
                            int  rotation = m.GetInt(index++);
                            int  p_id     = m.GetInt(index++);
                            int  t_id     = m.GetInt(index++);
                            bool flip     = m.GetBool(index++);
                            World[1, _x, _y] = new Portal(foregroundId, rotation, p_id, t_id, flip);
                            break;

                        case 93:
                        case 94:
                            int r = m.GetInt(index++);
                            World[1, _x, _y] = new Effect(foregroundId, r);
                            break;

                        case 98:
                        case 99:
                            int sid = m.GetInt(index++);
                            World[1, _x, _y] = new Switch(foregroundId, sid);
                            break;

                        case 100:
                        case 104:
                            int  sid2 = m.GetInt(index++);
                            bool inv  = m.GetBool(index++);
                            World[1, _x, _y] = new Door(foregroundId, sid2, inv);
                            break;
                        }
                    }
                }

                /*for (int x = 0; x < Width; x++)
                 *  for (int y = 0; y < Height; y++)
                 *      if (World[1, x, y].Id == 20)
                 *          //await PlaceBlock(1, x, y, 80);*/

                startTime     = DateTime.Now;
                Tick          = new System.Timers.Timer(50);
                Tick.Elapsed += async(object s, ElapsedEventArgs e) => { await TickEvent(s, e); };

                await RegenerateMapVoters();
            }
            break;

            case MessageType.PlayerJoin:
            case MessageType.PlayerAdd:
                var playerExisting = Players.FirstOrDefault(p => p.Name == m.GetString(1).ToLower());
                Players.Add(new Player(m.GetInt(0), m.GetString(1).ToLower()));
                player = Players.FirstOrDefault(p => p.Id == m.GetInt(0));

                Console.Write("+ ", Color.Green);
                if (player.IsMod)
                {
                    Console.Write(player.Name, Color.Orange);
                    player.OnMessage = Player.AdminMessageHandler;
                    await SayCommand($"giveedit {player.Name}");
                }
                else
                {
                    Console.Write(player.Name, Color.Silver);
                    player.OnMessage = Player.DefaultMessageHandler;

                    await player.Tell("The game is currently closed for moderators due to testing");
                }
                Console.WriteLine($" joined! ({new Random(player.Name.GetHashCode()).Next(0, 101)}% noob)");

                break;


            case MessageType.PlayerExit:
                player = Players.FirstOrDefault(p => p.Id == m.GetInt(0));
                Players.RemoveAll(p => p.Id == m.GetInt(0));

                Console.Write("- ", Color.Red);
                if (player.IsMod)
                {
                    Console.Write(player.Name, Color.Orange);
                }
                else
                {
                    Console.Write(player.Name, Color.Silver);
                }
                Console.WriteLine(" left!");

                // Did he leave while in game?
                if (PlayersInGame.FirstOrDefault(p => p.Id == player.Id) != null)
                {
                    PlayersInGame.RemoveAll(p => p.Id == player.Id);
                    if (PlayersSafe.Keys.FirstOrDefault(p => p.Id == player.Id) != null)
                    {
                        PlayersSafe.Remove(player);
                    }

                    if (2 * PlayersSafe.Count >= PlayersInGame.Count)
                    {
                        if (PlayersSafe.Count > 1)
                        {
                            await Say($"Round over! Players not finished are eliminated!");
                        }
                        await ContinueGame();
                    }
                }

                break;

            case MessageType.PlayerGod:
                player     = Players.FirstOrDefault(p => p.Id == m.GetInt(0));
                player.Afk = m.GetBool(1);

                // Did he use god mode to cheat?
                if (PlayersInGame.FirstOrDefault(p => p.Id == player.Id) != null)
                {
                    PlayersInGame.RemoveAll(p => p.Id == player.Id);
                    if (PlayersSafe.Keys.FirstOrDefault(p => p.Id == player.Id) != null)
                    {
                        PlayersSafe.Remove(player);
                    }

                    if (2 * PlayersSafe.Count >= PlayersInGame.Count)
                    {
                        if (PlayersSafe.Count > 1)
                        {
                            await Say($"Round over! Players not finished are eliminated!");
                        }
                        await ContinueGame();
                    }

                    await player.Tell($"You are eliminated!");
                    await SayCommand($"takegod {player.Name}");
                    await SayCommand($"reset {player.Name}");
                }

                /*else
                 * {
                 *  await player.Tell($"You are {(player.Afk ? "now" : "no longer")} afk!");
                 * }*/
                break;

            case MessageType.PlayerMove:
                player       = Players.FirstOrDefault(p => p.Id == m.GetInt(0));
                playerInGame = PlayersInGame.FirstOrDefault(p => p.Id == m.GetInt(0));

                {
                    int facex = m.GetInt(3);
                    int facey = m.GetInt(4);

                    double mx = m.GetDouble(5);
                    double my = m.GetDouble(6);

                    // If ingame, check when to close the door
                    if (playerInGame != null)
                    {
                        if (mx > TopLeftShiftCoord.X + 1 && mx < TopLeftShiftCoord.X + 42 && my > TopLeftShiftCoord.Y + 1 && my < TopLeftShiftCoord.X + 20)
                        {
                            if (isDoorOpen)
                            {
                                Console.WriteLine("...1");
                                EntranceMovement.Start();
                            }
                        }
                    }

                    // If not afk: Allow to vote
                    if (!player.Afk)
                    {
                        if (!isBuilding)
                        {
                            foreach (MapVote mv in MapVoteSigns)
                            {
                                if (mv.Inited)
                                {
                                    if (Math.Pow(mx - mv.X, 2) + Math.Pow(my - mv.Y, 2) < 0.5)
                                    {
                                        if (facey == -1)
                                        {
                                            await mv.NewVote(player);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    // Users in godmode should not annoy players in game
                    if (player.Afk && playerInGame == null)
                    {
                        if (mx > TopLeftShiftCoord.X - 1 && mx < TopLeftShiftCoord.X + 44 && my > TopLeftShiftCoord.Y - 1 && my < TopLeftShiftCoord.X + 22)
                        {
                            Console.WriteLine("...2");
                            await SayCommand($"takegod {player.Name}");
                            await SayCommand($"reset {player.Name}");

                            //await SayCommand($"tp {player.Name} 49 88");
                        }
                    }
                }

                break;

            //case MessageType.Won:
            //    await PlayerWon(Players.FirstOrDefault(p => p.Id == m.GetInt(0)));
            case (MessageType)32:
                player       = Players.FirstOrDefault(p => p.Id == m.GetInt(0));
                playerInGame = PlayersInGame.FirstOrDefault(p => p.Id == m.GetInt(0));

                if (m.GetInt(2) == 261)
                {
                    if (playerInGame != null)
                    {
                        await PlayerWon(Players.FirstOrDefault(p => p.Id == m.GetInt(0)));
                    }
                }
                break;

            case MessageType.PlaceBlock:
                int l = m.GetInt(1);
                int x = m.GetInt(2);
                int y = m.GetInt(3);

                Block blockBefore = World[l, x, y];

                // Assign block to the world
                World[l, x, y] = new Block(m.GetInt(4));

                switch (m.GetInt(4))
                {
                // Signs
                case 55:
                case 56:
                case 57:
                case 58:
                    string text  = m.GetString(5);
                    int    morph = m.GetInt(6);
                    World[l, x, y] = new Sign(m.GetInt(4), text, morph);
                    break;

                // Portals
                case 59:
                    int  rotation = m.GetInt(5);
                    int  p_id     = m.GetInt(6);
                    int  t_id     = m.GetInt(7);
                    bool flip     = m.GetBool(8);
                    World[l, x, y] = new Portal(m.GetInt(4), rotation, p_id, t_id, flip);
                    break;

                // Effects
                case 93:
                case 94:
                    int r = m.GetInt(5);
                    World[l, x, y] = new Effect(m.GetInt(4), r);
                    break;

                // Switches
                case 98:
                case 99:
                    int sid = m.GetInt(5);
                    World[1, x, y] = new Switch(m.GetInt(4), sid);
                    break;

                // Switch Doors
                case 100:
                    int  sid2 = m.GetInt(5);
                    bool inv  = m.GetBool(5);
                    World[1, x, y] = new Door(m.GetInt(4), sid2, inv);
                    break;
                }
                break;

            case MessageType.Chat:
                if (m.GetInt(0) != BotId)
                {
                    player = Players.FirstOrDefault(p => p.Id == m.GetInt(0));
                    await player.OnMessage(new Command(player, m.GetString(1)));
                }

                break;
            }
        }
Beispiel #3
0
        public async Task MessageHandler(Message m)
        {
            Player player;

            switch (m.Type)
            {
            case MessageType.Init:
                await Con.SendAsync(MessageType.Chat, "[ShiftBot Scanner] Connected!");

                World = new Block[2, m.GetInt(9), m.GetInt(10)];

                int index = 11;
                for (int y = 0; y < m.GetInt(9); y++)
                {
                    for (int x = 0; x < m.GetInt(10); x++)
                    {
                        int value = 0;
                        if (m[index++] is int iValue)
                        {
                            value = iValue;
                        }

                        var backgroundId = value >> 16;
                        var foregroundId = 65535 & value;

                        World[1, x, y] = new Block(foregroundId);
                        World[0, x, y] = new Block(backgroundId);
                        switch (foregroundId)
                        {
                        case 55:
                        case 56:
                        case 57:
                        case 58:
                            string text  = m.GetString(index++);
                            int    morph = m.GetInt(index++);
                            World[1, x, y] = new Sign(foregroundId, text, morph);
                            break;

                        case 59:
                            int  rotation = m.GetInt(index++);
                            int  p_id     = m.GetInt(index++);
                            int  t_id     = m.GetInt(index++);
                            bool flip     = m.GetBool(index++);
                            World[1, x, y] = new Portal(foregroundId, rotation, p_id, t_id, flip);
                            break;

                        case 93:
                        case 94:
                            int r = m.GetInt(index++);
                            World[1, x, y] = new Effect(foregroundId, r);
                            break;

                        case 98:
                        case 99:
                            int sid = m.GetInt(index++);
                            World[1, x, y] = new Switch(foregroundId, sid);
                            break;

                        case 100:
                            int  sid2 = m.GetInt(index++);
                            bool inv  = m.GetBool(index++);
                            World[1, x, y] = new Door(foregroundId, sid2, inv);
                            break;
                        }
                    }
                }

                break;

            case MessageType.PlayerJoin:
            case MessageType.PlayerAdd:
                Players.Add(new Player(m.GetInt(0), m.GetString(1).ToLower()));
                break;


            case MessageType.PlayerExit:
                Players.RemoveAll(p => p.Id == m.GetInt(0));
                break;

            case MessageType.PlaceBlock:
                int L = m.GetInt(1);
                int X = m.GetInt(2);
                int Y = m.GetInt(3);

                // Assign block to the world
                World[L, X, Y] = new Block(m.GetInt(4));

                switch (m.GetInt(4))
                {
                // Signs
                case 55:
                case 56:
                case 57:
                case 58:
                    string text  = m.GetString(5);
                    int    morph = m.GetInt(6);
                    World[L, X, Y] = new Sign(m.GetInt(4), text, morph);
                    break;

                // Portals
                case 59:
                    int  rotation = m.GetInt(5);
                    int  p_id     = m.GetInt(6);
                    int  t_id     = m.GetInt(7);
                    bool flip     = m.GetBool(8);
                    World[L, X, Y] = new Portal(m.GetInt(4), rotation, p_id, t_id, flip);
                    break;

                // Effects
                case 93:
                case 94:
                    int r = m.GetInt(5);
                    World[L, X, Y] = new Effect(m.GetInt(4), r);
                    break;

                // Switches
                case 98:
                case 99:
                    int sid = m.GetInt(5);
                    World[L, X, Y] = new Switch(m.GetInt(4), sid);
                    break;

                // Switch Doors
                case 100:
                    int  sid2 = m.GetInt(5);
                    bool inv  = m.GetBool(5);
                    World[L, X, Y] = new Door(m.GetInt(4), sid2, inv);
                    break;
                }
                break;

            case MessageType.Chat:
                if (m.GetString(1).StartsWith("!", StringComparison.Ordinal) || m.GetString(1).StartsWith(".", StringComparison.Ordinal))
                {
                    string[] param = m.GetString(1).ToLower().Substring(1).Split(" ", StringSplitOptions.RemoveEmptyEntries);
                    string   cmd   = param[0];
                    player = Players.FirstOrDefault(p => p.Id == m.GetInt(0));

                    switch (cmd)
                    {
                    case "scan":
                        if (param.Length > 2)
                        {
                            try
                            {
                                int _x = int.Parse(param[1]);
                                int _y = int.Parse(param[2]);

                                Block[,,] game = new Block[2, 38, 22];

                                for (int l = 0; l < 2; l++)
                                {
                                    for (int x = _x; x < _x + 38; x++)
                                    {
                                        for (int y = _y; y < _y + 22; y++)
                                        {
                                            game[l, x - _x, y - _y] = World[l, x, y];
                                        }
                                    }
                                }

                                int n = 0;
                                foreach (MapInfo k in Program.Maps)
                                {
                                    n = Math.Max(n, k.Id);
                                }
                                n++;

                                Directory.CreateDirectory($"../../../levels/{n}");

                                using (StreamWriter file = File.CreateText($"../../../levels/{n}/map.json"))
                                {
                                    var serializer = JsonConvert.SerializeObject(game, Program.Json_settings);
                                    file.WriteLine(serializer);
                                }

                                Program.Maps.Add(new MapInfo
                                {
                                    Id    = n,
                                    Title = "Untitled Map"
                                });

                                using (StreamWriter file = File.CreateText($"../../../levels/list.json"))
                                {
                                    var serializer = JsonConvert.SerializeObject(Program.Maps, Program.Json_settings);
                                    file.WriteLine(serializer);
                                }

                                await Con.SendAsync(MessageType.Chat, "[ShiftBot Scanner] Scanned and saved!");
                            }
                            catch
                            {
                                await Con.SendAsync(MessageType.Chat, "[ShiftBot Scanner] An error occured!");
                            }
                        }
                        break;
                    }
                }
                break;
            }
        }