CurrentPlayerPicker.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Networking;
  6. using System.Linq;
  7. public class CurrentPlayerPicker : MonoBehaviour
  8. {
  9. public GameObject serverManager;
  10. public int numberOfPlayers;
  11. public Text textfield;
  12. public GameObject sorryTaken;
  13. public GameObject selectionPanel;
  14. public GameObject errorPanel;
  15. public GameObject connectingPanel;
  16. public GameObject nextPanel;
  17. private string currentPlayer;
  18. private float elapsed = 0.0f;
  19. private float period = 1.0f;
  20. private bool selectionComplete;
  21. private string finalPlayerNumber;
  22. // CARD STUFF CARD STUFF CARD STUFF CARD STUFF
  23. public RawImage goalCard;
  24. public RawImage personCard;
  25. public RawImage goalCardView;
  26. public RawImage personaCardView;
  27. public List<Texture> goalCardImages;
  28. public List<Texture> personCardImages;
  29. //for action card selection during game running
  30. public Button actionButton;
  31. public RawImage actionCard;
  32. public GameObject actionCardView;
  33. public List<Texture> actionCardImages;
  34. //dictionary to map card images and unique IDs
  35. private Dictionary<string, int> personCardIDs;
  36. private Dictionary<string, int> goalCardIDs;
  37. private Dictionary<string, int> actionCardIDs;
  38. //mapping character cards and goal cards
  39. private Dictionary<int, List<int>> personToGoalCardMap;
  40. // Start is called before the first frame update
  41. void Start()
  42. {
  43. selectionComplete = false;
  44. finalPlayerNumber = "0";
  45. // Initialize dictionaries and assign unique IDs
  46. personCardIDs = new Dictionary<string, int>();
  47. goalCardIDs = new Dictionary<string, int>();
  48. actionCardIDs = new Dictionary<string, int>();
  49. // Assign IDs to character cards
  50. for (int i = 0; i < personCardImages.Count; i++)
  51. {
  52. personCardIDs[personCardImages[i].name] = i + 1; // ID starts from 1
  53. }
  54. // Assign IDs to goal cards
  55. for (int i = 0; i < goalCardImages.Count; i++)
  56. {
  57. goalCardIDs[goalCardImages[i].name] = i + 1; // ID starts from 1
  58. }
  59. // Assign IDs to action cards
  60. for (int i = 0; i < actionCardImages.Count; i++)
  61. {
  62. actionCardIDs[actionCardImages[i].name] = i + 1; // ID starts from 1
  63. }
  64. InitializePersonToGoalCardMap(); //initialize the character to goal cards
  65. //click actionButton to get the action card
  66. actionButton.onClick.AddListener(SelectActionCard);
  67. }
  68. // Update is called once per frame
  69. void Update()
  70. {
  71. if(selectionComplete)
  72. return;
  73. elapsed += Time.deltaTime;
  74. if (elapsed >= period) {
  75. elapsed = 0.0f;
  76. //Debug.Log("Inside");
  77. string currentGame = serverManager.GetComponent<CurrentGameConnect>().GetGameNumber();
  78. // GET ALL ALREADY SELECTED PLAYERS FROM SERVER
  79. // SET BUTTONS TO NOT ACTIVE OF THOSE PLAYERS
  80. string serverUrl_check = "https://smarthubs.media.tuwien.ac.at/api/is_player_available/"+ currentGame+ "/";
  81. for(int i = 1; i <= numberOfPlayers; i++)
  82. {
  83. //Debug.Log(i);
  84. StartCoroutine(AvaliablePlayers(serverUrl_check + i.ToString(), i.ToString()));
  85. }
  86. }
  87. }
  88. public void UpdatePlayerTexture(string clr)
  89. {
  90. this.gameObject.GetComponent<Image>().color = GameObject.Find("PlayerBtn" + clr).GetComponent<Button>().colors.normalColor;
  91. }
  92. public void SelectCards(string playerNumber, string gameNumber)
  93. {
  94. int randomPerson = Random.Range(0, personCardImages.Count);
  95. personCard.texture = personCardImages[randomPerson];
  96. personaCardView.texture = personCardImages[randomPerson];
  97. int personCardID = personCardIDs[personCard.texture.name];
  98. Debug.Log("person card ID: " + personCardID);
  99. //int range = 0;
  100. //int min = 0;
  101. /* if(finalPlayerNumber == "1")
  102. {
  103. range = 6;
  104. }
  105. if(finalPlayerNumber == "2")
  106. {
  107. min = 6;
  108. range = 12;
  109. }
  110. if(finalPlayerNumber == "3")
  111. {
  112. min = 12;
  113. range = 18;
  114. }
  115. if(finalPlayerNumber == "4")
  116. {
  117. min = 18;
  118. range = 24;
  119. }
  120. if(finalPlayerNumber == "5")
  121. {
  122. min = 24;
  123. range = 30;
  124. }
  125. if(finalPlayerNumber == "6")
  126. {
  127. min = 30;
  128. range = 36;
  129. }
  130. int randomGoal = Random.Range(min, range); */
  131. List<int> feasibleGoalCardIDs = personToGoalCardMap[personCardID]; //feasible goal card id for the selected character card
  132. // randomly select the goal cards from whole list
  133. int randomGoalIndex = Random.Range(0, feasibleGoalCardIDs.Count);
  134. int randomGoalID = feasibleGoalCardIDs[randomGoalIndex];
  135. Debug.Log("current feasible goal cards: " + randomGoalID);
  136. //Debug.Log("current feasible goal cards list: " + feasibleGoalCardIDs);
  137. Debug.Log("Current feasible goal cards list: " + string.Join(", ", feasibleGoalCardIDs));
  138. //get the texture of the seleccted goal cards
  139. string selectedGoalCardName = goalCardIDs.FirstOrDefault(x => x.Value == randomGoalID).Key;
  140. Texture selectedGoalCardTexture = goalCardImages.FirstOrDefault(x => x.name == selectedGoalCardName);
  141. // TELL THE SERVER THAT THE CARD IS TAKEN
  142. //goalCard.texture = goalCardImages[randomGoal];
  143. //goalCardView.texture = goalCardImages[randomGoal];
  144. goalCard.texture = selectedGoalCardTexture;
  145. goalCardView.texture = selectedGoalCardTexture;
  146. int goalCardID = goalCardIDs[goalCard.texture.name];
  147. Debug.Log("goal card ID: " + goalCardID);
  148. // Send the selected cards to the server
  149. StartCoroutine(UploadCharacterCard(gameNumber, personCardID));
  150. StartCoroutine(UploadGoalCard(gameNumber, goalCardID));
  151. nextPanel.SetActive(true);
  152. }
  153. // mapping character cards to goal cards
  154. private void InitializePersonToGoalCardMap()
  155. {
  156. personToGoalCardMap = new Dictionary<int, List<int>>
  157. {
  158. { 1, new List<int> { 1, 4, 6, 10, 12, 14, 15, 16, 17, 19, 20, 22, 23, 24, 26, 27, 29, 30, 31, 33, 34, 36, 37, 39, 40 } },
  159. { 2, new List<int> { 3, 6, 10, 22, 24, 29, 39, 40 } },
  160. { 3, new List<int> { 1, 4, 6, 10, 11, 12, 14, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 33, 36, 37, 39, 40 } },
  161. { 4, new List<int> { 1, 3, 4, 6, 10, 11, 12, 14, 15, 16, 17, 19, 21, 22, 24, 25, 26, 27, 28, 29, 30, 31, 33, 39, 40 } },
  162. { 5, new List<int> { 1, 3, 6, 10, 12, 14, 15, 16, 17, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 33, 36, 37, 39, 40 } },
  163. { 6, new List<int> { 1, 4, 6, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31, 33, 34, 36, 37, 39, 40 } },
  164. { 7, new List<int> { 3, 10, 11, 12, 14, 15, 16, 17, 18, 20, 21, 23, 28, 30, 31, 36, 37, 39, 40 } },
  165. { 8, new List<int> { 1, 3, 4, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 33, 34, 36, 37, 39, 40 } },
  166. { 9, new List<int> { 4, 6, 10, 11, 14, 15, 16, 17, 19, 20, 21, 23, 26, 27, 28, 29, 31, 33, 34, 36, 37, 39, 40 } },
  167. { 10, new List<int> { 4, 6, 10, 11, 15, 16, 20, 23, 24, 26, 27, 29, 30, 31, 33, 36, 37, 39, 40 } },
  168. { 11, new List<int> { 3, 10, 11, 16, 23, 24, 27, 29, 30, 31, 36, 37, 40 } }
  169. };
  170. Debug.Log("constrains between character and goal");
  171. }
  172. public IEnumerator AvaliablePlayers(string serverUrl, string player)
  173. {
  174. UnityWebRequest www = UnityWebRequest.Get(serverUrl);
  175. yield return www.SendWebRequest();
  176. if (www.result != UnityWebRequest.Result.Success)
  177. {
  178. //Debug.Log(www.error);
  179. }
  180. else
  181. {
  182. if(www.downloadHandler.text == "False")
  183. {
  184. GameObject[] playaz = GameObject.FindGameObjectsWithTag("pl");
  185. foreach(GameObject playa in playaz)
  186. {
  187. if(playa.name.Contains(player))
  188. {
  189. playa.SetActive(false);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. public void choosePlayer(string playerNumber)
  196. {
  197. StartCoroutine(ChoosePlayer1(playerNumber));
  198. }
  199. public IEnumerator ChoosePlayer1(string playerNumber)
  200. {
  201. string currentGame = serverManager.GetComponent<CurrentGameConnect>().GetGameNumber();
  202. string serverUrl_check = "https://smarthubs.media.tuwien.ac.at/api/is_player_available/"+ currentGame+ "/" + playerNumber;
  203. UnityWebRequest www = UnityWebRequest.Get(serverUrl_check);
  204. yield return www.SendWebRequest();
  205. if (www.result != UnityWebRequest.Result.Success)
  206. {
  207. Debug.Log(www.error);
  208. errorPanel.SetActive(true);
  209. Debug.Log("choose player1 EPanel.");
  210. }
  211. else
  212. {
  213. if(www.downloadHandler.text == "False")
  214. {
  215. Debug.Log(www.error);
  216. sorryTaken.SetActive(true);
  217. }
  218. else
  219. {
  220. StartCoroutine(ChoosePlayer2(playerNumber, currentGame));
  221. }
  222. }
  223. }
  224. public IEnumerator ChoosePlayer2(string playerNumber, string gameNumber)
  225. {
  226. string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/select_player";
  227. WWWForm form = new WWWForm();
  228. form.AddField("game", gameNumber);
  229. form.AddField("player", playerNumber);
  230. using (UnityWebRequest www = UnityWebRequest.Post(serverUrl, form))
  231. {
  232. yield return www.SendWebRequest();
  233. if (www.result != UnityWebRequest.Result.Success)
  234. {
  235. Debug.Log(www.error);
  236. // PROMPT ERROR
  237. errorPanel.SetActive(true);
  238. Debug.Log("choose player2 EPanel.");
  239. }
  240. else
  241. {
  242. Debug.Log("Form upload complete!");
  243. // CLOSE THE PANEL
  244. selectionComplete = true;
  245. this.finalPlayerNumber = playerNumber;
  246. this.textfield.text = "Player " + playerNumber;
  247. UpdatePlayerTexture(playerNumber);
  248. // SUCCESS YOU CHOSE YOUR PLAYER
  249. SelectCards(playerNumber, gameNumber);
  250. // DIACTIVATE THE PANEL
  251. this.selectionPanel.SetActive(false);
  252. }
  253. }
  254. }
  255. public string GetPlayerNumber()
  256. {
  257. return this.finalPlayerNumber;
  258. }
  259. //transfer the charatcer card and goal cards to server
  260. private IEnumerator UploadCharacterCard(string gameNumber, int personCardID) //upload the character cards
  261. {
  262. string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/add_char_card_to_player";
  263. WWWForm form = new WWWForm();
  264. Debug.Log("charCard gameNumber: " + gameNumber);
  265. Debug.Log("charCard playerNumber: " + finalPlayerNumber);
  266. Debug.Log("charCard ID: " + personCardID);
  267. form.AddField("game", gameNumber);
  268. form.AddField("player", finalPlayerNumber);
  269. form.AddField("charid", personCardID);
  270. // Convert the person card texture to a byte array
  271. //byte[] personCardBytes = GetTextureBytes(personCard.texture as Texture2D);
  272. //form.AddBinaryData("characterCard", personCardBytes, "characterCard.png", "image/png");
  273. using (UnityWebRequest www = UnityWebRequest.Post(serverUrl, form))
  274. {
  275. yield return www.SendWebRequest();
  276. if (www.result != UnityWebRequest.Result.Success)
  277. {
  278. Debug.Log(www.error);
  279. errorPanel.SetActive(true);
  280. Debug.Log("character card EPanel.");
  281. }
  282. else
  283. {
  284. Debug.Log("Character card uploaded.");
  285. }
  286. }
  287. }
  288. private IEnumerator UploadGoalCard(string gameNumber, int goalCardID) //upload the goal cards
  289. {
  290. string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/add_card_to_player";
  291. WWWForm form = new WWWForm();
  292. Debug.Log("goalCard gameNumber: " + gameNumber);
  293. Debug.Log("goalCard playerNumber: " + finalPlayerNumber);
  294. Debug.Log("goalCard ID: " + goalCardID);
  295. form.AddField("game", gameNumber);
  296. form.AddField("player", finalPlayerNumber);
  297. form.AddField("cid", goalCardID);
  298. // Convert the goal card texture to a byte array
  299. //byte[] goalCardBytes = GetTextureBytes(goalCard.texture as Texture2D);
  300. //form.AddBinaryData("goalCard", goalCardBytes, "goalCard.png", "image/png");
  301. using (UnityWebRequest www = UnityWebRequest.Post(serverUrl, form))
  302. {
  303. yield return www.SendWebRequest();
  304. if (www.result != UnityWebRequest.Result.Success)
  305. {
  306. Debug.Log(www.error);
  307. errorPanel.SetActive(true);
  308. Debug.Log("goal card EPanel.");
  309. }
  310. else
  311. {
  312. Debug.Log("Goal card uploaded.");
  313. }
  314. }
  315. }
  316. /*private byte[] GetTextureBytes(Texture2D texture)
  317. {
  318. return texture.EncodeToPNG();
  319. }*/
  320. // Action card selection
  321. public void SelectActionCard()
  322. {
  323. actionCardView.gameObject.SetActive(true);
  324. int randomAction = Random.Range(0, actionCardImages.Count);
  325. actionCard.texture = actionCardImages[randomAction];
  326. int actionCardID = actionCardIDs[actionCard.texture.name];
  327. Debug.Log("Selected action card texture: " + actionCard.texture.name);
  328. Debug.Log("Action card ID: " + actionCardID);
  329. // Send the selected action card to the server
  330. StartCoroutine(UploadActionCard(actionCardID));
  331. }
  332. // Transfer the action card to server (server should be able to adding multiple action card IDs)
  333. private IEnumerator UploadActionCard(int actionCardID)
  334. {
  335. string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/add_action_card_to_player";
  336. WWWForm form = new WWWForm();
  337. form.AddField("game", serverManager.GetComponent<CurrentGameConnect>().GetGameNumber());
  338. form.AddField("player", finalPlayerNumber);
  339. form.AddField("actionCardID", actionCardID.ToString());
  340. using (UnityWebRequest www = UnityWebRequest.Post(serverUrl, form))
  341. {
  342. yield return www.SendWebRequest();
  343. if (www.result != UnityWebRequest.Result.Success)
  344. {
  345. Debug.Log(www.error);
  346. errorPanel.SetActive(true);
  347. Debug.Log("action card EPanel.");
  348. }
  349. else
  350. {
  351. Debug.Log("Action card uploaded.");
  352. }
  353. }
  354. }
  355. }