Example #1
0
        public void PlayerListByTeam(Connection conn, HazelReader reader)
        {
            HazelWriter writer = new HazelWriter();

            writer.WriteByte(6);

            int count = players.Count;

            writer.Write(count);
            if (count == 0)
            {
                MainClass.rooms.Remove(number);
                conn.SendBytes(writer.bytes);
                return;
            }

            IEnumerable <IGrouping <int, Player> > teams = players.GroupBy(p => p.team);

            writer.Write(teams.Count());
            foreach (IGrouping <int, Player> team in teams)
            {
                writer.Write(team.Key);
                writer.Write(team.Count());
                foreach (Player p in team)
                {
                    writer.Write(p.name);
                }
            }
            conn.SendBytes(writer.bytes);
        }
Example #2
0
        static void CreateRoom(Connection conn, HazelReader reader)
        {
            UserInfo    info   = userByConnection [conn];
            HazelWriter writer = new HazelWriter();

            writer.WriteByte(3);
            if (info.room > 0 || info.name == null)
            {
                writer.Write(-1);
                conn.SendBytes(writer.bytes);
                return;
            }
            while (rooms.ContainsKey(lastRoomNumber))
            {
                lastRoomNumber++;
            }
            int    roomNumber = lastRoomNumber;
            string password   = reader.ReadString();

            rooms.Add(roomNumber, new Room(roomNumber, reader.ReadString())
            {
                type       = reader.ReadInt(),
                maxPlayers = reader.ReadInt(),
                password   = reader.ReadString()
            });
            int id = rooms [roomNumber].AddUser(info, password);

            info.room = roomNumber;
            Console.WriteLine(conn.EndPoint.ToString() + " created room " + roomNumber);
            writer.Write(roomNumber);
            writer.Write(id);
            conn.SendBytes(writer.bytes);
        }
Example #3
0
    public void Login()
    {
        if (connection.State != ConnectionState.Connected)
        {
            PopupLog("Not connected");
            return;
        }
        if (!string.IsNullOrEmpty(sessionkey))
        {
            PopupLog("Already logged in");
            return;
        }
        string uname = usernameField.text;
        string pass  = passwordField.text;

        if (string.IsNullOrEmpty(uname) || string.IsNullOrEmpty(pass))
        {
            PopupLog("Both username and password can't be empty");
            return;
        }
        HazelWriter writer = new HazelWriter();

        writer.WriteByte(0);
        writer.Write(uname);
        writer.Write(MD5(pass + "134"));
        connection.SendBytes(writer.bytes, SendOption.Reliable);
    }
Example #4
0
    void Relog()
    {
        HazelWriter writer = new HazelWriter();

        writer.WriteByte(1);
        writer.Write(sessionkey);
        connection.SendBytes(writer.bytes, SendOption.Reliable);
    }
Example #5
0
        static void MyUserInfo(Connection conn, HazelReader reader)
        {
            HazelWriter writer = new HazelWriter();

            writer.WriteByte(7);
            UserInfo info = userByConnection [conn];

            writer.Write(info.room);
            //other stuff
            conn.SendBytes(writer.bytes);
        }
Example #6
0
    public void JoinRoom(int number)
    {
        if (room > 0)
        {
            PopupLog("Already in room " + room);
            return;
        }
        HazelWriter writer = new HazelWriter();

        writer.WriteByte(7);
        writer.Write(number);
        connection.SendBytes(writer.bytes);
    }
Example #7
0
        public void PlayerCount(Connection conn, HazelReader reader)
        {
            HazelWriter writer = new HazelWriter();

            writer.WriteByte(5);

            int count = players.Count;

            writer.Write(count);
            conn.SendBytes(writer.bytes);
            if (count == 0)
            {
                MainClass.rooms.Remove(number);
            }
        }
