// 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 }
public void GarbageCollect() { foreach (var ship in QuadTreeNode.ShipsByToken.Values) { LocalQuadTreeNode currentNode = QuadTreeNode; QuadTreeNode <Spaceship> bestFitNode = currentNode.SmallestNodeWhichContains(ship.Bounds); Messages.TransferShip msg; if (bestFitNode == null) { msg = new Messages.TransferShip() { Ship = ship, Path = currentNode.Parent.Path() }; } else { msg = new Messages.TransferShip() { Ship = ship, Path = bestFitNode.Path() }; } Console.Error.WriteLine("Transferring request for ship {0} (pos=({1}) was sent from node at {2} to node at {3}", ship.Token, ship.Pos, this.ApiUrl, msg.Path); Bus.SendMessage(msg, ArbiterPeer); QuadTreeNode.ShipsByToken.Remove(ship.Token); } }
/// <summary> /// Initializes an instance of the program. /// </summary> Program(CmdLineOptions options) { this.options = options; this.bus = new NetNode(listenPort: (int)options.LocalBusPort); this.persistence = options.PersistenceUrl != null ? new Persistence(options.PersistenceUrl) : null; LiteNetLib.NetPeer arbiterPeer = this.bus.Connect(options.Arbiter, (int)options.ArbiterBusPort); // On startup, the local SGame node assumes it manages the whole universe. var localTree = new LocalQuadTreeNode(new SShared.Quad(0.0, 0.0, UniverseSize), 0); var rootNode = localTree; this.api = new Api(options.ApiUrl, rootNode, localTree, bus, arbiterPeer, options.LocalBusPort, persistence); this.router = new Router <Api>(api); }