示例#1
0
        /// <summary>
        /// Set a memory in your story
        /// </summary>
        /// <param name="token">Provide the token of the play-through where the memory should be changed.</param>
        /// <param name="recallValue">The recall value of the memory.</param>
        /// <param name="saveValue">The new value of the memory.</param>
        /// <param name="callback">Called when the mood has successfully been set.</param>
        public static void SetMemory(string token, string recallValue, string saveValue, Action callback = null)
        {
            var memory = new SetMemoryParams(recallValue, saveValue);

            var request = new HTTPRequest(
                new Uri($"{BaseUrl}/play/set-memory/"), HTTPMethods.Post, (
                    (originalRequest, response) =>
            {
                if (!response.IsSuccess)
                {
                    Debug.LogError("Error:" + originalRequest.Response.DataAsText);
                    return;
                }

                CharismaLogger.Log($"Set memory - '{memory.memoryRecallValue}' with value '{memory.saveValue}'");
                callback?.Invoke();
            }))
            {
                RawData = Encoding.UTF8.GetBytes(CharismaUtilities.ToJson(memory))
            };

            request.SetHeader("Authorization", $"Bearer {token}");
            request.AddHeader("Content-Type", "application/json");
            request.UseAlternateSSL = true;
            request.Send();
        }
示例#2
0
        /// <summary>
        /// Set the mood of a Character in your story.
        /// </summary>
        /// <param name="token">Provide the token of the play-through where a characters mood should be set.</param>
        /// <param name="characterName">The name of the character who's mood should be set.</param>
        /// <param name="mood">The mood to update to.</param>
        /// <param name="callback">Called when the mood has successfully been set.</param>
        public static void SetMood(string token, string characterName, Mood mood, Action callback = null)
        {
            if (mood == null || characterName == null)
            {
                Debug.LogError("Charisma: You need to provide both a character name and a character mood modifier.");
                return;
            }

            var newMood = new SetMoodParams(characterName, mood);

            CharismaLogger.Log($"Setting new mood for {characterName}: CharismaUtilities.ToJson(newMood)");

            var request = new HTTPRequest(
                new Uri($"{BaseUrl}/play/set-mood"), HTTPMethods.Post, (
                    (originalRequest, response) =>
            {
                if (!response.IsSuccess)
                {
                    Debug.LogError("Error:" + originalRequest.Response.DataAsText);
                    return;
                }

                CharismaLogger.Log($"Updated mood of '{characterName}'");

                callback?.Invoke();
            }))
            {
                RawData = Encoding.UTF8.GetBytes(CharismaUtilities.ToJson(newMood))
            };

            request.SetHeader("Authorization", $"Bearer {token}");
            request.AddHeader("Content-Type", "application/json");
            request.UseAlternateSSL = true;
            request.Send();
        }
示例#3
0
        /// <summary>
        /// Connect to Charisma
        /// </summary>
        /// <param name="onReadyCallback">Called when successfully connected to Charisma.</param>
        public void Connect(Action onReadyCallback)
        {
            if (IsConnected)
            {
                return;
            }

            var options = new SocketOptions
            {
                ConnectWith           = TransportTypes.WebSocket,
                AdditionalQueryParams = new ObservableDictionary <string, string> {
                    { "token", Token }
                }
            };

            _socketManager = new SocketManager(new Uri($"{BaseUrl}/socket.io/"), options)
            {
                Encoder = new LitJsonEncoder()
            };

            _socket = _socketManager.GetSocket("/play");

            _socket.On(SocketIOEventTypes.Connect, (socket, packet, args) =>
            {
                CharismaLogger.Log("Connected to socket");
            });

            _socket.On("error", (socket, packet, args) => {
                Debug.LogError(args[0].ToString());
            });

            _socket.On("status", (socket, packet, args) => {
                if ((string)args[0] == "ready")
                {
                    CharismaLogger.Log("Ready to begin play");
                    IsReadyToPlay = true;

                    onReadyCallback?.Invoke();
                }
                else
                {
                    Debug.LogError("Charisma: Failed to set up websocket connection to server");
                }
            });

            _socket.On("message", (socket, packet, args) =>
            {
                MainThreadConsumer.Instance.Enqueue((async() =>
                {
                    var response = await CharismaUtilities.GenerateResponse(packet.Payload);

                    OnMessage?.Invoke(response.ConversationId, response);


                    CharismaLogger.Log($"Received message");
                }));
            });

            _socket.On("start-typing", (socket, packet, args) =>
            {
                OnStartTyping?.Invoke(JsonConvert.DeserializeObject <Conversation>(packet.Payload).ConversationId);

                CharismaLogger.Log("Start typing");
            });

            _socket.On("stop-typing", (socket, packet, args) =>
            {
                OnStopTyping?.Invoke(JsonConvert.DeserializeObject <Conversation>(packet.Payload).ConversationId);

                CharismaLogger.Log("Stop typing");
            });

            _socket.On("problem", (socket, packet, args) =>
            {
                CharismaLogger.Log(JsonConvert.DeserializeObject <CharismaError>(packet.Payload).Error);
            });
        }