예제 #1
0
파일: Api.cs 프로젝트: UofG-CS21/SGame
        // start the gameTime stopwatch on API creation
        public Api(string apiUrl, SGameQuadTreeNode rootNode, LocalQuadTreeNode quadTreeNode, NetNode bus, NetPeer arbiterPeer, uint localBusPort, Persistence persistence)
        {
            this._gameTime = new GameTime();
            //#if DEBUG
            //   this.gameTime.SetElapsedMillisecondsManually(0);
            //#endif

            this.ApiUrl       = apiUrl;
            this.Bus          = bus;
            this.LocalBusPort = localBusPort;
            this.ArbiterPeer  = arbiterPeer;

            this.QuadTreeNode = quadTreeNode;
            this.RootNode     = rootNode;
            this.Persistence  = persistence;
            this.DeadShips    = new Dictionary <string, Spaceship>();

            this.Bus.PeerConnectedEvent    += OnPeerConnected;
            this.Bus.PeerDisconnectedEvent += OnPeerDisconnected;
            this.Bus.PacketProcessor.Events <Messages.ShipConnected>().OnMessageReceived    += OnShipConnected;
            this.Bus.PacketProcessor.Events <Messages.ShipDisconnected>().OnMessageReceived += OnShipDisconnected;
            this.Bus.PacketProcessor.Events <Messages.ShipTransferred>().OnMessageReceived  += OnShipTransferred;
            this.Bus.PacketProcessor.Events <Messages.NodeConfig>().OnMessageReceived       += OnNodeConfigReceived;
            this.Bus.PacketProcessor.Events <Messages.NodeOffline>().OnMessageReceived      += OnNodeOffline;
            this.Bus.PacketProcessor.Events <Messages.ScanShoot>().OnMessageReceived        += OnScanShootReceived;
#if DEBUG
            this.Bus.PacketProcessor.Events <Messages.Sudo>().OnMessageReceived += OnSudo;
#endif
        }
예제 #2
0
 public RemoteQuadTreeNode(SGameQuadTreeNode parent, Quadrant quadrant, uint depth, NetNode bus, LiteNetLib.NetPeer nodePeer, string apiUrl)
     : base(parent, quadrant, depth)
 {
     this.Bus      = bus;
     this.NodePeer = nodePeer;
     this.ApiUrl   = ApiUrl;
 }
예제 #3
0
파일: Api.cs 프로젝트: UofG-CS21/SGame
        /// <summary>
        /// Called when node configuration is received from the Arbiter, either for us (which means we need to apply it)
        /// or for another node (which means that we need to update our routing table)
        /// </summary>
        private void OnNodeConfigReceived(NetPeer arbiterPeer, Messages.NodeConfig msg)
        {
            if (arbiterPeer != this.ArbiterPeer)
            {
                // Do not accept configuration not from the arbiter
                return;
            }

            var nodeToReplace = RootNode.NodeAtPath(msg.Path, () => new DummyQuadTreeNode());

            SGameQuadTreeNode replacementNode;

            if (msg.ApiUrl == this.ApiUrl)
            {
                Console.Error.WriteLine(">>> This node is now at {0} <<<", msg.Path);

                replacementNode = QuadTreeNode;
            }
            else
            {
                Console.Error.WriteLine(">>> The node {0} is now at {1} <<<", msg.ApiUrl, msg.Path);

                var busNetPeer = Bus.Host.ConnectedPeerList.Where((peer) => (string)peer.Tag == msg.ApiUrl).FirstOrDefault();
                if (busNetPeer == null)
                {
                    var endpoint = new IPEndPoint(msg.BusAddress, (int)msg.BusPort);
                    Console.Error.WriteLine("Estabilishing direct bus connection to {0}", endpoint);
                    busNetPeer     = Bus.Host.Connect(endpoint, NetNode.Secret);
                    busNetPeer.Tag = msg.ApiUrl;
                }

                replacementNode = new RemoteQuadTreeNode(new Quad(0, 0, 0), Bus, busNetPeer, msg.ApiUrl);
            }

            if (nodeToReplace.Parent != null)
            {
                nodeToReplace.Parent.SetChild(nodeToReplace.Quadrant, replacementNode);
            }
            else
            {
                SGameQuadTreeNode[] rootChildren = new SGameQuadTreeNode[4] {
                    null, null, null, null
                };
                if (RootNode != null)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        rootChildren[i] = (SGameQuadTreeNode)RootNode.Child((Quadrant)i);
                    }
                }
                RootNode = replacementNode;
                for (int i = 0; i < 4; i++)
                {
                    RootNode.SetChild((Quadrant)i, rootChildren[i]);
                }
            }
        }
예제 #4
0
 public DummyQuadTreeNode(SGameQuadTreeNode parent, Quadrant quadrant, uint depth)
     : base(parent, quadrant, depth)
 {
 }