/// <summary> /// Gets the artefact string. /// </summary> /// <returns>The formatted artefact string</returns> /// <param name="artefact">The <see cref="Artefact"/> instance</param> /// <param name="baseLevel">The distance from the most derived type, of the type to format the artefact string for</param> /// <param name="levelValid"><c>out</c> parameter that indicates if <paramref name="baseLevel"/> was valid (not higher than type inheritance depth)</param> public static string GetString(Artefact artefact, int baseLevel, out bool levelValid) { Type TArtefact = artefact.GetType(); Type[] typeHeirarchy = _typeHeirarchyCache.ContainsKey(TArtefact) ? _typeHeirarchyCache[TArtefact] : _typeHeirarchyCache[TArtefact] = Artefact.GetTypeHeirarchy(TArtefact); levelValid = baseLevel < typeHeirarchy.Length ? true : false; return levelValid ? GetString(artefact, typeHeirarchy[baseLevel]) : string.Empty; }
public void GiveArtefact(Character character, Artefact artefact) { this.Inventory.DropArtefact(artefact); character.Inventory.GetArtefact(artefact); Console.WriteLine($"--[Персонаж {this.Name} передал артефакт {artefact.GetType().Name} персонажу {character.Name}]--"); }
/// <summary> /// Update the specified artefact. /// </summary> /// <param name="artefact">Artefact.</param> /// <remarks>IRepository implementation</remarks> public void Update(Artefact artefact) { Console.WriteLine("Update(#{1}:{0})", artefact.GetType().FullName, artefact.Id.HasValue ? artefact.Id.ToString() : "(null)", OperationContext.Current == null ? "(OpCtx=null)" : OperationContext.Current.ToString()); // .RequestContext == null ? "(RqCtx=null)" : // OperationContext.Current.RequestContext.RequestMessage == null ? "(RqMsg=null)" : // OperationContext.Current.RequestContext.RequestMessage.ToString()); ITransaction transaction = null; try { if (Session.Transaction == null || !Session.Transaction.IsActive) transaction = Session.BeginTransaction(); // int id = artefact.Id.Value; // if (_artefactCache.ContainsKey(id)) // _artefactCache[id].CopyMembersFrom(artefact); // else // _artefactCache[id] = artefact; // Session.Update(artefact, artefact.Id); // TODO: Think about this: if Session.Update fails _artefactCache will be invalid?? Session.Merge(artefact); // TODO: Think about this: if Session.Update fails _artefactCache will be invalid?? if (transaction != null) transaction.Commit(); } catch (Exception ex) { if (transaction != null) transaction.Rollback(); throw Error(ex, artefact); } finally { if (transaction != null) transaction.Dispose(); } }
/// <summary> /// Remove the specified artefact. /// </summary> /// <param name="artefact">Artefact.</param> /// <remarks>IRepository implementation</remarks> public void Remove(Artefact artefact) { Console.WriteLine("Remove(#{1}:{0})", artefact.GetType().FullName, artefact.Id.HasValue ? artefact.Id.ToString() : "(null)", OperationContext.Current == null ? "(OpCtx=null)" : OperationContext.Current.ToString()); // .RequestContext == null ? "(RqCtx=null)" : // OperationContext.Current.RequestContext.RequestMessage == null ? "(RqMsg=null)" : // OperationContext.Current.RequestContext.RequestMessage.ToString()); ITransaction transaction = null; // int id = -1; try { if (!artefact.IsTransient) { int id = artefact.Id.Value; if (Session.Transaction == null || !Session.Transaction.IsActive) transaction = Session.BeginTransaction(); Session.Delete(artefact.Id); if (_artefactCache.ContainsKey(id)) _artefactCache.Remove(id); if (transaction != null) transaction.Commit(); } } catch (Exception ex) { if (transaction != null) transaction.Rollback(); // else // Session.Save(artefact, id); throw Error(ex, artefact); } finally { if (transaction != null) transaction.Dispose(); } }
/// <summary> /// Gets the identifier. /// </summary> /// <returns>The identifier.</returns> /// <param name="artefact">Artefact.</param> /// <remarks>IRepository implementation</remarks> public int GetId(Artefact artefact) { Console.WriteLine("GetId(#{1}:{0})", artefact.GetType().FullName, artefact.Id.HasValue ? artefact.Id.ToString() : "(null)", OperationContext.Current == null ? "(OpCtx=null)" : OperationContext.Current.ToString()); // .RequestContext == null ? "(RqCtx=null)" : // OperationContext.Current.RequestContext.RequestMessage == null ? "(RqMsg=null)" : // OperationContext.Current.RequestContext.RequestMessage.ToString()); try { return artefact.IsTransient ? (artefact.Id = (int)Session.GetIdentifier(artefact)).Value : artefact.Id.Value; } catch (Exception ex) { throw Error(ex, artefact); } }
/// <summary> /// Gets the artefact string. /// </summary> /// <returns> /// The artefact string. /// </returns> /// <param name='artefact'> /// Artefact. /// </param> /// <exception cref="ArgumentNullException"> /// Is thrown when an argument passed to a method is invalid because it is <see langword="null" /> . /// </exception> public string GetArtefactString(Artefact artefact) { if (artefact == null) throw new ArgumentNullException("artefact"); if (string.IsNullOrEmpty(Format)) return string.Empty; Type T = artefact.GetType(); // StringBuilder sb = new StringBuilder(ArtefactFormat, 256); string sb = Format; int i, i2 = 0; string memberName; MemberInfo mi; object member; string memberString; BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.GetField | BindingFlags.GetProperty; i = sb.IndexOf('{'); while (i >= 0 && i < sb.Length - 1) { i = sb.IndexOf('{', i); if (i >= 0) { i2 = sb.IndexOf('}', i + 1); if (i2 >= 0) { memberName = sb.Substring(i + 1, i2 - i - 1); mi = T.GetMember(memberName)[0]; member = T.InvokeMember(memberName, bf, null, artefact, new object[] {}); sb = sb.Remove(i, i2 - i + 1); memberString = member == null ? "(null)" : member.ToString(); sb = sb.Insert(i, memberString); i += memberString.Length; } else break; } else break; } return sb.ToString(); }
public void UseArtifact(Artefact artefact, Character character) { int ind = Inventory.findArtefactSlot(artefact); if (ind != -1) { Console.WriteLine("Character {0} cast a spell {1} on {2}", this.Name, artefact.GetType().Name, character.Name); artefact.MagicCast(this, character); if (!artefact._isRenewable) { Inventory.DropArtefact(artefact); } } else { throw new Exception("This character doesn't have this artifact"); } }