-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.cs
165 lines (143 loc) · 5.43 KB
/
Server.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System.Threading.Tasks;
using Grpc.Core;
using Google.Protobuf.WellKnownTypes;
using System.Collections.Generic;
using System;
using InkGRPC;
namespace ink_runtime_grpc
{
class InkServer : Story.StoryBase
{
// contains a Dictionary of Guids to stories
private Dictionary<string, Ink.Runtime.Story> runningStories;
// dictionary of story titles to ink json
private Dictionary<string, string> stories;
private string storyPath;
public InkServer(string StoryPath)
{
runningStories = new Dictionary<string, Ink.Runtime.Story>();
stories = new Dictionary<string, string>();
storyPath = StoryPath;
}
public override Task<NewStoryReply> NewStory(NewStoryRequest request, ServerCallContext context)
{
string guid = Guid.NewGuid().ToString();
Ink.Runtime.Story newStory;
string json = request.Ink.ToString();
try
{
newStory = new Ink.Runtime.Story(json);
runningStories.Add(guid, newStory);
return Task.FromResult(new NewStoryReply { Id = guid });
}
catch (Exception e)
{
throw new RpcException(new Status(StatusCode.InvalidArgument, "Unable to create story: " + e.Message));
}
}
public override Task<NewStoryReply> StartStory(StartStoryRequest request, ServerCallContext context)
{
string guid = Guid.NewGuid().ToString();
Ink.Runtime.Story newStory;
try
{
newStory = new Ink.Runtime.Story(stories[request.StoryTitle]);
runningStories.Add(guid, newStory);
return Task.FromResult(new NewStoryReply { Id = guid });
}
catch (Exception e)
{
throw new RpcException(new Status(StatusCode.InvalidArgument, "Unable to start story: " + e.Message));
}
}
public override Task<ListStoriesReply> ListStories(Empty request, ServerCallContext context)
{
try
{
var reply = new ListStoriesReply();
foreach (string title in stories.Keys)
{
reply.StoryTitles.Add(title);
}
return Task.FromResult(reply);
}
catch (Exception e)
{
throw new RpcException(new Status(StatusCode.InvalidArgument, "Unable to list story: " + e.Message));
}
}
public override Task<ContinueReply> Continue(ContinueRequest request, ServerCallContext context)
{
try
{
Ink.Runtime.Story story = runningStories[request.Id];
var storyState = MarshalStoryState(story);
return Task.FromResult(new ContinueReply { Story = storyState });
}
catch (Exception e)
{
throw new RpcException(new Status(StatusCode.InvalidArgument, "Unable to continue story: " + e.Message));
}
}
public override Task<Empty> ChoosePathString(ChoosePathStringRequest request, ServerCallContext context)
{
try
{
Ink.Runtime.Story story = runningStories[request.Id];
story.ChoosePathString(request.Path, request.ResetCallstack);
return Task.FromResult(new Empty());
}
catch (Exception e)
{
throw new RpcException(new Status(StatusCode.InvalidArgument, "Unable to continue story: " + e.Message));
}
}
public override Task<ChooseChoiceReply> ChooseChoice(ChooseChoiceRequest request, ServerCallContext context)
{
Ink.Runtime.Story story = runningStories[request.Id];
var success = false;
try
{
story.ChooseChoiceIndex(request.ChoiceIndex);
success = true;
}
catch { }
return Task.FromResult(new ChooseChoiceReply { Success = success });
}
private static InkGRPC.StoryState MarshalStoryState(Ink.Runtime.Story story)
{
var state = new InkGRPC.StoryState();
state.Text = story.Continue();
state.CanContinue = story.canContinue;
foreach (Ink.Runtime.Choice choice in story.currentChoices)
{
state.Choices.Add(new InkGRPC.Choice { Index = choice.index, Text = choice.text });
}
foreach (string tag in story.currentTags)
{
state.CurrentTags.Add(tag);
}
if (story.globalTags != null)
{
foreach (string tag in story.globalTags)
{
state.GlobalTags.Add(tag);
}
}
return state;
}
public int LoadStories()
{
var files = System.IO.Directory.GetFiles(storyPath, "*.json");
int loadedFiles = 0;
foreach (string file in files)
{
string title = System.IO.Path.GetFileNameWithoutExtension(file);
string story = System.IO.File.ReadAllText(file);
loadedFiles +=1;
stories[title] = story;
}
return loadedFiles;
}
}
}