Пример #1
0
        private PlayFabCharacterData CreateNewCharacterInitialData(string playfabID, string characterID, string characterName)
        {
            PlayFabCharacterData newCharacterInitialData = new PlayFabCharacterData(playfabID, characterID, characterName, 0, 0, 0, 1, 0, 0);

            newCharacterInitialData.IsInitialCharacterData = true;

            return(newCharacterInitialData);
        }
 public ConnectedClient(IClient client, string playfabID)
 {
     Client                  = client;
     ClientID                = client.ID;
     PlayFabID               = playfabID;
     CurrentCharacterData    = new PlayFabCharacterData();
     Client.MessageReceived += OnMessage;
 }
Пример #3
0
        //Save character data
        public void TrySaveCharacterData(PlayFabCharacterData characterData)
        {
            GetEntityTokenRequest requestToken = GetTokenForRequest();

            PlayFabAuthenticationAPI.GetEntityToken(requestToken,
                                                    result =>
            {
                SaveCharacterData(characterData);
            },
                                                    error =>
            {
                Debug.Log($"Failed to get entity token");
            });
        }
Пример #4
0
        //Uses the PlayFab account ID for that player to return the correct connected client for this character
        private ushort ReturnClientConnectionByPlayFabCharacterID(PlayFabCharacterData characterData)
        {
            foreach (KeyValuePair <ushort, ConnectedClient> kvp in ServerManager.Instance.ConnectedClients)
            {
                if (kvp.Value != null)
                {
                    if (kvp.Value.PlayFabID == characterData.PlayFabID)
                    {
                        return(kvp.Value.ClientID);
                    }
                }
            }

            return(9999); // there will NEVER be this many players on a single server
        }
Пример #5
0
        public void SpawnPlayerOnServer(ushort clientID, PlayFabCharacterData characterData)
        {
            if (!CurrentPlayers.ContainsKey(clientID))
            {
                InstantiateAndAddPlayer(clientID, characterData.WorldPosition);

                PlayerSpawnData spawnData = new PlayerSpawnData(
                    clientID,
                    characterData.WorldPosition,
                    characterData.CharacterName
                    );

                SyncronizePlayerCharacterOnClients(spawnData);
            }
            else
            {
                Debug.Log("Player already spawned for this client");
            }
        }
Пример #6
0
        //Creates, sets, and returns <string, object> dictionary required by PlayFabs object list base on existing values held in characterData
        Dictionary <string, object> SetCharacterInfoData(PlayFabCharacterData characterData)
        {
            return(new Dictionary <string, object>()
            {
                { "CharacterID", characterData.CharacterID },
                { "PlayFabID", characterData.PlayFabID },
                { "CharacterName", characterData.CharacterName },

                { "CharacterLevel", characterData.CharacterLevel },
                { "CharacterXP", characterData.CharacterXP },
                { "CharacterGold", characterData.CharacterGold },

                { "WorldPositionX", characterData.WorldPosition.x },
                { "WorldPositionY", characterData.WorldPosition.y },
                { "WorldPositionZ", characterData.WorldPosition.z },

                { "InitialCharacterSave", characterData.IsInitialCharacterData }
            });
        }
Пример #7
0
        void RetrieveCharacterData(ushort ConnectedClientID, string characterID)
        {
            PlayFab.DataModels.EntityKey characterEntityKey = CreateKeyForEntity(characterID, "character");
            GetObjectsRequest            getObjectsRequest  = CreateGetCharacterObjectRequestEscaped(characterEntityKey);

            PlayFabDataAPI.GetObjects(getObjectsRequest,
                                      result =>
            {
                PlayFabCharacterData characterData = PlayFabSimpleJson.DeserializeObject <PlayFabCharacterData>(result.Objects["CharacterData"].EscapedDataObject);
                Debug.Log($"character position for retrieved character: {characterData.WorldPositionX}, {characterData.WorldPositionY}, {characterData.WorldPositionZ}");
                characterData.SetWorldPosition(characterData.WorldPositionX, characterData.WorldPositionY, characterData.WorldPositionZ);
                Debug.Log($"character position AS VECTOR 3 for retrieved character: {characterData.WorldPosition.ToString()}");

                SetCurrentCharacterDataForConnectedClient(ConnectedClientID, characterData);
            }, error =>
            {
                Debug.Log("Error setting player info from PlayFab result object");
                Debug.Log(error.ErrorMessage);
            });
        }
