Пример #1
0
 private uint GetShortestPath()
 {
     string[] result;
     Command request = new Command();
     request.Request(ClientCmd.GET_SHORTEST_PATH);
     request.Add(currentLocation.ToString());
     request.Add(city.Id.ToString());
     result = request.Send(socket, true);
     return uint.Parse(result[1]);
 }
Пример #2
0
        //konstruktor
        public Enemies(uint location, TcpClient clientTcp)
        {
            client = clientTcp.Client;

            //odpyta bazke o moby dla danej lokaclizacji
            Command command = new Command();
            command.Request(ClientCmd.GET_ENEMIES);
            command.Add(location.ToString());
            string[] result = command.Send(client, true);

            if (result[0] == ServerCmd.ENEMIES)
            {
                mobsCount = uint.Parse(result[1]);

                for (int i = 2; i < result.Length; i += 11)
                {
                    Mob mb = new Mob(
                                ulong.Parse(result[i]),
                                (result[i + 1]),
                                uint.Parse(result[i + 2]),
                                ulong.Parse(result[i + 3]),
                                uint.Parse(result[i + 4]),
                                uint.Parse(result[i + 5]),
                                uint.Parse(result[i + 6]),
                                uint.Parse(result[i + 7]),
                                uint.Parse(result[i + 8]),
                                uint.Parse(result[i + 9]),
                                (result[i + 10])
                                );

                    enemiesList.Add(mb);
                }
            }
        }
Пример #3
0
        //konstruktor gracza
        public Player(ulong _id, TcpClient userClient)
        {
            //ustawienie identyfikatora gracza
            id = _id;

            //wczytanie ustawień dla połączenia z serwerem
            host = Properties.Settings.Default.Host;
            port = Properties.Settings.Default.Port;

            //inicjalizacja gniazda połączenia z serwerem
            try
            {
                client = userClient;
                code = new UTF8Encoding();
                Command request = new Command(ClientCmd.GET_PLAYER_DATA);
                request.Add(id.ToString());

                string[] dane = request.Apply(client.Client, true);

                if (dane[0] == ServerCmd.PLAYER_DATA)
                {
                    login = dane[1];
                    password = dane[2];
                    access = int.Parse(dane[3]);
                    email = dane[4];
                }
            }
            catch
            {
                //obsługa wyjątku
            }
        }
Пример #4
0
        public CharacterEquipment(ulong characterId, TcpClient clientTcp)
        {
            id = characterId;
            client = clientTcp.Client;

            Command command = new Command();
            command.Request(ClientCmd.GET_CHARACTER_EQUIPMENT);
            command.Add(id.ToString());
            string[] dane = command.Send(client, true);

            if (dane[0] == ServerCmd.CHARACTER_EQUIPMENT)
            {
                head = uint.Parse(dane[1]);
                chest = uint.Parse(dane[2]);
                legs = uint.Parse(dane[3]);
                weapon = uint.Parse(dane[4]);
                shield = uint.Parse(dane[5]);
            }
        }
Пример #5
0
        public Character(ulong characterId, TcpClient clientTcp)
        {
            client = clientTcp.Client;
            id = characterId;

            Command command = new Command();
            command.Request(ClientCmd.GET_CHARACTER_DATA);
            command.Add(id.ToString());
            string[] dane = command.Send(client, true);

            if (dane[0] == ServerCmd.CHARACTER_DATA)
            {
                name = dane[1];
                level = uint.Parse(dane[2]);
                exp = uint.Parse(dane[3]);
                gold = uint.Parse(dane[4]);
                strength = uint.Parse(dane[5]);
                stamina = uint.Parse(dane[6]);
                dexterity = uint.Parse(dane[7]);
                luck = uint.Parse(dane[8]);

                status = dane[9];
                lastDamage = UInt64.Parse(dane[10]);
                damage = UInt64.Parse(dane[11]);
                lastFatigue = UInt64.Parse(dane[12]);
                fatigue = UInt64.Parse(dane[13]);
                
                location = uint.Parse(dane[14]);
                travelEndTime = UInt64.Parse(dane[15]);
                travelDestination = UInt32.Parse(dane[16]);
            }
            equip = new CharacterEquipment(id, clientTcp);
            storage = new CharacterStorage(id, clientTcp);
            pointRegenerationTime = 30;
            fullRegenerationTime = 15 * 60;
            //przykładowa zmiana imienia postaci
            //this.Name = "updateTest";
        }