Example #8
0
    public void CreateRoom()
    {
        if (room > 0)
        {
            PopupLog("Already in room " + room);
            return;
        }
        HazelWriter writer = new HazelWriter();

        writer.WriteByte(6);
        writer.Write("Test room");
        writer.Write(0);
        writer.Write(4);
        writer.Write(string.Empty);
        Debug.Log("asking to create room");

        connection.SendBytes(writer.bytes);
    }
Example #9
0
        public void PlayerList(Connection conn, HazelReader reader)
        {
            HazelWriter writer = new HazelWriter();

            writer.WriteByte(4);
            int count = players.Count;

            writer.Write(count);
            if (count == 0)
            {
                MainClass.rooms.Remove(number);
                conn.SendBytes(writer.bytes);
                return;
            }
            foreach (Player p in players)
            {
                writer.Write(p.name);
            }
            conn.SendBytes(writer.bytes);
        }
Example #10
0
        static void RoomList(Connection conn, HazelReader reader)
        {
            HazelWriter writer = new HazelWriter();

            writer.WriteByte(2);
            writer.Write(rooms.Count);
            IEnumerable <Room> openRooms = rooms.Values.Where(r => (!r.playing && r.players.Count < r.maxPlayers));

            foreach (Room r in openRooms)
            {
                writer.Write(r.number);
                writer.Write(r.name);
                writer.Write(!string.IsNullOrEmpty(r.password));
                writer.Write(r.playing);
                writer.Write(r.maxPlayers);
                writer.Write(r.players.Count);
            }

            conn.SendBytes(writer.bytes);
        }
Example #11
0
        static void Login(Connection conn, HazelReader reader)
        {
            string uname = reader.ReadString();
            string pass  = reader.ReadString();

            string tryHash = MD5(uname + "2570" + pass);

            MySqlCommand cmd = dbconn.CreateCommand();

            cmd.CommandText = "SELECT id FROM accountinfo WHERE username = @uname AND passhash = @passhash";
            cmd.Parameters.AddWithValue("@uname", uname);
            cmd.Parameters.AddWithValue("@passhash", tryHash);
            cmd.Prepare();
            MySqlDataReader sqlReader = cmd.ExecuteReader();
            HazelWriter     writer    = new HazelWriter();

            writer.WriteByte(0);
            if (sqlReader.Read())
            {
                int    plyrid     = sqlReader.GetInt32(0);
                string ip         = conn.EndPoint.ToString().Split(':')[0];
                string sessionkey = MD5(uname + DateTime.UtcNow.Ticks + ip);
                string session    = GenerateSession(sessionkey, conn);
                userByConnection.Add(conn, new UserInfo()
                {
                    id = plyrid, connection = conn
                });
                connectionBySession.Add(session, conn);

                writer.Write(sessionkey);
            }
            else
            {
                writer.Write(string.Empty);
            }
            sqlReader.Close();
            sqlReader.Dispose();
            cmd.Dispose();
            conn.SendBytes(writer.bytes);
        }
Example #12
0
        static void JoinRoom(Connection conn, HazelReader reader)
        {
            UserInfo    info   = userByConnection [conn];
            HazelWriter writer = new HazelWriter();

            writer.WriteByte(3);
            int roomNumber = reader.ReadInt();

            if (info.name == null || (info.room > 0 && info.room != roomNumber))
            {
                writer.Write(-1);
                conn.SendBytes(writer.bytes);
                return;
            }
            if (!rooms.ContainsKey(roomNumber))
            {
                writer.Write(-2);
                conn.SendBytes(writer.bytes);
                RoomList(conn, reader);
                return;
            }
            string password = reader.ReadString();
            int    id       = rooms [roomNumber].AddUser(info, password);

            if (id > -1)
            {
                //join successful
                info.room = roomNumber;
                writer.Write(roomNumber);
                writer.Write(id);
                conn.SendBytes(writer.bytes);
            }
            else
            {
                writer.Write(id);
                RoomList(conn, reader);
                conn.SendBytes(writer.bytes);
            }
        }