public IEnumerator FindMatch() { yield return(new WaitForSeconds(0f)); if (!Waiting) { Waiting = true; WWWForm form = new WWWForm(); form.AddField("user", User); WWW w = new WWW(SERVER_ADDRESS + "?act=matchmaking", form); yield return(w); if (!string.IsNullOrEmpty(w.error)) { print(w.error); } else { print(w.text); string[] datas = w.text.Split(';'); foreach (string data in datas) { string[] keypair = data.Split('='); switch (keypair[0]) { case "player1": if (keypair[1].Equals(User)) { ScreenController.Change("WaitingForPlayer"); StartCoroutine(WaitForPlayer()); IsPlayer1 = true; } Player1 = keypair[1]; break; case "player2": if (keypair[1].Equals(User)) { ScreenController.Change("MapSetup"); gameController.enabled = true; playerController.enabled = true; IsPlayer1 = false; StartCoroutine(ReadMatchStatus()); } Player2 = keypair[1]; break; case "matchid": MatchId = keypair[1]; break; } } } Waiting = false; } }
public void Play() { GameEnded = false; MainGameMessage.text = ""; MapSetupMessage.text = ""; CellController.ClearSelection(); CellController.Locked = false; CellController.Attack = false; ScreenController.Change("Matchmaking"); StartCoroutine(FindMatch()); }
public IEnumerator Login(string user, string pass) { yield return(new WaitForSeconds(0f)); if (!Waiting) { Waiting = true; WWWForm form = new WWWForm(); form.AddField("user", user); form.AddField("pass", pass); WWW w = new WWW(SERVER_ADDRESS + "?act=check_pass", form); yield return(w); if (!string.IsNullOrEmpty(w.error)) { print(w.error); } else { switch (w.text) { case "1": User = user; Pass = pass; Signed = true; print("Signed-in"); StartCoroutine(WriteOnline()); ScreenController.Change("MainMenu"); break; case "0": User = ""; Pass = ""; Signed = false; LoginError("Invalid Password"); break; case "-1": User = ""; Pass = ""; Signed = false; LoginError("User not found"); break; default: print(w.text); break; } } Waiting = false; } }
private IEnumerator ReturnToMenu() { if (!Waiting) { Waiting = true; for (int i = 3; i > 0; i--) { MainGameMessage.text = "Returning to menu in " + i; yield return(new WaitForSeconds(1f)); } ScreenController.Change("MainMenu"); Application.LoadLevel(0); Waiting = false; } }
private IEnumerator WaitForPlayer() { yield return(new WaitForSeconds(.5f)); if (!GameEnded) { WWWForm form = new WWWForm(); form.AddField("id", MatchId); WWW w = new WWW(SERVER_ADDRESS + "?act=read_match", form); yield return(w); if (!string.IsNullOrEmpty(w.error)) { print(w.error); } else { print(w.text); string[] datas = w.text.Split(';'); foreach (string data in datas) { string[] keypair = data.Split('='); switch (keypair[0]) { case "player2": if (!string.IsNullOrEmpty(keypair[1])) { ScreenController.Change("MapSetup"); gameController.enabled = true; playerController.enabled = true; StartCoroutine(ReadMatchStatus()); } else { StartCoroutine(WaitForPlayer()); } Player2 = keypair[1]; break; } } } } }
private IEnumerator ReadMatchStatus() { yield return(new WaitForSeconds(1f)); WWWForm form = new WWWForm(); form.AddField("id", MatchId); WWW w = new WWW(SERVER_ADDRESS + "?act=read_match", form); yield return(w); if (!string.IsNullOrEmpty(w.error)) { print(w.error); } else { string[] datas = w.text.Split(';'); foreach (string data in datas) { string[] keypair = data.Split('='); switch (keypair[0]) { case "status": if (keypair[1] == "closed" && !GameEnded) { //Match Error CellController.Locked = true; ScreenController.Change("MainGame"); GameObject.Find("Map").SetActive(false); instance.MainGameTitle.text = "Draw (connection problem)"; StartCoroutine(ReturnToMenu()); } else { StartCoroutine(ReadMatchStatus()); } break; } } } }
public IEnumerator WaitForReady() { yield return(new WaitForSeconds(.5f)); if (!GameEnded) { WWWForm form = new WWWForm(); form.AddField("id", MatchId); WWW w = new WWW(SERVER_ADDRESS + "?act=read_match", form); yield return(w); if (!string.IsNullOrEmpty(w.error)) { print(w.error); } else { print(w.text); string[] datas = w.text.Split(';'); bool p1_ready = false; bool p2_ready = false; string map = null; foreach (string data in datas) { string[] keypair = data.Split('='); switch (keypair[0]) { case "p1_ready": p1_ready = keypair[1] == "1" ? true : false; break; case "p2_ready": p2_ready = keypair[1] == "1" ? true : false; break; case "map1": if (!IsPlayer1) { map = keypair[1].Replace('$', '=').Replace('&', ';'); } break; case "map2": if (IsPlayer1) { map = keypair[1].Replace('$', '=').Replace('&', ';'); } break; } } if (p1_ready && p2_ready) { GameEnded = false; CellController.ClearSelection(); ScreenController.Change("MainGame"); MainGameTitle.text = IsPlayer1 ? "Your turn" : "Enemy's turn"; if (!IsPlayer1) { StartCoroutine(WaitForTurn()); } CellController.Locked = !IsPlayer1; CellController.Attack = true; if (map != null) { LoadMap(map); } } else { StartCoroutine(WaitForReady()); } } } }
public void Profile() { ScreenController.Change("Profile"); StartCoroutine(UpdateProfile()); }
public void MainMenu() { ScreenController.Change("MainMenu"); }