Пример #8
0
        private void SaveCharacterWorldStateInPlayfab(ClientDisconnectedEventArgs e)
        {
            Debug.Log("Client quit game");
            if (PlayerManager.Instance.CurrentPlayers.ContainsKey(e.Client.ID))
            {
                GameObject           character     = PlayerManager.Instance.CurrentPlayers[e.Client.ID].gameObject;
                PlayFabCharacterData characterData = ConnectedClients[e.Client.ID].CurrentCharacterData;
                characterData.WorldPosition = character.transform.position;

                Debug.Log("actual character data? Position should not be 0,0,0");
                Debug.Log(ConnectedClients[e.Client.ID].CurrentCharacterData.WorldPosition);

                characterData.IsInitialCharacterData = false;

                PlayFabAPICaller.Instance.TrySaveCharacterData(characterData);
            }
            else
            {
                Debug.Log("Client Key not found in CurrentPlayers");
            }
        }
Пример #9
0
        //Other classes should access any of these methods with the Try version of that method

        #region Character Creation
        public void TryCreateNewCharacter(string name, string playfabID, ushort clientID)
        {
            GrantCharacterToUserRequest grantCharacterToUserRequest = new GrantCharacterToUserRequest
            {
                CharacterName = name,
                CharacterType = "Basic",
                PlayFabId     = playfabID
            };

            PlayFabServerAPI.GrantCharacterToUser(grantCharacterToUserRequest,
                                                  result =>
            {
                PlayFabCharacterData newCharacterInitialData = CreateNewCharacterInitialData(playfabID, result.CharacterId, name);
                TrySaveCharacterData(newCharacterInitialData);
            },
                                                  error =>
            {
                Debug.Log($"Error: Unable To Set Initial Character Data");
                Debug.Log($"Error: {error.ErrorMessage}");
                ServerManager.Instance.SendToClient(clientID, Tags.RegisterNewCharacterResponse, new RegisterNewCharacterResponseData(false));
            });
        }
Пример #10
0
        void SaveCharacterData(PlayFabCharacterData characterData)
        {
            var dataList = new List <SetObject>();

            dataList.Add(ReturnNewSetObject("CharacterData", SetCharacterInfoData(characterData)));

            PlayFab.DataModels.EntityKey characterEntityKey = CreateKeyForEntity(characterData.CharacterID, "character");

            SetObjectsRequest setCharacterObjectDataRequest = new SetObjectsRequest()
            {
                Entity  = characterEntityKey,
                Objects = dataList
            };

            PlayFabDataAPI.SetObjects(setCharacterObjectDataRequest,
                                      result =>
            {
                if (characterData.IsInitialCharacterData)
                {
                    ushort clientID = ReturnClientConnectionByPlayFabCharacterID(characterData);

                    if (clientID != 9999)
                    {
                        ServerManager.Instance.SendToClient(clientID, Tags.RegisterNewCharacterResponse, new RegisterNewCharacterResponseData(true));
                    }
                    else
                    {
                        ServerManager.Instance.SendToClient(clientID, Tags.RegisterNewCharacterResponse, new RegisterNewCharacterResponseData(false));
                    }
                }
            },
                                      error =>
            {
                Debug.Log($"Failed to save character state data");
                Debug.Log(error.ErrorMessage);
                Debug.Log(error.ErrorDetails);
            });
        }
Пример #11
0
 void SpawnCharacterOnServer(ushort clientID, PlayFabCharacterData characterData)
 {
     PlayerManager.Instance.SpawnPlayerOnServer(clientID, characterData);
 }
Пример #12
0
        //Assigns CurrentCharacterData inside a client connection for a connected player
        private void SetCurrentCharacterDataForConnectedClient(ushort ConnectedClientID, PlayFabCharacterData characterData)
        {
            bool ConnectedClientIDIsValid = IsConnectedClientIDValid(ConnectedClientID);

            if (ConnectedClientIDIsValid)
            {
                ServerManager.Instance.ConnectedClients[ConnectedClientID].CurrentCharacterData = characterData;
                SpawnCharacterOnServer(ConnectedClientID, characterData);
            }
        }