aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Server/SaveManager.cs
blob: d52d32a3d8c09110fafb93f639aeb692c4b98cf4 (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiftOS.Objects;
using System.IO;
using Newtonsoft.Json;
using NetSockets;
using static ShiftOS.Server.Program;

namespace ShiftOS.Server
{
    public static class SaveManager
    {
        [MudRequest("usr_getcp", typeof(Dictionary<string, object>))]
        public static void GetCodepoints(string guid, object contents)
        {
            var args = contents as Dictionary<string, object>;
            if (!args.ContainsKey("username"))
                throw new MudException("No 'username' argument supplied.");

            foreach(var savefile in Directory.GetFiles("saves"))
            {
                var save = ReadSave(savefile);
                if(save.Username == args["username"] as string)
                {
                    Program.ClientDispatcher.DispatchTo("usr_codepoints", guid, save.Codepoints);
                    return;
                }
            }

            throw new MudException("User " + args["username"] as string + " not found on this multi-user domain.");
            
        }

        [MudRequest("mud_login", typeof(Dictionary<string, object>))]
        public static void UserLogin(string guid, object contents)
        {
            var args = contents as Dictionary<string, object>;
            if (args["username"] != null && args["password"] != null)
            {
                foreach (var savefile in Directory.GetFiles("saves"))
                {
                    try
                    {
                        var save = JsonConvert.DeserializeObject<Save>(ReadEncFile(savefile));


                        if (save.Username == args["username"].ToString() && save.Password == args["password"].ToString())
                        {

                            Program.server.DispatchTo(new Guid(guid), new NetObject("mud_savefile", new ServerMessage
                            {
                                Name = "mud_savefile",
                                GUID = "server",
                                Contents = JsonConvert.SerializeObject(save)
                            }));
                            return;
                        }
                    }
                    catch { }
                }
                Program.server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage
                {
                    Name = "mud_login_denied",
                    GUID = "server"
                }));
            }
            else
            {
                Program.server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage
                {
                    Name = "mud_login_denied",
                    GUID = "server"
                }));
            }

        }

        [MudRequest("mud_checkuserexists", typeof(Dictionary<string, object>))]
        public static void CheckUserExists(string guid, object contents)
        {
            var args = contents as Dictionary<string, object>;
            if (args["username"] != null && args["password"] != null)
            {
                foreach (var savefile in Directory.GetFiles("saves"))
                {
                    try
                    {
                        var save = JsonConvert.DeserializeObject<Save>(ReadEncFile(savefile));


                        if (save.Username == args["username"].ToString() && save.Password == args["password"].ToString())
                        {
                            server.DispatchTo(new Guid(guid), new NetObject("mud_savefile", new ServerMessage
                            {
                                Name = "mud_found",
                                GUID = "server",
                            }));
                            return;
                        }
                    }
                    catch { }
                }
                server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage
                {
                    Name = "mud_notfound",
                    GUID = "server"
                }));
            }
            else
            {
                server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage
                {
                    Name = "mud_notfound",
                    GUID = "server"
                }));
            }

        }

        [MudRequest("mud_save", typeof(Save))]
        public static void SaveGame(string guid, object contents)
        {
            var sav = contents as Save;

            WriteEncFile("saves/" + sav.Username + ".save", JsonConvert.SerializeObject(sav, Formatting.Indented));


            try
            {
                Program.server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage
                {
                    Name = "mud_saved",
                    GUID = "server"
                }));
            }
            catch { }
        }

        [MudRequest("usr_givecp", typeof(Dictionary<string, object>))]
        public static void GiveCodepoints(string guid, object contents)
        {
            var args = contents as Dictionary<string, object>;
            if (args["username"] != null && args["amount"] != null)
            {
                string userName = args["username"] as string;
                long cpAmount = (long)args["amount"];

                if (Directory.Exists("saves"))
                {
                    foreach (var saveFile in Directory.GetFiles("saves"))
                    {
                        var saveFileContents = JsonConvert.DeserializeObject<Save>(ReadEncFile(saveFile));
                        if (saveFileContents.Username == userName)
                        {
                            saveFileContents.Codepoints += cpAmount;
                            WriteEncFile(saveFile, JsonConvert.SerializeObject(saveFileContents, Formatting.Indented));
                            Program.ClientDispatcher.Broadcast("update_your_cp", new
                            {
                                username = userName,
                                amount = cpAmount
                            });

                            return;
                        }
                    }
                }
            }

        }

        [MudRequest("usr_takecp", typeof(Dictionary<string, object>))]
        public static void TakeCodepoints(string guid, object contents)
        {
            var args = contents as Dictionary<string, object>;
            if (args["username"] != null && args["password"] != null && args["amount"] != null && args["yourusername"] != null)
            {
                string userName = args["username"] as string;
                string passw = args["password"] as string;
                int cpAmount = (int)args["amount"];

                if (Directory.Exists("saves"))
                {
                    foreach (var saveFile in Directory.GetFiles("saves"))
                    {
                        var saveFileContents = JsonConvert.DeserializeObject<Save>(ReadEncFile(saveFile));
                        if (saveFileContents.Username == userName && saveFileContents.Password == passw)
                        {
                            saveFileContents.Codepoints += cpAmount;
                            WriteEncFile(saveFile, JsonConvert.SerializeObject(saveFileContents, Formatting.Indented));
                            Program.ClientDispatcher.Broadcast("update_your_cp", new {
                                username = userName,
                                amount = -cpAmount
                            });
                            Program.ClientDispatcher.DispatchTo("update_your_cp", guid, new
                            {
                                username = args["yourusername"].ToString(),
                                amount = cpAmount
                            });
                            return;
                        }
                    }
                }
            }

        }

        private static Save ReadSave(string fPath)
        {
            return Newtonsoft.Json.JsonConvert.DeserializeObject<Save>(ReadEncFile(fPath));
        }


        private static string ReadEncFile(string fPath)
        {
            return Encryption.Decrypt(File.ReadAllText(fPath));
        }

        private static void WriteEncFile(string fPath, string contents)
        {
            File.WriteAllText(fPath, Encryption.Encrypt(contents));
        }

    }
}