Exemple #1
0
        public Player(AuthenticatedPlayer aplayer)
            : this()
        {
            DisplayName = aplayer.DisplayName;

            // Clean ID from URL reserved chars
            Id = CleanId(aplayer.PlayerId);
        }
        /// <summary>
        /// Initialize a local profile from a player service like Game center
        /// </summary>
        /// <param name="aplayer">Aplayer.</param>
        public void Initialize(AuthenticatedPlayer aplayer)
        {
            AuthenticatedPlayer = aplayer;

            // Call game service auth method (Game center)
            // ------------------------------------------------------------------------
            aplayer.Authenticate (() => {

                bool createNewProfile = false;

                // try to get from database (look at the getter)
                if (CachedPlayer == null) {

                    // Nothing found: create something
                    createNewProfile = true;

                } else {
                    // Game center id is not the same as stored id?
                    if (aplayer.IsAuthenticated) {
                        if (CachedPlayer.Id != Player.CleanId (aplayer.PlayerId)) {
                            createNewProfile = true;
                        }
                    }
                }

                // Locally create the player
                // ------------------------------------------------------------------------
                if (createNewProfile) {

                    // Use game service infos if available
                    Player freshlyCreatedPlayer = new Player (aplayer);

                    savePlayer (freshlyCreatedPlayer);
                }

                // Check the player on the server side
                ServiceGetPlayer ws = new ServiceGetPlayer (CachedPlayer.Id);

                // Make a GET and see if the player exists
                // ------------------------------------------------------------------------
                ws.Request ((serverPlayer) => {
                    if (serverPlayer != null) {

                        // Merge server data
                        Player localPlayer = CachedPlayer;

                        // Server credits should be right
                        localPlayer.Credits = Math.Max (serverPlayer.Credits - localPlayer.DisconnectedCreditsUsed, 0);
                        localPlayer.Coins = Math.Max (serverPlayer.Coins - localPlayer.DisconnectedCoinsEarned, 0); // We don't want negative values

                        savePlayer (localPlayer);

                        // Simply update
                        UpdatePlayer ();
                    }
                },
                (code, exception) => {

                    if (code == 102) {

                        // New player to create
                        Logger.I("Unknow player to server");

                        // Create player on server
                        // ------------------------------------------------------------------------
                        ServiceCreatePlayer wsCreate = new ServiceCreatePlayer (CachedPlayer);
                        wsCreate.CreatePlayer ();

                    } else {
                        // Well, we crashed. See the log?
                        Logger.E( "Calling profile service failed!");
                    }

                    // Update anyway
                    UpdatePlayer ();
                });
            });
        }