protected void NewPlayer(object source, NetworkEventArgs args) { if (args.BasePacket.Header.GetValue<string>("prog") != "shooter" && Game.Network.NetworkType == NetworkType.Host) { (Game.Network as ConnectionHost).Disconnect(args.SourceConnection); return; } Player p = args.Data as Player; p.Connection = args.SourceConnection; int index = 0; for (; index < _game.Players.Length; index++) { if (_game.Players[index] == null) break; } if (index == 4) { if (Game.Network.NetworkType == NetworkType.Host) (Game.Network as ConnectionHost).Disconnect(args.SourceConnection); return; } _game.Players[index] = p; args.Forward(); if (Game.Network.NetworkType == NetworkType.Host) { Game.Network.SendEvent((int)PacketCode.Chat, "-- player " + p.Name + " has connected --", true, args.SourceConnection); args.SendReply((int)CorePacketCode.AssignNewHeaderValue, new NetworkLibrary.Utilities.Header("id", index)); } UpdatePlayers(); }
protected void NewChat(object source, NetworkEventArgs args) { string text = args.Data as string; for (int i = _chat.Length - 1; i >= 0; i--) { string temp = _chat[i].Text; _chat[i].Text = text; text = temp; _chat[i].Draw(); } args.Forward(); }
void PlayerSelectionChanged(object source, NetworkEventArgs args) { int[] values = args.Data as int[]; DrawOption(values[0], _playerSelection[values[0]], false); _playerSelection[values[0]] = values[1]; if (_playerSelection[values[0]] != -1) DrawOption(values[0], _playerSelection[values[0]], true); else { DrawOption(values[0], _guns.Count, true); if (_playerSelection[0] == -1 && _playerSelection[1] == -1) { Menu m = new Menu(Label.CreateLabelArrayFromText("Press any key to continue.")); m.Start(-1, 5); } } args.Forward(); }
/// <summary> /// Event called when any client or host connection has a change in a network data collection. /// </summary> /// <param name="packet">The base packet containing the raw data.</param> /// <param name="data">NetworkEvent data containing the contents of the NetworkPacket in a parsed format.</param> private void ConnectionCollectionChanged(NetworkPacket packet, NetworkEventArgs data) { try { //Make sure the data is of the correct type. if (data.DataType == typeof(NotifyCollectionChangedEventArgs)) { //Grab the collection changed information from the data. NotifyCollectionChangedEventArgs args = data.Data as NotifyCollectionChangedEventArgs; //Check if we have the source collection whose items was changed. if (_registeredObjects.ContainsKey(data.NetworkDataId)) { //Get the source collection we want to manipulate IList collection = _registeredObjects[data.NetworkDataId] as IList; //Disable the listener for this object while we do our changes. ListenerDisable((INetworkData)collection); //apply the changes that occured at the client/host to our collection. switch (args.Action) { case NotifyCollectionChangedAction.Add: for (int i = 0; i < args.NewItems.Count; i++) collection.Insert(args.NewStartingIndex + i, args.NewItems[i]); break; case NotifyCollectionChangedAction.Move: for (int i = 0; i < args.NewItems.Count; i++) { object temp = collection[args.OldStartingIndex + (args.OldStartingIndex > args.NewStartingIndex ? i : 0)]; collection.Remove(temp); collection.Insert(args.NewStartingIndex + i, temp); } break; case NotifyCollectionChangedAction.Remove: for (int i = 0; i < args.OldItems.Count; i++) collection.RemoveAt(args.OldStartingIndex + i); break; case NotifyCollectionChangedAction.Replace: for (int i = 0; i < args.NewItems.Count; i++) collection[args.OldStartingIndex + i] = args.NewItems[i]; break; case NotifyCollectionChangedAction.Reset: collection.Clear(); break; } //Enable the listener for our collection. ListenerEnable((INetworkData)collection); //Forward if necessary. data.Forward(); } else if (OnNotificationOccured != null) OnNotificationOccured(data, string.Format("A CollectionChanged packet was received for a collection on a network id but the network id object was not found in registered objects.")); } else if (OnNotificationOccured != null) OnWarningOccured(data, new Warning(string.Format("A network packet for collection changed was received but that data was of an invalid type. Expected a type of '{0}' but received '{1}'.", typeof(NotifyCollectionChangedEventArgs).FullName, data.DataType.FullName))); } catch (Exception e) { ThrowException(data, e); } }
//Network related code void PlayerNetworkBuys(object source, NetworkEventArgs args) { UpdatePlayer((int)args.Data); DrawOption((int)args.Data, _playerSelection[(int)args.Data], true); args.Forward(); }