aboutsummaryrefslogtreecommitdiff
path: root/source/WindowsFormsApplication1/Online/Hacking/Matchmaker.cs
blob: 1fb6f1b73b5b44f1c40cdd60c25fcf6e4d6af411 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* ShiftOS Online Hacker Battles - Matchmaker
 * 
 * These classes deal with keeping things in line on the client-side of things.
 * They deal with CSP (Client-Side Prediction), sending and receiving messages to
 * and from the ShiftOS server, as well as making sure that when you join or leave a
 * lobby, the server and other clients actually KNOW you did.
 * 
 * I wouldn't mess with this unless you really, really understand what you're doing,
 * as in most cases, modification to the server is required as well (in the case of
 * adding new commands). I'd leave modification to the system creator (Michael VanOverbeek) who
 * actually wrote this. He's the guy who knows all about how the server works. Wait... why am
 * I talking in third person?
 */

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ShiftOS.Online.Hacking
{
    public class Matchmaker
    {
        //All available lobbies.
        public static List<ServerInfo> Servers = null;
        
        //All players in the lobby.
        public static List<Network> Players = null;

        //Some useful info about the lobby in which the player is.
        //Also contains server IP to be sent to Package_Grabber.SendMessage().
        public static ServerInfo SelectedServer = null;

        //Enemy network information (name, codepoints, etc)
        public static Network SelectedNetwork = null;

        //There's only one transmitter because generally the player
        //won't be interacting with the enemy playfield enough to
        //warrent a request to the server.
        public static NetListener SelectedNetworkListener = null; //Listen for updates from the opponent.
        public static NetTransmitter SelectedNetworkTransmitter = null; //Send messages to the server for enemy updates on opponent clients.
        public static NetListener PlayerListener = null; //For receiving non-CSP updates from the server.
        public static NetTransmitter SecondaryTransmitter = null; //For sending CSP-created update requests to the opponent (enemy health damage, etc)

        //Timer that'll run during matchmaking.
        public static Timer MakerTimer = null;

        /// <summary>
        /// This either starts matchmaking or grabs server info. Try it out I guess.
        /// 
        /// Fires: Matchmaker.Initiated
        /// </summary>
        public static void Initiate()
        {
            MakerTimer = new Timer();
            MakerTimer.Interval = 100;

            Servers = new List<ServerInfo>();
            foreach(var c in Package_Grabber.clients)
            {
                c.Value.OnReceived += (o, e) =>
                {
                    try
                    {
                        var om = (e.Data.Object as ObjectModel);
                        if (om.Command == "server_info")
                        {
                            var si = JsonConvert.DeserializeObject<ServerInfo>(om.OptionalObject as string);
                            si.IPAddress = c.Value.RemoteHost;
                            Servers.Add(si);
                            invoke(() =>
                            {
                                Initiated?.Invoke(null, new EventArgs());
                            });
                        }
                    }
                    catch
                    {

                    }
                };
                Package_Grabber.SendMessage(c.Value.RemoteHost, "get_info");
            }
        }

        /// <summary>
        /// Matchmake in the supplied lobby.
        /// 
        /// Fires: MorePlayersFound upon player leave/join.
        /// </summary>
        /// <param name="si">The server to matchmake in.</param>
        public static void Matchmake(ServerInfo si)
        {
            SelectedServer = si;
            var rnd = new Random();
            Players = new List<Network>();
            var server = Package_Grabber.clients[si.IPAddress];
            server.OnReceived += (o, e) =>
            {
                try
                {
                    var om = e.Data.Object as ObjectModel;
                    if (om.Command == "matchmaking")
                    {
                        Players = JsonConvert.DeserializeObject<List<Network>>(om.OptionalObject as string);
                        invoke(() =>
                        {
                            MorePlayersFound?.Invoke(null, new EventArgs());
                        });
                    }
                }
                catch
                {

                }
            };
            Package_Grabber.SendMessage(si.IPAddress, "get_matchmaking");
            int index = 0;
            MakerTimer.Tick += (o, e) =>
            {
                try
                {
                    if (Players[index].Name != API.CurrentSave.MyOnlineNetwork.Name && Players[index].Name != null)
                    {
                        SelectedNetwork = Players[index];
                        MakerTimer.Stop();
                        PlayerListener = new NetListener(si, SelectedNetwork);
                        SecondaryTransmitter = new NetTransmitter(si, API.CurrentSave.MyOnlineNetwork);
                        SelectedNetworkListener = new NetListener(si, API.CurrentSave.MyOnlineNetwork);
                        SelectedNetworkTransmitter = new NetTransmitter(si, SelectedNetwork);
                        var h = new HackUI(SelectedNetworkTransmitter, SelectedNetworkListener, PlayerListener, SecondaryTransmitter);
                        h.Show();
                        Package_Grabber.SendMessage(SelectedServer.IPAddress, $"leave_lobby {JsonConvert.SerializeObject(API.CurrentSave.MyOnlineNetwork)}");
                    }
                    else
                    {
                        index += 1;
                    }
                }
                catch
                {
                }
            };
            MakerTimer.Interval = 50;
            MakerTimer.Start();
        }


        /// <summary>
        /// Fired when Initiate() finishes.
        /// </summary>
        public static event EventHandler Initiated;

        /// <summary>
        /// Fired when someone enters/exits a lobby that we are in.
        /// </summary>
        public static event EventHandler MorePlayersFound;

        /// <summary>
        /// Helper method to allow me to invoke some code on the ShiftOS desktop thread (for UI access)
        /// </summary>
        /// <param name="method">The code to invoke (use a lambda expression or just pump a void through.)</param>
        public static void invoke(Action method)
        {
            API.CurrentSession.Invoke(method);
        }

        internal static void DestroySession()
        {
            Servers.Clear();
            Players.Clear();
            ClearEvents();
            SelectedNetwork = null;
            SelectedNetworkTransmitter = null;
            SelectedNetworkListener = null;
            PlayerListener = null;
            //Good to go, I guess.
        }

        public static void ClearEvents()
        {
            Initiated = null;
            MorePlayersFound = null;
        }
    }

    public class NetListener
    {
        public NetListener(ServerInfo si, Network net)
        {
            register_events(si, net);
            
        }

        public List<Module> MyModules = null;

        private void register_events(ServerInfo si, Network net)
        {
            MyModules = new List<Module>();
            var server = Package_Grabber.clients[si.IPAddress];
            server.OnReceived += (o, e) =>
            {
                if (e.Data.Object is ObjectModel)
                {
                    
                    var data = (e.Data.Object as ObjectModel);
                    if (data.Command != null)
                    {
                        string[] args = data.Command.Split(' ');
                        if ((data.OptionalObject as Network)?.Name == net.Name)
                        {
                            switch (args[0].ToLower())
                            {
                                case "set_health":
                                    string hn = args[1];
                                    int hp = Convert.ToInt32(args[2]);
                                    invoke(() => { ModuleHealthSet?.Invoke(this, new Events.Health { host_name = hn, health = hp }); });
                                    break;
                                case "place_module":
                                    string hostname = args[1];
                                    int grade = Convert.ToInt32(args[2]);
                                    int newhp = Convert.ToInt32(args[3]);
                                    int x = Convert.ToInt32(args[4]);
                                    int y = Convert.ToInt32(args[5]);
                                    int type = Convert.ToInt32(args[6]);
                                    var moduleToPlace = new Module { Grade = grade, Hostname = hostname, HP = newhp, Type = type, X = x, Y = y };
                                    MyModules.Add(moduleToPlace);
                                    invoke(() => { ModulePlaced?.Invoke(this, new Events.ModulePlaced { new_module = moduleToPlace }); });
                                    break;
                                case "remove_module":
                                    string hostnametoremove = args[1];
                                    var m = new Module();
                                    foreach (var mod in MyModules)
                                    {
                                        if (mod.Hostname == hostnametoremove)
                                        {
                                            m = mod;
                                        }
                                    }
                                    MyModules.Remove(m);

                                    invoke(() => { ModuleRemoved?.Invoke(this, new Events.ModuleRemoved { new_module = hostnametoremove }); });
                                    break;
                                case "upgrade":
                                    invoke(() =>
                                    {
                                        string hostnametoupgrade = args[1];
                                        int newgrade = Convert.ToInt32(args[2]);
                                        ModuleUpgraded?.Invoke(this, new Events.ModuleUpgraded { hostname = hostnametoupgrade, grade = newgrade });
                                    });
                                    break;
                                case "finish":
                                    string json = data.Command.Remove(0, 7);
                                    var winner = JsonConvert.DeserializeObject<Network>(json);
                                    Won?.Invoke(this, new Events.Won(winner));
                                    break;
                                case "disable":
                                    invoke(() =>
                                    {
                                        string name = args[1];
                                        ModuleDisabled?.Invoke(this, new Events.Disabled { hostName = name });
                                    });
                                    break;
                            }
                        }
                    }
                }
            };
        }

        public void invoke(Action method)
        {
            API.CurrentSession.Invoke(method);
        }

        public event EventHandler<Events.Health> ModuleHealthSet;
        public event EventHandler<Events.ModulePlaced> ModulePlaced;
        public event EventHandler<Events.ModuleRemoved> ModuleRemoved;
        public event EventHandler<Events.ModuleUpgraded> ModuleUpgraded;
        public event EventHandler<Events.Disabled> ModuleDisabled;
        public event EventHandler<Events.Won> Won;
    }

    public class NetTransmitter
    {
        public ServerInfo serverInfo = null;
        public Network EnemyIdent = null;

        public NetTransmitter(ServerInfo si, Network enemy)
        {
            EnemyIdent = enemy;
            serverInfo = si;
            //HackUI will handle everything else to do with our network.
        }

        public void send_message(Messages msg, object value)
        {
            switch(msg)
            {
                case Messages.PlaceModule:
                    var m = value as Module;
                    Package_Grabber.SendMessage(serverInfo.IPAddress, $"place_module {m.Hostname} {m.Grade} {m.HP} {m.X} {m.Y} {m.Type}", EnemyIdent);
                    break;
                case Messages.Upgrade:
                    string upgradestr = value as string;
                    Package_Grabber.SendMessage(serverInfo.IPAddress, $"upgrade {upgradestr}", EnemyIdent);
                    break;
                case Messages.RemoveModule:
                    string hostnametoremove = value as string;
                    Package_Grabber.SendMessage(serverInfo.IPAddress, $"remove_module {hostnametoremove}", EnemyIdent);
                    break;
                case Messages.SetHealth:
                    string healthsetstr = value as string;
                    Package_Grabber.SendMessage(serverInfo.IPAddress, $"set_health {healthsetstr}", EnemyIdent);
                    break;
                case Messages.FinishBattle:
                    string json = JsonConvert.SerializeObject(value as Network);
                    Package_Grabber.SendMessage(serverInfo.IPAddress, $"finish {json}");
                    break;
                case Messages.Disabled:
                    string hnamestr = value as string;
                    Package_Grabber.SendMessage(serverInfo.IPAddress, $"disable {hnamestr}", EnemyIdent);
                    break;
            }
        }

        public enum Messages
        {
            PlaceModule,
            Upgrade,
            RemoveModule,
            SetHealth,
            Disabled,
            FinishBattle,
        }
    }

    namespace Events
    {
        public class Health : EventArgs
        {
            public string host_name { get; set; }
            public int health { get; set; }
        }

        public class Disabled : EventArgs
        {
            public string hostName { get; set; }
        }

        public class Won : EventArgs
        {
            public Network Winner { get; private set; }

            public Won(Network winner)
            {
                Winner = winner;
            }
        }

        public class ModulePlaced : EventArgs
        {
            public Module new_module { get; set; }
        }

        public class ModuleRemoved : EventArgs
        {
            public string new_module { get; set; }
        }

        public class ModuleUpgraded : EventArgs
        {
            public string hostname { get; set; }
            public int grade { get; set; }
        }



    }
}