Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.87 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using MySql.Data.MySqlClient;
  4. using System;
  5. using UnityEngine.UI;
  6. using TMPro;
  7. using UnityEngine.Networking;
  8.  
  9. public class DownloadIdeas : MonoBehaviour
  10. {
  11.  
  12.     //DataBase
  13.     static String ConnectionString = "server=XXX; password=XXX; user=XXX; database=XXX;charset=utf8";
  14.     static MySqlCommand komenda;
  15.     static MySqlDataReader czytnik;
  16.     static string polecenie = "";
  17.     //DataBase
  18.  
  19.  
  20.  
  21.     //PrefabInCategory
  22.     public GameObject PrefabIdeaInCategories;
  23.     public Transform[] CategoryParrents;
  24.     //PrefabInCategory
  25.  
  26.     //PrefabInSearcher
  27.     public TMP_Text PlaceHolder;
  28.     public TMP_InputField InputSearch;
  29.     public GameObject PrefabInSearcher;
  30.     public Transform SearcherParrent;
  31.     //PrefabInSearcher
  32.  
  33.     public String[] TableName;
  34.     public Sprite[] CategorySprite;
  35.  
  36.     //generally
  37.     public GameObject IdeaContent;
  38.     public GameObject InternetAlert;
  39.     //generally
  40.  
  41.     public GameObject PanelAlert, ButtonAlertExit;
  42.     public Text TextAlert;
  43.  
  44.     void OnEnable()
  45.     {
  46.         StartCoroutine(CheckVersion());
  47.     }
  48.    
  49.     public void UpdateCanvas()
  50.     {
  51.  
  52.         for (int i = 0; i < CategoryParrents.Length; i++)
  53.         {
  54.             LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)CategoryParrents[i].transform);
  55.             Canvas.ForceUpdateCanvases();
  56.             CategoryParrents[i].GetComponent<VerticalLayoutGroup>().SetLayoutVertical();
  57.         }
  58.  
  59.         LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)SearcherParrent.transform);
  60.         Canvas.ForceUpdateCanvases();
  61.         SearcherParrent.GetComponent<VerticalLayoutGroup>().SetLayoutVertical();
  62.     }
  63.  
  64.  
  65.  
  66.     IEnumerator CheckVersion()
  67.     {
  68.         ShowAlert("Sprawdzam wersję aplikacji", false);
  69.  
  70.         WWW WWWCheckVerion = new WWW("http://skydomesoftware.cba.pl/CLP-Wersja.txt");
  71.         yield return WWWCheckVerion;
  72.         float CheckedVersion = float.Parse(WWWCheckVerion.text);
  73.         if (CheckedVersion > float.Parse(Application.version))
  74.         {
  75.             ShowAlert("Aplikacja jest nieaktualna. Pobierz najnowszą wersję ze sklepu Play", true);
  76.         }
  77.         else
  78.         {
  79.             ShowAlert("Aplikacja jest aktualna. Pobieram pomysły...", false);
  80.             MainFunction();
  81.         }
  82.        // CheckAllIdeas();
  83.     }
  84.  
  85.     public void CheckAllIdeas()
  86.     {
  87.        if(SearcherParrent.childCount > 0)
  88.         {
  89.             Debug.Log("Wszystko w porządku :)");
  90.         }
  91.         else
  92.         {
  93.             ShowAlert("Spróbuj połączyć się z innym źródłem internetu. Prawdopodobnie połączenie z bazą danych jest blokowane przez dostawcę internetu", true);
  94.         }
  95.     }
  96.  
  97.  
  98.     public void ShowAlert(string message, bool ButtonOn)
  99.     {
  100.         TextAlert.text = message;
  101.         PanelAlert.SetActive(true);
  102.         if (ButtonOn == true)
  103.             ButtonAlertExit.SetActive(true);
  104.         else
  105.             ButtonAlertExit.SetActive(false);
  106.        
  107.  
  108.  
  109.     }
  110.  
  111.  
  112.     public void Update()
  113.     {
  114.         if (Application.internetReachability == NetworkReachability.NotReachable)
  115.         {
  116.             InternetAlert.SetActive(true);
  117.         }
  118.         else
  119.         {
  120.             InternetAlert.SetActive(false);
  121.         }
  122.     }
  123.     public void searcher()
  124.     {
  125.         for (int i = 0; i < SearcherParrent.childCount; i++)
  126.         {
  127.             if (SearcherParrent.GetChild(i).gameObject.transform.GetChild(0).GetComponent<TMP_Text>().text.ToLower().Contains(InputSearch.text.ToLower()))
  128.             {
  129.                 SearcherParrent.GetChild(i).gameObject.SetActive(true);
  130.             }
  131.             else
  132.             {
  133.                 SearcherParrent.GetChild(i).gameObject.SetActive(false);
  134.             }
  135.         }
  136.     }
  137.  
  138.     public void MainFunction()
  139.     {
  140.         PanelAlert.SetActive(true);
  141.         TextAlert.text = "Pobieranie pomysłów...";
  142.         for (int i = 0; i < TableName.Length; i++)
  143.         {
  144.             Download(CategoryParrents[i], TableName[i], i, CategorySprite[i]);
  145.         }
  146.     }
  147.  
  148.     public void AddLike(string sign, int id, int TableNR)
  149.     {
  150.         try
  151.         {
  152.             using (MySqlConnection polaczenie = new MySqlConnection(ConnectionString))
  153.             {
  154.                 polaczenie.Open();
  155.                 polecenie = "update " + TableName[TableNR] + " set Likes =" + sign + " where ID = '" + id + "' ";
  156.                 komenda = new MySqlCommand(polecenie, polaczenie);
  157.                 komenda.ExecuteNonQuery();
  158.                 czytnik = komenda.ExecuteReader();
  159.                 czytnik.Close();
  160.                 polaczenie.Close();
  161.             }
  162.         }
  163.         catch (MySqlException ex)
  164.         {
  165.             ShowAlert("Add like " + ex.ToString(), true);
  166.         }
  167.  
  168.     }
  169.  
  170.     public void DownloadContent(GameObject Idea, int id, int TableNR)
  171.     {
  172.         try
  173.         {
  174.             using (MySqlConnection polaczenie = new MySqlConnection(ConnectionString))
  175.             {
  176.                 polaczenie.Open();
  177.                 polecenie = "select * from " + TableName[TableNR] + " where ID = '" + id + "' ";
  178.                 komenda = new MySqlCommand(polecenie, polaczenie);
  179.                 komenda.ExecuteNonQuery();
  180.                 czytnik = komenda.ExecuteReader();
  181.  
  182.                 while (czytnik.Read())
  183.                 {
  184.                     for (int i = 0; i <= 8; i++)
  185.                     {
  186.                         Idea.GetComponent<IdeaButtonName>().ContentPL[i] = czytnik["ContentPL" + (i+1)].ToString();
  187.                         Idea.GetComponent<IdeaButtonName>().ContentENG[i] = czytnik["ContentENG" + (i+1)].ToString();
  188.                     }
  189.                  
  190.                 }
  191.                 czytnik.Close();
  192.                 polaczenie.Close();
  193.             }
  194.         }
  195.         catch (MySqlException ex)
  196.         {
  197.             ShowAlert("DOWNLOAD CONTENT ERROR: " + ex.ToString(), true);
  198.         }
  199.  
  200.     }
  201.     public void AddComment(string Comment, int id, int TableNR)
  202.     {
  203.         try
  204.         {
  205.             using (MySqlConnection polaczenie = new MySqlConnection(ConnectionString))
  206.             {
  207.                 polaczenie.Open();
  208.                 polecenie = "update " + TableName[TableNR] + " set Comments = '" + Comment + "' where ID = '" + id + "' ";
  209.                 komenda = new MySqlCommand(polecenie, polaczenie);
  210.                 komenda.ExecuteNonQuery();
  211.                 czytnik = komenda.ExecuteReader();
  212.                 czytnik.Close();
  213.                 polaczenie.Close();
  214.             }
  215.         }
  216.         catch (MySqlException ex)
  217.         {
  218.             ShowAlert("Add Comment " + ex.ToString(), true);
  219.         }
  220.     }
  221.  
  222.     public void AddView(string sign, int id, int TableNR)
  223.     {
  224.  
  225.         try
  226.         {
  227.             using (MySqlConnection polaczenie = new MySqlConnection(ConnectionString))
  228.             {
  229.                 polaczenie.Open();
  230.                 polecenie = "update " + TableName[TableNR] + " set Views =" + sign + " where ID = '" + id + "' ";
  231.                 komenda = new MySqlCommand(polecenie, polaczenie);
  232.                 komenda.ExecuteNonQuery();
  233.                 czytnik = komenda.ExecuteReader();
  234.                 czytnik.Close();
  235.                 polaczenie.Close();
  236.             }
  237.         }
  238.         catch (MySqlException ex)
  239.         {
  240.             ShowAlert("Add view " + ex.ToString(), true);
  241.         }
  242.    
  243.     }
  244.  
  245.     public void Download(Transform CategoryParrent, string table, int CategoryNumber, Sprite CategorySprite)
  246.     {
  247.  
  248.         try
  249.         {
  250.             using (MySqlConnection polaczenie = new MySqlConnection(ConnectionString))
  251.             {
  252.                 polaczenie.Open();
  253.                 polecenie = "select * from " + table + " order by ID";
  254.                 komenda = new MySqlCommand(polecenie, polaczenie);
  255.                 komenda.ExecuteNonQuery();
  256.                 czytnik = komenda.ExecuteReader();
  257.                 int IdeaNumber = 0;
  258.  
  259.                 while (czytnik.Read())
  260.                 {
  261.                     IdeaNumber++;
  262.                     int id = int.Parse(czytnik["ID"].ToString());
  263.                     int categoryNumber = CategoryNumber;
  264.                     string title = czytnik["Title"].ToString();
  265.                     string[] ContentPL = new string[9];
  266.                     string[] ContentENG = new string[9];
  267.                     //for (int i = 0; i <= 8; i++)
  268.                     //{
  269.                     //    ContentPL[i] = czytnik["ContentPL" + (i + 1)].ToString();
  270.                     //    ContentENG[i] = czytnik["ContentENG" + (i + 1)].ToString();
  271.                     //}
  272.                     string pictureUrl = czytnik["Picture"].ToString();
  273.                     string link = czytnik["Link"].ToString();
  274.                     string likes = czytnik["Likes"].ToString();
  275.                     string comments = czytnik["Comments"].ToString();
  276.                     string views = czytnik["Views"].ToString();
  277.  
  278.  
  279.                     //PrefabIdeaInCategories
  280.                     PrefabIdeaInCategories.GetComponent<IdeaButtonName>().ID = id;
  281.                     PrefabIdeaInCategories.GetComponent<IdeaButtonName>().CategoryNumber = categoryNumber;
  282.                     PrefabIdeaInCategories.GetComponent<IdeaButtonName>().TextNumber.text = IdeaNumber.ToString() + ".";
  283.                     PrefabIdeaInCategories.GetComponent<IdeaButtonName>().Title = title;
  284.                     Array.Resize(ref PrefabIdeaInCategories.GetComponent<IdeaButtonName>().ContentPL, 9);
  285.                     Array.Resize(ref PrefabIdeaInCategories.GetComponent<IdeaButtonName>().ContentENG, 9);
  286.                
  287.                     PrefabIdeaInCategories.GetComponent<IdeaButtonName>().PictureUrl = pictureUrl;
  288.                     PrefabIdeaInCategories.GetComponent<IdeaButtonName>().Link = link;
  289.                     PrefabIdeaInCategories.GetComponent<IdeaButtonName>().Likes = likes;
  290.                     PrefabIdeaInCategories.GetComponent<IdeaButtonName>().Comments = comments;
  291.                     PrefabIdeaInCategories.GetComponent<IdeaButtonName>().views = views;
  292.                     PrefabIdeaInCategories.GetComponent<IdeaButtonName>().CategorySprite = CategorySprite;
  293.                     PrefabIdeaInCategories.GetComponent<IdeaButtonName>().IdeaContent = IdeaContent;
  294.                     PrefabIdeaInCategories.GetComponent<IdeaButtonName>().DownloadIdeasScript = this.gameObject;
  295.                     Instantiate(PrefabIdeaInCategories, CategoryParrent);
  296.                     //PrefabIdeaInCategories
  297.  
  298.  
  299.                     //PrefabInSearcher
  300.                     PrefabInSearcher.GetComponent<IdeaButtonName>().ID = id;
  301.                     PrefabInSearcher.GetComponent<IdeaButtonName>().CategoryNumber = CategoryNumber;
  302.                     PrefabInSearcher.GetComponent<IdeaButtonName>().Title = title;
  303.                     Array.Resize(ref PrefabInSearcher.GetComponent<IdeaButtonName>().ContentPL, 9);
  304.                     Array.Resize(ref PrefabInSearcher.GetComponent<IdeaButtonName>().ContentENG, 9);
  305.                     //for (int i = 0; i <= 8; i++)
  306.                     //{
  307.                     //    PrefabInSearcher.GetComponent<IdeaButtonName>().ContentPL[i] = ContentPL[i];
  308.                     //    PrefabInSearcher.GetComponent<IdeaButtonName>().ContentENG[i] = ContentENG[i];
  309.                     //}
  310.                     PrefabInSearcher.GetComponent<IdeaButtonName>().PictureUrl = pictureUrl;
  311.                     PrefabInSearcher.GetComponent<IdeaButtonName>().Link = link;
  312.                     PrefabInSearcher.GetComponent<IdeaButtonName>().Likes = likes;
  313.                     PrefabInSearcher.GetComponent<IdeaButtonName>().Comments = comments;
  314.                     PrefabInSearcher.GetComponent<IdeaButtonName>().views = views;
  315.                     PrefabInSearcher.GetComponent<IdeaButtonName>().CategorySprite = CategorySprite;
  316.                     PrefabInSearcher.GetComponent<IdeaButtonName>().IdeaContent = IdeaContent;
  317.                     PrefabInSearcher.GetComponent<IdeaButtonName>().DownloadIdeasScript = this.gameObject;
  318.                     Instantiate(PrefabInSearcher, SearcherParrent);
  319.                     // PrefabInSearcher
  320.                 }
  321.                 polaczenie.Close();
  322.             }
  323.             czytnik.Close();
  324.  
  325.             PlaceHolder.text = "Wyszukaj wśród (" + SearcherParrent.childCount + ") pomysłów";
  326.             PanelAlert.SetActive(false);
  327.             if (SearcherParrent.childCount < 10)
  328.             {
  329.                 ShowAlert("Błąd. Baza danych jest pusta. Spróbuj zaktualizować aplikację.\n" +
  330.                     "Twoja wersja aplikacji: " + Application.version + Environment.NewLine +
  331.                     "Twoje urządzenie: " + SystemInfo.deviceModel +
  332.                     "Ilość pomysłów: " + SearcherParrent.childCount, true);
  333.             }
  334.  
  335.         }
  336.         catch (MySqlException ex)
  337.         {
  338.             ShowAlert("First run " + ex.ToString(), true);
  339.         }
  340.     }
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement