Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
    void RoomListReply(Connection conn, HazelReader reader)
    {
        int count = reader.ReadInt();

        Debug.Log("there are " + count + " rooms");
        rooms.Clear();
        while (count > 0)
        {
            rooms.Add(reader.ReadRoomInfo());
            count--;
        }
        foreach (Transform t in roomListEntryHolder)
        {
            Destroy(t.gameObject);
        }
        foreach (RoomInfo r in rooms)
        {
            RoomListEntry re = ((GameObject)Instantiate(roomListEntryPrefab)).GetComponent <RoomListEntry> ();
            re.transform.SetParent(roomListEntryHolder, false);
            re.name.text        = r.number + ". " + r.name;
            re.roomNumber       = r.number;
            re.playerCount.text = r.curPlayers + "/" + r.maxPlayers;
            if (r.hasPassword)
            {
                re.hasPassword.SetActive(true);
            }
            if (r.playing)
            {
                re.playing.SetActive(true);
            }
        }
    }
Exemplo n.º 4
0
    void MoveRoom(Connection conn, HazelReader reader)
    {
        int reply = reader.ReadInt();

        if (reply < 0)
        {
            if (reply == -1)
            {
                PopupLog("You have been kicked.");
            }
            else if (reply == -2)
            {
                PopupLog("Room full");
            }
            else if (reply == -3)
            {
                PopupLog("Wrong password. If the room seem to have no password, try refreshing.");
            }
            else if (reply == -4)
            {
                PopupLog("Room not found");
            }
            else if (reply == -5)
            {
                PopupLog("Room is either full or already playing.");
            }
            else
            {
                PopupLog("Unknown error : " + reply + "\nTry restarting");
            }
        }
    }
Exemplo n.º 5
0
        static void Relog(Connection conn, HazelReader reader)
        {
            string ip         = conn.EndPoint.ToString().Split(':')[0];
            string sessionkey = reader.ReadString();
            string session    = GenerateSession(sessionkey, conn);

            if (connectionBySession.ContainsKey(session))
            {
                Console.WriteLine(ip + " reconnected");
                userByConnection [connectionBySession [session]].connection = conn;
                connectionBySession [session].Close();
                connectionBySession[session] = conn;
                UserInfo info = userByConnection [conn];
                if (info.room > 0 && rooms.ContainsKey(info.room))
                {
                    if (rooms [info.room].HasUser(info.id))
                    {
                        Room room = rooms [info.room];
                        room.AddUser(info, room.password);
                    }
                    else
                    {
                        info.room = 0;
                    }
                }
            }
            else
            {
                Console.WriteLine(ip + " reconnect failed");
            }
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
0
    private void DataReceived(object sender, DataReceivedEventArgs args)
    {
        connection = (Connection)sender;

        HazelReader reader = new HazelReader(args.Bytes);

        byte header = reader.ReadByte();

        if (DataHandlers.ContainsKey(header))
        {
            UnityMainThreadDispatcher.Instance().Enqueue(() => DataHandlers [header] (connection, reader));
        }
        args.Recycle();
    }
Exemplo n.º 8
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);
            }
        }
Exemplo n.º 9
0
 void LoginReply(Connection conn, HazelReader reader)
 {
     sessionkey = reader.ReadString();
     if (string.IsNullOrEmpty(sessionkey))
     {
         sessionkey = null;
         PopupLog("Login failed");
     }
     else
     {
         Debug.Log("Login successful");
         Refresh();
         roomListPanel.SetActive(true);
         loginPanel.SetActive(false);
     }
 }
Exemplo n.º 10
0
        static void LeaveRoom(Connection conn, HazelReader reader)
        {
            UserInfo info = userByConnection [conn];

            if (info.room <= 0)
            {
                return;
            }
            if (rooms [info.room].playing)
            {
                return;
            }
            if (rooms [info.room].HasPlayer(info.id))
            {
                rooms [info.room].Leave(conn);
            }
            info.room = 0;
        }
Exemplo n.º 11
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);
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
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);
        }
Exemplo n.º 14
0
        public void Move(Connection conn, HazelReader reader)
        {
            Player p = playerByConnection [conn];
            int3   targetPosition = reader.ReadInt3();
            bool   pending        = reader.ReadBool;

            if (!p.doneTurn && targetPosition.y <= p.position.y)
            {
                //and it is within 1 block range from him,
                int3 absDeltaPos = targetPosition - p.position;
                absDeltaPos = new int3(Mathf.Abs(absDeltaPos.x), Mathf.Abs(absDeltaPos.y), Mathf.Abs(absDeltaPos.z));
                if ((absDeltaPos.y == 0 && absDeltaPos.x == 2) || (absDeltaPos.x == 1 && absDeltaPos.y == 1))
                {
                    //and target platform available

                    Platform dest = platforms.SingleOrDefault(w => w.position == targetPosition);

                    if (dest != null)
                    {
                        //and there are no player nor wall on the targetPosition,
                        if (!players.Exists(p2 => p2.position.xz == targetPosition.xz && targetPosition.y == p.position.y))
                        {
                            //turn done
                            playerById[id].doneTurn = true;
                            turnRemaining--;
                            //move
                            if (pending)
                            {
                                pendingMove.Add(id, dest);
                            }
                            else
                            {
                                UpdateTransform(p, dest);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 15
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);
            }
        }
Exemplo n.º 16
0
        private static void DataReceivedHandler(object sender, Hazel.DataReceivedEventArgs args)
        {
            Connection  conn   = (Connection)sender;
            HazelReader reader = new HazelReader(args.Bytes);

            byte header = reader.ReadByte();

            if (!userByConnection.ContainsKey(conn) && header > 2)
            {
                return;
            }
            if (userByConnection[conn].name == null && header > 3)
            {
                return;
            }

            if (DataHandlers.ContainsKey(header))
            {
                DataHandlers [header] (conn, reader);
            }

            args.Recycle();
        }
Exemplo n.º 17
0
 static void RoomStuff(Connection conn, HazelReader reader)
 {
 }