Пример #6
0
        //wysyłanie danych gracza
        private bool SendPlayerData(ulong playerId, Command response, Socket socket)
        {
            string[] dane = new string[3];

            dane = GetPlayerData(playerId);

            //utworzenie odpowiedzi
            response.Request(ServerCmd.PLAYER_DATA);
            response.Add(dane);
            response.Apply(socket);

            AddLogAsynch("[" + GetServerDateTime() + "][ " + dane[0] + " ]: Pobrał dane gracza.");
            return true;
        }
Пример #7
0
        //właściwa obsługa klienta
        private void ClientService(object s)
        {
            //gniazdo klienta, którego obsługujemy
            Socket socket = s as Socket;

            //dekoder UTF-8
            code = new UTF8Encoding();

            //zdefiniowanie nazwy klienta - potem zamieniana na nazwę użytkownika
            string clientName = socket.RemoteEndPoint.ToString();

            //informacja o tym czy klientowi udało się zalogować
            bool successLog = false;

            //bufor do pobierania danych od klienta
            byte[] buf;

            //inicjalizacja wielkości paczki z żądaniem
            int packageSize = 0;

            /* -------------- INICJALIZACJA OBIEKTÓW DLA GRACZA -------------- */

            //obiekt postaci
            Character character = null;

            /* --------------------------------------------------------------- */

            while (socket.Connected && isRunning && IsConnected(socket))
            {
                if (socket.Available > 0)
                {
                    //jeżeli ostatnia paczka została odczytana
                    if (packageSize == 0)
                    {
                        //to ustaw bufor na 4 bajty = int32
                        buf = new byte[4];

                        //i odczytaj wielkość nowej paczki
                        socket.Receive(buf);
                        packageSize = BitConverter.ToInt32(buf, 0);
                    }
                    else
                    {
                        //jeżeli wielkość paczki jest ustalona
                        //to ustaw bufor na wielkość jej odpowiadającą
                        buf = new byte[packageSize];

                        //komenda wczytana z bufora
                        string cmd = code.GetString(buf, 0, socket.Receive(buf));

                        //po wczytaniu ustaw wielkość paczki na 0 zgłaszając tym samym gotowość do przyjęcia następnej
                        packageSize = 0;

                        //utworzenie obiektu komendy
                        Command response = new Command();

                        //zamiana lini komendy na nazwę akcji args[0] i argumenty - reszta tablicy
                        string[] args = CommandsToArguments(cmd);

                        switch (args[0])
                        {
                            /*
                             * LOGOWANIE
                             */
                            case ClientCmd.LOGIN:
                                ulong userID = Login(args[1], args[2]);
                                if (userID == 0)
                                {
                                    AddLogAsynch("[" + GetServerDateTime() + "][Klient]: Nieudana próba logowania (Login: "******")");
                                }
                                else
                                {
                                    AddLogAsynch("[" + GetServerDateTime() + "][ " + args[1] + " ]: Udane logowanie. ID = " + userID);
                                    clientName = args[1];
                                    successLog = true;

                                    //utworzenie obiektu postaci dla zalogowanego gracza
                                    character = new Character(userID, dataBase);

                                    //uaktualnienie w bazie danych daty ostatniego logowania
                                    ExecuteQuery("UPDATE `" + dataBase.MySqlBase + "`.`player` SET `lastlogin` = '" + GetServerDateTime() + "' WHERE `player`.`id` =" + userID + ";", dataBase);
                                }
                                //utworzenie odpowiedzi
                                response.Request(ClientCmd.LOGIN);
                                response.Add(userID.ToString());
                                response.Add(DateTime.UtcNow.Ticks.ToString());
                                response.Apply(socket);
                                break;

                            /*
                             * WYSYŁANIE DANYCH GRACZA
                             * kolejność danych: komenda, Login, hasło, dostęp, email
                             */
                            case ClientCmd.GET_PLAYER_DATA:
                                Thread sendPlayerDataTh = new Thread(unused => SendPlayerData(ulong.Parse(args[1]), response, socket));
                                sendPlayerDataTh.Priority = ThreadPriority.BelowNormal;
                                sendPlayerDataTh.IsBackground = true;
                                sendPlayerDataTh.Start();
                                break;

                            /*
                             * WYSŁANIE DANYCH POSTACI
                             * kolejność danych: komenda, imie, poziom, doświadczenie, złoto, siła, wytrzymałość, zręczność, szczęście
                             */
                            case ClientCmd.GET_CHARACTER_DATA:
                                response.Request(ServerCmd.CHARACTER_DATA);
                                response.Add(character.Name);
                                response.Add(character.Level.ToString());
                                response.Add(character.Experience.ToString());
                                response.Add(character.Gold.ToString());
                                response.Add(character.Strength.ToString());
                                response.Add(character.Stamina.ToString());
                                response.Add(character.Dexterity.ToString());
                                response.Add(character.Luck.ToString());
                                response.Add(character.Status);
                                response.Add(character.LastDamage.ToString());
                                response.Add(character.Damage.ToString());
                                response.Add(character.LastFatigue.ToString());
                                response.Add(character.Fatigue.ToString());
                                response.Add(character.Location.ToString());
                                response.Add(character.TravelEndTime.ToString());
                                response.Add(character.TravelDestination.ToString());

                                response.Apply(socket);

                                break;
                            /*
                             * UAKTUALNIANIE PÓL BAZY DANYCH
                             * kolejność danych: komenda, tabela, pola, wartości, pole warunku, wartość pola warunku
                             */
                            case ClientCmd.UPDATE_DATA_BASE:
                                string log = "";
                                string UpdateQuery = CreateMySqlUpdateQuery(args, ref log);
                                AddLogAsynch("[" + GetServerDateTime() + "][ " + clientName + " ]: " + log);
                                ExecuteQuery(UpdateQuery, dataBase);
                                //utworzenie odpowiedzi
                                response.Request(ServerCmd.DATA_BASE_UPDATED);
                                response.Apply(socket);
                                break;
                            case ClientCmd.GET_CHARACTER_EQUIPMENT:
                                response.Request(ServerCmd.CHARACTER_EQUIPMENT);
                                response.Add(character.Equipment.Head.ToString());
                                response.Add(character.Equipment.Chest.ToString());
                                response.Add(character.Equipment.Legs.ToString());
                                response.Add(character.Equipment.Weapon.ToString());
                                response.Add(character.Equipment.Shield.ToString());
                                response.Apply(socket);
                                break;
                            case ClientCmd.GET_CITIES:
                                response.Request(ServerCmd.CITIES);
                                response.Add(map.CitiesNumber.ToString());
                                foreach (City city in map.CityData)
                                {
                                    response.Add(city.Id.ToString());
                                    response.Add(city.Name);
                                    response.Add(city.AccessLevel.ToString());
                                    response.Add(city.LeftCoordinate.ToString());
                                    response.Add(city.TopCoordinate.ToString());
                                    response.Add(city.Icon);
                                }
                                response.Apply(socket);
                                break;
                            case ClientCmd.GET_SKILLS:
                                response.Request(ServerCmd.SKILLS);
                                //AddLogAsynch("Pobiernie umiejętności");
                                foreach (Skill skill in skills.SkillList)
                                {
                                    response.Add(skill.Id.ToString());
                                    response.Add(skill.AccessLevel.ToString());
                                    response.Add(skill.Strength.ToString());
                                    response.Add(skill.Stamina.ToString());
                                    response.Add(skill.Dexterity.ToString());
                                    response.Add(skill.Luck.ToString());
                                }
                                response.Apply(socket);
                                break;
                            default:
                                AddLogAsynch("[" + GetServerDateTime() + "][Klient]: Odebrano nieznaną komendę!");
                                break;
                        }
                        response.Clear();
                    }
                }
            }
            if (successLog)
            {
                AddLogAsynch("[" + GetServerDateTime() + "][ " + clientName + " ]: Gracz rozłączył się z serwerem.");
            }
        }
