// Add class to control spell's public PlayerDetails(Actor actor) { level = 1; xp = 0; life = ConfigGame.lifeMax; lifeMax = ConfigGame.lifeMax; mana = ConfigGame.manaMax; manaMax = ConfigGame.manaMax; regLife = ConfigGame.regLife; regMana = ConfigGame.regMana; gold = ConfigGame.initialGold; isReady = false; resetStatistics(); }
/// <summary> /// Helper method of <see cref = "HandleRaiseEventOperation" />. /// Stores an event for new actors. /// </summary> /// <param name = "actor"> /// The actor. /// </param> /// <param name = "raiseEventRequest"> /// The raise event request. /// </param> /// <returns> /// True if <see cref = "RaiseEventRequest.Cache" /> is valid. /// </returns> protected bool UpdateEventCache(Actor actor, RaiseEventRequest raiseEventRequest) { CustomEvent customEvent; switch (raiseEventRequest.Cache) { case (byte)CacheOperation.AddToRoomCache: customEvent = new CustomEvent(actor.ActorNr, raiseEventRequest.EvCode, raiseEventRequest.Data) { Cache = raiseEventRequest.Cache }; this.eventCache.AddEvent(customEvent); return true; case (byte)CacheOperation.AddToRoomCacheGlobal: customEvent = new CustomEvent(0, raiseEventRequest.EvCode, raiseEventRequest.Data) { Cache = raiseEventRequest.Cache }; this.eventCache.AddEvent(customEvent); return true; case (byte)CacheOperation.MergeCache: { EventCache eventCache; if (!this.eventCacheOld.TryGetValue(actor.ActorNr, out eventCache)) { eventCache = new EventCache(); this.eventCacheOld.Add(actor.ActorNr, eventCache); } Hashtable @event; if (eventCache.TryGetValue(raiseEventRequest.EvCode, out @event)) { // null events are removed if (raiseEventRequest.Data == null) { eventCache.Remove(raiseEventRequest.EvCode); } else { // merge or delete foreach (DictionaryEntry pair in raiseEventRequest.Data) { // null values are removed if (pair.Value == null) { @event.Remove(pair.Key); } else { @event[pair.Key] = pair.Value; } } } } else if (raiseEventRequest.Data != null) { // new @event = raiseEventRequest.Data; eventCache.Add(raiseEventRequest.EvCode, @event); } return true; } case (byte)CacheOperation.RemoveCache: { EventCache eventCache; if (!this.eventCacheOld.TryGetValue(actor.ActorNr, out eventCache)) { return true; } eventCache.Remove(raiseEventRequest.EvCode); return true; } case (byte)CacheOperation.ReplaceCache: { EventCache eventCache; if (!this.eventCacheOld.TryGetValue(actor.ActorNr, out eventCache)) { eventCache = new EventCache(); this.eventCacheOld.Add(actor.ActorNr, eventCache); } eventCache.Remove(raiseEventRequest.EvCode); if (raiseEventRequest.Data != null) { Hashtable @event = raiseEventRequest.Data; eventCache.Add(raiseEventRequest.EvCode, @event); } return true; } default: { return false; } } }
/// <summary> /// Tries to add a <see cref = "LitePeer" /> to this game instance. /// </summary> /// <param name = "peer"> /// The peer to add. /// </param> /// <param name = "actor"> /// When this method returns this out param contains the <see cref = "Actor" /> associated with the <paramref name = "peer" />. /// </param> /// <returns> /// Returns true if no actor exists for the specified peer and a new actor for the peer has been successfully added. /// The actor parameter is set to the newly created <see cref = "Actor" /> instance. /// Returns false if an actor for the specified peer already exists. /// The actor paramter is set to the existing <see cref = "Actor" /> for the specified peer. /// </returns> protected virtual bool TryAddPeerToGame(LitePeer peer, out Actor actor) { // check if the peer already exists in this game actor = this.Actors.GetActorByPeer(peer); if (actor != null) { return false; } // create new actor instance actor = new Actor(peer); this.actorNumberCounter++; actor.ActorNr = this.actorNumberCounter; this.Actors.Add(actor); if (Log.IsDebugEnabled) { Log.DebugFormat("Actor added: {0} to game: {1}", actor.ActorNr, this.Name); } return true; }
/// <summary> /// Sends a <see cref = "LeaveEvent" /> to all <see cref = "Actor" />s. /// </summary> /// <param name = "peer"> /// The peer. /// </param> /// <param name = "leaveRequest"> /// The <see cref="LeaveRequest"/> sent by the peer or null if the peer have been disconnected without sending a leave request. /// </param> protected virtual void PublishLeaveEvent(Actor actor, LeaveRequest leaveRequest) { if (this.Actors.Count > 0 && actor != null) { IEnumerable<int> actorNumbers = this.Actors.GetActorNumbers(); var leaveEvent = new LeaveEvent(actor.ActorNr, actorNumbers.ToArray()); this.PublishEvent(leaveEvent, this.Actors, new SendParameters()); } }
/// <summary> /// Sends the complete list of games to one actor in the lobby. /// </summary> /// <param name="actor"> /// The actor. /// </param> private void PublishGameList(Actor actor) { // Room list needs to be cloned because it is serialized in other thread and is changed at the same time var customEvent = new CustomEvent(actor.ActorNr, (byte)LiteLobbyEventCode.GameList, (Hashtable)this.roomList.Clone()); this.PublishEvent(customEvent, actor, new SendParameters { Unreliable = true }); }
/// <summary> /// This override disables the event publishing. /// </summary> /// <param name="peer"> /// The peer. /// </param> /// <param name="leaveOperation"> /// The leave operation. /// </param> protected override void PublishLeaveEvent(Actor actor, LeaveRequest leaveOperation) { // lobbies don't publish a leave event to the clients }
public void addPlayer(Actor actor) { listPlayers.Add(new PlayerDetails(actor)); }
public void removePlayer(Actor actor) { PlayerDetails remove = listPlayers.Find(e => e.actor == actor); listPlayers.Remove(remove); }
/// <summary> /// Publishes an event to a single actor on a specified channel. /// </summary> /// <param name = "e"> /// The event to publish. /// </param> /// <param name = "actor"> /// The <see cref = "Actor" /> who should receive the event. /// </param> /// <param name = "sendParameters"> /// The send Parameters. /// </param> protected void PublishEvent(LiteEventBase e, Actor actor, SendParameters sendParameters) { var eventData = new EventData(e.Code, e); actor.Peer.SendEvent(eventData, sendParameters); }
/// <summary> /// Helper method of <see cref = "HandleRaiseEventOperation" />. /// Stores an event for new actors. /// </summary> /// <param name = "actor"> /// The actor. /// </param> /// <param name = "raiseEventRequest"> /// The raise event request. /// </param> /// <param name="msg"> /// Contains an error message if the method returns false. /// </param> /// <returns> /// True if <see cref = "RaiseEventRequest.Cache" /> is valid. /// </returns> protected bool UpdateEventCache(Actor actor, RaiseEventRequest raiseEventRequest, out string msg) { msg = null; CustomEvent customEvent; switch (raiseEventRequest.Cache) { case (byte)CacheOperation.DoNotCache: return true; case (byte)CacheOperation.AddToRoomCache: customEvent = new CustomEvent(actor.ActorNr, raiseEventRequest.EvCode, raiseEventRequest.Data); this.eventCache.AddEvent(customEvent); return true; case (byte)CacheOperation.AddToRoomCacheGlobal: customEvent = new CustomEvent(0, raiseEventRequest.EvCode, raiseEventRequest.Data); this.eventCache.AddEvent(customEvent); return true; } // cache operations for the actor event cache currently only working with hashtable data Hashtable eventData; if (raiseEventRequest.Data == null || raiseEventRequest.Data is Hashtable) { eventData = (Hashtable)raiseEventRequest.Data; } else { msg = string.Format("Cache operation '{0}' requires a Hashtable as event data.", raiseEventRequest.Cache); return false; } switch (raiseEventRequest.Cache) { case (byte)CacheOperation.MergeCache: this.actorEventCache.MergeEvent(actor.ActorNr, raiseEventRequest.EvCode, eventData); return true; case (byte)CacheOperation.RemoveCache: this.actorEventCache.RemoveEvent(actor.ActorNr, raiseEventRequest.EvCode); return true; case (byte)CacheOperation.ReplaceCache: this.actorEventCache.ReplaceEvent(actor.ActorNr, raiseEventRequest.EvCode, eventData); return true; default: msg = string.Format("Unknown cache operation '{0}'.", raiseEventRequest.Cache); return false; } }