fork download
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections.Generic;
  4. using UnityEngine.Networking;
  5.  
  6. //Declaring the namespace all my scripts are found under
  7. namespace Panzerwolf
  8. {
  9. //Using everything from the previous scripts,
  10. //this one should be understandable without a significant amount of commenting.
  11. //You could try commenting it out yourself to test your understanding.
  12. //If you see functions here that aren't being called through code,
  13. //they are being called through UI elements in the program
  14. public class ChatServer : MonoBehaviour
  15. {
  16. NetworkServerSimple server = null;
  17.  
  18. //Unity stuff
  19. [SerializeField]GameObject StopServer;
  20. [SerializeField]GameObject CreateServer;
  21. [SerializeField]GameObject channelView;
  22. [SerializeField]GameObject channelPrefab;
  23.  
  24. [SerializeField]InputField channelInput;
  25.  
  26. //Returns an int
  27. [SerializeField]Dropdown dropdownClassPicker;
  28.  
  29. public int chatServerPort = 9999;
  30. public int maxConnections = 48;
  31.  
  32. //This is the value I use to classify channels as Global, Local, Party or Guild
  33. public int channelClassification = 0;
  34.  
  35. public Dictionary<ChatChannelId, ChatChannel> channels = new Dictionary<ChatChannelId, ChatChannel>();
  36. Dictionary<string, ChatChannel> channelsByName = new Dictionary<string, ChatChannel>();
  37. // Dictionary<string, ChatChannel> channelsByAlias = new Dictionary<string, ChatChannel>();
  38.  
  39. public Dictionary<ChatPersonId, ChatPerson> people = new Dictionary<ChatPersonId, ChatPerson>();
  40. public Dictionary<int, ChatPerson> logins = new Dictionary<int, ChatPerson>();
  41.  
  42.  
  43. //Setup the server
  44. public void Setup()
  45. {
  46. if(server == null)
  47. {
  48. server = new NetworkServerSimple();
  49.  
  50. //Setting up the handlers
  51. server.RegisterHandler(MsgType.Connect, OnConnect);
  52. server.RegisterHandler(MsgType.Disconnect, OnDisconnect);
  53. server.RegisterHandler(ChatMsg.ChannelJoin, OnChannelJoin);
  54. server.RegisterHandler(ChatMsg.ChannelLeave, OnChannelLeave);
  55. // server.RegisterHandler(ChatMsg.ChannelCreate, OnChannelCreate);
  56. server.RegisterHandler(ChatMsg.Talk, OnTalk);
  57. server.RegisterHandler(ChatMsg.Login, OnLogin);
  58. // server.RegisterHandler(ChatMsg.Logoff, OnLogoff);
  59.  
  60. server.Listen(chatServerPort);
  61.  
  62. Debug.Log ("Server up!");
  63. // CreateChannel ("Global");
  64. // CreateChannel ("Local");
  65. // CreateChannel ("Party");
  66.  
  67. //Unity UI stuff
  68. CreateServer.SetActive (false);
  69. StopServer.SetActive (true);
  70. }
  71.  
  72. }
  73.  
  74.  
  75. public void Stop()
  76. {
  77. //Unity UI stuff
  78. for(int i = 0; i < channelView.transform.childCount; i++)
  79. {
  80. Destroy (channelView.transform.GetChild (i).gameObject);
  81. }
  82.  
  83. CreateServer.SetActive (true);
  84. StopServer.SetActive (false);
  85.  
  86. //Clear the channel lists
  87. channels.Clear ();
  88. channelsByName.Clear ();
  89.  
  90. //Stop the server
  91. server.Stop ();
  92.  
  93. //Reset the server to null
  94. server = null;
  95. }
  96.  
  97. //This gets called once every frame
  98. void Update ()
  99. {
  100. if (server != null)
  101. {
  102. //Send incoming data and outgoing data
  103. server.Update ();
  104. }
  105. }
  106.  
  107.  
  108. //Method to get reference to channel using name
  109. #region Utility
  110. ChatChannel FindChannel(string name)
  111. {
  112. if (channelsByName.ContainsKey(name))
  113. {
  114. return channelsByName[name];
  115. }
  116. return null;
  117. }
  118.  
  119.  
  120. //Method to get reference to channel using id
  121. ChatChannel FindChannel(ChatChannelId id)
  122. {
  123. if (channels.ContainsKey(id))
  124. {
  125. return channels[id];
  126. }
  127. return null;
  128. }
  129.  
  130. //Method to get reference to person using id
  131. ChatPerson FindPerson(ChatPersonId id)
  132. {
  133. if (people.ContainsKey(id))
  134. {
  135. return people[id];
  136. }
  137. return null;
  138. }
  139. #endregion
  140.  
  141.  
  142. //Known
  143. void OnConnect(NetworkMessage netMsg)
  144. {
  145. Debug.Log("Chat client connect");
  146. }
  147.  
  148.  
  149. //Known
  150. void OnDisconnect(NetworkMessage netMsg)
  151. {
  152. //When someone disconnects, remove their connection from the logins list
  153. logins.Remove (netMsg.conn.connectionId);
  154. Debug.Log("Chat client disconnect");
  155. }
  156.  
  157.  
  158. //Known
  159. void OnLogin(NetworkMessage netMsg)
  160. {
  161. var msg = netMsg.ReadMessage<LoginMessage> ();
  162.  
  163. if(logins.ContainsKey(netMsg.conn.connectionId))
  164. {
  165. Debug.LogError ("Client already logged in");
  166. return;
  167. }
  168.  
  169. var person = new ChatPerson (msg.personName, netMsg.conn);
  170.  
  171. //Add person to logins list at index connectionId
  172. logins [netMsg.conn.connectionId] = person;
  173.  
  174. //Add person to people list
  175. people [person.personId] = person;
  176.  
  177. Debug.Log("Login: " + person.personName + " " + person.personId);
  178.  
  179.  
  180. //Send LoginResponse to the client that is requesting a logon
  181. var response = new LoginResponseMessage();
  182. response.personName = msg.personName;
  183. response.personId = person.personId;
  184. netMsg.conn.Send(ChatMsg.Login, response);
  185. }
  186.  
  187. void OnChannelJoin(NetworkMessage netMsg)
  188. {
  189. if (!logins.ContainsKey(netMsg.conn.connectionId))
  190. {
  191. Debug.LogError("Not logged in");
  192. return;
  193. }
  194.  
  195. var msg = netMsg.ReadMessage<ChannelJoinRequestMessage>();
  196.  
  197. Debug.Log ("Server OnChannelJoin called");
  198. var channel = FindChannel(msg.name);
  199. if (channel == null)
  200. {
  201. Debug.LogError("channel not found " + msg.name);
  202. var outMsg = new ChannelJoinFailedMessage ();
  203. netMsg.conn.Send (ChatMsg.ChannelFailed, outMsg);
  204. return;
  205. }
  206.  
  207. var person = FindPerson(msg.personId);
  208. if (person == null)
  209. {
  210. Debug.LogError("person not found " + msg.personId);
  211. return;
  212. }
  213.  
  214. channel.JoinServer(person);
  215. Debug.Log("Join: " + channel.channelName + " " + person.personId);
  216.  
  217. }
  218.  
  219. void OnChannelLeave(NetworkMessage netMsg)
  220. {
  221. if(!logins.ContainsKey(netMsg.conn.connectionId))
  222. {
  223. Debug.LogError ("Can't leave channel: not logged in");
  224. return;
  225. }
  226.  
  227. var msg = netMsg.ReadMessage<ChannelLeaveRequestMessage> ();
  228.  
  229. var channel = FindChannel (msg.channelId);
  230. if(channel == null)
  231. {
  232. Debug.LogError ("Channel not found");
  233. return;
  234. }
  235.  
  236. var person = FindPerson (msg.personId);
  237. if(person == null)
  238. {
  239. Debug.LogError ("Person not found");
  240. return;
  241. }
  242.  
  243.  
  244. channel.LeaveServer (person);
  245. Debug.Log ("Left channel");
  246. }
  247.  
  248.  
  249.  
  250. void OnTalk(NetworkMessage netMsg)
  251. {
  252. if (!logins.ContainsKey(netMsg.conn.connectionId))
  253. {
  254. Debug.LogError("OnTalk Not logged in");
  255. return;
  256. }
  257.  
  258. var msg = netMsg.ReadMessage<TalkMessage>();
  259.  
  260. var channel = FindChannel(msg.channelId);
  261. if (channel == null)
  262. {
  263. Debug.LogError("OnTalk channel not found " + msg.channelId);
  264. return;
  265. }
  266.  
  267. var person = FindPerson(msg.personId);
  268. if (person == null)
  269. {
  270. Debug.LogError("OnTalk person not found " + msg.personId);
  271. return;
  272. }
  273.  
  274. channel.ServerSay(person, msg.text);
  275. Debug.Log("Talk: " + msg.text + " " + person.personId);
  276. }
  277.  
  278. public void ButtonCreateChannel()
  279. {
  280. string Name = channelInput.text;
  281. string Alias = "";
  282. Color color = new Color ();
  283.  
  284. if(channelClassification == 0)
  285. {
  286. foreach(var ch in channels)
  287. {
  288. if (ch.Value.channelAlias == "Global")
  289. {
  290. Debug.LogError ("Can only have one Global aliased channel");
  291. return;
  292. }
  293. }
  294. color = Color.white;
  295. Alias = "Global";
  296. }
  297.  
  298. else if(channelClassification == 1)
  299. {
  300. color = Color.yellow;
  301. Alias = "Local";
  302. }
  303.  
  304. else if(channelClassification == 2)
  305. {
  306. color = Color.cyan;
  307. Alias = "Party";
  308. }
  309.  
  310. else if(channelClassification == 3)
  311. {
  312. color = Color.green;
  313. Alias = "Guild";
  314. }
  315.  
  316. var newChannel = CreateChannel (Name, color, Alias);
  317.  
  318. if (newChannel == null)
  319. {
  320. return;
  321. }
  322.  
  323. GameObject channel = Instantiate (channelPrefab);
  324. channel.transform.SetParent (channelView.transform);
  325. channel.transform.GetChild (0).GetComponent<Button> ().onClick.AddListener (() => RemoveChannel (Name, newChannel.channelId));
  326. channel.transform.GetChild (1).GetComponent<Text> ().text = channelInput.text;
  327. channel.transform.GetChild (1).GetComponent<Text> ().color = color;
  328. channel.name = "Channel: " + Name;
  329.  
  330.  
  331. channelInput.text = string.Empty;
  332. }
  333. public void ChangeColor()
  334. {
  335. channelClassification = dropdownClassPicker.value;
  336. Debug.Log (channelClassification);
  337. }
  338.  
  339. public void RemoveChannel(string name, ChatChannelId channelid )
  340. {
  341. Debug.Log ("Removechannel called!");
  342. channels [channelid].people.Clear ();
  343. channels [channelid].peopleList.Clear ();
  344. channels.Remove (channelid);
  345. channelsByName.Remove (name);
  346. Destroy(GameObject.Find ("Channel: " + name));
  347. }
  348.  
  349. ChatChannel CreateChannel(string name, Color color, string alias)
  350. {
  351.  
  352. foreach (var ch in channels.Values)
  353. {
  354. if (ch.channelName == name)
  355. {
  356. Debug.Log("Create: already exists " + name);
  357. return null;
  358. }
  359. }
  360.  
  361. var channel = new ChatChannel(name, color, alias);
  362. channels[channel.channelId] = channel;
  363. channelsByName[channel.channelName] = channel;
  364. // channel.CreateServer();
  365.  
  366.  
  367. Debug.Log("Created channel " + channel.channelName);
  368. return channel;
  369. // var response = new ChannelCreateResponseMessage();
  370. // response.channelName = channel.channelName;
  371. // response.channelId = channel.channelId;
  372. //
  373. // netMsg.conn.Send(ChatMsg.ChannelCreate, response);
  374.  
  375. }
  376.  
  377.  
  378.  
  379. }
  380.  
  381. }
  382.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(1,7): error CS0246: The type or namespace name `UnityEngine' could not be found. Are you missing an assembly reference?
prog.cs(2,7): error CS0246: The type or namespace name `UnityEngine' could not be found. Are you missing an assembly reference?
prog.cs(4,7): error CS0246: The type or namespace name `UnityEngine' could not be found. Are you missing an assembly reference?
prog.cs(14,28): error CS0246: The type or namespace name `MonoBehaviour' could not be found. Are you missing an assembly reference?
prog.cs(16,3): error CS0246: The type or namespace name `NetworkServerSimple' could not be found. Are you missing an assembly reference?
prog.cs(19,19): error CS0246: The type or namespace name `GameObject' could not be found. Are you missing an assembly reference?
prog.cs(20,19): error CS0246: The type or namespace name `GameObject' could not be found. Are you missing an assembly reference?
prog.cs(21,19): error CS0246: The type or namespace name `GameObject' could not be found. Are you missing an assembly reference?
prog.cs(22,19): error CS0246: The type or namespace name `GameObject' could not be found. Are you missing an assembly reference?
prog.cs(24,19): error CS0246: The type or namespace name `InputField' could not be found. Are you missing an assembly reference?
prog.cs(27,19): error CS0246: The type or namespace name `Dropdown' could not be found. Are you missing an assembly reference?
prog.cs(35,21): error CS0246: The type or namespace name `ChatChannelId' could not be found. Are you missing an assembly reference?
prog.cs(35,36): error CS0246: The type or namespace name `ChatChannel' could not be found. Are you missing an assembly reference?
prog.cs(36,22): error CS0246: The type or namespace name `ChatChannel' could not be found. Are you missing an assembly reference?
prog.cs(39,21): error CS0246: The type or namespace name `ChatPersonId' could not be found. Are you missing an assembly reference?
prog.cs(39,35): error CS0246: The type or namespace name `ChatPerson' could not be found. Are you missing an assembly reference?
prog.cs(40,26): error CS0246: The type or namespace name `ChatPerson' could not be found. Are you missing an assembly reference?
prog.cs(110,3): error CS0246: The type or namespace name `ChatChannel' could not be found. Are you missing an assembly reference?
prog.cs(121,3): error CS0246: The type or namespace name `ChatChannel' could not be found. Are you missing an assembly reference?
prog.cs(131,3): error CS0246: The type or namespace name `ChatPerson' could not be found. Are you missing an assembly reference?
prog.cs(143,18): error CS0246: The type or namespace name `NetworkMessage' could not be found. Are you missing an assembly reference?
prog.cs(150,21): error CS0246: The type or namespace name `NetworkMessage' could not be found. Are you missing an assembly reference?
prog.cs(159,16): error CS0246: The type or namespace name `NetworkMessage' could not be found. Are you missing an assembly reference?
prog.cs(187,22): error CS0246: The type or namespace name `NetworkMessage' could not be found. Are you missing an assembly reference?
prog.cs(219,23): error CS0246: The type or namespace name `NetworkMessage' could not be found. Are you missing an assembly reference?
prog.cs(250,15): error CS0246: The type or namespace name `NetworkMessage' could not be found. Are you missing an assembly reference?
prog.cs(339,42): error CS0246: The type or namespace name `ChatChannelId' could not be found. Are you missing an assembly reference?
prog.cs(349,3): error CS0246: The type or namespace name `ChatChannel' could not be found. Are you missing an assembly reference?
Compilation failed: 28 error(s), 0 warnings
stdout
Standard output is empty