Show device specific div

79 Views Asked by At

When the game starts, I need to show the start game button to the first connected player and the waiting text to the second player, but it shows the waiting text to both players, how can I solve it?

Oyun başladığında ilk bağlanan oyuncuya oyunu başlat butonunu, ikinci oyuncuya da bekleme yazısını göstermem gerekiyor ama her iki oyuncuya da bekleme yazısını gösteriyor, bunu nasıl çözebilirim

1

There are 1 best solutions below

7
Marc Schärer On

This is relatively straight forward to achieve. What you want to do is have your controller.html check if the controller is the master controller using airconsole.getMasterControllerDeviceId(). The following is a minimal example of how to decide if you are the on the master controller that drives the experience or another controller:

<html>
  <head>
    <script type="text/javascript" src="https://www.airconsole.com/api/airconsole-1.8.0.js"></script>
    <script type="text/javascript">
      var airconsole;
      
      function init() {
        airconsole = new AirConsole({ orientation: "portrait" });

        airconsole.onConnect = function(device_id) {
          if (airconsole.getMasterControllerDeviceId() === airconsole.device_id) {
            configureMaster();
          } else {
            configureClient();
          }
        }

        airconsole.onDisconnect = function (device_id) {
          if (airconsole.getMasterControllerDeviceId() === airconsole.device_id) {
            configureMaster();
          } else {
            configureClient();
          }
        }

        const configureClient = () => {
          document.getElementById("game_output").innerHTML = "You are one of the additional players";
        }
        const configureMaster = () => {
          document.getElementById("game_output").innerHTML = "You are the controlling player";
        }
        airconsole.onReady = () => {     
          if (airconsole.getMasterControllerDeviceId() === airconsole.device_id) {
            configureMaster();
          } else {
            configureClient();
          }
        };

        airconsole.onCustomDeviceStateChange = (device_id, data) => {
          if (airconsole.getMasterControllerDeviceId() === airconsole.device_id) {
            configureMaster();
          } else {
            configureClient();
          }
        };

    </script>
   
    <div id="game_output">... Waiting for Screen ...</div>
  </body>
</html>