Advertisement
Guest User

TexasBot

a guest
Feb 8th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.29 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using UnityEngine.SceneManagement;
  5. using System.Collections.Generic;
  6. using SimpleJSON;
  7.  
  8. public class TexasBot : MonoBehaviour {
  9.  
  10.     // Initiate Singleton
  11.     public static TexasBot Instance { get; private set; }
  12.  
  13.     public bool activated = false;
  14.     public bool expired = false;
  15.     public string username;
  16.     public string password = "bot";
  17.     public int aggresive;
  18.     public int passive;
  19.     public int normal;
  20.     public string room;
  21.  
  22.     const int waitTime_short = 2;
  23.     const int waitTime_medium = 5;
  24.     const int waitTime_long = 10;
  25.  
  26.     BotLogHandler botLogHandler;
  27.     private static ILogger logger = Debug.logger;
  28.  
  29.     enum Phase
  30.     {
  31.         Login, Menu, Lobby, Room, Playing
  32.     }
  33.  
  34.     Phase currentPhase = Phase.Login;
  35.  
  36.     void Awake()
  37.     {
  38.         // Singleton - Jika Instance tidak kosong (sudah diisi) dan sudah diinstansiasi tetapi
  39.         // bukan class ini (TexasClient), maka Hapus Class/GameObject ini
  40.         if (Instance != null && Instance != this)
  41.         {
  42.             Destroy(gameObject);
  43.         }
  44.         // Singleton - Aktifkan Instance (Class static TexasClient ke class ini)
  45.         Instance = this;
  46.         // Singleton - Opsional, kamu dapat mengatur Singleton ini agar tidak dihapus ketika
  47.         // berpindah Scene
  48.         DontDestroyOnLoad(gameObject);
  49.     }
  50.    
  51.     private void Start()
  52.     {
  53.         string[] args = System.Environment.GetCommandLineArgs();
  54.  
  55.         Debug.Log("Checking arg bot");
  56.        
  57.  
  58.         for (int i = 0; i < args.Length; i++)
  59.         {
  60.            
  61.             var arg = args[i];
  62.             Debug.Log("ARG : "+arg);
  63.  
  64.             if (arg == "-bot")
  65.             {
  66.                 activated = true;
  67.                 Debug.Log("BOT : activated");
  68.             }
  69.             else if (arg == "-u")
  70.             {
  71.                 username = args[i + 1];
  72.             }
  73.             else if (arg == "-brain")
  74.             {
  75.                 var brain = args[i + 1].Split('|');
  76.                 aggresive = int.Parse(brain[0]);
  77.                 passive = int.Parse(brain[1]);
  78.                 normal = int.Parse(brain[2]);
  79.             }
  80.             else if (arg == "-room")
  81.             {
  82.                 room = args[i + 1];
  83.             }
  84.         }
  85.         if (activated)
  86.         {
  87.             botLogHandler = new BotLogHandler();
  88.             logger.logHandler = botLogHandler;
  89.             logger.Log("TexasBot", "Texas Bot Started");
  90.             thinkAction();
  91.         }
  92.     }
  93.  
  94.     void thinkAction()
  95.     {
  96.         if (activated)
  97.         {
  98.             logger.Log("BOT : Thinking action");
  99.             switch (currentPhase)
  100.             {
  101.                 case Phase.Login:
  102.                     {
  103.                         StartCoroutine(DoLogin());
  104.                         break;
  105.                     }
  106.                 case Phase.Menu:
  107.                     {
  108.                         StartCoroutine(DoMenu());
  109.                         break;
  110.                     }
  111.                 case Phase.Lobby:
  112.                     {
  113.                         if (!expired)
  114.                         {
  115.                             StartCoroutine(DoFindRoom());
  116.                         }else
  117.                         {
  118.                             StartCoroutine(LogOut());
  119.                         }
  120.                         break;
  121.                     }
  122.                 case Phase.Room:
  123.                     {
  124.                         StartCoroutine(DoStartPlaying());
  125.                         break;
  126.                     }
  127.                 case Phase.Playing:
  128.                     {
  129.                         ManagePlay();
  130.                         break;
  131.                     }
  132.             }
  133.            
  134.         }
  135.     }
  136.  
  137.     IEnumerator DoLogin()
  138.     {
  139.         yield return new WaitUntil(() => (PhotonNetwork.connected));
  140.         yield return new WaitForSeconds(waitTime_short);
  141.         logger.Log("BOT : Doing login");
  142.         var obj_username = GameObject.Find("Username").transform.GetChild(0).gameObject;
  143.         var obj_password = GameObject.Find("Password").transform.GetChild(0).gameObject;
  144.         obj_username.GetComponent<InputField>().text = username;
  145.         obj_password.GetComponent<InputField>().text = password;
  146.         back:
  147.         GameObject.FindObjectOfType<LoginController>().LoginGuest();
  148.         yield return new WaitUntil(() => TexasClient.Instance.WWWTextGuestSignInResponse != "");
  149.         yield return new WaitForSeconds(waitTime_medium);
  150.         if (SceneManager.GetActiveScene().buildIndex == App.SCENE_MENU)
  151.         {
  152.             logger.Log("BOT : Login Succeded");
  153.             currentPhase = Phase.Menu;
  154.             thinkAction();
  155.         }else
  156.         {
  157.             logger.Log("BOT : login failed, trying again");
  158.             goto back;
  159.         }
  160.     }
  161.  
  162.     IEnumerator DoMenu()
  163.     {
  164.         logger.Log("BOT : Doing menu");
  165.         back:
  166.         GameObject.FindObjectOfType<MenuController>().TapTable();
  167.         yield return new WaitForSeconds(waitTime_medium);
  168.         if (SceneManager.GetActiveScene().buildIndex == App.SCENE_ROOM)
  169.         {
  170.             logger.Log("BOT : Goint to lobby");
  171.             currentPhase = Phase.Lobby;
  172.             thinkAction();
  173.         }
  174.         else
  175.         {
  176.             logger.Log("BOT : go to lobby failed, retrying");
  177.             goto back;
  178.         }
  179.     }
  180.  
  181.     IEnumerator DoFindRoom()
  182.     {
  183.         logger.Log("BOT : Doing find room");
  184.         back:
  185.         yield return new WaitForSeconds(waitTime_medium);
  186.         if(RoomController.self.contentParent.childCount == 0)
  187.         {
  188.             logger.Log("BOT : no room available, finding again");
  189.             RoomController.self.Refresh();
  190.             goto back;
  191.         }
  192.         logger.Log("BOT : selecting room.."+ RoomController.self.contentParent.childCount);
  193.         RoomListController selectedRoom = new RoomListController();
  194.         selectedRoom.roomId = "";
  195.         foreach (Transform g in RoomController.self.contentParent)
  196.         {
  197.             if (g.GetComponent<RoomListController>())
  198.             {
  199.                 var rlc = g.GetComponent<RoomListController>();
  200.                 logger.Log("BOT : room " + rlc.roomId+" vs "+room);
  201.                 if (rlc.roomId == room)
  202.                 {
  203.                     logger.Log("BOT : room found, joining room");
  204.                     selectedRoom = rlc;
  205.                     break;
  206.                 }
  207.             }
  208.         }
  209.         if(selectedRoom.roomId == "")
  210.         {
  211.             logger.Log("BOT : room not found");
  212.             RoomController.self.Refresh();
  213.             goto back;
  214.         }
  215.         selectedRoom.JoinRoom();
  216.         while (SceneManager.GetActiveScene().buildIndex != App.SCENE_GAME)
  217.         {
  218.             yield return new WaitForSeconds(1f);
  219.         }
  220.         if (SceneManager.GetActiveScene().buildIndex == App.SCENE_GAME)
  221.         {
  222.             logger.Log("BOT : going to room");
  223.             currentPhase = Phase.Room;
  224.             thinkAction();
  225.         }
  226.         else
  227.         {
  228.             logger.Log("BOT : go to room failed, retrying");
  229.             goto back;
  230.         }
  231.     }
  232.  
  233.     IEnumerator DoStartPlaying()
  234.     {
  235.         logger.Log("BOT : Doing play");
  236.         yield return new WaitForSeconds(waitTime_medium);
  237.         List<int> empty = new List<int>();
  238.         back:
  239.         for (int i = 0; i < GameController.self.players.Length; i++)
  240.         {
  241.             var p = GameController.self.players[i];
  242.             if (!p.IsActivate())
  243.             {
  244.                 empty.Add(i);
  245.             }
  246.         }
  247.         Random.seed = System.Environment.TickCount;
  248.         var rnd = Random.Range(0, empty.Count);
  249.         if (!GameController.self.players[rnd].IsActivate())
  250.         {
  251.             logger.Log("BOT : Found empty sit");
  252.             GameController.self.players[rnd].UserTap();
  253.             GameController.self.players[rnd].Aggresive = aggresive;
  254.             GameController.self.players[rnd].Passive = passive;
  255.             GameController.self.players[rnd].Normal = normal;
  256.         }
  257.         else
  258.         {
  259.             logger.Log("BOT : empty sit not found, keep searching");
  260.             empty.Clear();
  261.             yield return new WaitForSeconds(waitTime_medium);
  262.             goto back;
  263.         }
  264.  
  265.        
  266.         var chipset = GameObject.FindObjectOfType<InitialChipSetting>();
  267.         yield return new WaitUntil(() => (chipset.gameObject.activeSelf));
  268.         yield return new WaitForSeconds(waitTime_short);
  269.         float rnd2 = Random.Range(chipset.slider.minValue, (chipset.slider.maxValue + 1f));
  270.         chipset.slider.value = rnd2;
  271.         chipset.Submit();
  272.  
  273.         yield return new WaitForSeconds(waitTime_short);
  274.         if (!GameController.self.players[GameController.self.MIDDLE_INDEX].IsActivate())
  275.         {
  276.             logger.Log("BOT : still not playing, searching");
  277.             empty.Clear();
  278.             yield return new WaitForSeconds(waitTime_medium);
  279.             goto back;
  280.         }else
  281.         {
  282.             logger.Log("BOT : Begin playing");
  283.             currentPhase = Phase.Playing;
  284.             thinkAction();
  285.         }
  286.     }
  287.  
  288.     void ManagePlay()
  289.     {
  290.         logger.Log("BOT : Manage play");
  291.         StartCoroutine(WaitForTurn());
  292.         StartCoroutine(WaitForLose());
  293.     }
  294.  
  295.     IEnumerator WaitForLose()
  296.     {
  297.         back:
  298.         logger.Log("BOT : Waiting for lose");
  299.         yield return new WaitUntil(() => (GameController.self.players[GameController.self.MIDDLE_INDEX].coinValue <= 0));
  300.         var chipset = GameObject.FindObjectOfType<InitialChipSetting>();
  301.         if (chipset.gameObject.activeSelf)
  302.         {
  303.             float rnd2 = Random.Range(chipset.slider.minValue, (chipset.slider.maxValue + 1f));
  304.             chipset.slider.value = rnd2;
  305.             chipset.Submit();
  306.         }
  307.         goto back;
  308.     }
  309.  
  310.     IEnumerator WaitForTurn()
  311.     {
  312.         back:
  313.         logger.Log("BOT : Waiting for turn");
  314.         yield return new WaitUntil(() => (GameObject.FindObjectOfType<NormalAction>() != null));
  315.         NormalAction act = GameObject.FindObjectOfType<NormalAction>();
  316.  
  317.         logger.Log("BOT : its turn time");
  318.         Random.seed = System.Environment.TickCount;
  319.         var pl = GameController.self.players[GameController.self.MIDDLE_INDEX];
  320.         var rnd = Random.Range(1, pl.MAX_PLAY_TIME);
  321.  
  322.         logger.Log("BOT : thinking, wait current time until >= "+rnd);
  323.         while(GameController.self.players[GameController.self.MIDDLE_INDEX].currentTime < System.Math.Floor(rnd))
  324.         {
  325.             logger.Log("BOT : current time => "+ GameController.self.players[GameController.self.MIDDLE_INDEX].currentTime);
  326.             yield return null;
  327.         }
  328.  
  329.         logger.Log("BOT : calculating");
  330.         ListCard.self.CalculateCards(pl.card0, pl.card1);
  331.         float acceptance = 0;
  332.         int cardres = ListCard.self.cardResult;
  333.  
  334.         int random = 0;
  335.         switch (ListCard.self.cardResult)
  336.         {
  337.             case ListCard.RESULT_ROYALFLUSH:
  338.                 acceptance = 0.95f;
  339.                 break;
  340.             case ListCard.RESULT_STRAIGHTFLUSH:
  341.                 acceptance = 0.9f;
  342.                 break;
  343.             case ListCard.RESULT_4OFAKIND:
  344.                 acceptance = 0.85f;
  345.                 break;
  346.             case ListCard.RESULT_FULLHOUSE:
  347.                 acceptance = 0.8f;
  348.                 break;
  349.             case ListCard.RESULT_FLUSH:
  350.                 acceptance = 0.75f;
  351.                 break;
  352.             case ListCard.RESULT_STRAIGHT:
  353.                 acceptance = 0.7f;
  354.                 break;
  355.             case ListCard.RESULT_3OFAKIND:
  356.                 acceptance = 0.65f;
  357.                 break;
  358.             case ListCard.RESULT_DUALPAIRS:
  359.                 acceptance = 0.6f;
  360.                 break;
  361.             case ListCard.RESULT_PAIRS:
  362.                 acceptance = 0.55f;
  363.                 break;
  364.             case ListCard.RESULT_HIGHCARD:
  365.                 acceptance = 0.45f;
  366.                 break;
  367.         }
  368.  
  369.         var follow = false;
  370.         var ActionFollow = PlayerItemNew.ACTION_NULL;
  371.         var smallest = pl.coinValue;
  372.         foreach (PlayerItemNew p in GameController.self.players)
  373.         {
  374.             if (p.IsActivate())
  375.             {
  376.                 if (p.coinValue < smallest)
  377.                 {
  378.                     smallest = p.coinValue;
  379.                 }
  380.  
  381.                 if (p.Passive > 50)
  382.                 {
  383.                     if (!p.isAI && p.action != PlayerItemNew.ACTION_FOLD)
  384.                     {
  385.                         ListCard.self.CalculateCards(p.card0, p.card1);
  386.                         //kalo lebih bagus follow
  387.                         if (cardres < ListCard.self.cardResult)
  388.                         {
  389.                             follow = true;
  390.                             ActionFollow = p.action;
  391.                         }
  392.                         else
  393.                         {
  394.                             follow = false;
  395.                             break;
  396.                         }
  397.                     }
  398.                 }
  399.             }
  400.         }
  401.  
  402.         Random.seed = System.Environment.TickCount;
  403.         random = Random.Range(1, 101);
  404.  
  405.         if (random < acceptance * 100)
  406.         {
  407.             if (pl.coinValue > 0 && GameController.self.currentBet < pl.coinValue)
  408.             {
  409.                 if ((follow & (ActionFollow == PlayerItemNew.ACTION_RAISE || ActionFollow == PlayerItemNew.ACTION_ALL_IN)) || pl.Aggresive > 50)
  410.                 {
  411.                     long raiseVal = (long)((pl.Aggresive / 100) * smallest * (acceptance));
  412.                     if (raiseVal == 0)
  413.                     {
  414.                         raiseVal = pl.coinValue;
  415.                     }
  416.                     if (pl.raiseCounter > 2)
  417.                     {
  418.                         act.DoCheck();
  419.                     }
  420.                     else
  421.                     {
  422.                         if (raiseVal == 0)
  423.                         {
  424.                             act.DoCheck();
  425.                         }
  426.                         else
  427.                         {
  428.                             act.DoCall();
  429.                             yield return new WaitUntil(() => (RaiseSlider.self.IsActive()));
  430.                             RaiseSlider.self.slider.value = raiseVal;
  431.                             act.DoCall();
  432.                         }
  433.                     }
  434.                 }
  435.                 else
  436.                 {
  437.                     act.DoCheck();
  438.                 }
  439.             }
  440.             else
  441.             {
  442.                 act.DoCheck();
  443.             }
  444.         }
  445.         else
  446.         {
  447.             if ((follow & ActionFollow == PlayerItemNew.ACTION_CHECK) || GameController.self.currentBet == 0)
  448.             {
  449.                 act.DoCheck();
  450.             }
  451.             else
  452.             {
  453.                 act.DoFold();
  454.             }
  455.         }
  456.         logger.Log("BOT : action decided, waiting for turn again");
  457.         goto back;
  458.     }
  459.  
  460.     public void CheckBotExistInRoom(string id)
  461.     {
  462.         StartCoroutine(GetBotData(id));
  463.     }
  464.  
  465.     IEnumerator GetBotData(string uid)
  466.     {
  467.         Debug.Log("BOT : Refreshing bot data with user_id "+ uid);
  468.         WWWForm form = new WWWForm();
  469.         form.AddField("user_id", uid);
  470.         WWW www = new WWW(App.BASIC_URL_USER + "Get_all_bot_on_room", form);
  471.         yield return www;
  472.  
  473.         if (www.error == null)
  474.         {
  475.             Debug.Log("BOT : Data => " + www.text);
  476.             expired = true;
  477.             var obj = JSON.Parse(www.text);
  478.             if (obj["return"] != null)
  479.             {
  480.                 obj = JSON.Parse("{}");
  481.             }
  482.             if (www.text.StartsWith("["))
  483.             {
  484.                 obj = obj[0];
  485.             }
  486.             if (obj["ROOM_ID"] != null)
  487.             {
  488.                 var id = obj["ROOM_ID"].ToString().Replace("\"", "");
  489.                 if (room == id)
  490.                 {
  491.                     Debug.Log("BOT : bot still exist");
  492.                     expired = false;
  493.                     if (obj["AGGRESIVE"]!=null)
  494.                     {
  495.                         aggresive = int.Parse(obj["AGGRESIVE"].ToString().Replace("\"", ""));
  496.                     }
  497.                     if (obj["PASSIVE"] != null)
  498.                     {
  499.                         passive = int.Parse(obj["PASSIVE"].ToString().Replace("\"", ""));
  500.                     }
  501.                     if (obj["NORMAL"] != null)
  502.                     {
  503.                         normal = int.Parse(obj["NORMAL"].ToString().Replace("\"", ""));
  504.                     }
  505.                     GameController.self.players[GameController.self.MIDDLE_INDEX].UpdateBot();
  506.                 }
  507.             }
  508.         }
  509.  
  510.         if (expired)
  511.         {
  512.             Debug.Log("BOT : bot disappeared, going to sign out");
  513.             currentPhase = Phase.Lobby;
  514.             GameController.self.ExitRoom();
  515.             thinkAction();
  516.         }
  517.     }
  518.  
  519.     IEnumerator LogOut()
  520.     {
  521.         yield return new WaitUntil(() => (SceneManager.GetActiveScene().buildIndex == App.SCENE_ROOM));
  522.         Loading.self.Open();
  523.         WWWForm form = new WWWForm();
  524.         form.AddField("user_id", App.userId);
  525.         WWW www = new WWW(App.BASIC_URL_USER + "logout", form);
  526.         yield return www;
  527.         Loading.self.Close();
  528.         Application.Quit();
  529.     }
  530. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement