public void OnRealTimeMessageReceived(bool isReliable, string senderId, byte[] data) { string message = System.Text.Encoding.Unicode.GetString(data); string[] splitMessage = message.Split('#'); switch (splitMessage[0]) { // CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT // CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT case GPMessageStrings.JUMP_TO: // server sends that we have to jump somewhere with camera Vector3 jumpTo = new Vector3(int.Parse(splitMessage[1]), int.Parse(splitMessage[2]), Camera.main.transform.position.z); Camera.main.transform.DOMove(jumpTo, Vector2.Distance(Camera.main.transform.position, jumpTo) * JUMP_TIME_PER_ONE); break; case GPMessageStrings.SIGN_PLACED: // Server sends that a sign was placed int[] lastPos = new int[] { int.Parse(splitMessage[1]), int.Parse(splitMessage[2]) }; Cell.CellOcc lastType = (Cell.CellOcc)System.Enum.Parse(typeof(Cell.CellOcc), splitMessage[3]); // Place sign locally ClientCellStorage.PlaceCellAt(lastPos, lastType); break; case GPMessageStrings.ADD_BORDER: // Server sends that a border was added // Get data and add a border locally int countOfPoints = int.Parse(splitMessage[1]); int[,] points = new int[countOfPoints, 2]; float[,] winLine = new float[2, 2]; for (int k = 0; k < countOfPoints; k++) { points[k, 0] = int.Parse(splitMessage[2 * (k + 3)]); points[k, 1] = int.Parse(splitMessage[2 * (k + 3) + 1]); } winLine[0, 0] = float.Parse(splitMessage[2]); winLine[0, 1] = float.Parse(splitMessage[3]); winLine[1, 0] = float.Parse(splitMessage[4]); winLine[1, 1] = float.Parse(splitMessage[5]); Cell.CellOcc winType = (Cell.CellOcc)System.Enum.Parse(typeof(Cell.CellOcc), splitMessage[6 + countOfPoints * 2]); BluetoothClientBorder.AddBorderPoints(points, winLine, winType); // Someone won the game so call it in clientcellstorage ClientCellStorage.SomeoneWon(winType); break; case GPMessageStrings.SEND_SCORE: // Server sends score int xScore = int.Parse(splitMessage[1]); int oScore = int.Parse(splitMessage[2]); Scoring.SetScore(xScore, oScore); break; case GPMessageStrings.TURN_OF: // Server sends whose turn it is Cell.CellOcc whoseTurn = (Cell.CellOcc)System.Enum.Parse(typeof(Cell.CellOcc), splitMessage[1]); ClientUIInScript.UpdateImage(whoseTurn); break; // SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER // SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER case GPMessageStrings.TRY_PLACE_AT: // client is trying to place at pos Vector2 pos = new Vector2(int.Parse(splitMessage[1]), int.Parse(splitMessage[2])); // If it's not server's turn try placing at pos if (!GameLogic.IsItServersTurn()) { GameLogic.WantToPlaceAt(pos); } break; // BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH // BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH case GPMessageStrings.SEND_MSG: BluetoothMessageManager.ShowEmojiMessage(EmojiSprites.GetEmoji(splitMessage[1])); break; } }
/// <summary> /// Done reading the message event /// </summary> /// <param name="readMessage"></param> void DoneReadingEvent(string readMessage) { string[] differentMessages = readMessage.Split(new string[] { "|||" }, StringSplitOptions.None); for (int i = 0; i < differentMessages.Length - 1; i++) { string[] splitMessage = differentMessages[i].Split('#'); // SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER // SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER if (isServer) { switch (splitMessage[0]) { case "CLIENTREADY": // client sends he is ready isClientReady = bool.Parse(splitMessage[1]); CheckAllReady(); break; case "TRYPLACEAT": // client is trying to place at pos Vector2 pos = new Vector2(int.Parse(splitMessage[1]), int.Parse(splitMessage[2])); // If it's not server's turn try placing at pos if (!GameLogic.IsItServersTurn()) { GameLogic.WantToPlaceAt(pos); } break; case "WHOSETURN": // Client is asking for whose turn it is Bluetooth.Instance().Send(BluetoothMessageStrings.TURN_OF + "#" + gameLogic.WhoseTurn.ToString()); break; case "SMB": // Client is asking for latest border data Bluetooth.Instance().Send(BluetoothMessageStrings.ADD_BORDER + "#" + lastBorder.ToString() + "#" + lastBorderID); break; } // CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT // CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT CLIENT } else { switch (splitMessage[0]) { case "STARTGAME": // Server sends that the game has been started // Switch scenes ScaneManager.Instance.GoToScene("ClientBluetoothGame"); break; case "WLP": // Server sends where last sign has been placed int[] lastPos = new int[] { int.Parse(splitMessage[1]), int.Parse(splitMessage[2]) }; if (!(lastPos[0] == lastSignPlaced[0] && lastPos[1] == lastSignPlaced[1])) { secondToLastSignPlaced[0] = lastSignPlaced[0]; secondToLastSignPlaced[1] = lastSignPlaced[1]; lastSignPlaced[0] = lastPos[0]; lastSignPlaced[1] = lastPos[1]; // Store new sign pos as last sign Cell.CellOcc lastType = (Cell.CellOcc)Enum.Parse(typeof(Cell.CellOcc), splitMessage[3]); // Place sign locally ClientCellStrg.PlaceCellAt(lastPos, lastType); } break; case "LBI": // Server sends what the last border's bluetooth id is // The server has a newer border placed if (lastBorderID != int.Parse(splitMessage[1])) { Bluetooth.Instance().Send(BluetoothMessageStrings.SEND_ME_BORDER); } break; case "TURNOF": // Server sends whose turn it is Cell.CellOcc whoseTurn = (Cell.CellOcc)Enum.Parse(typeof(Cell.CellOcc), splitMessage[1]); ClientUIInScript.UpdateImage(whoseTurn); break; case "ADDBORDER": // Server sends border data // set lates bluetooth border id lastBorderID = int.Parse(splitMessage[splitMessage.Length - 1]); // Get data and add a border locally int countOfPoints = int.Parse(splitMessage[1]); int[,] points = new int[countOfPoints, 2]; float[,] winLine = new float[2, 2]; for (int k = 0; k < countOfPoints; k++) { points[k, 0] = int.Parse(splitMessage[2 * (k + 3)]); points[k, 1] = int.Parse(splitMessage[2 * (k + 3) + 1]); } winLine[0, 0] = float.Parse(splitMessage[2]); winLine[0, 1] = float.Parse(splitMessage[3]); winLine[1, 0] = float.Parse(splitMessage[4]); winLine[1, 1] = float.Parse(splitMessage[5]); Cell.CellOcc winType = (Cell.CellOcc)Enum.Parse(typeof(Cell.CellOcc), splitMessage[6 + countOfPoints * 2]); BluetoothClientBorder.AddBorderPoints(points, winLine, winType); // Because a border was added someone won the game so call the event ClientCellStrg.SomeoneWon(winType); break; case "JPT": // Server sends to jump to this pos because new game has been started Vector3 jumpTo = new Vector3(int.Parse(splitMessage[1]), int.Parse(splitMessage[2]), Camera.main.transform.position.z); Camera.main.transform.DOMove(jumpTo, Vector2.Distance(Camera.main.transform.position, jumpTo) * JUMP_TIME_PER_ONE); break; case "SSCR": int xScore = int.Parse(splitMessage[1]); int oScore = int.Parse(splitMessage[2]); Scoring.SetScore(xScore, oScore); break; } } // BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH // BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH BOTH switch (splitMessage[0]) { case "SMSG": BluetoothMessageManager.ShowEmojiMessage(EmojiSprites.GetEmoji(splitMessage[1])); break; } } }