123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Networking;
- using System.Linq;
- public class CurrentPlayerPicker : MonoBehaviour
- {
- public GameObject serverManager;
- public int numberOfPlayers;
- public Text textfield;
- public GameObject sorryTaken;
- public GameObject selectionPanel;
- public GameObject errorPanel;
- public GameObject connectingPanel;
- public GameObject nextPanel;
- private string currentPlayer;
- private float elapsed = 0.0f;
- private float period = 1.0f;
- private bool selectionComplete;
- private string finalPlayerNumber;
-
- public RawImage goalCard;
- public RawImage personCard;
- public RawImage goalCardView;
- public RawImage personaCardView;
- public List<Texture> goalCardImages;
- public List<Texture> personCardImages;
-
- public Button actionButton;
- public RawImage actionCard;
- public GameObject actionCardView;
-
- public List<Texture> actionCardImages;
-
- private Dictionary<string, int> personCardIDs;
- private Dictionary<string, int> goalCardIDs;
- private Dictionary<string, int> actionCardIDs;
-
- private Dictionary<int, List<int>> personToGoalCardMap;
-
- void Start()
- {
- selectionComplete = false;
- finalPlayerNumber = "0";
-
- personCardIDs = new Dictionary<string, int>();
- goalCardIDs = new Dictionary<string, int>();
- actionCardIDs = new Dictionary<string, int>();
-
- for (int i = 0; i < personCardImages.Count; i++)
- {
- personCardIDs[personCardImages[i].name] = i + 1;
- }
-
- for (int i = 0; i < goalCardImages.Count; i++)
- {
- goalCardIDs[goalCardImages[i].name] = i + 1;
- }
-
- for (int i = 0; i < actionCardImages.Count; i++)
- {
- actionCardIDs[actionCardImages[i].name] = i + 1;
- }
- InitializePersonToGoalCardMap();
-
- actionButton.onClick.AddListener(SelectActionCard);
- }
-
- void Update()
- {
- if(selectionComplete)
- return;
- elapsed += Time.deltaTime;
-
- if (elapsed >= period) {
- elapsed = 0.0f;
-
- string currentGame = serverManager.GetComponent<CurrentGameConnect>().GetGameNumber();
-
-
- string serverUrl_check = "https://smarthubs.media.tuwien.ac.at/api/is_player_available/"+ currentGame+ "/";
- for(int i = 1; i <= numberOfPlayers; i++)
- {
-
- StartCoroutine(AvaliablePlayers(serverUrl_check + i.ToString(), i.ToString()));
- }
- }
- }
- public void UpdatePlayerTexture(string clr)
- {
- this.gameObject.GetComponent<Image>().color = GameObject.Find("PlayerBtn" + clr).GetComponent<Button>().colors.normalColor;
- }
- public void SelectCards(string playerNumber, string gameNumber)
- {
- int randomPerson = Random.Range(0, personCardImages.Count);
- personCard.texture = personCardImages[randomPerson];
- personaCardView.texture = personCardImages[randomPerson];
- int personCardID = personCardIDs[personCard.texture.name];
- Debug.Log("person card ID: " + personCardID);
-
-
-
-
-
-
- List<int> feasibleGoalCardIDs = personToGoalCardMap[personCardID];
-
- int randomGoalIndex = Random.Range(0, feasibleGoalCardIDs.Count);
- int randomGoalID = feasibleGoalCardIDs[randomGoalIndex];
- Debug.Log("current feasible goal cards: " + randomGoalID);
-
- Debug.Log("Current feasible goal cards list: " + string.Join(", ", feasibleGoalCardIDs));
-
- string selectedGoalCardName = goalCardIDs.FirstOrDefault(x => x.Value == randomGoalID).Key;
- Texture selectedGoalCardTexture = goalCardImages.FirstOrDefault(x => x.name == selectedGoalCardName);
-
-
-
-
- goalCard.texture = selectedGoalCardTexture;
- goalCardView.texture = selectedGoalCardTexture;
- int goalCardID = goalCardIDs[goalCard.texture.name];
- Debug.Log("goal card ID: " + goalCardID);
-
- StartCoroutine(UploadCharacterCard(gameNumber, personCardID));
- StartCoroutine(UploadGoalCard(gameNumber, goalCardID));
- nextPanel.SetActive(true);
- }
-
-
- private void InitializePersonToGoalCardMap()
- {
- personToGoalCardMap = new Dictionary<int, List<int>>
- {
- { 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 } },
- { 2, new List<int> { 3, 6, 10, 22, 24, 29, 39, 40 } },
- { 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 } },
- { 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 } },
- { 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 } },
- { 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 } },
- { 7, new List<int> { 3, 10, 11, 12, 14, 15, 16, 17, 18, 20, 21, 23, 28, 30, 31, 36, 37, 39, 40 } },
- { 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 } },
- { 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 } },
- { 10, new List<int> { 4, 6, 10, 11, 15, 16, 20, 23, 24, 26, 27, 29, 30, 31, 33, 36, 37, 39, 40 } },
- { 11, new List<int> { 3, 10, 11, 16, 23, 24, 27, 29, 30, 31, 36, 37, 40 } }
- };
- Debug.Log("constrains between character and goal");
- }
-
- public IEnumerator AvaliablePlayers(string serverUrl, string player)
- {
- UnityWebRequest www = UnityWebRequest.Get(serverUrl);
- yield return www.SendWebRequest();
- if (www.result != UnityWebRequest.Result.Success)
- {
-
- }
- else
- {
- if(www.downloadHandler.text == "False")
- {
- GameObject[] playaz = GameObject.FindGameObjectsWithTag("pl");
- foreach(GameObject playa in playaz)
- {
- if(playa.name.Contains(player))
- {
- playa.SetActive(false);
- }
- }
- }
- }
- }
- public void choosePlayer(string playerNumber)
- {
- StartCoroutine(ChoosePlayer1(playerNumber));
- }
- public IEnumerator ChoosePlayer1(string playerNumber)
- {
- string currentGame = serverManager.GetComponent<CurrentGameConnect>().GetGameNumber();
- string serverUrl_check = "https://smarthubs.media.tuwien.ac.at/api/is_player_available/"+ currentGame+ "/" + playerNumber;
- UnityWebRequest www = UnityWebRequest.Get(serverUrl_check);
- yield return www.SendWebRequest();
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.Log(www.error);
- errorPanel.SetActive(true);
- Debug.Log("choose player1 EPanel.");
- }
- else
- {
- if(www.downloadHandler.text == "False")
- {
- Debug.Log(www.error);
- sorryTaken.SetActive(true);
- }
- else
- {
- StartCoroutine(ChoosePlayer2(playerNumber, currentGame));
- }
- }
- }
- public IEnumerator ChoosePlayer2(string playerNumber, string gameNumber)
- {
- string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/select_player";
- WWWForm form = new WWWForm();
-
- form.AddField("game", gameNumber);
- form.AddField("player", playerNumber);
- using (UnityWebRequest www = UnityWebRequest.Post(serverUrl, form))
- {
- yield return www.SendWebRequest();
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.Log(www.error);
-
- errorPanel.SetActive(true);
- Debug.Log("choose player2 EPanel.");
- }
- else
- {
- Debug.Log("Form upload complete!");
-
-
- selectionComplete = true;
- this.finalPlayerNumber = playerNumber;
- this.textfield.text = "Player " + playerNumber;
- UpdatePlayerTexture(playerNumber);
-
- SelectCards(playerNumber, gameNumber);
-
- this.selectionPanel.SetActive(false);
- }
- }
-
- }
- public string GetPlayerNumber()
- {
- return this.finalPlayerNumber;
- }
-
- private IEnumerator UploadCharacterCard(string gameNumber, int personCardID)
- {
- string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/add_char_card_to_player";
- WWWForm form = new WWWForm();
- Debug.Log("charCard gameNumber: " + gameNumber);
- Debug.Log("charCard playerNumber: " + finalPlayerNumber);
- Debug.Log("charCard ID: " + personCardID);
- form.AddField("game", gameNumber);
- form.AddField("player", finalPlayerNumber);
- form.AddField("charid", personCardID);
-
-
-
- using (UnityWebRequest www = UnityWebRequest.Post(serverUrl, form))
- {
- yield return www.SendWebRequest();
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.Log(www.error);
- errorPanel.SetActive(true);
- Debug.Log("character card EPanel.");
- }
- else
- {
- Debug.Log("Character card uploaded.");
- }
- }
- }
- private IEnumerator UploadGoalCard(string gameNumber, int goalCardID)
- {
- string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/add_card_to_player";
- WWWForm form = new WWWForm();
- Debug.Log("goalCard gameNumber: " + gameNumber);
- Debug.Log("goalCard playerNumber: " + finalPlayerNumber);
- Debug.Log("goalCard ID: " + goalCardID);
- form.AddField("game", gameNumber);
- form.AddField("player", finalPlayerNumber);
- form.AddField("cid", goalCardID);
-
-
-
- using (UnityWebRequest www = UnityWebRequest.Post(serverUrl, form))
- {
- yield return www.SendWebRequest();
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.Log(www.error);
- errorPanel.SetActive(true);
- Debug.Log("goal card EPanel.");
- }
- else
- {
- Debug.Log("Goal card uploaded.");
- }
- }
- }
-
-
- public void SelectActionCard()
- {
- actionCardView.gameObject.SetActive(true);
- int randomAction = Random.Range(0, actionCardImages.Count);
- actionCard.texture = actionCardImages[randomAction];
- int actionCardID = actionCardIDs[actionCard.texture.name];
- Debug.Log("Selected action card texture: " + actionCard.texture.name);
- Debug.Log("Action card ID: " + actionCardID);
-
- StartCoroutine(UploadActionCard(actionCardID));
-
- }
-
- private IEnumerator UploadActionCard(int actionCardID)
- {
- string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/add_action_card_to_player";
- WWWForm form = new WWWForm();
- form.AddField("game", serverManager.GetComponent<CurrentGameConnect>().GetGameNumber());
- form.AddField("player", finalPlayerNumber);
- form.AddField("actionCardID", actionCardID.ToString());
- using (UnityWebRequest www = UnityWebRequest.Post(serverUrl, form))
- {
- yield return www.SendWebRequest();
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.Log(www.error);
- errorPanel.SetActive(true);
- Debug.Log("action card EPanel.");
- }
- else
- {
- Debug.Log("Action card uploaded.");
- }
- }
- }
-
- }
-
|