Advertisement
Guest User

Untitled

a guest
May 25th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.67 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.EventSystems;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using Sfs2X;
  8. using Sfs2X.Logging;
  9. using Sfs2X.Util;
  10. using Sfs2X.Core;
  11. using Sfs2X.Entities;
  12. using Sfs2X.Entities.Managers;
  13. using Sfs2X.Requests;
  14. using UnityEngine.SceneManagement;
  15. using Sfs2X.Entities.Variables;
  16.  
  17. public class InGameConnector : MonoBehaviour
  18. {
  19.  
  20. //-----------------------
  21. // Initialize Sripts
  22. //-----------------------
  23.  
  24. public bool hosting;
  25.  
  26. //-----------------------
  27. // Initialize variables
  28. //-----------------------
  29.  
  30. private SmartFox sfs;
  31. public string ding;
  32. public string changedVar;
  33. public bool FIRST_IS_ALWAYS_HOST;
  34. public Text debugText;
  35.  
  36. public List<User> users;
  37. public int userAmount;
  38. public List<UserVariable> userVars;
  39. public List<UserVariable> userMessage;
  40. public InputField inputFieldRaise;
  41. public Text potAmountText;
  42. public Text chipsAmountText;
  43. public Text StateButtonText;
  44. //---------------------
  45. // Card shit initialized
  46. //---------------------
  47.  
  48. public DealCards cardDealer;
  49. public string p1k1;
  50. private string p1k2;
  51. private string p2k1;
  52. private string p2k2;
  53. private string p3k1;
  54. private string p3k2;
  55. private string p4k1;
  56. private string p4k2;
  57. public string finalCard1;
  58. public string finalCard2;
  59. public string raiseMessage;
  60. public string foldMessage;
  61. public string allInMessage;
  62. public string checkMessage;
  63. public string deleteCardsMessage;
  64. public string activateButtons;
  65. public string deActivateButtons;
  66. public string turnMessage;
  67.  
  68.  
  69.  
  70.  
  71. public int potAmount;
  72. public int raiseAmount;
  73. public int callAmount;
  74.  
  75. public bool roundStart;
  76. public bool userMadeAChoice;
  77. public bool folded;
  78. public bool givenTurn;
  79. public int currentUser;
  80. public int currentTurn;
  81.  
  82. //-------------------
  83. // UI Elements
  84. //-------------------
  85.  
  86. public InputField input;
  87. public Button sendButton;
  88. public int totalChipsAmount;
  89.  
  90. public Button check;
  91. public Button fold;
  92. public Button raise;
  93. public Button allIn;
  94.  
  95. //----------------------
  96. // Scene is loaded and started initializing
  97. //----------------------
  98.  
  99. void Start()
  100. {
  101. hosting = false;
  102. userMessage = new List<UserVariable> ();
  103. totalChipsAmount = 500;
  104. potAmount = 0;
  105. chipsAmountText.text = "chips: " +totalChipsAmount;
  106. currentTurn = 0;
  107. }
  108.  
  109. //---------------------
  110. // Initializing smartfox
  111. //---------------------
  112. void Awake()
  113. {
  114.  
  115. if (SmartFoxConnection.IsInitialized)
  116. {
  117. sfs = SmartFoxConnection.Connection;
  118. }
  119. else
  120. {
  121. SceneManager.LoadScene("Login");
  122. return;
  123. }
  124.  
  125. sfs.AddEventListener(SFSEvent.USER_VARIABLES_UPDATE, onUserVarsUpdate);
  126. sfs.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
  127. sfs.AddEventListener(SFSEvent.USER_EXIT_ROOM, OnUserExitRoom);
  128.  
  129. //Lijst waar alle in de server opgehaalde variabelen komen
  130. userVars = new List<UserVariable>();
  131.  
  132. //bepaal wie host is
  133. users = sfs.UserManager.GetUserList();
  134. foreach (User people in users)
  135. {
  136. if (users[0].Name == sfs.MySelf.Name)
  137. {
  138. hosting = true;
  139. }
  140. }
  141. }
  142.  
  143.  
  144.  
  145. //----------------------
  146. // Reset functions
  147. //----------------------
  148.  
  149. public void reset()
  150. {
  151. sfs.RemoveAllEventListeners();
  152. sfs.Disconnect();
  153. // Return to lobby scene
  154. SceneManager.LoadScene("Login");
  155. }
  156.  
  157. public void newRound()
  158. {
  159. raiseAmount = 0;
  160. callAmount = 10;
  161. potAmount = 0;
  162. DestroyAllCardsMessage();
  163. }
  164.  
  165. //----------------------
  166. // Ingame buttons
  167. //----------------------
  168.  
  169. public int CountUsers()
  170. {
  171. return users.Count;
  172. }
  173.  
  174. public ArrayList GetPLayerNames()
  175. {
  176. ArrayList allNames = new ArrayList();
  177.  
  178. foreach (User people in users)
  179. {
  180. if (people.Name != "01234567890123456789012")
  181. {
  182. allNames.Add (people.Name);
  183. }
  184. }
  185. return allNames;
  186. }
  187.  
  188. public void onStateButtonClick()
  189. {
  190. if (StateButtonText.text == "Start Round")
  191. {
  192. //deActivateClientButtons ();
  193. getCards ();
  194. sfs.Send (new SetUserVariablesRequest (userVars));
  195. Debug.Log ("kaarten verstuurd naar spelers");
  196.  
  197. StateButtonText.text = "Round 1";
  198. roundStart = true;
  199. currentUser = users.Count - 1;
  200. } else if (StateButtonText.text == "Round 1" || StateButtonText.text == "Round 2" || StateButtonText.text == "Round 3" || StateButtonText.text == "Round 4")
  201. {
  202. // does nothing
  203. } else if (StateButtonText.text == "Turn")
  204. {
  205. cardDealer.SetCurrentGameState (GameState.dealCardState);
  206.  
  207. StateButtonText.text = "Round 2";
  208. roundStart = true;
  209. } else if (StateButtonText.text == "River")
  210. {
  211. cardDealer.SetCurrentGameState (GameState.dealCardState);
  212.  
  213. StateButtonText.text = "Round 3";
  214. roundStart = true;
  215. } else if (StateButtonText.text == "Flop")
  216. {
  217. cardDealer.SetCurrentGameState (GameState.dealCardState);
  218. StateButtonText.text = "Round 4";
  219. roundStart = true;
  220. } else if (StateButtonText.text == "Show Cards")
  221. {
  222. cardDealer.ShowPlayerCards ();
  223. StateButtonText.text = "New Round";
  224. } else if (StateButtonText.text == "New Round")
  225. {
  226. //activateClientButtons ();
  227. cardDealer.SetCurrentGameState (GameState.beforeGameState);
  228. newRound ();
  229. StateButtonText.text = "Start Round";
  230. }
  231. }
  232.  
  233. public void OnLeaveGameButtonClick()
  234. {
  235. // Remove SFS2X listeners
  236. reset();
  237. }
  238.  
  239. private void getCards()
  240. {
  241. Debug.Log("werkt: user count = " + users.Count);
  242. for (int i = 0; i < users.Count; i++)
  243. {
  244. string kaart1 = cardDealer.DrawCards();
  245. string kaart2 = cardDealer.DrawCards();
  246. if (i == 1)
  247. {
  248. p1k1 = kaart1;
  249. p1k2 = kaart2;
  250. userVars.Add(new SFSUserVariable(users[i].Name + "kaart1", p1k1));
  251. userVars.Add(new SFSUserVariable(users[i].Name + "kaart2", p1k2));
  252. }
  253. if (i == 2)
  254. {
  255. p2k1 = kaart1;
  256. p2k2 = kaart2;
  257. userVars.Add(new SFSUserVariable(users[i].Name + "kaart1", p2k1));
  258. userVars.Add(new SFSUserVariable(users[i].Name + "kaart2", p2k2));
  259. }
  260. if (i == 3)
  261. {
  262. p3k1 = kaart1;
  263. p3k2 = kaart2;
  264. userVars.Add(new SFSUserVariable(users[i].Name + "kaart1", p3k1));
  265. userVars.Add(new SFSUserVariable(users[i].Name + "kaart2", p3k2));
  266. }
  267. if (i == 4)
  268. {
  269. p4k1 = kaart1;
  270. p4k2 = kaart2;
  271. userVars.Add(new SFSUserVariable(users[i].Name + "kaart1", p4k1));
  272. userVars.Add(new SFSUserVariable(users[i].Name + "kaart2", p4k2));
  273. }
  274. }
  275. }
  276.  
  277. //--------------------
  278. // Update
  279. //--------------------
  280.  
  281. void Update()
  282. {
  283. print ("currentUser = " + currentUser);
  284. //update all the smartfox events every frame
  285. if (sfs != null)
  286. sfs.ProcessEvents();
  287.  
  288. // running the rounds, just for the host
  289. if (roundStart && sfs.MySelf.Name == "01234567890123456789012")
  290. {
  291. switch (currentUser)
  292. {
  293. case 1:
  294. if (!givenTurn)
  295. {
  296. for (int i = 0; i < users.Count; i++)
  297. {
  298. print ("user name is: " + users [i].Name + " and index is: " + i);
  299. }
  300. setMyTurn (users [1].Name);
  301. givenTurn = true;
  302. }
  303. if (userMadeAChoice)
  304. {
  305. switch (currentTurn)
  306. {
  307. case 0:
  308. StateButtonText.text = "Turn";
  309. currentTurn++;
  310. break;
  311. case 1:
  312. StateButtonText.text = "River";
  313. currentTurn++;
  314. break;
  315. case 2:
  316. StateButtonText.text = "Flop";
  317. currentTurn++;
  318. break;
  319. case 3:
  320. StateButtonText.text = "Show Cards";
  321. currentTurn = 0;
  322. break;
  323. }
  324. userMadeAChoice = false;
  325. //sendButton.interactable = true;
  326. givenTurn = false;
  327. currentUser = users.Count -1 ;
  328.  
  329. roundStart = false;
  330. }
  331. break;
  332. case 2:
  333. if (!givenTurn)
  334. {
  335. for (int i = 0; i < users.Count; i++)
  336. {
  337. print ("user name is: " + users [i].Name + " and index is: " + i);
  338. }
  339. setMyTurn (users [2].Name);
  340. givenTurn = true;
  341. }
  342. if (userMadeAChoice)
  343. {
  344. print("hij merkt dat de tweede user zijn turn heeft beeindigd");
  345. userMadeAChoice = false;
  346. givenTurn = false;
  347. currentUser = 1;
  348. }
  349. break;
  350. case 3:
  351. if (!givenTurn)
  352. {
  353. for (int i = 0; i < users.Count; i++)
  354. {
  355. print ("user name is: " + users [i].Name + " and index is: " + i);
  356. }
  357. givenTurn = true;
  358. setMyTurn (users [3].Name);
  359. }
  360. if (userMadeAChoice)
  361. {
  362. userMadeAChoice = false;
  363. givenTurn = false;
  364. currentUser = 2;
  365. }
  366. break;
  367.  
  368. case 4:
  369. if (!givenTurn)
  370. {
  371. for (int i = 0; i < users.Count; i++)
  372. {
  373. print ("user name is: " + users [i].Name + " and index is: " + i);
  374. }
  375. givenTurn = true;
  376. setMyTurn (users [4].Name);
  377. }
  378. if (userMadeAChoice)
  379. {
  380. userMadeAChoice = false;
  381. givenTurn = false;
  382. currentUser = 3;
  383. }
  384. break;
  385. }
  386. }
  387. }
  388.  
  389. //---------------------------------
  390. // Smartfox events
  391. // ----------------------------------
  392.  
  393. //Show the the debug text from smartfox events
  394. private void trace(string msg)
  395. {
  396. debugText.text = (msg + (debugText.text != "" ? "\n" : "") + debugText.text);
  397. }
  398.  
  399. //when connection is lost
  400. private void OnConnectionLost(BaseEvent evt)
  401. {
  402. reset();
  403. string reason = (string)evt.Params["reason"];
  404.  
  405. if (reason != ClientDisconnectionReason.MANUAL)
  406. {
  407. trace("Connection was lost. Reason: " + reason);
  408. }
  409. }
  410.  
  411. private void OnUserExitRoom(BaseEvent evt)
  412. {
  413. User user = (User)evt.Params["user"];
  414.  
  415. if (user != sfs.MySelf)
  416. {
  417. Room room = (Room)evt.Params["room"];
  418.  
  419. trace("User " + user.Name + " left the game");
  420. }
  421. }
  422.  
  423. void onUserVarsUpdate(BaseEvent evt)
  424. {
  425. Debug.Log("update start");
  426. ArrayList changedVars = (ArrayList)evt.Params["changedVars"];
  427. User user = (User)evt.Params["user"];
  428.  
  429. foreach (string chv in changedVars)
  430. {
  431. print("svs.Myself.Name = " + sfs.MySelf.Name);
  432. print("chv = " + chv);
  433. if (!hosting || sfs.MySelf.Name != "01234567890123456789012")
  434. {
  435. if (chv == sfs.MySelf.Name + "kaart1")
  436. {
  437. finalCard1 = user.GetVariable(chv).Value.ToString();
  438. print("finalCard1 = " + finalCard1);
  439. }
  440. else if (chv == sfs.MySelf.Name + "kaart2")
  441. {
  442. finalCard2 = user.GetVariable(chv).Value.ToString();
  443. print("finalCard2 = " + finalCard2);
  444. SendFinalCards();
  445. }
  446. if (chv == "deleteCards")
  447. {
  448. foreach (GameObject obsoleteCard in GameObject.FindGameObjectsWithTag("card"))
  449. {
  450. Destroy(obsoleteCard);
  451. }
  452. }
  453. if (chv == "activateButtons")
  454. {
  455. raise.interactable = true;
  456. allIn.interactable = true;
  457. check.interactable = true;
  458. fold.interactable = true;
  459. }
  460. if (chv == "deActivateButtons")
  461. {
  462. if (raise != null)
  463. {
  464. raise.interactable = false;
  465. allIn.interactable = false;
  466. check.interactable = false;
  467. fold.interactable = false;
  468. }
  469. }
  470. if (chv == sfs.MySelf.Name + "myTurn")
  471. {
  472. raise.interactable = true;
  473. allIn.interactable = true;
  474. check.interactable = true;
  475. fold.interactable = true;
  476. }
  477. }
  478. if (sfs.MySelf.Name == "01234567890123456789012")
  479. {
  480. if (chv == "raise")
  481. {
  482. string raiseAmountReceived = user.GetVariable("raiseAmount").Value.ToString();
  483. int finalRaiseAmount = int.Parse(raiseAmountReceived);
  484. trace(user.Name + " raised with: " + finalRaiseAmount);
  485. potAmount += finalRaiseAmount;
  486. callAmount = finalRaiseAmount;
  487. potAmountText.text = potAmount.ToString();
  488. userMadeAChoice = true;
  489.  
  490. }
  491. else if (chv == "fold")
  492. {
  493. trace(user.Name + " folded");
  494. userMadeAChoice = true;
  495. }
  496. else if (chv == "allIn")
  497. {
  498. string allInAmountReceived = user.GetVariable("allInAmount").Value.ToString();
  499. potAmount += int.Parse(allInAmountReceived);
  500. potAmountText.text = potAmount.ToString();
  501. trace(user.Name + " went all in " + totalChipsAmount);
  502. print ("someone went all in so userMadeAChoice will be set to true now");
  503. userMadeAChoice = true;
  504. }
  505. else if (chv == "check")
  506. {
  507. potAmount += callAmount;
  508. potAmountText.text = potAmount.ToString();
  509. trace(user.Name + "checked");
  510. userMadeAChoice = true;
  511. }
  512. }
  513.  
  514. }
  515. }
  516.  
  517. public void SendFinalCards()
  518. {
  519. List<string> cardsList = new List<string>();
  520. cardsList.Add(finalCard1);
  521. cardsList.Add(finalCard2);
  522. this.gameObject.GetComponent<ClientGame>().DrawCards(cardsList);
  523. }
  524.  
  525. public void raiseButton()
  526. {
  527.  
  528. if (inputFieldRaise.text == "")
  529. {
  530. trace("Please enter a value");
  531. }
  532.  
  533.  
  534. else if (int.Parse(inputFieldRaise.text) <= 0)
  535. {
  536. trace("Please enter a positive value");
  537.  
  538. }
  539.  
  540. else if ((int.Parse(inputFieldRaise.text + callAmount)) <= totalChipsAmount)
  541. {
  542. raiseAmount = int.Parse(inputFieldRaise.text) + callAmount;
  543. totalChipsAmount -= raiseAmount;
  544. userMessage.Add(new SFSUserVariable("raise", raiseMessage));
  545. userMessage.Add(new SFSUserVariable("raiseAmount", raiseAmount));
  546. sfs.Send(new SetUserVariablesRequest(userMessage));
  547. userMessage.Clear();
  548. chipsAmountText.text = totalChipsAmount.ToString();
  549. trace("You have " + totalChipsAmount + " chips left");
  550. chipsAmountText.text = "chips: " + totalChipsAmount;
  551. // disable buttons after choosing an action, as this indicates this players turn has ended
  552. raise.interactable = false;
  553. allIn.interactable = false;
  554. check.interactable = false;
  555. fold.interactable = false;
  556. }
  557. else
  558. {
  559. trace("Not enough chips available to raise that high");
  560. }
  561. }
  562. public void checkButton()
  563. {
  564. totalChipsAmount -= callAmount;
  565. userMessage.Add(new SFSUserVariable("check", checkMessage));
  566. sfs.Send(new SetUserVariablesRequest(userMessage));
  567. userMessage.Clear();
  568. chipsAmountText.text = "chips: " + totalChipsAmount;
  569. // disable buttons after choosing an action, as this indicates this players turn has ended
  570. raise.interactable = false;
  571. allIn.interactable = false;
  572. check.interactable = false;
  573. fold.interactable = false;
  574. }
  575. public void foldButton()
  576. {
  577. userMessage.Add(new SFSUserVariable("fold", foldMessage));
  578. sfs.Send(new SetUserVariablesRequest(userMessage));
  579. userMessage.Clear();
  580. folded = true;
  581. // disable buttons after choosing an action, as this indicates this players turn has ended
  582. raise.interactable = false;
  583. allIn.interactable = false;
  584. check.interactable = false;
  585. fold.interactable = false;
  586. }
  587. public void allInButton()
  588. {
  589. userMessage.Add(new SFSUserVariable("allInAmount", totalChipsAmount));
  590. totalChipsAmount = 0;
  591. userMessage.Add(new SFSUserVariable("allIn", allInMessage));
  592. sfs.Send(new SetUserVariablesRequest(userMessage));
  593. userMessage.Clear();
  594. trace("You went all in");
  595. print (sfs.MySelf.Name + " went all in");
  596. chipsAmountText.text = "chips: " + totalChipsAmount;
  597. // disable buttons after choosing an action, as this indicates this players turn has ended
  598. raise.interactable = false;
  599. allIn.interactable = false;
  600. check.interactable = false;
  601. fold.interactable = false;
  602. }
  603.  
  604. public void setMyTurn(string playerName)
  605. {
  606. turnMessage = "turned";
  607. userMessage.Add(new SFSUserVariable(playerName + "myTurn", turnMessage));
  608. sfs.Send (new SetUserVariablesRequest (userMessage));
  609. userMessage.Clear ();
  610. }
  611.  
  612. public void DestroyAllCardsMessage()
  613. {
  614. userMessage.Add(new SFSUserVariable("deleteCards", deleteCardsMessage));
  615. sfs.Send(new SetUserVariablesRequest(userMessage));
  616. userMessage.Clear();
  617. }
  618.  
  619. public ArrayList PutAllCardsInAList ()
  620. {
  621. ArrayList AllPlayerCards = new ArrayList();
  622. AllPlayerCards.Add(p1k1);
  623. AllPlayerCards.Add(p1k2);
  624. AllPlayerCards.Add(p2k1);
  625. AllPlayerCards.Add(p2k2);
  626. AllPlayerCards.Add(p3k1);
  627. AllPlayerCards.Add(p3k2);
  628. AllPlayerCards.Add(p4k1);
  629. AllPlayerCards.Add(p4k2);
  630. return AllPlayerCards;
  631. }
  632.  
  633. public void activateClientButtons()
  634. {
  635. userMessage.Add(new SFSUserVariable("activateButtons", activateButtons));
  636. sfs.Send(new SetUserVariablesRequest(userMessage));
  637. userMessage.Clear();
  638. }
  639.  
  640. public void deActivateClientButtons()
  641. {
  642. userMessage.Add(new SFSUserVariable("deActivateButtons", deActivateButtons));
  643. sfs.Send(new SetUserVariablesRequest(userMessage));
  644. userMessage.Clear();
  645. }
  646. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement