fork download
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Security.Policy;
  4. using UnityEngine.Networking;
  5.  
  6.  
  7.  
  8. //Declaring the namespace all my scripts are found under
  9. namespace Panzerwolf
  10. {
  11. //Declaring public struct.
  12. //In this case ChatChannelId will only ever be referring
  13. //to a value, not an instance of a class,
  14. //thus I declare it as a struct.
  15. [System.Serializable]
  16. public struct ChatChannelId
  17. {
  18. //What the struct contains
  19. public int value;
  20.  
  21. //Override methods, for adding functionality to the baseline methods
  22. public override string ToString ()
  23. {
  24. return "Channel:" + value;
  25. //This will return "ChatChannel:value" when called
  26. //Very useful but not required anymore now I guess,
  27. //I primarily used it for debugging
  28. }
  29.  
  30.  
  31. //The GetHashCode method is what a dictionary calls from an object
  32. //to index it in
  33. public override int GetHashCode()
  34. {
  35. //Typically two structs of type "ChatChannelId" would have the same HashCode.
  36. //We override this by using their value as HashCodes,
  37. //thus returning two unique HashCodes and separating them in a Dictionary
  38. return (int)value;
  39. }
  40.  
  41. //Generic method Equals
  42. public override bool Equals(object obj)
  43. {
  44. //Checks if object to compare is actually a ChatChannelId,
  45. //and if the object can be typecast as a ChatChannelId
  46. return obj is ChatChannelId && this == (ChatChannelId)obj;
  47. //Returns true only of both conditions match, else returns false
  48. }
  49.  
  50. //Generic method CompareValue
  51. //This is so the struct can be compared
  52. //to other structs of the same type
  53. //without returning true, by using the
  54. //value of each struct
  55. public static bool operator ==(ChatChannelId c1, ChatChannelId c2)
  56. {
  57. //Grabs the values for both structs and compares them
  58. return c1.value == c2.value;
  59. //Returns true if true(well, duh!)
  60. }
  61.  
  62. //Generic method !CompareValue
  63. //See above
  64. public static bool operator !=(ChatChannelId c1, ChatChannelId c2)
  65. {
  66. return c1.value != c2.value;
  67. }
  68. }
  69.  
  70. //Declaring public struct
  71. //See above
  72. [System.Serializable]
  73. public struct ChatPersonId
  74. {
  75. public int value;
  76.  
  77. public override string ToString ()
  78. {
  79. return "ChatPerson:" + value;
  80. }
  81.  
  82. public override int GetHashCode()
  83. {
  84. return (int)value;
  85. }
  86.  
  87. public override bool Equals (object obj)
  88. {
  89. return obj is ChatPersonId && this == (ChatPersonId)obj;
  90. }
  91.  
  92. public static bool operator ==(ChatPersonId c1, ChatPersonId c2)
  93. {
  94. return c1.value == c2.value;
  95. }
  96.  
  97. public static bool operator !=(ChatPersonId c1, ChatPersonId c2)
  98. {
  99. return c1.value != c2.value;
  100. }
  101. }
  102.  
  103. //Declaring public class
  104. //This is a class..
  105. //Why a class you ask?
  106. //Because school is in session
  107. [System.Serializable]
  108. public class ChatPerson
  109. {
  110. //Declare static int so that it is a separate instance
  111. //that carries the same value(including changes) across more than
  112. //one instance of ChatPerson
  113. static int nextPersonId = 2000;
  114.  
  115. //Name
  116. public string personName;
  117.  
  118. //Struct ID
  119. public ChatPersonId personId;
  120.  
  121. //A reference to the connection
  122. public NetworkConnection connection;
  123. //This will only be valid on server side
  124.  
  125. //ChatPerson constructor for client
  126. public ChatPerson(string name, ChatPersonId id)
  127. {
  128. personName = name;
  129. personId = id;
  130. }
  131.  
  132. //ChatPerson constructor for server
  133. public ChatPerson(string name, NetworkConnection conn)
  134. {
  135. personName = name;
  136. personId.value = nextPersonId++;
  137. connection = conn;
  138. }
  139. }
  140.  
  141.  
  142. //Declaring public class
  143. //See above
  144. [System.Serializable]
  145. public class ChatChannel
  146. {
  147. //Declare static
  148. //See above
  149. static int nextChannelId = 1000;
  150.  
  151. //Declaring public int(i use this one for message count)
  152. //Note that it is not static so each channel
  153. //has their own instance of the type "arbitraryCounter"
  154. public int arbitraryCounter = 0;
  155.  
  156. //Name
  157. public string channelName;
  158.  
  159. //Color of text in channel
  160. public Color channelColor;
  161.  
  162. //Alias of the channel
  163. public string channelAlias;
  164.  
  165. //Struct ID
  166. public ChatChannelId channelId;
  167.  
  168. //**NOT USED**
  169. public ChatPerson channelOwner;
  170.  
  171. //Declare public Dictionary.
  172. //A dictionary is a list where you can input a key and get the associated value,
  173. //or vise versa.
  174. public Dictionary<ChatPersonId, ChatPerson> people = new Dictionary<ChatPersonId, ChatPerson> ();
  175. //Calling "people[randomPersonId]" would return the corresponding ChatPerson VALUE
  176. //associated with the KEY, while calling "people[randomChatPerson]" would return
  177. //the ChatPersonId KEY associated with the VALUE.
  178.  
  179.  
  180. //Declare public list
  181. //A list is a place where you can store a reference of instances of the same type class.
  182. public List<ChatPerson> peopleList = new List<ChatPerson>();
  183. //Getting a reference to a spesific instance can be done with the Find and FindIndex method
  184. //Use Find(delegate method) or FindIndex(int index)
  185.  
  186. //////////////////////////////////////////////////////////////////////////////////////////////
  187. // Example: peopleList.Find(x => x.personName == personNameImLookingFor); //
  188. // //
  189. // Explanation: Find(x // this is the one you want to grab //
  190. // => // where //
  191. // x.value == value)// compares all references in the list to value //
  192. // //
  193. // This will return a reference to the first instance in the list //
  194. // that matches your search method. //
  195. //////////////////////////////////////////////////////////////////////////////////////////////
  196.  
  197. //See above
  198. public List<TalkMessage> messages = new List<TalkMessage>();
  199.  
  200. //ChatChannel constructor for client
  201. public ChatChannel (string name, ChatChannelId id, Color color, string alias)
  202. {
  203. channelName = name;
  204. channelId = id;
  205. channelColor = color;
  206. }
  207.  
  208. //ChatChannel constructor for server
  209. public ChatChannel(string name, Color color, string alias)
  210. {
  211. channelName = name;
  212. channelColor = color;
  213. channelAlias = alias;
  214. channelId.value = nextChannelId++;
  215. }
  216.  
  217. //**NOT USED**
  218. //This is part of how I would allow a person to create a channel
  219. // public bool CreateServer(ChatPerson person)
  220. // {
  221. // people [person.personId] = person;
  222. // peopleList.Add (person);
  223. // return true;
  224. // }
  225.  
  226.  
  227. //Declaring public bool
  228. //Why a bool you ask?
  229. //Because I like that the method returns true if it succeeds,
  230. //and false if not.
  231. public bool JoinServer(ChatPerson person)
  232. {
  233. //Checks if the people dictionary contains the person trying to join
  234. if(people.ContainsKey(person.personId))
  235. {
  236. //Logs error to unity console or .exe console if dev mode is enabled
  237. Debug.LogError ("Person already exists in channel");
  238. return false;
  239. }
  240.  
  241. //Create a new NetworkMessage to send
  242. //Make it a type ChannelJoinResponseMessage
  243. //This encrypts the NetworkMessage to this type,
  244. //and requires that any receivers must decode it
  245. //with a reader of the same type
  246. var outMsg = new ChannelJoinResponseMessage ();
  247.  
  248. //Set message values
  249. outMsg.channelName = channelName;
  250. outMsg.channelAlias = channelAlias;
  251. outMsg.channelId = channelId;
  252. outMsg.channelColor = channelColor;
  253. outMsg.personId = person.personId;
  254. outMsg.personName = person.personName;
  255.  
  256.  
  257. //For each person in the peoplelist,
  258. foreach(var other in peopleList)
  259. {
  260. //send them the message containing the person that has joined
  261. other.connection.Send (ChatMsg.ChannelJoin, outMsg);
  262. }
  263.  
  264. //Add the person to the list
  265. people [person.personId] = person;
  266. peopleList.Add (person);
  267.  
  268. //For each person in the peopleList
  269. foreach(var other in peopleList)
  270. {
  271. //Change the message to contain information about the
  272. //other person in the channel peopleList
  273. outMsg.personId = other.personId;
  274. outMsg.personName = other.personName;
  275.  
  276. //send the person joining the channel a message containing
  277. //information about the others in the channel
  278. person.connection.Send (ChatMsg.ChannelJoin, outMsg);
  279. }
  280.  
  281. //return true when successful
  282. return true;
  283. }
  284.  
  285. //See above
  286. public bool LeaveServer(ChatPerson person)
  287. {
  288. //See above
  289. if(!people.ContainsKey(person.personId))
  290. {
  291. //See above
  292. Debug.LogError ("Person doesn't exist in channel");
  293. return false;
  294. }
  295.  
  296. //See above
  297. var outMsg = new ChannelLeaveResponseMessage ();
  298. outMsg.channelId = channelId;
  299. outMsg.personId = person.personId;
  300.  
  301. //See above
  302. foreach(var other in peopleList)
  303. {
  304. //Check if the connection is actually there
  305. if (other.connection != null)
  306. //Send the message containing information about who left
  307. other.connection.Send (ChatMsg.ChannelLeave, outMsg);
  308. else
  309. //Remove the invalid connection's person from the list
  310. peopleList.Remove (other);
  311. }
  312.  
  313. Debug.LogError ("Removing person from channel..");
  314.  
  315. //Remove the person leaving the server from the dictionary and list
  316. people.Remove (person.personId);
  317. peopleList.Remove (person);
  318.  
  319. //See above
  320. return true;
  321. }
  322.  
  323. //See above
  324. public bool ServerSay(ChatPerson person, string text)
  325. {
  326. //See above
  327. if(!people.ContainsKey(person.personId))
  328. {
  329. //See above
  330. Debug.LogError ("Channel does not contain player");
  331. return false;
  332. }
  333.  
  334. //In the process for implementing commands atm
  335. //Disregard this
  336. // if (text.Substring(0,1) == "/")
  337. // Debug.LogError ("Was a command");
  338.  
  339. //See above
  340. var outMsg = new TalkMessage ();
  341. outMsg.personId = person.personId;
  342. outMsg.channelId = channelId;
  343. outMsg.text = text;
  344. outMsg.count = arbitraryCounter;
  345.  
  346. //Add to the counter
  347. arbitraryCounter++;
  348.  
  349. //See above
  350. foreach(var other in peopleList)
  351. {
  352. other.connection.Send (ChatMsg.Talk, outMsg);
  353. }
  354.  
  355. return true;
  356. }
  357.  
  358. //Add person to channel
  359. public void ClientAdd(ChatPerson person)
  360. {
  361. people [person.personId] = person;
  362. peopleList.Add (person);
  363. }
  364.  
  365. //Remove person from channel
  366. public void ClientRemove(ChatPerson person)
  367. {
  368. peopleList.Remove (person);
  369. }
  370. }
  371. }
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(4,7): error CS0246: The type or namespace name `UnityEngine' could not be found. Are you missing an assembly reference?
prog.cs(122,10): error CS0246: The type or namespace name `NetworkConnection' could not be found. Are you missing an assembly reference?
prog.cs(133,34): error CS0246: The type or namespace name `NetworkConnection' could not be found. Are you missing an assembly reference?
prog.cs(160,10): error CS0246: The type or namespace name `Color' could not be found. Are you missing `System.Drawing' using directive?
prog.cs(198,15): error CS0246: The type or namespace name `TalkMessage' could not be found. Are you missing an assembly reference?
prog.cs(201,54): error CS0246: The type or namespace name `Color' could not be found. Are you missing `System.Drawing' using directive?
prog.cs(209,35): error CS0246: The type or namespace name `Color' could not be found. Are you missing `System.Drawing' using directive?
Compilation failed: 8 error(s), 0 warnings
stdout
Standard output is empty