From 309f9fe361af7b0a508770951af7c548dfca78ad Mon Sep 17 00:00:00 2001 From: MichaelTheShifter Date: Sat, 25 Jun 2016 21:22:46 -0400 Subject: [PATCH] Added online Hacker Battles with matchmaking system It's untested, but in theory it'll all work perfectly. --- source/WindowsFormsApplication1/API.cs | 4 - .../Apps/NetworkBrowser.Designer.cs | 133 +++++-- .../Apps/NetworkBrowser.cs | 193 +++++++--- .../Apps/NetworkBrowser.resx | 7 + .../Controls/Computer.cs | 5 + .../Engine/Lua_Interp.cs | 12 - .../Engine/SaveSystem.cs | 1 + .../Gameplay/HackUI.cs | 90 ++++- .../Online/Hacking/Matchmaker.cs | 260 +++++++++++++ .../Online/Hacking/Objects.cs | 37 ++ .../WindowsFormsApplication1/ShiftOS.csproj | 362 ++++++++---------- 11 files changed, 796 insertions(+), 308 deletions(-) create mode 100644 source/WindowsFormsApplication1/Online/Hacking/Matchmaker.cs create mode 100644 source/WindowsFormsApplication1/Online/Hacking/Objects.cs diff --git a/source/WindowsFormsApplication1/API.cs b/source/WindowsFormsApplication1/API.cs index fcf03a1..5ab9631 100644 --- a/source/WindowsFormsApplication1/API.cs +++ b/source/WindowsFormsApplication1/API.cs @@ -367,10 +367,6 @@ public static void LaunchMod(string modSAA) { CreateForm(new Labyrinth(), "Labyrinth", null); } - else if (File.ReadAllText(modSAA) == HiddenQuickChatCommand) - { - CreateForm(new QuickChat(), "QuickChat", null); - } else { try diff --git a/source/WindowsFormsApplication1/Apps/NetworkBrowser.Designer.cs b/source/WindowsFormsApplication1/Apps/NetworkBrowser.Designer.cs index cf9468d..2b79b6d 100644 --- a/source/WindowsFormsApplication1/Apps/NetworkBrowser.Designer.cs +++ b/source/WindowsFormsApplication1/Apps/NetworkBrowser.Designer.cs @@ -29,10 +29,12 @@ protected override void Dispose(bool disposing) private void InitializeComponent() { this.components = new System.ComponentModel.Container(); + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(NetworkBrowser)); this.panel1 = new System.Windows.Forms.Panel(); this.label3 = new System.Windows.Forms.Label(); this.btnscreen = new System.Windows.Forms.Button(); this.pnlmynet = new System.Windows.Forms.Panel(); + this.pgtotalhealth = new ShiftOS.ProgressBarEX(); this.flmodules = new System.Windows.Forms.FlowLayoutPanel(); this.label5 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); @@ -44,15 +46,23 @@ private void InitializeComponent() this.panel2 = new System.Windows.Forms.Panel(); this.lbnetdesc = new System.Windows.Forms.Label(); this.lbtitle = new System.Windows.Forms.Label(); - this.pgtotalhealth = new ShiftOS.ProgressBarEX(); this.tmrcalctotal = new System.Windows.Forms.Timer(this.components); + this.button1 = new System.Windows.Forms.Button(); + this.pnlonline = new System.Windows.Forms.Panel(); + this.lbonlinedesc = new System.Windows.Forms.Label(); + this.lbonlineheader = new System.Windows.Forms.Label(); + this.lbonlineservers = new System.Windows.Forms.ListBox(); + this.btnjoinlobby = new System.Windows.Forms.Button(); this.panel1.SuspendLayout(); this.pnlmynet.SuspendLayout(); this.panel2.SuspendLayout(); + this.pnlonline.SuspendLayout(); this.SuspendLayout(); // // panel1 // + this.panel1.Controls.Add(this.pnlonline); + this.panel1.Controls.Add(this.button1); this.panel1.Controls.Add(this.label3); this.panel1.Controls.Add(this.btnscreen); this.panel1.Controls.Add(this.pnlmynet); @@ -104,6 +114,28 @@ private void InitializeComponent() this.pnlmynet.Size = new System.Drawing.Size(404, 447); this.pnlmynet.TabIndex = 6; // + // pgtotalhealth + // + this.pgtotalhealth.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.pgtotalhealth.BackColor = System.Drawing.Color.Black; + this.pgtotalhealth.BlockSeparation = 3; + this.pgtotalhealth.BlockWidth = 5; + this.pgtotalhealth.Color = System.Drawing.Color.Gray; + this.pgtotalhealth.Label = "Progress:"; + this.pgtotalhealth.Location = new System.Drawing.Point(7, 98); + this.pgtotalhealth.MaxValue = 100; + this.pgtotalhealth.MinValue = 0; + this.pgtotalhealth.Name = "pgtotalhealth"; + this.pgtotalhealth.Orientation = ShiftOS.ProgressBarEX.ProgressBarOrientation.Horizontal; + this.pgtotalhealth.ShowLabel = false; + this.pgtotalhealth.ShowValue = true; + this.pgtotalhealth.Size = new System.Drawing.Size(394, 32); + this.pgtotalhealth.Step = 10; + this.pgtotalhealth.Style = ShiftOS.ProgressBarEX.ProgressBarExStyle.Continuous; + this.pgtotalhealth.TabIndex = 4; + this.pgtotalhealth.Value = 0; + // // flmodules // this.flmodules.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) @@ -209,7 +241,7 @@ private void InitializeComponent() this.lbnetdesc.Name = "lbnetdesc"; this.lbnetdesc.Size = new System.Drawing.Size(283, 290); this.lbnetdesc.TabIndex = 1; - this.lbnetdesc.Text = "No network selected."; + this.lbnetdesc.Text = resources.GetString("lbnetdesc.Text"); // // lbtitle // @@ -221,31 +253,80 @@ private void InitializeComponent() this.lbtitle.TabIndex = 0; this.lbtitle.Text = "Network Browser"; // - // pgtotalhealth - // - this.pgtotalhealth.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.pgtotalhealth.BackColor = System.Drawing.Color.Black; - this.pgtotalhealth.BlockSeparation = 3; - this.pgtotalhealth.BlockWidth = 5; - this.pgtotalhealth.Color = System.Drawing.Color.Gray; - this.pgtotalhealth.Location = new System.Drawing.Point(7, 98); - this.pgtotalhealth.MaxValue = 100; - this.pgtotalhealth.MinValue = 0; - this.pgtotalhealth.Name = "pgtotalhealth"; - this.pgtotalhealth.Orientation = ShiftOS.ProgressBarEX.ProgressBarOrientation.Horizontal; - this.pgtotalhealth.ShowValue = true; - this.pgtotalhealth.Size = new System.Drawing.Size(394, 32); - this.pgtotalhealth.Step = 10; - this.pgtotalhealth.Style = ShiftOS.ProgressBarEX.ProgressBarExStyle.Continuous; - this.pgtotalhealth.TabIndex = 4; - this.pgtotalhealth.Value = 0; - // // tmrcalctotal // this.tmrcalctotal.Enabled = true; this.tmrcalctotal.Tick += new System.EventHandler(this.tmrcalctotal_Tick); // + // button1 + // + this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.button1.Location = new System.Drawing.Point(549, 465); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(83, 23); + this.button1.TabIndex = 9; + this.button1.Text = "Battle online!"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // pnlonline + // + this.pnlonline.Controls.Add(this.btnjoinlobby); + this.pnlonline.Controls.Add(this.lbonlineservers); + this.pnlonline.Controls.Add(this.lbonlinedesc); + this.pnlonline.Controls.Add(this.lbonlineheader); + this.pnlonline.Location = new System.Drawing.Point(12, 12); + this.pnlonline.Name = "pnlonline"; + this.pnlonline.Size = new System.Drawing.Size(404, 447); + this.pnlonline.TabIndex = 10; + // + // lbonlinedesc + // + this.lbonlinedesc.Location = new System.Drawing.Point(4, 37); + this.lbonlinedesc.Name = "lbonlinedesc"; + this.lbonlinedesc.Size = new System.Drawing.Size(386, 364); + this.lbonlinedesc.TabIndex = 3; + this.lbonlinedesc.Text = "You can battle other Shifters over the Internet by joining a lobby and waiting fo" + + "r a match to be made. Please select a server from the list below."; + // + // lbonlineheader + // + this.lbonlineheader.AutoSize = true; + this.lbonlineheader.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F); + this.lbonlineheader.Location = new System.Drawing.Point(3, 5); + this.lbonlineheader.Name = "lbonlineheader"; + this.lbonlineheader.Size = new System.Drawing.Size(106, 20); + this.lbonlineheader.TabIndex = 2; + this.lbonlineheader.Text = "Online battles"; + // + // lbonlineservers + // + this.lbonlineservers.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.lbonlineservers.BackColor = System.Drawing.Color.Black; + this.lbonlineservers.ForeColor = System.Drawing.Color.White; + this.lbonlineservers.FormattingEnabled = true; + this.lbonlineservers.Location = new System.Drawing.Point(1, 72); + this.lbonlineservers.Name = "lbonlineservers"; + this.lbonlineservers.Size = new System.Drawing.Size(389, 329); + this.lbonlineservers.TabIndex = 4; + this.lbonlineservers.SelectedIndexChanged += new System.EventHandler(this.lbonlineservers_SelectedIndexChanged); + // + // btnjoinlobby + // + this.btnjoinlobby.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.btnjoinlobby.Enabled = false; + this.btnjoinlobby.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.btnjoinlobby.Location = new System.Drawing.Point(306, 415); + this.btnjoinlobby.Name = "btnjoinlobby"; + this.btnjoinlobby.Size = new System.Drawing.Size(84, 23); + this.btnjoinlobby.TabIndex = 8; + this.btnjoinlobby.Text = "Join Lobby"; + this.btnjoinlobby.UseVisualStyleBackColor = true; + this.btnjoinlobby.Click += new System.EventHandler(this.btnjoinlobby_Click); + // // NetworkBrowser // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -263,6 +344,8 @@ private void InitializeComponent() this.pnlmynet.PerformLayout(); this.panel2.ResumeLayout(false); this.panel2.PerformLayout(); + this.pnlonline.ResumeLayout(false); + this.pnlonline.PerformLayout(); this.ResumeLayout(false); } @@ -286,5 +369,11 @@ private void InitializeComponent() private System.Windows.Forms.Label label4; private ProgressBarEX pgtotalhealth; private System.Windows.Forms.Timer tmrcalctotal; + private System.Windows.Forms.Panel pnlonline; + private System.Windows.Forms.Label lbonlinedesc; + private System.Windows.Forms.Label lbonlineheader; + private System.Windows.Forms.Button button1; + private System.Windows.Forms.ListBox lbonlineservers; + private System.Windows.Forms.Button btnjoinlobby; } } \ No newline at end of file diff --git a/source/WindowsFormsApplication1/Apps/NetworkBrowser.cs b/source/WindowsFormsApplication1/Apps/NetworkBrowser.cs index 127140f..e99e9a6 100644 --- a/source/WindowsFormsApplication1/Apps/NetworkBrowser.cs +++ b/source/WindowsFormsApplication1/Apps/NetworkBrowser.cs @@ -23,6 +23,36 @@ public NetworkBrowser() private void NetworkBrowser_Load(object sender, EventArgs e) { + Online.Hacking.Matchmaker.Initiated += (o, a) => + { + if (selected_server == null) + { + lbonlineservers.Items.Clear(); + foreach (var s in Online.Hacking.Matchmaker.Servers) + { + lbonlineservers.Items.Add(s.ServerName); + } + } + }; + Online.Hacking.Matchmaker.MorePlayersFound += (o, a) => + { + if(selected_server != null) + { + lbonlineheader.Text = selected_server.ServerName + " - Lobby"; + lbonlinedesc.Text = $@"Network Browser is waiting for the server to pair you with another network... + + +Players in lobby: {Online.Hacking.Matchmaker.Players.Count} + +My Network: + - {Hacking.MyNetwork.Count} available modules + - {API.Codepoints} codepoints + - {API.CurrentSave.MyOnlineNetwork.Wins} wins, {API.CurrentSave.MyOnlineNetwork.Losses} losses. + +You can set your name and description in the Network Status page."; + + } + }; LoadNetworks(); SetupSidePane(); pnlmynet.Hide(); @@ -107,16 +137,29 @@ public void SetupSidePane() public void SetupUI(List tier) { - if (tier.Count > 0) + pnlonline.Visible = online_mode; + pnlonline.BringToFront(); + btnjoinlobby.Enabled = (selected_server != null); + if (online_mode) { - foreach (var t in tier) + if(Online.Hacking.Matchmaker.Servers == null) { - lbnets.Items.Add(t); + Online.Hacking.Matchmaker.Initiate(); } } else { - AddLeader(CurrentTier); + if (tier.Count > 0) + { + foreach (var t in tier) + { + lbnets.Items.Add(t); + } + } + else + { + AddLeader(CurrentTier); + } } } @@ -197,61 +240,68 @@ private void btnstartbattle_Click(object sender, EventArgs e) } else { - string tier_upgrade = null; - if (SelectedNet.IsLeader == true) + if (online_mode == false) { - tier_upgrade = "nb_tier_" + CurrentTier; - } - var hui = new HackUI(SelectedNet); - hui.OnWin += (object s, EventArgs a) => - { - if (tier_upgrade != null) + string tier_upgrade = null; + if (SelectedNet.IsLeader == true) { - API.Upgrades[tier_upgrade] = true; - if(CurrentTier == "easy") - { - if(API.Upgrades["midgamebridge"] == false) - { - var term = new Terminal(); - API.CreateForm(term, API.LoadedNames.TerminalName, API.GetIcon("Terminal")); - term.StartDanaRossStory(); - } - } - else if(CurrentTier == "medium") - { - var h = new HoloChat(); - var fakeroom = new FakeChatClient(); - fakeroom.Name = "The Hacker Alliance"; - fakeroom.Topic = "The Hacker Alliance - We are the masters. DevX cannot control us."; - fakeroom.OtherCharacters = new List(new [] {"Richard Ladouceur"}); - fakeroom.Messages = JsonConvert.DeserializeObject>(Properties.Resources.AustinWalkerCompletionStory); - API.CreateForm(h, "QuickChat", API.GetIcon("QuickChat")); - var t = new Thread(new ThreadStart(new Action(() => - { - Thread.Sleep(200); - h.Invoke(new Action(() => - { - h.SetupFakeClient(fakeroom); - })); - }))); - - } - foreach(var mod in SelectedNet.Network) - { - if (mod.Type != SystemType.Core) - { - mod.HP = 0; - Hacking.MyNetwork.Add(mod); - } - } - SetupMyNet(); + tier_upgrade = "nb_tier_" + CurrentTier; } - API.CurrentSave.CompletedNets.Add(SelectedNet.Name); - SelectedNet = null; - LoadNetworks(); - SetupSidePane(); - }; - hui.Show(); + var hui = new HackUI(SelectedNet); + hui.OnWin += (object s, EventArgs a) => + { + if (tier_upgrade != null) + { + API.Upgrades[tier_upgrade] = true; + if (CurrentTier == "easy") + { + if (API.Upgrades["midgamebridge"] == false) + { + var term = new Terminal(); + API.CreateForm(term, API.LoadedNames.TerminalName, API.GetIcon("Terminal")); + term.StartDanaRossStory(); + } + } + else if (CurrentTier == "medium") + { + var h = new HoloChat(); + var fakeroom = new FakeChatClient(); + fakeroom.Name = "The Hacker Alliance"; + fakeroom.Topic = "The Hacker Alliance - We are the masters. DevX cannot control us."; + fakeroom.OtherCharacters = new List(new[] { "Richard Ladouceur" }); + fakeroom.Messages = JsonConvert.DeserializeObject>(Properties.Resources.AustinWalkerCompletionStory); + API.CreateForm(h, "QuickChat", API.GetIcon("QuickChat")); + var t = new Thread(new ThreadStart(new Action(() => + { + Thread.Sleep(200); + h.Invoke(new Action(() => + { + h.SetupFakeClient(fakeroom); + })); + }))); + + } + foreach (var mod in SelectedNet.Network) + { + if (mod.Type != SystemType.Core) + { + mod.HP = 0; + Hacking.MyNetwork.Add(mod); + } + } + SetupMyNet(); + } + API.CurrentSave.CompletedNets.Add(SelectedNet.Name); + SelectedNet = null; + LoadNetworks(); + SetupSidePane(); + }; + hui.Show(); + } + else + { + //nyi + } } } @@ -297,5 +347,36 @@ private void tmrcalctotal_Tick(object sender, EventArgs e) pgtotalhealth.MaxValue = total; pgtotalhealth.Value = hp; } + + bool online_mode = false; + + private void button1_Click(object sender, EventArgs e) + { + online_mode = !online_mode; + LoadNetworks(); + } + + Online.Hacking.ServerInfo selected_server = null; + + private void lbonlineservers_SelectedIndexChanged(object sender, EventArgs e) + { + var server_name = lbonlineservers.SelectedItem as string; + var server = new Online.Hacking.ServerInfo(); + foreach(var srv in Online.Hacking.Matchmaker.Servers) + { + if (srv.ServerName == server_name) + server = srv; + } + selected_server = server; + LoadNetworks(); + } + + private void btnjoinlobby_Click(object sender, EventArgs e) + { + lbonlineservers.Hide(); + API.CurrentSave.MyOnlineNetwork.Codepoints = API.Codepoints; + Package_Grabber.SendMessage(selected_server.IPAddress, "join_lobby", API.CurrentSave.MyOnlineNetwork); + Online.Hacking.Matchmaker.Matchmake(selected_server); + } } } diff --git a/source/WindowsFormsApplication1/Apps/NetworkBrowser.resx b/source/WindowsFormsApplication1/Apps/NetworkBrowser.resx index fa83c0a..e5cb9b1 100644 --- a/source/WindowsFormsApplication1/Apps/NetworkBrowser.resx +++ b/source/WindowsFormsApplication1/Apps/NetworkBrowser.resx @@ -117,6 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + Welcome to the Network Browser. Here, you can choose various enemy networks to battle and conquer. On the left is the network selection panel, and it will let you choose what network you'd like to fight. When you beat a network, it's removed from the list. + +Below the network selection panel is a Tier button. You can click it to change the difficulty of networks that you will fight. The higher the tier, the harder the battle. Beside that button, is the Screen button. Clicking it will switch between the Network Selection panel and your Network Status panel which will show you the health of all your modules if they are below max. + +You can start the battle using the "Start Battle" button, and you can join an online session using the "Battle online" button. It is wise to practice offline, as offline networks are more predictable than online ones - and you require more speed to battle against another real player. But, this is ShiftOS. Do what you wish. + 17, 17 diff --git a/source/WindowsFormsApplication1/Controls/Computer.cs b/source/WindowsFormsApplication1/Controls/Computer.cs index 8f849d3..1d287f8 100644 --- a/source/WindowsFormsApplication1/Controls/Computer.cs +++ b/source/WindowsFormsApplication1/Controls/Computer.cs @@ -14,6 +14,8 @@ public partial class Computer : UserControl { public int TotalHP = 100; + public Online.Hacking.NetTransmitter Transmitter = null; + public Computer() { InitializeComponent(); @@ -24,6 +26,7 @@ public Computer() public void Repair(int hp) { this._HP += hp; + Transmitter?.send_message(Online.Hacking.NetTransmitter.Messages.SetHealth, $"{Hostname} {_HP}"); var h = OnRepair; if(h != null) { @@ -175,6 +178,7 @@ public void Deteriorate(int amount) } else { this._HP -= amount / DamageDefector; + Transmitter?.send_message(Online.Hacking.NetTransmitter.Messages.SetHealth, $"{Hostname} {_HP}"); EventHandler handler = HP_Decreased; if (handler != null) { @@ -187,6 +191,7 @@ public void Deteriorate(int amount) public void Disable() { + Transmitter?.send_message(Online.Hacking.NetTransmitter.Messages.Disabled, $"{Hostname}"); var t = new Timer(); t.Interval = 1000; int i = 0; diff --git a/source/WindowsFormsApplication1/Engine/Lua_Interp.cs b/source/WindowsFormsApplication1/Engine/Lua_Interp.cs index ab87fa7..c4a9162 100644 --- a/source/WindowsFormsApplication1/Engine/Lua_Interp.cs +++ b/source/WindowsFormsApplication1/Engine/Lua_Interp.cs @@ -249,18 +249,6 @@ public void RegisterCore() return new Color(); } }); - mod.speechrec_create = new Func(() => - { - return new SpeechListener(); - }); - mod.speechrec_on_recognize = new Action((obj, func) => - { - obj.OnRecognize += (object s, EventArgs a) => - { - mod($"{func}('{s.ToString()}')"); - }; - obj.Engine.RecognizeAsync(); - }); mod.get_desktop_session = new Func
(() => { return API.CurrentSession; diff --git a/source/WindowsFormsApplication1/Engine/SaveSystem.cs b/source/WindowsFormsApplication1/Engine/SaveSystem.cs index aab5ab2..9381d9b 100644 --- a/source/WindowsFormsApplication1/Engine/SaveSystem.cs +++ b/source/WindowsFormsApplication1/Engine/SaveSystem.cs @@ -34,6 +34,7 @@ public class Save public int PriceDivider = 1; public string CloudID = ""; public List CompletedNets = new List(); + public ShiftOS.Online.Hacking.Network MyOnlineNetwork = new ShiftOS.Online.Hacking.Network(); } public class PrivateBitnoteAddress diff --git a/source/WindowsFormsApplication1/Gameplay/HackUI.cs b/source/WindowsFormsApplication1/Gameplay/HackUI.cs index ee36b8f..2d17596 100644 --- a/source/WindowsFormsApplication1/Gameplay/HackUI.cs +++ b/source/WindowsFormsApplication1/Gameplay/HackUI.cs @@ -22,12 +22,24 @@ public HackUI() public event EventHandler OnWin; + private bool InOnlineBattle = false; + private Online.Hacking.NetTransmitter transmitter = null; + private Online.Hacking.NetListener receiver = null; + public HackUI(EnemyHacker enemy) { ThisEnemyHacker = enemy; InitializeComponent(); } + public HackUI(Online.Hacking.NetTransmitter t, Online.Hacking.NetListener l) + { + InOnlineBattle = true; + transmitter = t; + receiver = l; + InitializeComponent(); + } + public Computer ThisPlayerPC = null; private decimal TotalPlayerHP = 0; private EnemyHacker EnemyNet = null; @@ -212,6 +224,11 @@ private void this_Closing(object sender, FormClosingEventArgs e) public void AddModule(Computer newModule) { + if (InOnlineBattle) + { + newModule.Transmitter = transmitter; + transmitter?.send_message(Online.Hacking.NetTransmitter.Messages.PlaceModule, new Online.Hacking.Module { Grade = newModule.Grade, Hostname = newModule.Hostname, HP = newModule.HP, Type = (int)newModule.Type, X = newModule.Left, Y = newModule.Top }); + } pnlyou.Controls.Add(newModule); int hp = newModule.HP; WriteLine($"[Network] Welcome to the network, {newModule.Hostname}!"); @@ -274,6 +291,7 @@ public void AddModule(Computer newModule) AllPlayerComputers.Remove(newModule); newModule.Dispose(); WriteLine($"[Network] {newModule.Hostname} has gone OFFLINE."); + transmitter?.send_message(Online.Hacking.NetTransmitter.Messages.RemoveModule, newModule.Hostname); }; newModule.Select += (object s, EventArgs e) => { @@ -1078,9 +1096,11 @@ private void HackUI_Load(object sender, EventArgs e) Hacking.RepairTimer.Stop(); //Don't want the player to be able to repair dead modules during a battle! this.TopMost = true; this.WindowState = FormWindowState.Maximized; - //this.TopMost = true; LoadPlayerScreen(); - LoadEnemyScreen(); + if (InOnlineBattle) + LoadOnlineEnemy(); + else + LoadEnemyScreen(); tmrvisualizer.Interval = 10; tmrvisualizer.Start(); } @@ -1532,6 +1552,72 @@ public void WriteLine(string text) #endregion + #region ONLINE ENEMY + + public void LoadOnlineEnemy() + { + AllEnemyComputers = new List(); + tmrenemyhealthdetect.Start(); + //register event handlers + receiver.ModuleHealthSet += Receiver_ModuleHealthSet; + receiver.ModulePlaced += Receiver_ModulePlaced; + receiver.ModuleUpgraded += Receiver_ModuleUpgraded; + receiver.ModuleRemoved += Receiver_ModuleRemoved; + } + + private void Receiver_ModuleRemoved(object sender, Online.Hacking.Events.ModuleRemoved e) + { + Computer c = null; + foreach(var m in AllEnemyComputers) + { + if(m.Hostname == e.new_module) + { + c = m; + } + } + AllEnemyComputers.Remove(c); + c.Dispose(); + } + + private void Receiver_ModuleUpgraded(object sender, Online.Hacking.Events.ModuleUpgraded e) + { + foreach(var m in AllEnemyComputers) + { + if (m.Hostname == e.hostname) + m.Grade = e.grade; + } + } + + private void Receiver_ModulePlaced(object sender, Online.Hacking.Events.ModulePlaced e) + { + var newModule = new Module((SystemType)e.new_module.Type, e.new_module.Grade, e.new_module.Hostname); + newModule.HP = e.new_module.HP; + newModule.X = e.new_module.X; + newModule.Y = e.new_module.Y; + AddEnemyModule(newModule.Deploy()); + } + + private void Receiver_ModuleHealthSet(object sender, Online.Hacking.Events.Health e) + { + var mod = new Computer(); + foreach(var m in AllEnemyComputers) + { + if (m.Hostname == e.host_name) + mod = m; + } + int health_amount = mod.HP - e.health; + if(health_amount > 0) + { + mod.Repair(health_amount); + } + else if(health_amount < 0) + { + mod.LaunchAttack(AttackType.Virus, -health_amount); + } + } + + #endregion + bool playing = true; private void button2_Click(object sender, EventArgs e) diff --git a/source/WindowsFormsApplication1/Online/Hacking/Matchmaker.cs b/source/WindowsFormsApplication1/Online/Hacking/Matchmaker.cs new file mode 100644 index 0000000..93aa8d9 --- /dev/null +++ b/source/WindowsFormsApplication1/Online/Hacking/Matchmaker.cs @@ -0,0 +1,260 @@ +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 + { + public static List Servers = null; + public static List Players = null; + + public static ServerInfo SelectedServer = null; + public static Network SelectedNetwork = null; + public static NetListener SelectedNetworkListener = null; + public static NetTransmitter SelectedNetworkTransmitter = null; + + public static Timer MakerTimer = null; + + public static void Initiate() + { + MakerTimer = new Timer(); + MakerTimer.Interval = 100; + + Servers = new List(); + foreach(var c in Package_Grabber.clients) + { + c.Value.OnReceived += (o, e) => + { + var om = (e.Data.Object as ObjectModel); + if(om.Command == "server_info") + { + var si = om.OptionalObject as ServerInfo; + si.IPAddress = c.Value.RemoteHost; + Servers.Add(si); + invoke(() => + { + Initiated?.Invoke(null, new EventArgs()); + }); + } + }; + Package_Grabber.SendMessage(c.Value.RemoteHost, "get_info"); + } + } + + public static void Matchmake(ServerInfo si) + { + SelectedServer = si; + var rnd = new Random(); + Players = new List(); + var server = Package_Grabber.clients[si.IPAddress]; + server.OnReceived += (o, e) => + { + var om = e.Data.Object as ObjectModel; + if (om.Command == "matchmaking") + { + Players = JsonConvert.DeserializeObject>(om.OptionalObject as string); + invoke(() => + { + MorePlayersFound?.Invoke(null, new EventArgs()); + }); + } + }; + Package_Grabber.SendMessage(si.IPAddress, "get_matchmaking"); + MakerTimer.Tick += (o, e) => + { + int index = rnd.Next(0, Players.Count - 1); + if (Players[index] != API.CurrentSave.MyOnlineNetwork) + { + SelectedNetwork = Players[index]; + MakerTimer.Stop(); + SelectedNetworkListener = new NetListener(si, SelectedNetwork); + SelectedNetworkTransmitter = new NetTransmitter(si, SelectedNetwork); + } + }; + } + + + + public static event EventHandler Initiated; + public static event EventHandler MorePlayersFound; + public static void invoke(Action method) + { + API.CurrentSession.Invoke(method); + } + + } + + public class NetListener + { + public NetListener(ServerInfo si, Network net) + { + register_events(si, net); + + } + + public List MyModules = null; + + private void register_events(ServerInfo si, Network net) + { + MyModules = new List(); + var server = Package_Grabber.clients[si.IPAddress]; + server.OnReceived += (o, e) => + { + if(e.Data.Object is string) + { + string data = e.Data.Object as string; + string[] args = data.Split(' '); + if (args[1] == net.Name + ";" + net.Description) { + switch (args[0].ToLower()) + { + case "set_health": + string hn = args[2]; + int hp = Convert.ToInt32(args[3]); + invoke(() => { ModuleHealthSet?.Invoke(this, new Events.Health { host_name = hn, health = hp }); }); + break; + case "place_module": + string hostname = args[2]; + int grade = Convert.ToInt32(args[3]); + int newhp = Convert.ToInt32(args[4]); + int x = Convert.ToInt32(args[5]); + int y = Convert.ToInt32(args[6]); + int type = Convert.ToInt32(args[7]); + 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[2]; + 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[2]; + int newgrade = Convert.ToInt32(args[3]); + ModuleUpgraded?.Invoke(this, new Events.ModuleUpgraded { hostname = hostnametoupgrade, grade = newgrade }); + }); + break; + case "disable": + invoke(() => + { + string name = args[2]; + ModuleDisabled?.Invoke(this, new Events.Disabled { hostName = name }); + }); + break; + } + } + } + }; + } + + public void invoke(Action method) + { + API.CurrentSession.Invoke(method); + } + + public event EventHandler ModuleHealthSet; + public event EventHandler ModulePlaced; + public event EventHandler ModuleRemoved; + public event EventHandler ModuleUpgraded; + public event EventHandler ModuleDisabled; + } + + public class NetTransmitter + { + public ServerInfo serverInfo = null; + public string EnemyIdent = ""; + + public NetTransmitter(ServerInfo si, Network enemy) + { + EnemyIdent = enemy.Name + ";" + enemy.Description; + serverInfo = si; + var h = new HackUI(this, Matchmaker.SelectedNetworkListener); + //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 {EnemyIdent} {m.Hostname} {m.Grade} {m.HP} {m.X} {m.Y} {m.Type}"); + break; + case Messages.Upgrade: + string upgradestr = value as string; + Package_Grabber.SendMessage(serverInfo.IPAddress, $"upgrade {EnemyIdent} {upgradestr}"); + break; + case Messages.RemoveModule: + string hostnametoremove = value as string; + Package_Grabber.SendMessage(serverInfo.IPAddress, $"remove_module {EnemyIdent} {hostnametoremove}"); + break; + case Messages.SetHealth: + string healthsetstr = value as string; + Package_Grabber.SendMessage(serverInfo.IPAddress, $"set_health {EnemyIdent} {healthsetstr}"); + break; + case Messages.Disabled: + string hnamestr = value as string; + Package_Grabber.SendMessage(serverInfo.IPAddress, $"disable {EnemyIdent} {hnamestr}"); + break; + } + } + + public enum Messages + { + PlaceModule, + Upgrade, + RemoveModule, + SetHealth, + Disabled, + } + } + + 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 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; } + } + + + + } +} diff --git a/source/WindowsFormsApplication1/Online/Hacking/Objects.cs b/source/WindowsFormsApplication1/Online/Hacking/Objects.cs new file mode 100644 index 0000000..5012fcc --- /dev/null +++ b/source/WindowsFormsApplication1/Online/Hacking/Objects.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShiftOS.Online.Hacking +{ + [Serializable] + public class Module + { + public string Hostname { get; set; } + public int Grade { get; set; } + public int HP { get; set; } + public int X { get; set; } + public int Y { get; set; } + public int Type { get; set; } //will be converted by the client + } + + [Serializable] + public class Network + { + public string Name { get; set; } + public int Codepoints { get; set; } + public string Description { get; set; } + public int Losses { get; set; } + public int Wins { get; set; } + } + + [Serializable] + public class ServerInfo + { + public string ServerName { get; set; } + public int PlayersAwaitingMatch { get; set; } + public string IPAddress { get; set; } + } +} diff --git a/source/WindowsFormsApplication1/ShiftOS.csproj b/source/WindowsFormsApplication1/ShiftOS.csproj index 3380ad8..4066257 100644 --- a/source/WindowsFormsApplication1/ShiftOS.csproj +++ b/source/WindowsFormsApplication1/ShiftOS.csproj @@ -84,72 +84,66 @@ - - Form - - - AlternateDesktop.cs - - + Form - + Appscape.cs - + Form - + AppscapeUploader.cs - + Form - + Artpad.cs - - + + Form - + BitnoteConverter.cs - + Form - + BitnoteDigger.cs - + Form - + BitnoteWallet.cs - + Form - + Color_Picker.cs - + UserControl - + Computer.cs - + UserControl - + Connection.cs - + Form - + ConnectionManager.cs @@ -158,22 +152,22 @@ CreditScroller.cs - + UserControl - + DesktopIcon.cs - + Form - + Dodge.cs - + Form - + File Skimmer.cs @@ -207,142 +201,120 @@ GameSettings.cs - + Form - + Graphic_Picker.cs - - + + Form - - Hacking_Enemy.cs - - - Form - - - Hacking_YourHealth.cs - - - Form - - + HackUI.cs - + Form - + HijackScreen.cs - + Form - + HoloChat.cs - + UserControl - + IconControl.cs - + Form - + IconManager.cs - + UserControl - + ImageSelector.cs - + Form - + infobox.cs - + Form - - Jumper.cs - - - Form - - + KnowledgeInput.cs - + Form - + Labyrinth.cs - - UserControl - - - ListViewEx.cs - - - - + + + Form - + NameChanger.cs - + Form - + NetGen.cs - + UserControl - + NetModuleStatus.cs - + Form - + NetworkBrowser.cs - + UserControl - + Notification.cs - - - + + + + + Form - + PanelManager.cs - + Form - + Pong.cs - + UserControl - + ProgressBarEX.cs @@ -351,126 +323,115 @@ True Resources.resx - + Form - - QuickChat.cs - - - - Form - - + Shifter.cs - + UserControl - + ShifterColorInput.cs - + UserControl - + ShifterGraphicInput.cs - + UserControl - + ShifterIntInput.cs - + UserControl - + ShifterTextInput.cs - + Form - + Shiftnet.cs - + Form - + ShiftnetDecryptor.cs - - + + Form - + SkinLoader.cs - - + Form - + TextPad.cs - - + + Form - + WidgetManager.cs - + UserControl - + WindowBorder.cs - - - AlternateDesktop.cs - - + + Appscape.cs - + AppscapeUploader.cs - + Artpad.cs - + BitnoteConverter.cs - + BitnoteDigger.cs - + BitnoteWallet.cs - + Color_Picker.cs - + Computer.cs - + Connection.cs - + ConnectionManager.cs CreditScroller.cs - + DesktopIcon.cs - + Dodge.cs - + File Skimmer.cs @@ -488,67 +449,55 @@ GameSettings.cs - + Graphic_Picker.cs - - Hacking_Enemy.cs - - - Hacking_YourHealth.cs - - + HackUI.cs - + HijackScreen.cs - + HoloChat.cs - + IconControl.cs - + IconManager.cs - + ImageSelector.cs - + infobox.cs - - Jumper.cs - - + KnowledgeInput.cs - - ListViewEx.cs - - + NameChanger.cs - + NetGen.cs - + NetModuleStatus.cs - + NetworkBrowser.cs - + Notification.cs - + PanelManager.cs - + Pong.cs - + ProgressBarEX.cs @@ -556,52 +505,46 @@ Designer Resources.Designer.cs - - QuickChat.cs - - + Shifter.cs - + ShifterColorInput.cs - + ShifterGraphicInput.cs - + ShifterIntInput.cs - + ShifterTextInput.cs - + Shiftnet.cs - + ShiftnetDecryptor.cs - + Shiftorium.cs - + ShiftOSDesktop.cs - + SkinLoader.cs - - Template.cs - - + Terminal.cs - + TextPad.cs - + WidgetManager.cs - + WindowBorder.cs @@ -626,7 +569,7 @@ - + @@ -964,14 +907,14 @@ - - + + Form - + Shiftorium.cs - + @@ -1002,25 +945,19 @@ - + Form - + ShiftOSDesktop.cs - + Form - - Template.cs - - - Form - - + Terminal.cs - + @@ -1041,6 +978,7 @@ True +