public void Save() => zMachine.SaveState();
/// <summary> /// POST: api/Messages /// Receive a message from a user and reply to it /// </summary> public async Task <HttpResponseMessage> Post([FromBody] Activity activity) { switch (activity.Text) { case "/reset": activity.GetStateClient().BotState.DeleteStateForUser(activity.ChannelId, activity.From.Id); break; } BotIO io = new BotIO(); ZMachine zMachine = new ZMachine(io); string gameFile = Environment.GetEnvironmentVariable("GameFile"); string path = HostingEnvironment.MapPath($"~/Games/{gameFile}"); FileStream fs = File.OpenRead(path); zMachine.LoadFile(fs); BotData data = activity.GetStateClient().BotState.GetUserData(activity.ChannelId, activity.From.Id); byte[] state = data.GetProperty <byte[]>("ZState"); try { if (state != null) { using (MemoryStream ms = new MemoryStream(state)) zMachine.RestoreState(ms); zMachine.FinishRead(activity.Text); } } catch (Exception) { activity.GetStateClient().BotState.DeleteStateForUser(activity.ChannelId, activity.From.Id); } zMachine.Run(true); Stream s = zMachine.SaveState(); using (MemoryStream ms = new MemoryStream()) { s.CopyTo(ms); data.SetProperty("ZState", ms.ToArray()); } activity.GetStateClient().BotState.SetUserData(activity.ChannelId, activity.From.Id, data); if (activity.Type == ActivityTypes.Message) { ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); // return our reply to the user Activity reply = activity.CreateReply(io.Text); await connector.Conversations.ReplyToActivityAsync(reply); } else { HandleSystemMessage(activity); } var response = Request.CreateResponse(HttpStatusCode.OK); return(response); }