Esempio n. 1
0
        private void __OnRegistered(Network.Node player)
        {
            if (player == null)
            {
                return;
            }

            short index = player.index;

            player.RegisterHandler((short)HostMessageHandle.Ready, delegate(NetworkReader reader)
            {
                Ready(index);
            });

            player.RegisterHandler((short)HostMessageHandle.NotReady, delegate(NetworkReader reader)
            {
                NotReady(index);
            });

            Node node;

            if (GetNode(player.index, out node))
            {
                IEnumerable <KeyValuePair <int, Network.Node> > room = GetRoom(node.roomIndex);
                if (room != null)
                {
                    NetworkWriter writer = new NetworkWriter();
                    writer.Write((short)HostMessageHandle.Ready);
                    writer.Write(new ReadyMessage());
                    HostMessage hostMessage = new HostMessage(-1, writer.Position, writer.AsArray());
                    Lobby.Node  instance;
                    int         i;
                    foreach (KeyValuePair <int, Network.Node> pair in room)
                    {
                        instance = pair.Value as Lobby.Node;
                        if (instance != null)
                        {
                            hostMessage.index = instance.index;
                            for (i = 0; i < instance._count; ++i)
                            {
                                Send(node.connectionId, (short)Network.HostMessageType.Rpc, hostMessage);
                            }
                        }
                    }

                    IEnumerable <int> neighborRoomIndices = GetNeighborRoomIndices(node.roomIndex);
                    if (neighborRoomIndices != null)
                    {
                        foreach (int neighborRoomIndex in neighborRoomIndices)
                        {
                            room = GetRoom(neighborRoomIndex);
                            if (room == null)
                            {
                                continue;
                            }

                            foreach (KeyValuePair <int, Network.Node> pair in room)
                            {
                                instance = pair.Value as Lobby.Node;
                                if (instance != null)
                                {
                                    hostMessage.index = instance.index;
                                    for (i = 0; i < instance._count; ++i)
                                    {
                                        Send(node.connectionId, (short)Network.HostMessageType.Rpc, hostMessage);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /*private void __OnUnregistered(Network.Node node)
         * {
         *  if (node == null || __players == null)
         *      return;
         *
         *  int index = node.index;
         *  Player player;
         *  if (!__players.TryGetValue(index, out player))
         *      return;
         *
         *  //__players.Remove(index);
         *
         *  if (__rooms == null)
         *      return;
         *
         *  Room room;
         *  if (!__rooms.TryGetValue(player.roomIndex, out room) || room == null)
         *      return;
         *
         *  Node temp = node as Node;
         *  while(room.NotReady(player.index))
         *  {
         *      if(temp != null)
         *      {
         *          -- temp._count;
         *
         *          if (temp._onNotReady != null)
         *              temp._onNotReady();
         *      }
         *  }
         * }*/

        private void __OnRegistered(Network.Node node)
        {
            if (node == null)
            {
                return;
            }

            int  index = node.index;
            Node temp  = node as Node;

            if (temp != null && __players != null && __rooms != null)
            {
                Player player;
                if (__players.TryGetValue(index, out player))
                {
                    Room room;
                    if (__rooms.TryGetValue(player.roomIndex, out room) && room != null)
                    {
                        temp._count = room.Get(index);
                    }
                }
            }

            node.RegisterHandler((short)HostMessageHandle.Ready, delegate(NetworkReader reader)
            {
                if (__players != null && __rooms != null)
                {
                    Player player;
                    if (__players.TryGetValue(index, out player))
                    {
                        Room room;
                        if (__rooms.TryGetValue(player.roomIndex, out room) && room != null)
                        {
                            int count = room.count + 1;
                            if (room.Ready(player.index))
                            {
                                if (count == room.count)
                                {
                                    if (onReady != null)
                                    {
                                        onReady(player.roomIndex, count);
                                    }
                                }
                            }
                            else
                            {
                                Debug.LogError("Ready Fail:" + index);

                                return;
                            }
                        }
                    }
                }

                if (temp != null)
                {
                    ++temp._count;

                    if (temp._onReady != null)
                    {
                        temp._onReady();
                    }
                }
            });

            node.RegisterHandler((short)HostMessageHandle.NotReady, delegate(NetworkReader reader)
            {
                if (__players != null && __rooms != null)
                {
                    Player player;
                    if (__players.TryGetValue(index, out player))
                    {
                        Room room;
                        if (__rooms.TryGetValue(player.roomIndex, out room) && room != null)
                        {
                            int count = room.count - 1;
                            if (room.NotReady(player.index))
                            {
                                if (count == room.count)
                                {
                                    if (onNotReady != null)
                                    {
                                        onNotReady(player.roomIndex, count);
                                    }
                                }
                            }
                            else
                            {
                                Debug.LogError("Not Ready Fail:" + index);

                                return;
                            }
                        }
                    }
                }

                if (temp != null)
                {
                    --temp._count;

                    if (temp._onNotReady != null)
                    {
                        temp._onNotReady();
                    }
                }
            });
        }