PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/LootBard/BTerrariaMod/ServerWrapper.cs

http://TerrariaMEF.codeplex.com
C# | 259 lines | 173 code | 32 blank | 54 comment | 51 complexity | e5bfdb731ebfff623c90e0ea907e9670 MD5 | raw file
  1. // ------------------------------------------------------------------------------
  2. // <copyright file="ServerWrapper.cs" company="MyCompany.com">
  3. // Copyright © MyCompany.com. All rights reserved.
  4. // </copyright>
  5. // <author>Me</author>
  6. // ------------------------------------------------------------------------------
  7. namespace Terraria
  8. {
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using BTerrariaMod;
  13. using BTerrariaMod.Bards;
  14. using BTerrariaMod.Sugar;
  15. using Microsoft.Xna.Framework;
  16. /// <summary>
  17. /// Wraps the Terraria Server, providing extensibility points for modding.
  18. /// </summary>
  19. public class ServerWrapper : Terraria.Main
  20. {
  21. /// <summary>
  22. /// The last status message.
  23. /// </summary>
  24. private string lastStatus;
  25. /// <summary>
  26. /// The DropBard
  27. /// </summary>
  28. private DropBard dropBard;
  29. /// <summary>
  30. /// Is Wrapper initialized?
  31. /// </summary>
  32. private bool wrapperInitialized = false;
  33. /// <summary>
  34. /// The Chest bard
  35. /// </summary>
  36. private ChestBard chestBard;
  37. /// <summary>
  38. /// The World bard.
  39. /// </summary>
  40. private WorldBard worldBard;
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="ServerWrapper"/> class.
  43. /// </summary>
  44. /// <param name="args">The args.</param>
  45. public ServerWrapper(string[] args)
  46. {
  47. for (int i = 0; i < (int)args.Length; i++)
  48. {
  49. if (args[i].ToLower() == "-config")
  50. {
  51. i++;
  52. this.LoadDedConfig(args[i]);
  53. }
  54. if (args[i].ToLower() == "-port")
  55. {
  56. i++;
  57. try
  58. {
  59. int num1 = Convert.ToInt32(args[i]);
  60. Netplay.serverPort = num1;
  61. }
  62. catch
  63. {
  64. }
  65. }
  66. if (args[i].ToLower() == "-players" || args[i].ToLower() == "-maxplayers")
  67. {
  68. i++;
  69. try
  70. {
  71. int num2 = Convert.ToInt32(args[i]);
  72. SetNetPlayers(num2);
  73. }
  74. catch
  75. {
  76. }
  77. }
  78. if (args[i].ToLower() == "-pass" || args[i].ToLower() == "-password")
  79. {
  80. i++;
  81. Netplay.password = args[i];
  82. }
  83. if (args[i].ToLower() == "-world")
  84. {
  85. i++;
  86. SetWorld(args[i]);
  87. }
  88. if (args[i].ToLower() == "-worldname")
  89. {
  90. i++;
  91. SetWorldName(args[i]);
  92. }
  93. if (args[i].ToLower() == "-motd")
  94. {
  95. i++;
  96. NewMOTD(args[i]);
  97. }
  98. if (args[i].ToLower() == "-banlist")
  99. {
  100. i++;
  101. Netplay.banFile = args[i];
  102. }
  103. if (args[i].ToLower() == "-autoshutdown")
  104. {
  105. autoShut();
  106. }
  107. if (args[i].ToLower() == "-secure")
  108. {
  109. Netplay.spamCheck = true;
  110. }
  111. if (args[i].ToLower() == "-autocreate")
  112. {
  113. i++;
  114. string str1 = args[i];
  115. autoCreate(str1);
  116. }
  117. if (args[i].ToLower() == "-loadlib")
  118. {
  119. i++;
  120. string str2 = args[i];
  121. loadLib(str2);
  122. }
  123. }
  124. verboseNetplay = true;
  125. Console.WriteLine("Starting dedicated server");
  126. Console.WriteLine("Max recipes is " + Recipe.maxRecipes);
  127. this.dropBard = new BTerrariaMod.Bards.DropBard(this.AnnounceStatus);
  128. this.chestBard = new BTerrariaMod.Bards.ChestBard(this.AnnounceStatus);
  129. this.worldBard = new BTerrariaMod.Bards.WorldBard(this.AnnounceStatus);
  130. }
  131. /// <summary>
  132. /// Processes updates which have occurred in game time.
  133. /// </summary>
  134. /// <param name="gameTime">The game time.</param>
  135. protected override void Update(GameTime gameTime)
  136. {
  137. if (this.dropBard != null)
  138. {
  139. this.dropBard.AnnounceDrops(item);
  140. }
  141. this.SearchOpenChests();
  142. if (!this.wrapperInitialized && Netplay.anyClients)
  143. {
  144. this.InitialUpdate(gameTime);
  145. this.wrapperInitialized = true;
  146. }
  147. this.CheckCommands();
  148. base.Update(gameTime);
  149. }
  150. /// <summary>
  151. /// Announces the status.
  152. /// </summary>
  153. /// <param name="text">The text.</param>
  154. private void AnnounceStatus(string text)
  155. {
  156. if (text != null && text.Length > 0 && text != this.lastStatus)
  157. {
  158. Console.WriteLine("Player Names:" + this.ActivePlayers().Select(p => p.name).Aggregate((s1, s2) => s1 + "," + s2));
  159. Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd-hh:mm:ss") + "Broadcasting:" + text);
  160. Messenger.SendToAll(text);
  161. }
  162. else
  163. {
  164. Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd-hh:mm:ss") + "Not Broadcasting:" + text);
  165. }
  166. this.lastStatus = text;
  167. }
  168. /// <summary>
  169. /// Checks the commands.
  170. /// </summary>
  171. private void CheckCommands()
  172. {
  173. var commandRequests = player.FirstOrDefault(p => p != null && p.active && p.chatText.StartsWith("/"));
  174. if (commandRequests == null)
  175. {
  176. return;
  177. }
  178. this.AnnounceStatus("Found command, attempting to process");
  179. switch (commandRequests.chatText)
  180. {
  181. case "/orbs" :
  182. this.worldBard.AnnounceOrbs(Main.tile);
  183. break;
  184. case "/spawn":
  185. this.worldBard.AnnounceSpawn(Main.spawnTileX, Main.spawnTileY);
  186. break;
  187. default:
  188. break;
  189. }
  190. }
  191. /// <summary>
  192. /// Searches the open chests.
  193. /// </summary>
  194. private void SearchOpenChests()
  195. {
  196. var openChests = this.ActivePlayers().Where(p => p.chest >= 0).Select(p => p.chest);
  197. foreach (var activePlayer in this.ActivePlayers().Where(p => p.chest >= 0))
  198. {
  199. this.chestBard.ChestOpened(activePlayer, chest[activePlayer.chest]);
  200. }
  201. }
  202. /// <summary>
  203. /// Processes the initial game update.
  204. /// </summary>
  205. /// <param name="gameTime">The game time.</param>
  206. private void InitialUpdate(GameTime gameTime)
  207. {
  208. Console.WriteLine("server netmode=" + Main.netMode);
  209. }
  210. /// <summary>
  211. /// Returns the active players with the player index.
  212. /// </summary>
  213. /// <returns>The active players with the player index.</returns>
  214. private IEnumerable<Tuple<int, Player>> ActivePlayersWithIndex()
  215. {
  216. return player.JoinIndex().Where(p => p != null && p.Item2 != null && p.Item2.active);
  217. }
  218. /// <summary>
  219. /// Returns the active players.
  220. /// </summary>
  221. /// <returns>The active players.</returns>
  222. private IEnumerable<Player> ActivePlayers()
  223. {
  224. return player.Where(p => p != null && p.active == true);
  225. }
  226. }
  227. }