Guest User

Untitled

a guest
Jan 13th, 2016
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.16 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class MainMenu : MonoBehaviour
  6. {
  7. #region UI
  8. private GameObject m_MenuEffects;
  9. //Screens
  10. private GameObject m_MenuScreen;
  11. private GameObject m_LoginScreen;
  12. private GameObject m_GameScreen;
  13. private GameObject m_HostServerScreen;
  14. private GameObject m_ServerStartedScreen;
  15. private GameObject m_NormalGameScreen;
  16.  
  17. //Buttons
  18. private Button m_LoginButton;
  19.  
  20. //Dialogs
  21. private GameObject m_ExitDialog;
  22. private GameObject m_BannedDialog;
  23. private GameObject m_RenewDialog;
  24. private GameObject m_OptionsDialog;
  25.  
  26. //Option Overlays
  27. private GameObject m_GraphicOptionsOverlay;
  28.  
  29. //Details
  30. private Text m_LoginDetails;
  31. private Text m_ConnectionDetails;
  32. private Text m_HostDetails;
  33. #endregion
  34.  
  35. public enum MenuScreenStatus
  36. {
  37. LoginScreen,
  38. GameScreen,
  39. HostServerScreen,
  40. ServerStartedScreen,
  41. NormalGameScreen
  42. }
  43. public enum DialogScreenStatus
  44. {
  45.  
  46. }
  47.  
  48. #region Database Connection Details
  49. private string m_Url = "http://46.105.118.169/~uklifega/jksadj1328.php";
  50. private InputField m_Username;
  51. private InputField m_Password;
  52. private enum LoginStatus
  53. {
  54. UsernamePasswordNull,
  55. UserBanned,
  56. UserInactive,
  57. IncorrectUsernamePassword,
  58. UserMember,
  59. UserServerOwner
  60. }
  61. private enum LoginConnectionStatus
  62. {
  63. AttemptLogin,
  64. WaitingForResponse,
  65. ResponseRecieved,
  66. CheckingUsernamePassword
  67. }
  68. //private LoginStatus m_LoginStatus;
  69. #endregion
  70.  
  71. private bool m_Server = false;
  72. private void Awake()
  73. {
  74. m_MenuEffects = GameObject.Find("MenuEffects");
  75.  
  76. //Assign all Screen Variables
  77. m_MenuScreen = GameObject.Find("Menu_Screen"); m_LoginScreen = GameObject.Find("Login_Screen"); m_GameScreen = GameObject.Find("Game_Screen");
  78. m_HostServerScreen = GameObject.Find("HostServer_Screen"); m_ServerStartedScreen = GameObject.Find("ServerStarted_Screen");
  79. m_NormalGameScreen = GameObject.Find("NormalGame_Screen");
  80.  
  81. //Assign all Button Variables
  82. m_LoginButton = GameObject.Find("Login_Button").GetComponent<Button>();
  83.  
  84. //Assign all Dialog Variables
  85. m_ExitDialog = GameObject.Find("Exit_Dialog"); m_BannedDialog = GameObject.Find("Banned_Dialog"); m_RenewDialog = GameObject.Find("Renew_Dialog");
  86. m_OptionsDialog = GameObject.Find("Options_Dialog");
  87.  
  88. //Assign all Overlay Variables
  89. m_GraphicOptionsOverlay = GameObject.Find("GraphicOptions_Overlay");
  90.  
  91. //Assign all Details Variables
  92. m_ConnectionDetails = GameObject.Find("Connection_Details").GetComponent<Text>(); m_HostDetails = GameObject.Find("Host_Details").GetComponent<Text>();
  93. m_LoginDetails = GameObject.Find("Login_Details").GetComponent<Text>();
  94.  
  95. //Assign Username and Password Input Field
  96. m_Username = GameObject.Find("Username_Input").GetComponent<InputField>(); m_Password = GameObject.Find("Password_Input").GetComponent<InputField>();
  97. //Disable all that arnt needed
  98. m_GameScreen.SetActive(false); m_HostServerScreen.SetActive(false); m_ServerStartedScreen.SetActive(false); m_NormalGameScreen.SetActive(false);
  99. m_ExitDialog.SetActive(false); m_BannedDialog.SetActive(false); m_RenewDialog.SetActive(false); m_OptionsDialog.SetActive(false);
  100. m_GraphicOptionsOverlay.SetActive(false);
  101.  
  102. }
  103.  
  104. #region Switch Statements
  105. private void LoginStatusDetailed(LoginStatus loginStatus)
  106. {
  107. switch (loginStatus)
  108. {
  109. case LoginStatus.IncorrectUsernamePassword://Incorrect Username or Password
  110. DisplayLoginStatus("Incorrect Username/Password");
  111. m_LoginButton.interactable = true;
  112. break;
  113. case LoginStatus.UserBanned://Banned
  114. DisplayLoginStatus("Account Banned");
  115. m_LoginButton.interactable = true;
  116. break;
  117. case LoginStatus.UserInactive://Inactive Membership
  118. DisplayLoginStatus("Inactive Account");
  119. m_LoginButton.interactable = true;
  120. break;
  121. case LoginStatus.UserMember://Active Membership
  122. DisplayLoginStatus("Logged In");
  123. m_LoginButton.interactable = true;
  124. break;
  125. case LoginStatus.UsernamePasswordNull://Username or Password is Null
  126. DisplayLoginStatus("Username/Password Field is Empty");
  127. m_LoginButton.interactable = true;
  128. break;
  129. case LoginStatus.UserServerOwner://Can Host a Server
  130. DisplayLoginStatus("Server Account");
  131. m_LoginButton.interactable = true;
  132. break;
  133. }
  134. }
  135. private void LoginConnectionState(LoginConnectionStatus loginConnectionStatus)
  136. {
  137. switch(loginConnectionStatus)
  138. {
  139. case LoginConnectionStatus.AttemptLogin:
  140. DisplayLoginStatus("Attempting to Login");
  141. break;
  142. case LoginConnectionStatus.CheckingUsernamePassword:
  143. DisplayLoginStatus("Checking Username and Password");
  144. break;
  145. case LoginConnectionStatus.ResponseRecieved:
  146. DisplayLoginStatus("Response Recieved");
  147. break;
  148. case LoginConnectionStatus.WaitingForResponse:
  149. DisplayLoginStatus("Waiting for Response");
  150. break;
  151. }
  152. }
  153. private void CheckCredentials(string text)
  154. {
  155. switch (text)
  156. {
  157. case "null":
  158. LoginStatusDetailed(LoginStatus.UsernamePasswordNull);
  159. break;
  160. case "banned":
  161. LoginStatusDetailed(LoginStatus.UserBanned);
  162. break;
  163. case "server":
  164. m_Server = true;
  165. MenuSwap("ServerMenu");
  166. LoginStatusDetailed(LoginStatus.UserServerOwner);
  167. break;
  168. case "member":
  169. m_Server = false;
  170. MenuSwap("GameMenu");
  171. LoginStatusDetailed(LoginStatus.UserMember);
  172. break;
  173. case "inactive":
  174. LoginStatusDetailed(LoginStatus.UserInactive);
  175. break;
  176. case "unsuccess":
  177. LoginStatusDetailed(LoginStatus.IncorrectUsernamePassword);
  178. break;
  179. }
  180. }
  181. #endregion
  182.  
  183. private void DisplayLoginStatus(string message)
  184. {
  185. m_LoginDetails.text = message;
  186. }
  187.  
  188. #region ButtonPress
  189. public void MenuSwap(string buttonType)
  190. {
  191. switch(buttonType)
  192. {
  193. case "Menu":
  194. ShowMenu(MenuScreenStatus.LoginScreen);
  195. break;
  196. case "Login":
  197. //Attempt to Login
  198. StartCoroutine(AttemptLogin());
  199. break;
  200. case "ServerMenu":
  201. ShowMenu(MenuScreenStatus.HostServerScreen);
  202. //PhotonNetwork.ConnectUsingSettings("0.01");
  203. break;
  204. case "GameMenu":
  205. ShowMenu(MenuScreenStatus.NormalGameScreen);
  206. //PhotonNetwork.ConnectUsingSettings("0.01");
  207. break;
  208. }
  209. }
  210. public void DialogSwap(string Function)
  211. {
  212. switch(Function)
  213. {
  214. case "Exit":
  215. //Open the Exit Dialog
  216. m_ExitDialog.SetActive(true);
  217. break;
  218. case "Options":
  219. //Open the Options Dialog
  220. m_OptionsDialog.SetActive(true);
  221. m_GraphicOptionsOverlay.SetActive(true);
  222. break;
  223. case "Credits":
  224. //Open the Credits Dialog
  225. //None-existant
  226. break;
  227. }
  228. }
  229. public void ShowMenu(MenuScreenStatus menuScreenStatus)
  230. {
  231. switch (menuScreenStatus)
  232. {
  233. case MenuScreenStatus.GameScreen:
  234. ShowGameMenu();
  235. break;
  236. case MenuScreenStatus.HostServerScreen:
  237. ShowServerMenu();
  238. break;
  239. case MenuScreenStatus.LoginScreen:
  240. ShowLoginMenu();
  241. break;
  242. case MenuScreenStatus.NormalGameScreen:
  243. ShowNormalGameMenu();
  244. break;
  245. case MenuScreenStatus.ServerStartedScreen:
  246. ShowServerStartedMenu();
  247. break;
  248. }
  249. }
  250. #endregion
  251.  
  252. #region ShowMenus
  253. private void ShowLoginMenu()
  254. {
  255. m_LoginScreen.SetActive(true);
  256. m_HostServerScreen.SetActive(false);
  257. m_GameScreen.SetActive(false);
  258. m_NormalGameScreen.SetActive(false);
  259. m_ServerStartedScreen.SetActive(false);
  260.  
  261. m_ExitDialog.SetActive(false);
  262. m_BannedDialog.SetActive(false);
  263. m_RenewDialog.SetActive(false);
  264. m_OptionsDialog.SetActive(false);
  265. }
  266. private void ShowServerMenu()
  267. {
  268. m_LoginScreen.SetActive(false);
  269. m_HostServerScreen.SetActive(true);
  270. m_GameScreen.SetActive(false);
  271. m_NormalGameScreen.SetActive(false);
  272. m_ServerStartedScreen.SetActive(false);
  273.  
  274. m_ExitDialog.SetActive(false);
  275. m_BannedDialog.SetActive(false);
  276. m_RenewDialog.SetActive(false);
  277. m_OptionsDialog.SetActive(false);
  278. }
  279. private void ShowGameMenu()
  280. {
  281. m_LoginScreen.SetActive(false);
  282. m_HostServerScreen.SetActive(false);
  283. m_GameScreen.SetActive(true);
  284. m_NormalGameScreen.SetActive(false);
  285. m_ServerStartedScreen.SetActive(false);
  286.  
  287. m_ExitDialog.SetActive(false);
  288. m_BannedDialog.SetActive(false);
  289. m_RenewDialog.SetActive(false);
  290. m_OptionsDialog.SetActive(false);
  291. }
  292. private void ShowNormalGameMenu()
  293. {
  294. m_LoginScreen.SetActive(false);
  295. m_HostServerScreen.SetActive(false);
  296. m_GameScreen.SetActive(false);
  297. m_NormalGameScreen.SetActive(true);
  298. m_ServerStartedScreen.SetActive(false);
  299.  
  300. m_ExitDialog.SetActive(false);
  301. m_BannedDialog.SetActive(false);
  302. m_RenewDialog.SetActive(false);
  303. m_OptionsDialog.SetActive(false);
  304. }
  305. private void ShowServerStartedMenu()
  306. {
  307. m_LoginScreen.SetActive(false);
  308. m_HostServerScreen.SetActive(false);
  309. m_GameScreen.SetActive(false);
  310. m_NormalGameScreen.SetActive(false);
  311. m_ServerStartedScreen.SetActive(true);
  312.  
  313. m_ExitDialog.SetActive(false);
  314. m_BannedDialog.SetActive(false);
  315. m_RenewDialog.SetActive(false);
  316. m_OptionsDialog.SetActive(false);
  317. }
  318. #endregion
  319. #region DialogMenus
  320. private void ExitDialog()
  321. {
  322. m_ExitDialog.SetActive(true);
  323. m_BannedDialog.SetActive(false);
  324. m_RenewDialog.SetActive(false);
  325. m_OptionsDialog.SetActive(false);
  326. }
  327. private void BannedDialog()
  328. {
  329. m_ExitDialog.SetActive(false);
  330. m_BannedDialog.SetActive(true);
  331. m_RenewDialog.SetActive(false);
  332. m_OptionsDialog.SetActive(false);
  333. }
  334. private void RenewDialog()
  335. {
  336. m_ExitDialog.SetActive(false);
  337. m_BannedDialog.SetActive(false);
  338. m_RenewDialog.SetActive(true);
  339. m_OptionsDialog.SetActive(false);
  340. }
  341. private void OptionsDialog()
  342. {
  343. m_ExitDialog.SetActive(false);
  344. m_BannedDialog.SetActive(false);
  345. m_RenewDialog.SetActive(false);
  346. m_OptionsDialog.SetActive(true);
  347. }
  348. private void CloseDialog()
  349. {
  350. m_ExitDialog.SetActive(false);
  351. m_BannedDialog.SetActive(false);
  352. m_RenewDialog.SetActive(false);
  353. m_OptionsDialog.SetActive(false);
  354. }
  355. #endregion
  356.  
  357. #region ConnectToDatabase
  358. private IEnumerator AttemptLogin()
  359. {
  360. m_LoginButton.interactable = false;
  361. LoginConnectionState(LoginConnectionStatus.AttemptLogin);
  362. yield return new WaitForSeconds(.5f);
  363. WWWForm form = new WWWForm();
  364. form.AddField("name", m_Username.text);
  365. form.AddField("password", m_Password.text);
  366.  
  367. WWW url = new WWW(m_Url, form);
  368. LoginConnectionState(LoginConnectionStatus.WaitingForResponse);
  369. yield return new WaitForSeconds(.5f);
  370. yield return url;
  371.  
  372. if (url.error == null)
  373. {//No errors
  374. LoginConnectionState(LoginConnectionStatus.ResponseRecieved);
  375. yield return new WaitForSeconds(.5f);
  376. CheckCredentials(url.text);
  377. }
  378. else
  379. {//Error
  380. DisplayLoginStatus(url.error);
  381. }
  382. }
  383. string ConnectionState()
  384. {/*
  385. switch (PhotonNetwork.connectionStateDetailed)
  386. {
  387. case PeerState.Authenticated:
  388. return "Authenticated";
  389. case PeerState.Authenticating:
  390. return "Authenticating";
  391. case PeerState.ConnectedToGameserver:
  392. return "Connected To Gameserver";
  393. case PeerState.ConnectedToMaster:
  394. return "Connected To Masterserver";
  395. case PeerState.ConnectedToNameServer:
  396. return "Connected To Nameserver";
  397. case PeerState.ConnectingToGameserver:
  398. return "Connecting To Gameserver";
  399. case PeerState.ConnectingToMasterserver:
  400. return "Connecting To Masterserver";
  401. case PeerState.ConnectingToNameServer:
  402. return "Connecting To NameServer";
  403. case PeerState.Disconnected:
  404. return "Disconnected";
  405. case PeerState.Disconnecting:
  406. return "Disconnecting";
  407. case PeerState.DisconnectingFromGameserver:
  408. return "Disconnecting From Gameserver";
  409. case PeerState.DisconnectingFromMasterserver:
  410. return "Disconnecting From Masterserver";
  411. case PeerState.DisconnectingFromNameServer:
  412. return "Disconnecting From NameServer";
  413. case PeerState.Joined:
  414. return "Joined";
  415. case PeerState.JoinedLobby:
  416. return "Joined Lobby";
  417. case PeerState.Joining:
  418. return "Joining";
  419. case PeerState.Leaving:
  420. return "Leaving";
  421. case PeerState.PeerCreated:
  422. return "Peer Created";
  423. case PeerState.Queued:
  424. return "Queued";
  425. case PeerState.QueuedComingFromGameserver:
  426. return "Queued Coming From Gameserver";
  427. case PeerState.Uninitialized:
  428. return "Uninitialized";
  429. }*/
  430. return null;
  431. }
  432. void Update()
  433. {
  434. // if (PhotonNetwork.connectionStateDetailed == PeerState.PeerCreated)
  435. // m_ConnectionDetails.text = "";
  436. // else
  437. // m_ConnectionDetails.text = "Connection Status: " + ConnectionState();
  438. }
  439. #endregion
  440. }
Add Comment
Please, Sign In to add comment