Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- using System.Collections.Generic;
- using SimpleJSON;
- public class TexasBot : MonoBehaviour {
- // Initiate Singleton
- public static TexasBot Instance { get; private set; }
- public bool activated = false;
- public bool expired = false;
- public string username;
- public string password = "bot";
- public int aggresive;
- public int passive;
- public int normal;
- public string room;
- const int waitTime_short = 2;
- const int waitTime_medium = 5;
- const int waitTime_long = 10;
- BotLogHandler botLogHandler;
- private static ILogger logger = Debug.logger;
- enum Phase
- {
- Login, Menu, Lobby, Room, Playing
- }
- Phase currentPhase = Phase.Login;
- void Awake()
- {
- // Singleton - Jika Instance tidak kosong (sudah diisi) dan sudah diinstansiasi tetapi
- // bukan class ini (TexasClient), maka Hapus Class/GameObject ini
- if (Instance != null && Instance != this)
- {
- Destroy(gameObject);
- }
- // Singleton - Aktifkan Instance (Class static TexasClient ke class ini)
- Instance = this;
- // Singleton - Opsional, kamu dapat mengatur Singleton ini agar tidak dihapus ketika
- // berpindah Scene
- DontDestroyOnLoad(gameObject);
- }
- private void Start()
- {
- string[] args = System.Environment.GetCommandLineArgs();
- Debug.Log("Checking arg bot");
- for (int i = 0; i < args.Length; i++)
- {
- var arg = args[i];
- Debug.Log("ARG : "+arg);
- if (arg == "-bot")
- {
- activated = true;
- Debug.Log("BOT : activated");
- }
- else if (arg == "-u")
- {
- username = args[i + 1];
- }
- else if (arg == "-brain")
- {
- var brain = args[i + 1].Split('|');
- aggresive = int.Parse(brain[0]);
- passive = int.Parse(brain[1]);
- normal = int.Parse(brain[2]);
- }
- else if (arg == "-room")
- {
- room = args[i + 1];
- }
- }
- if (activated)
- {
- botLogHandler = new BotLogHandler();
- logger.logHandler = botLogHandler;
- logger.Log("TexasBot", "Texas Bot Started");
- thinkAction();
- }
- }
- void thinkAction()
- {
- if (activated)
- {
- logger.Log("BOT : Thinking action");
- switch (currentPhase)
- {
- case Phase.Login:
- {
- StartCoroutine(DoLogin());
- break;
- }
- case Phase.Menu:
- {
- StartCoroutine(DoMenu());
- break;
- }
- case Phase.Lobby:
- {
- if (!expired)
- {
- StartCoroutine(DoFindRoom());
- }else
- {
- StartCoroutine(LogOut());
- }
- break;
- }
- case Phase.Room:
- {
- StartCoroutine(DoStartPlaying());
- break;
- }
- case Phase.Playing:
- {
- ManagePlay();
- break;
- }
- }
- }
- }
- IEnumerator DoLogin()
- {
- yield return new WaitUntil(() => (PhotonNetwork.connected));
- yield return new WaitForSeconds(waitTime_short);
- logger.Log("BOT : Doing login");
- var obj_username = GameObject.Find("Username").transform.GetChild(0).gameObject;
- var obj_password = GameObject.Find("Password").transform.GetChild(0).gameObject;
- obj_username.GetComponent<InputField>().text = username;
- obj_password.GetComponent<InputField>().text = password;
- back:
- GameObject.FindObjectOfType<LoginController>().LoginGuest();
- yield return new WaitUntil(() => TexasClient.Instance.WWWTextGuestSignInResponse != "");
- yield return new WaitForSeconds(waitTime_medium);
- if (SceneManager.GetActiveScene().buildIndex == App.SCENE_MENU)
- {
- logger.Log("BOT : Login Succeded");
- currentPhase = Phase.Menu;
- thinkAction();
- }else
- {
- logger.Log("BOT : login failed, trying again");
- goto back;
- }
- }
- IEnumerator DoMenu()
- {
- logger.Log("BOT : Doing menu");
- back:
- GameObject.FindObjectOfType<MenuController>().TapTable();
- yield return new WaitForSeconds(waitTime_medium);
- if (SceneManager.GetActiveScene().buildIndex == App.SCENE_ROOM)
- {
- logger.Log("BOT : Goint to lobby");
- currentPhase = Phase.Lobby;
- thinkAction();
- }
- else
- {
- logger.Log("BOT : go to lobby failed, retrying");
- goto back;
- }
- }
- IEnumerator DoFindRoom()
- {
- logger.Log("BOT : Doing find room");
- back:
- yield return new WaitForSeconds(waitTime_medium);
- if(RoomController.self.contentParent.childCount == 0)
- {
- logger.Log("BOT : no room available, finding again");
- RoomController.self.Refresh();
- goto back;
- }
- logger.Log("BOT : selecting room.."+ RoomController.self.contentParent.childCount);
- RoomListController selectedRoom = new RoomListController();
- selectedRoom.roomId = "";
- foreach (Transform g in RoomController.self.contentParent)
- {
- if (g.GetComponent<RoomListController>())
- {
- var rlc = g.GetComponent<RoomListController>();
- logger.Log("BOT : room " + rlc.roomId+" vs "+room);
- if (rlc.roomId == room)
- {
- logger.Log("BOT : room found, joining room");
- selectedRoom = rlc;
- break;
- }
- }
- }
- if(selectedRoom.roomId == "")
- {
- logger.Log("BOT : room not found");
- RoomController.self.Refresh();
- goto back;
- }
- selectedRoom.JoinRoom();
- while (SceneManager.GetActiveScene().buildIndex != App.SCENE_GAME)
- {
- yield return new WaitForSeconds(1f);
- }
- if (SceneManager.GetActiveScene().buildIndex == App.SCENE_GAME)
- {
- logger.Log("BOT : going to room");
- currentPhase = Phase.Room;
- thinkAction();
- }
- else
- {
- logger.Log("BOT : go to room failed, retrying");
- goto back;
- }
- }
- IEnumerator DoStartPlaying()
- {
- logger.Log("BOT : Doing play");
- yield return new WaitForSeconds(waitTime_medium);
- List<int> empty = new List<int>();
- back:
- for (int i = 0; i < GameController.self.players.Length; i++)
- {
- var p = GameController.self.players[i];
- if (!p.IsActivate())
- {
- empty.Add(i);
- }
- }
- Random.seed = System.Environment.TickCount;
- var rnd = Random.Range(0, empty.Count);
- if (!GameController.self.players[rnd].IsActivate())
- {
- logger.Log("BOT : Found empty sit");
- GameController.self.players[rnd].UserTap();
- GameController.self.players[rnd].Aggresive = aggresive;
- GameController.self.players[rnd].Passive = passive;
- GameController.self.players[rnd].Normal = normal;
- }
- else
- {
- logger.Log("BOT : empty sit not found, keep searching");
- empty.Clear();
- yield return new WaitForSeconds(waitTime_medium);
- goto back;
- }
- var chipset = GameObject.FindObjectOfType<InitialChipSetting>();
- yield return new WaitUntil(() => (chipset.gameObject.activeSelf));
- yield return new WaitForSeconds(waitTime_short);
- float rnd2 = Random.Range(chipset.slider.minValue, (chipset.slider.maxValue + 1f));
- chipset.slider.value = rnd2;
- chipset.Submit();
- yield return new WaitForSeconds(waitTime_short);
- if (!GameController.self.players[GameController.self.MIDDLE_INDEX].IsActivate())
- {
- logger.Log("BOT : still not playing, searching");
- empty.Clear();
- yield return new WaitForSeconds(waitTime_medium);
- goto back;
- }else
- {
- logger.Log("BOT : Begin playing");
- currentPhase = Phase.Playing;
- thinkAction();
- }
- }
- void ManagePlay()
- {
- logger.Log("BOT : Manage play");
- StartCoroutine(WaitForTurn());
- StartCoroutine(WaitForLose());
- }
- IEnumerator WaitForLose()
- {
- back:
- logger.Log("BOT : Waiting for lose");
- yield return new WaitUntil(() => (GameController.self.players[GameController.self.MIDDLE_INDEX].coinValue <= 0));
- var chipset = GameObject.FindObjectOfType<InitialChipSetting>();
- if (chipset.gameObject.activeSelf)
- {
- float rnd2 = Random.Range(chipset.slider.minValue, (chipset.slider.maxValue + 1f));
- chipset.slider.value = rnd2;
- chipset.Submit();
- }
- goto back;
- }
- IEnumerator WaitForTurn()
- {
- back:
- logger.Log("BOT : Waiting for turn");
- yield return new WaitUntil(() => (GameObject.FindObjectOfType<NormalAction>() != null));
- NormalAction act = GameObject.FindObjectOfType<NormalAction>();
- logger.Log("BOT : its turn time");
- Random.seed = System.Environment.TickCount;
- var pl = GameController.self.players[GameController.self.MIDDLE_INDEX];
- var rnd = Random.Range(1, pl.MAX_PLAY_TIME);
- logger.Log("BOT : thinking, wait current time until >= "+rnd);
- while(GameController.self.players[GameController.self.MIDDLE_INDEX].currentTime < System.Math.Floor(rnd))
- {
- logger.Log("BOT : current time => "+ GameController.self.players[GameController.self.MIDDLE_INDEX].currentTime);
- yield return null;
- }
- logger.Log("BOT : calculating");
- ListCard.self.CalculateCards(pl.card0, pl.card1);
- float acceptance = 0;
- int cardres = ListCard.self.cardResult;
- int random = 0;
- switch (ListCard.self.cardResult)
- {
- case ListCard.RESULT_ROYALFLUSH:
- acceptance = 0.95f;
- break;
- case ListCard.RESULT_STRAIGHTFLUSH:
- acceptance = 0.9f;
- break;
- case ListCard.RESULT_4OFAKIND:
- acceptance = 0.85f;
- break;
- case ListCard.RESULT_FULLHOUSE:
- acceptance = 0.8f;
- break;
- case ListCard.RESULT_FLUSH:
- acceptance = 0.75f;
- break;
- case ListCard.RESULT_STRAIGHT:
- acceptance = 0.7f;
- break;
- case ListCard.RESULT_3OFAKIND:
- acceptance = 0.65f;
- break;
- case ListCard.RESULT_DUALPAIRS:
- acceptance = 0.6f;
- break;
- case ListCard.RESULT_PAIRS:
- acceptance = 0.55f;
- break;
- case ListCard.RESULT_HIGHCARD:
- acceptance = 0.45f;
- break;
- }
- var follow = false;
- var ActionFollow = PlayerItemNew.ACTION_NULL;
- var smallest = pl.coinValue;
- foreach (PlayerItemNew p in GameController.self.players)
- {
- if (p.IsActivate())
- {
- if (p.coinValue < smallest)
- {
- smallest = p.coinValue;
- }
- if (p.Passive > 50)
- {
- if (!p.isAI && p.action != PlayerItemNew.ACTION_FOLD)
- {
- ListCard.self.CalculateCards(p.card0, p.card1);
- //kalo lebih bagus follow
- if (cardres < ListCard.self.cardResult)
- {
- follow = true;
- ActionFollow = p.action;
- }
- else
- {
- follow = false;
- break;
- }
- }
- }
- }
- }
- Random.seed = System.Environment.TickCount;
- random = Random.Range(1, 101);
- if (random < acceptance * 100)
- {
- if (pl.coinValue > 0 && GameController.self.currentBet < pl.coinValue)
- {
- if ((follow & (ActionFollow == PlayerItemNew.ACTION_RAISE || ActionFollow == PlayerItemNew.ACTION_ALL_IN)) || pl.Aggresive > 50)
- {
- long raiseVal = (long)((pl.Aggresive / 100) * smallest * (acceptance));
- if (raiseVal == 0)
- {
- raiseVal = pl.coinValue;
- }
- if (pl.raiseCounter > 2)
- {
- act.DoCheck();
- }
- else
- {
- if (raiseVal == 0)
- {
- act.DoCheck();
- }
- else
- {
- act.DoCall();
- yield return new WaitUntil(() => (RaiseSlider.self.IsActive()));
- RaiseSlider.self.slider.value = raiseVal;
- act.DoCall();
- }
- }
- }
- else
- {
- act.DoCheck();
- }
- }
- else
- {
- act.DoCheck();
- }
- }
- else
- {
- if ((follow & ActionFollow == PlayerItemNew.ACTION_CHECK) || GameController.self.currentBet == 0)
- {
- act.DoCheck();
- }
- else
- {
- act.DoFold();
- }
- }
- logger.Log("BOT : action decided, waiting for turn again");
- goto back;
- }
- public void CheckBotExistInRoom(string id)
- {
- StartCoroutine(GetBotData(id));
- }
- IEnumerator GetBotData(string uid)
- {
- Debug.Log("BOT : Refreshing bot data with user_id "+ uid);
- WWWForm form = new WWWForm();
- form.AddField("user_id", uid);
- WWW www = new WWW(App.BASIC_URL_USER + "Get_all_bot_on_room", form);
- yield return www;
- if (www.error == null)
- {
- Debug.Log("BOT : Data => " + www.text);
- expired = true;
- var obj = JSON.Parse(www.text);
- if (obj["return"] != null)
- {
- obj = JSON.Parse("{}");
- }
- if (www.text.StartsWith("["))
- {
- obj = obj[0];
- }
- if (obj["ROOM_ID"] != null)
- {
- var id = obj["ROOM_ID"].ToString().Replace("\"", "");
- if (room == id)
- {
- Debug.Log("BOT : bot still exist");
- expired = false;
- if (obj["AGGRESIVE"]!=null)
- {
- aggresive = int.Parse(obj["AGGRESIVE"].ToString().Replace("\"", ""));
- }
- if (obj["PASSIVE"] != null)
- {
- passive = int.Parse(obj["PASSIVE"].ToString().Replace("\"", ""));
- }
- if (obj["NORMAL"] != null)
- {
- normal = int.Parse(obj["NORMAL"].ToString().Replace("\"", ""));
- }
- GameController.self.players[GameController.self.MIDDLE_INDEX].UpdateBot();
- }
- }
- }
- if (expired)
- {
- Debug.Log("BOT : bot disappeared, going to sign out");
- currentPhase = Phase.Lobby;
- GameController.self.ExitRoom();
- thinkAction();
- }
- }
- IEnumerator LogOut()
- {
- yield return new WaitUntil(() => (SceneManager.GetActiveScene().buildIndex == App.SCENE_ROOM));
- Loading.self.Open();
- WWWForm form = new WWWForm();
- form.AddField("user_id", App.userId);
- WWW www = new WWW(App.BASIC_URL_USER + "logout", form);
- yield return www;
- Loading.self.Close();
- Application.Quit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement