// only server
        protected override void SpawnUnit(DualGamePlayer owner, int unitId, Transform location)
        {
            if (unitPrefab == null)
            {
                Debug.LogError("No unit prefab");
                return;
            }

            GameObject newUnitObj;

            #if UNITY_EDITOR
            newUnitObj = PrefabUtility.InstantiatePrefab(unitPrefab) as GameObject;
            newUnitObj.transform.position = location.position;
            newUnitObj.transform.rotation = location.rotation;
            #else
            newUnitObj = Instantiate(unitPrefab, location.position, location.rotation) as GameObject;
            #endif

            Unit newUnit = newUnitObj.GetComponent <Unit>();

            units.Add(newUnit);
            numberOfUnitsPerPlayer[owner.role]++;

            newUnit.playerNetId = owner.netId;
            newUnit.playerRole  = owner.role;

            NetworkServer.Spawn(newUnit.gameObject);
        }
 public override void OnRoleChanged(DualGamePlayer player, int oldRole)
 {
     playerList.OnRoleChanged(player, oldRole);
     if (NetworkServer.active && !isPlaying)
     {
         UpdatePlayButton();
     }
 }
 public override void OnPlayerRemoved(DualGamePlayer player)
 {
     playerList.OnPlayerRemoved(player);
     if (NetworkServer.active && !isPlaying)
     {
         UpdatePlayButton();
     }
 }
        public void OnRoleChanged(DualGamePlayer player, int oldRole)
        {
            int newRole = player.role;

            if (oldRole == newRole)
            {
                //Debug.LogWarning("It didn't change!");
                return;
            }

            if (oldRole < maxPlayers)
            {
                bool alreadyReplaced = false;

                for (int i = 0; i < playerContainer.childCount; i++)
                {
                    CNMPlayer p = playerContainer.GetChild(i).GetComponent <CNMPlayer>();

                    //Debug.Log("Checking one...");
                    if (p != null)
                    {
                        if (p.role == oldRole)
                        {
                            //Debug.Log("Already replaced");

                            alreadyReplaced = true;
                            break;
                        }

                        /*else if(p.role > oldRole)
                         * {
                         *  Debug.Log("Breaking here");
                         *  break;
                         * }*/
                    }
                }

                if (!alreadyReplaced)
                {
                    ShowWildcard(oldRole);
                }
            }

            int oldCount = playerContainer.childCount;

            player.transform.SetParent(null, false);
            int newCount = playerContainer.childCount;

            if (newCount != oldCount - 1)
            {
                Debug.LogError("Wrong 1");
            }

            AddPlayerToList(player);

            RedrawMoveButtons();
        }
        private void AddPlayerToList(DualGamePlayer player)
        {
            int newRole = player.role;

            //Debug.LogFormat("Adding player {0}", newRole);

            if (newRole < maxPlayers)
            { // is an actual player
                ReplaceWildcard(newRole, player.transform);
            }
            else
            { // is spectator
                bool added = false;

                if (separator == null)
                {
                    Debug.LogError("No separator");
                    return;
                }
                else if (playerContainer == null)
                {
                    Debug.LogError("No player container");
                    return;
                }

                for (int i = separator.GetSiblingIndex() + 1; i < playerContainer.childCount; i++)
                {
                    Transform      child = playerContainer.GetChild(i);
                    DualGamePlayer other = child.GetComponent <DualGamePlayer>();

                    if (other != null)
                    {
                        if (player.role < other.role)
                        {
                            added = true;
                            player.transform.SetParent(playerContainer, false);
                            player.transform.SetSiblingIndex(i);
                        }
                    }
                    else
                    {
                        Debug.LogError("Null player");
                    }
                }

                if (!added)
                {
                    player.transform.SetParent(playerContainer, false);

                    // I think this is not needed
                    player.transform.SetAsLastSibling();
                }
            }
        }
        protected override DualGamePlayer CreatePlayer(int connectionId, short playerControllerId)
        {
            bool           isLocal = (connectionId == 0);
            DualGamePlayer player  = null;

            if (isLocal)
            {
                JuloDebug.Err("Should not be called for hosted players");
            }
            else
            {
                player = NewPlayer(remotePlayerModel);
            }

            return(player);
        }
        public void OnPlayerAdded(DualGamePlayer player)
        {
            if (!initialized)
            {
                initialize();
            }
            // Debug.LogFormat("Adding in client player with role {0}", player.role);

            AddPlayerToList(player);

            if (!NetworkServer.active)
            {
                ((CNMPlayer)player).moveUp.gameObject.SetActive(false);
                ((CNMPlayer)player).moveDown.gameObject.SetActive(false);
            }

            RedrawMoveButtons();
        }
        public void OnPlayerRemoved(DualGamePlayer player)
        {
            if (player.role < 0)
            {
                Debug.LogErrorFormat("Invalid role {0}", player.role);
                return;
            }

            if (player.role < maxPlayers)
            { // if it was an actual player
                ShowWildcard(player.role);
            }
            else
            { // if it was spectator
            }

            player.transform.SetParent(null, false);

            RedrawMoveButtons();
        }
        public void NextTurn()
        {
            gameOver = UpdateUnitNumbers();

            if (gameOver)
            {
                Debug.Log("Game is over!!!");
            }
            else
            {
                do
                {
                    currentPlayer = (currentPlayer + 1) % currentMaxPlayers;
                } while(numberOfUnitsPerPlayer[currentPlayer] == 0);

                //Debug.LogFormat("Is turn of {0}", currentPlayer);

                DualGamePlayer current = GetPlayer(currentPlayer);

                if (current.connectionToClient != lastOwningConnection)
                {
                    foreach (Unit unit in units)
                    {
                        NetworkIdentity unitIdentity = unit.GetComponent <NetworkIdentity>();
                        if (lastOwningConnection != null)
                        {
                            unitIdentity.RemoveClientAuthority(lastOwningConnection);
                        }
                        unitIdentity.AssignClientAuthority(current.connectionToClient);
                    }

                    lastOwningConnection = current.connectionToClient;
                }

                ((CNMPlayer)current).RpcPlay();
            }
        }