Пример #8
0
 public void SetAmount(uint amount, Socket socket)
 {
     this.amount = amount;
     Command command = new Command();
     //ustawienie żądania uaktualnienia bazy danych
     command.Request(ClientCmd.UPDATE_DATA_BASE);
     //w tabeli character
     command.Add("character_storage");
     //pola name
     command.Add("amount");
     //na wartość updateTest
     command.Add(amount.ToString());
     //gdzie wartość pola id
     command.Add("id");
     //jest równa identyfikatorowi gracza
     command.Add(this.CharacterId.ToString() + " AND `character_storage`.`id_item` = " + this.CharacterId);
     //uaktualnij i nie czekaj na odpowiedź
     command.Send(socket, false);
 }
Пример #9
0
        public void AddItem(uint itemId, uint amount = 1)
        {
            Storage position = storage.Find(pos => pos.ItemId == itemId);

            if (position == null)
            {
                storage.Add(new Storage(this.id, itemId, amount));
            }
            else
            {
                position.Amount += amount;
            }

            Command request = new Command();
            request.Request(ClientCmd.ADD_ITEM);
            request.Add(itemId.ToString());
            request.Add(amount.ToString());
            request.Send(socket);
        }
Пример #10
0
        public void RemoveOneItem(uint itemId)
        {
            Storage position = storage.Find(pos => pos.ItemId == itemId);
            uint am = position.Amount;

            if (position.Amount > 1)
            {
                position.Amount--;
            }
            else
            {
                storage.RemoveAll(pos => pos.ItemId == itemId);
            }

            Command request = new Command();
            request.Request(ClientCmd.REMOVE_ONE_ITEM);
            request.Add(itemId.ToString());
            request.Send(socket);
        }
