PageRenderTime 59ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/fCraft/Player/PlayerEnumerable.cs

https://github.com/EpicClowny/LegendCraft
C# | 688 lines | 424 code | 90 blank | 174 comment | 187 complexity | 312916d3118171343118f58a805c3e8d MD5 | raw file
  1. // Copyright 2009-2012 Matvei Stefarov <me@matvei.org>
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using JetBrains.Annotations;
  6. // ReSharper disable LoopCanBeConvertedToQuery
  7. namespace fCraft {
  8. /// <summary> Contains a set of utilities that simplify working with sets of players.
  9. /// All the utilities are implemented as extension methods,
  10. /// and it is recommended that you invoke them as extension methods. </summary>
  11. public static class PlayerEnumerable {
  12. #region Rank Filters
  13. /// <summary> Filters a collection of players, leaving only those of the given rank. </summary>
  14. /// <param name="source"> Original collection of players. Will not get modified. </param>
  15. /// <param name="rank"> Desired rank. </param>
  16. /// <returns> Filtered collection of players. </returns>
  17. [NotNull]
  18. public static IEnumerable<Player> Ranked( this IEnumerable<Player> source, Rank rank ) {
  19. if( source == null ) throw new ArgumentNullException( "source" );
  20. if( rank == null ) throw new ArgumentNullException( "rank" );
  21. foreach( Player player in source ) {
  22. if( player.Info.Rank == rank ) {
  23. yield return player;
  24. }
  25. }
  26. }
  27. /// <summary> Filters a collection of players, leaving only those NOT of the given rank. </summary>
  28. /// <param name="source"> Original collection of players. Will not get modified. </param>
  29. /// <param name="rank"> Undesired rank. </param>
  30. /// <returns> Filtered collection of players. </returns>
  31. [NotNull]
  32. public static IEnumerable<Player> NotRanked( this IEnumerable<Player> source, Rank rank ) {
  33. if( source == null ) throw new ArgumentNullException( "source" );
  34. if( rank == null ) throw new ArgumentNullException( "rank" );
  35. foreach( Player player in source ) {
  36. if( player.Info.Rank != rank ) {
  37. yield return player;
  38. }
  39. }
  40. }
  41. /// <summary> Filters a collection of players, leaving only those above the given rank. </summary>
  42. /// <param name="source"> Original collection of players. Will not get modified. </param>
  43. /// <param name="minRank"> All ranks above this one will be kept. This and lower ranks will be filtered out. </param>
  44. /// <returns> Filtered collection of players. </returns>
  45. [NotNull]
  46. public static IEnumerable<Player> RankedAbove( this IEnumerable<Player> source, Rank minRank ) {
  47. if( source == null ) throw new ArgumentNullException( "source" );
  48. if( minRank == null ) throw new ArgumentNullException( "minRank" );
  49. foreach( Player player in source ) {
  50. if( player.Info.Rank > minRank ) {
  51. yield return player;
  52. }
  53. }
  54. }
  55. /// <summary> Filters a collection of players, leaving only those of or above the given rank. </summary>
  56. /// <param name="source"> Original collection of players. Will not get modified. </param>
  57. /// <param name="minRank"> Minimum desired rank. </param>
  58. /// <returns> Filtered collection of players. </returns>
  59. [NotNull]
  60. public static IEnumerable<Player> RankedAtLeast( this IEnumerable<Player> source, Rank minRank ) {
  61. if( source == null ) throw new ArgumentNullException( "source" );
  62. if( minRank == null ) throw new ArgumentNullException( "minRank" );
  63. foreach( Player player in source ) {
  64. if( player.Info.Rank >= minRank ) {
  65. yield return player;
  66. }
  67. }
  68. }
  69. /// <summary> Filters a collection of players, leaving only those below the given rank. </summary>
  70. /// <param name="source"> Original collection of players. Will not get modified. </param>
  71. /// <param name="maxRank"> All ranks below this one will be kept. This and higher ranks will be filtered out. </param>
  72. /// <returns> Filtered collection of players. </returns>
  73. [NotNull]
  74. public static IEnumerable<Player> RankedBelow( this IEnumerable<Player> source, Rank maxRank ) {
  75. if( source == null ) throw new ArgumentNullException( "source" );
  76. if( maxRank == null ) throw new ArgumentNullException( "maxRank" );
  77. foreach( Player player in source ) {
  78. if( player.Info.Rank < maxRank ) {
  79. yield return player;
  80. }
  81. }
  82. }
  83. /// <summary> Filters a collection of players, leaving only those of or below the given rank. </summary>
  84. /// <param name="source"> Original collection of players. Will not get modified. </param>
  85. /// <param name="maxRank"> Maximum desired rank. </param>
  86. /// <returns> Filtered collection of players. </returns>
  87. [NotNull]
  88. public static IEnumerable<Player> RankedAtMost( this IEnumerable<Player> source, Rank maxRank ) {
  89. if( source == null ) throw new ArgumentNullException( "source" );
  90. if( maxRank == null ) throw new ArgumentNullException( "maxRank" );
  91. foreach( Player player in source ) {
  92. if( player.Info.Rank <= maxRank ) {
  93. yield return player;
  94. }
  95. }
  96. }
  97. #endregion
  98. #region Permissions
  99. /// <summary> Filters a collection of players, leaving only those who have the given permission. </summary>
  100. /// <param name="source"> Original collection of players. Will not get modified. </param>
  101. /// <param name="permission"> Permission that players are required to have. </param>
  102. /// <returns> Filtered collection of players. </returns>
  103. [NotNull]
  104. public static IEnumerable<Player> Can( [NotNull] this IEnumerable<Player> source, Permission permission ) {
  105. if( source == null ) throw new ArgumentNullException( "source" );
  106. foreach( Player player in source ) {
  107. if( player.Can( permission ) ) {
  108. yield return player;
  109. }
  110. }
  111. }
  112. /// <summary> Filters a collection of players, leaving only those who have the given permission,
  113. /// and with permission limits allowing operation on the given rank. </summary>
  114. /// <param name="source"> Original collection of players. Will not get modified. </param>
  115. /// <param name="permission"> Permission that players are required to have. </param>
  116. /// <param name="affectedRank"> Permission limit will be checked against this rank. </param>
  117. /// <returns> Filtered collection of players. </returns>
  118. [NotNull]
  119. public static IEnumerable<Player> Can( [NotNull] this IEnumerable<Player> source, Permission permission, [NotNull] Rank affectedRank ) {
  120. if( source == null ) throw new ArgumentNullException( "source" );
  121. if( affectedRank == null ) throw new ArgumentNullException( "affectedRank" );
  122. foreach( Player player in source ) {
  123. if( player.Can( permission, affectedRank ) ) {
  124. yield return player;
  125. }
  126. }
  127. }
  128. /// <summary> Filters a collection of players, leaving only those who do NOT have the given permission. </summary>
  129. /// <param name="source"> Original collection of players. Will not get modified. </param>
  130. /// <param name="permission"> Permission that players are required to NOT have. </param>
  131. /// <returns> Filtered collection of players. </returns>
  132. [NotNull]
  133. public static IEnumerable<Player> Cant( [NotNull] this IEnumerable<Player> source, Permission permission ) {
  134. foreach( Player player in source ) {
  135. if( !player.Can( permission ) ) {
  136. yield return player;
  137. }
  138. }
  139. }
  140. /// <summary> Filters a collection of players, leaving only those who do NOT have the given permission,
  141. /// or with permission limits NOT allowing operation on the given rank. </summary>
  142. /// <param name="source"> Original collection of players. Will not get modified. </param>
  143. /// <param name="permission"> Permission that players are required to NOT have. </param>
  144. /// <param name="affectedRank"> Permission limit will be checked against this rank. </param>
  145. /// <returns> Filtered collection of players. </returns>
  146. [NotNull]
  147. public static IEnumerable<Player> Cant( [NotNull] this IEnumerable<Player> source, Permission permission, [NotNull] Rank affectedRank ) {
  148. if( source == null ) throw new ArgumentNullException( "source" );
  149. if( affectedRank == null ) throw new ArgumentNullException( "affectedRank" );
  150. foreach( Player player in source ) {
  151. if( !player.Can( permission, affectedRank ) ) {
  152. yield return player;
  153. }
  154. }
  155. }
  156. /// <summary> Filters a collection of players, leaving only those who can see the target.
  157. /// Does not include the target itself. </summary>
  158. /// <param name="source"> Original collection of players. Will not get modified. </param>
  159. /// <param name="targetPlayer"> Player whose visibility is being tested. </param>
  160. /// <returns> Filtered collection of players. </returns>
  161. [NotNull]
  162. public static IEnumerable<Player> CanSee( [NotNull] this IEnumerable<Player> source, [NotNull] Player targetPlayer ) {
  163. if( source == null ) throw new ArgumentNullException( "source" );
  164. if( targetPlayer == null ) throw new ArgumentNullException( "targetPlayer" );
  165. foreach( Player player in source ) {
  166. if( player != targetPlayer && player.CanSee( targetPlayer ) ) {
  167. yield return player;
  168. }
  169. }
  170. }
  171. /// <summary> Filters a collection of players, leaving only those who can NOT see the target.
  172. /// Does not include the target itself. </summary>
  173. /// <param name="source"> Original collection of players. Will not get modified. </param>
  174. /// <param name="targetPlayer"> Player whose visibility is being tested. </param>
  175. /// <returns> Filtered collection of players. </returns>
  176. [NotNull]
  177. public static IEnumerable<Player> CantSee( [NotNull] this IEnumerable<Player> source, [NotNull] Player targetPlayer ) {
  178. if( source == null ) throw new ArgumentNullException( "source" );
  179. if( targetPlayer == null ) throw new ArgumentNullException( "targetPlayer" );
  180. foreach( Player player in source ) {
  181. if( player != targetPlayer && !player.CanSee( targetPlayer ) ) {
  182. yield return player;
  183. }
  184. }
  185. }
  186. /// <summary> Filters a collection of players, leaving only those who can be seen by the given player. </summary>
  187. /// <param name="source"> Original collection of players. Will not get modified. </param>
  188. /// <param name="observer"> Player whose vision is being tested. </param>
  189. /// <returns> Filtered collection of players. </returns>
  190. [NotNull]
  191. public static IEnumerable<Player> CanBeSeen( [NotNull] this IEnumerable<Player> source, [NotNull] Player observer ) {
  192. if( source == null ) throw new ArgumentNullException( "source" );
  193. if( observer == null ) throw new ArgumentNullException( "observer" );
  194. foreach( Player player in source ) {
  195. if( player != observer && observer.CanSee( player ) ) {
  196. yield return player;
  197. }
  198. }
  199. }
  200. /// <summary> Filters a collection of players, leaving only those who can NOT be seen by the given player. </summary>
  201. /// <param name="source"> Original collection of players. Will not get modified. </param>
  202. /// <param name="observer"> Player whose vision is being tested. </param>
  203. /// <returns> Filtered collection of players. </returns>
  204. [NotNull]
  205. public static IEnumerable<Player> CantBeSeen( [NotNull] this IEnumerable<Player> source, [NotNull] Player observer ) {
  206. if( source == null ) throw new ArgumentNullException( "source" );
  207. if( observer == null ) throw new ArgumentNullException( "observer" );
  208. foreach( Player player in source ) {
  209. if( player != observer && !observer.CanSee( player ) ) {
  210. yield return player;
  211. }
  212. }
  213. }
  214. #endregion
  215. #region Ignore
  216. /// <summary> Filters a collection of players, leaving only those who are ignoring the given player. </summary>
  217. /// <param name="source"> Original collection of players. Will not get modified. </param>
  218. /// <param name="player"> Player whose ignore standing is being checked. </param>
  219. /// <returns> Filtered collection of players. </returns>
  220. [NotNull]
  221. public static IEnumerable<Player> Ignoring( [NotNull] this IEnumerable<Player> source, [NotNull] Player player ) {
  222. if( source == null ) throw new ArgumentNullException( "source" );
  223. if( player == null ) throw new ArgumentNullException( "player" );
  224. foreach( Player otherPlayer in source ) {
  225. if( otherPlayer.IsIgnoring( player.Info ) ) {
  226. yield return otherPlayer;
  227. }
  228. }
  229. }
  230. /// <summary> Filters a collection of players, leaving only those who are NOT ignoring the given player. </summary>
  231. /// <param name="source"> Original collection of players. Will not get modified. </param>
  232. /// <param name="player"> Player whose ignore standing is being checked. </param>
  233. /// <returns> Filtered collection of players. </returns>
  234. [NotNull]
  235. public static IEnumerable<Player> NotIgnoring( [NotNull] this IEnumerable<Player> source, [NotNull] Player player ) {
  236. if( source == null ) throw new ArgumentNullException( "source" );
  237. if( player == null ) throw new ArgumentNullException( "player" );
  238. foreach( Player otherPlayer in source ) {
  239. if( !otherPlayer.IsIgnoring( player.Info ) ) {
  240. yield return otherPlayer;
  241. }
  242. }
  243. }
  244. /// <summary> Filters a collection of players, leaving only those who are ignoring the given player. </summary>
  245. /// <param name="source"> Original collection of players. Will not get modified. </param>
  246. /// <param name="playerInfo"> Player whose ignore standing is being checked. </param>
  247. /// <returns> Filtered collection of players. </returns>
  248. [NotNull]
  249. public static IEnumerable<Player> Ignoring( [NotNull] this IEnumerable<Player> source, [NotNull] PlayerInfo playerInfo ) {
  250. if( source == null ) throw new ArgumentNullException( "source" );
  251. if( playerInfo == null ) throw new ArgumentNullException( "playerInfo" );
  252. foreach( Player otherPlayer in source ) {
  253. if( otherPlayer.IsIgnoring( playerInfo ) ) {
  254. yield return otherPlayer;
  255. }
  256. }
  257. }
  258. /// <summary> Filters a collection of players, leaving only those who are NOT ignoring the given player. </summary>
  259. /// <param name="source"> Original collection of players. Will not get modified. </param>
  260. /// <param name="playerInfo"> Player whose ignore standing is being checked. </param>
  261. /// <returns> Filtered collection of players. </returns>
  262. [NotNull]
  263. public static IEnumerable<Player> NotIgnoring( [NotNull] this IEnumerable<Player> source, [NotNull] PlayerInfo playerInfo ) {
  264. if( source == null ) throw new ArgumentNullException( "source" );
  265. if( playerInfo == null ) throw new ArgumentNullException( "playerInfo" );
  266. foreach( Player otherPlayer in source ) {
  267. if( !otherPlayer.IsIgnoring( playerInfo ) ) {
  268. yield return otherPlayer;
  269. }
  270. }
  271. }
  272. /// <summary> Filters a collection of players, leaving only those who are ignored by the given player. </summary>
  273. /// <param name="source"> Original collection of players. Will not get modified. </param>
  274. /// <param name="ignorer"> Player whose disposition is being checked. </param>
  275. /// <returns> Filtered collection of players. </returns>
  276. [NotNull]
  277. public static IEnumerable<Player> IgnoredBy( [NotNull] this IEnumerable<Player> source, [NotNull] Player ignorer ) {
  278. if( source == null ) throw new ArgumentNullException( "source" );
  279. if( ignorer == null ) throw new ArgumentNullException( "ignorer" );
  280. foreach( Player otherPlayer in source ) {
  281. if( ignorer.IsIgnoring( otherPlayer.Info ) ) {
  282. yield return otherPlayer;
  283. }
  284. }
  285. }
  286. /// <summary> Filters a collection of players, leaving only those who are NOT ignored by the given player. </summary>
  287. /// <param name="source"> Original collection of players. Will not get modified. </param>
  288. /// <param name="ignorer"> Player whose disposition is being checked. </param>
  289. /// <returns> Filtered collection of players. </returns>
  290. [NotNull]
  291. public static IEnumerable<Player> NotIgnoredBy( [NotNull] this IEnumerable<Player> source, [NotNull] Player ignorer ) {
  292. if( source == null ) throw new ArgumentNullException( "source" );
  293. if( ignorer == null ) throw new ArgumentNullException( "ignorer" );
  294. foreach( Player otherPlayer in source ) {
  295. if( !ignorer.IsIgnoring( otherPlayer.Info ) ) {
  296. yield return otherPlayer;
  297. }
  298. }
  299. }
  300. #endregion
  301. #region Worlds
  302. /// <summary> Filters a collection of players, leaving only those who are currently located on the given world. </summary>
  303. /// <param name="source"> Original collection of players. Will not get modified. </param>
  304. /// <param name="world"> World that players are desired to be on. </param>
  305. /// <returns> Filtered collection of players. </returns>
  306. [NotNull]
  307. public static IEnumerable<Player> InWorld( [NotNull] this IEnumerable<Player> source, [NotNull] World world ) {
  308. if( source == null ) throw new ArgumentNullException( "source" );
  309. if( world == null ) throw new ArgumentNullException( "world" );
  310. foreach( Player player in source ) {
  311. if( player.World == world ) {
  312. yield return player;
  313. }
  314. }
  315. }
  316. /// <summary> Filters a collection of players, leaving only those who are currently NOT located on the given world. </summary>
  317. /// <param name="source"> Original collection of players. Will not get modified. </param>
  318. /// <param name="world"> World that players are desired to NOT be on. </param>
  319. /// <returns> Filtered collection of players. </returns>
  320. [NotNull]
  321. public static IEnumerable<Player> NotInWorld( [NotNull] this IEnumerable<Player> source, [NotNull] World world ) {
  322. if( source == null ) throw new ArgumentNullException( "source" );
  323. if( world == null ) throw new ArgumentNullException( "world" );
  324. foreach( Player player in source ) {
  325. if( player.World != world ) {
  326. yield return player;
  327. }
  328. }
  329. }
  330. #endregion
  331. #region Personal Inclusion / Exclusion
  332. /// <summary> Adds players to the given set.
  333. /// ]If the given sequence of players already contains player, no duplicate is added.
  334. /// Precisely speaking, produces the set union of a given collection of players and a given player. </summary>
  335. /// <param name="source"> Original set of players. Will not get modified. </param>
  336. /// <param name="includedPlayer"> Player to add to the set. </param>
  337. /// <returns> A set that contains all players in the input sequence, plus the given player. </returns>
  338. [NotNull]
  339. public static IEnumerable<Player> Union( [NotNull] this IEnumerable<Player> source, [NotNull] Player includedPlayer ) {
  340. bool found = false;
  341. foreach( Player player in source ) {
  342. yield return player;
  343. if( player == includedPlayer ) {
  344. found = true;
  345. }
  346. }
  347. if( !found ) {
  348. yield return includedPlayer;
  349. }
  350. }
  351. /// <summary> Removes player from the given set.
  352. /// Precisely speaking, produces the set difference between the given collection of players and a given player. </summary>
  353. /// <param name="source"> Original set of players. Will not get modified. </param>
  354. /// <param name="excludedPlayer"> Player to remove from the set. </param>
  355. /// <returns> A set that contains all players in the input sequence, minus the given player. </returns>
  356. [NotNull]
  357. public static IEnumerable<Player> Except( [NotNull] this IEnumerable<Player> source, [CanBeNull] Player excludedPlayer ) {
  358. foreach( Player player in source ) {
  359. if( player != excludedPlayer ) {
  360. yield return player;
  361. }
  362. }
  363. }
  364. #endregion
  365. #region IPAddress
  366. /// <summary> Filters a collection of players, leaving only those connected from a given IP. </summary>
  367. /// <param name="source"> Original collection of players. Will not get modified. </param>
  368. /// <param name="ip"> IP that we are including. </param>
  369. /// <returns> Filtered collection of players. </returns>
  370. [NotNull]
  371. public static IEnumerable<Player> FromIP( [NotNull] this IEnumerable<Player> source, [NotNull] IPAddress ip ) {
  372. if( source == null ) throw new ArgumentNullException( "source" );
  373. if( ip == null ) throw new ArgumentNullException( "ip" );
  374. foreach( Player player in source ) {
  375. if( ip.Equals( player.IP ) ) {
  376. yield return player;
  377. }
  378. }
  379. }
  380. /// <summary> Filters a collection of players, leaving only those NOT connected from a given IP. </summary>
  381. /// <param name="source"> Original collection of players. Will not get modified. </param>
  382. /// <param name="ip"> IP that we are excluding. </param>
  383. /// <returns> Filtered collection of players. </returns>
  384. [NotNull]
  385. public static IEnumerable<Player> NotFromIP( [NotNull] this IEnumerable<Player> source, [NotNull] IPAddress ip ) {
  386. if( source == null ) throw new ArgumentNullException( "source" );
  387. if( ip == null ) throw new ArgumentNullException( "ip" );
  388. foreach( Player player in source ) {
  389. if( !ip.Equals( player.IP ) ) {
  390. yield return player;
  391. }
  392. }
  393. }
  394. #endregion
  395. #region Messaging
  396. /// <summary> Broadcasts a message. </summary>
  397. /// <param name="source"> List of players who will receive the message. </param>
  398. /// <param name="message"> String/message to send. </param>
  399. /// <returns> Number of players who received the message. </returns>
  400. public static int Message( [NotNull] this IEnumerable<Player> source,
  401. [NotNull] string message ) {
  402. if( source == null ) throw new ArgumentNullException( "source" );
  403. if( message == null ) throw new ArgumentNullException( "message" );
  404. int i = 0;
  405. foreach( Packet packet in LineWrapper.Wrap( message ) ) {
  406. foreach( Player player in source ) {
  407. player.Send( packet );
  408. i++;
  409. }
  410. }
  411. return i;
  412. }
  413. /// <summary> Broadcasts a message. </summary>
  414. /// <param name="source"> List of players who will receive the message. </param>
  415. /// <param name="except"> Player to exclude from the recepient list. </param>
  416. /// <param name="message"> String/message to send. </param>
  417. /// <returns> Number of players who received the message. </returns>
  418. public static int Message( [NotNull] this IEnumerable<Player> source,
  419. [CanBeNull] Player except,
  420. [NotNull] string message ) {
  421. if( source == null ) throw new ArgumentNullException( "source" );
  422. if( message == null ) throw new ArgumentNullException( "message" );
  423. int i = 0;
  424. foreach( Packet packet in LineWrapper.Wrap( message ) ) {
  425. foreach( Player player in source ) {
  426. if( player == except ) continue;
  427. player.Send( packet );
  428. i++;
  429. }
  430. }
  431. return i;
  432. }
  433. /// <summary> Formats and broadcasts a message. </summary>
  434. /// <param name="source"> List of players who will receive the message. </param>
  435. /// <param name="except"> Player to exclude from the recepient list. </param>
  436. /// <param name="message"> String/message to send. </param>
  437. /// <param name="formatArgs"> Format parameters. Same semantics as String.Format </param>
  438. /// <returns> Number of players who received the message. </returns>
  439. [StringFormatMethod( "message" )]
  440. public static int Message( [NotNull] this IEnumerable<Player> source,
  441. [CanBeNull] Player except,
  442. [NotNull] string message,
  443. [NotNull] params object[] formatArgs ) {
  444. if( source == null ) throw new ArgumentNullException( "source" );
  445. if( message == null ) throw new ArgumentNullException( "message" );
  446. if( formatArgs == null ) throw new ArgumentNullException( "formatArgs" );
  447. int i = 0;
  448. foreach( Packet packet in LineWrapper.Wrap( String.Format( message, formatArgs ) ) ) {
  449. foreach( Player player in source ) {
  450. if( player == except ) continue;
  451. player.Send( packet );
  452. i++;
  453. }
  454. }
  455. return i;
  456. }
  457. /// <summary> Formats and broadcasts a message. </summary>
  458. /// <param name="source"> List of players who will receive the message. </param>
  459. /// <param name="message"> String/message to send. </param>
  460. /// <param name="formatArgs"> Format parameters. Same semantics as String.Format </param>
  461. /// <returns> Number of players who received the message. </returns>
  462. [StringFormatMethod( "message" )]
  463. public static int Message( [NotNull] this IEnumerable<Player> source,
  464. [NotNull] string message,
  465. [NotNull] params object[] formatArgs ) {
  466. if( source == null ) throw new ArgumentNullException( "source" );
  467. if( message == null ) throw new ArgumentNullException( "message" );
  468. if( formatArgs == null ) throw new ArgumentNullException( "formatArgs" );
  469. int i = 0;
  470. foreach( Packet packet in LineWrapper.Wrap( String.Format( message, formatArgs ) ) ) {
  471. foreach( Player player in source ) {
  472. player.Send( packet );
  473. i++;
  474. }
  475. }
  476. return i;
  477. }
  478. /// <summary> Broadcasts a message, prefixing wrapped lines. </summary>
  479. /// <param name="source"> List of players who will receive the message. </param>
  480. /// <param name="prefix"> Prefix to prepend to prepend to each line after the 1st,
  481. /// if any line-wrapping occurs. Does NOT get prepended to first line. </param>
  482. /// <param name="message"> String/message to send. </param>
  483. /// <returns> Number of players who received the message. </returns>
  484. public static int MessagePrefixed( [NotNull] this IEnumerable<Player> source, [NotNull] string prefix, [NotNull] string message ) {
  485. if( source == null ) throw new ArgumentNullException( "source" );
  486. if( prefix == null ) throw new ArgumentNullException( "prefix" );
  487. if( message == null ) throw new ArgumentNullException( "message" );
  488. int i = 0;
  489. foreach( Packet packet in LineWrapper.WrapPrefixed( prefix, message ) ) {
  490. foreach( Player player in source ) {
  491. player.Send( packet );
  492. i++;
  493. }
  494. }
  495. return i;
  496. }
  497. /// <summary> Formats and broadcasts a message, prefixing wrapped lines. </summary>
  498. /// <param name="source"> List of players who will receive the message. </param>
  499. /// <param name="prefix"> Prefix to prepend to prepend to each line after the 1st,
  500. /// if any line-wrapping occurs. Does NOT get prepended to first line. </param>
  501. /// <param name="message"> String/message to send. </param>
  502. /// <param name="formatArgs"> Format parameters. Same semantics as String.Format </param>
  503. /// <returns> Number of players who received the message. </returns>
  504. [StringFormatMethod( "message" )]
  505. public static int MessagePrefixed( [NotNull] this IEnumerable<Player> source, [NotNull] string prefix, [NotNull] string message, params object[] formatArgs ) {
  506. if( source == null ) throw new ArgumentNullException( "source" );
  507. if( message == null ) throw new ArgumentNullException( "message" );
  508. if( prefix == null ) throw new ArgumentNullException( "prefix" );
  509. if( formatArgs == null ) throw new ArgumentNullException( "formatArgs" );
  510. int i = 0;
  511. foreach( Packet packet in LineWrapper.WrapPrefixed( prefix, String.Format( message, formatArgs ) ) ) {
  512. foreach( Player player in source ) {
  513. player.Send( packet );
  514. i++;
  515. }
  516. }
  517. return i;
  518. }
  519. /// <summary> Formats and broadcasts a message, showing on top-left for those who use WoM. </summary>
  520. /// <param name="source"> List of players who will receive the message. </param>
  521. /// <param name="message"> String/message to send. </param>
  522. /// <param name="formatArgs"> Format parameters. Same semantics as String.Format </param>
  523. /// <returns> Number of players who received the message. </returns>
  524. [StringFormatMethod( "message" )]
  525. public static int MessageAlt( [NotNull] this IEnumerable<Player> source,
  526. [NotNull] string message,
  527. [NotNull] params object[] formatArgs ) {
  528. if( source == null ) throw new ArgumentNullException( "source" );
  529. if( message == null ) throw new ArgumentNullException( "message" );
  530. if( formatArgs == null ) throw new ArgumentNullException( "formatArgs" );
  531. int i = 0;
  532. foreach( Player player in source ) {
  533. player.MessageAlt( message, formatArgs );
  534. i++;
  535. }
  536. return i;
  537. }
  538. #endregion
  539. #region Packet Sending
  540. /// <summary> Broadcasts a packet with normal priority. </summary>
  541. /// <param name="source"> List of players who will receive the packet. </param>
  542. /// <param name="packet"> Packet to send. </param>
  543. /// <returns> Number of players who received the packet. </returns>
  544. public static int Send( [NotNull] this IEnumerable<Player> source, Packet packet ) {
  545. if( source == null ) throw new ArgumentNullException( "source" );
  546. int i = 0;
  547. foreach( Player player in source ) {
  548. player.Send( packet );
  549. i++;
  550. }
  551. return i;
  552. }
  553. /// <summary> Broadcasts a packet with normal priority. </summary>
  554. /// <param name="source"> List of players who will receive the packet. </param>
  555. /// <param name="except"> Player to exclude from the recepient list. </param>
  556. /// <param name="packet"> Packet to send. </param>
  557. /// <returns> Number of players who received the packet. </returns>
  558. public static int Send( [NotNull] this IEnumerable<Player> source, [CanBeNull] Player except, Packet packet ) {
  559. if( source == null ) throw new ArgumentNullException( "source" );
  560. int i = 0;
  561. foreach( Player player in source ) {
  562. if( player == except ) continue;
  563. player.Send( packet );
  564. i++;
  565. }
  566. return i;
  567. }
  568. /// <summary> Broadcasts a packet with low priority. </summary>
  569. /// <param name="source"> List of players who will receive the packet. </param>
  570. /// <param name="packet"> Packet to send. </param>
  571. /// <returns> Number of players who received the packet. </returns>
  572. public static int SendLowPriority( [NotNull] this IEnumerable<Player> source, Packet packet ) {
  573. if( source == null ) throw new ArgumentNullException( "source" );
  574. int i = 0;
  575. foreach( Player player in source ) {
  576. player.SendLowPriority( packet );
  577. i++;
  578. }
  579. return i;
  580. }
  581. /// <summary> Broadcasts a packet with low priority. </summary>
  582. /// <param name="source"> List of players who will receive the packet. </param>
  583. /// <param name="except"> Player to exclude from the recepient list. </param>
  584. /// <param name="packet"> Packet to send. </param>
  585. /// <returns> Number of players who received the packet. </returns>
  586. public static int SendLowPriority( [NotNull] this IEnumerable<Player> source, [CanBeNull] Player except, Packet packet ) {
  587. if( source == null ) throw new ArgumentNullException( "source" );
  588. int i = 0;
  589. foreach( Player player in source ) {
  590. if( player == except ) continue;
  591. player.SendLowPriority( packet );
  592. i++;
  593. }
  594. return i;
  595. }
  596. #endregion
  597. }
  598. }