Пример #11
0
        public Item GetItemById(uint id)
        {
            string[] result;
            Item item;
            Command request = new Command();
            request.Request(ClientCmd.GET_ITEM_BY_ID);
            request.Add(id.ToString());

            result = request.Send(socket, true);

            try
            {
                item = new Item(
                    uint.Parse(result[1]),
                    result[2],
                    result[3],
                    uint.Parse(result[4]),
                    result[5]
                    );
            }
            catch
            {
                item = null;
            }

            return item;
        }
Пример #12
0
        public CharacterStorage(ulong id, TcpClient client)
        {
            socket = client.Client;

            this.id = id;

            string[] result;
            items = new List<Item>();
            storage = new List<Storage>();

            Command request = new Command();
            request.Request(ClientCmd.GET_CHARACTER_STORAGE);
            request.Add(id.ToString());
            result = request.Send(socket, true);

            for (int i = 1; i < result.Length; i += 3)
            {
                Storage position = new Storage(
                    ulong.Parse(result[i]),
                    uint.Parse(result[i + 1]),
                    uint.Parse(result[i + 2])
                    );
                storage.Add(position);
            }
        }
Пример #13
0
        //logowanie do gry
        private void singin_Click(object sender, RoutedEventArgs e)
        {
            //inicjalizacja gniazda
            try
            {
                user = new TcpClient(host, port);

                //ustawienie czasu oczekiwania na odpowiedź serwera
                user.ReceiveTimeout = 5;
            }
            catch
            {
                MessageBox.Show("Czas oczekiwania na połączenie minął. Nie można było połączyć się z serwerem. Sprawdź połączenia z Internetami.", "Błąd połączenia!", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            //inicjalizacja kodera/dekodera
            code = new UTF8Encoding();

            //inicjalizacja bufora przechowującego dane do wysłania
            byte[] buf;

            Command cmd = new Command();
            cmd.Request(ClientCmd.LOGIN);
            cmd.Add(login.Text);
            cmd.Add(GetMD5Hash(password.Password));

            Stopwatch watch = new Stopwatch();
            watch.Start();
            string[] args = cmd.Apply(user.Client, true);

            if (Convert.ToUInt64(args[1]) == 0)
            {
                MessageBox.Show("[Klient] Nie udało się zalogować.");
                user.Close();
                singin.IsEnabled = true;
                return;
            }
            else
            {
                watch.Stop();
                new Interface(Convert.ToUInt64(args[1]), user, (long.Parse(args[2]) + watch.ElapsedTicks) - DateTime.UtcNow.Ticks).Show();
                this.Close();
            }
        }