diff --git a/ShiftOS.Server/Core.cs b/ShiftOS.Server/Core.cs index e14ca27..7bb5b1d 100644 --- a/ShiftOS.Server/Core.cs +++ b/ShiftOS.Server/Core.cs @@ -38,6 +38,37 @@ namespace ShiftOS.Server { public static class Core { + [MudRequest("mud_forward", typeof(ServerMessage))] + public static void ForwardMessage(string guid, ServerMessage message) + { + if (message.GUID == "all") + { + Server.Program.server.DispatchAll(new NetObject("forward", new ServerMessage + { + Name = "forward", + GUID = "Server", + Contents = JsonConvert.SerializeObject(message) + })); + } + else + { + try + { + Server.Program.server.DispatchTo(new Guid(message.GUID), new NetObject("forward", new ServerMessage + { + Name = "forward", + GUID = "Server", + Contents = JsonConvert.SerializeObject(message) + })); + } + catch + { + + } + } + } + + [MudRequest("getguid_reply", typeof(string))] public static void GuidBounce(string guid, object contents) { diff --git a/ShiftOS.WinForms/Applications/Pong.Designer.cs b/ShiftOS.WinForms/Applications/Pong.Designer.cs index f28868a..0254e94 100644 --- a/ShiftOS.WinForms/Applications/Pong.Designer.cs +++ b/ShiftOS.WinForms/Applications/Pong.Designer.cs @@ -79,8 +79,16 @@ namespace ShiftOS.WinForms.Applications this.tmrcountdown = new System.Windows.Forms.Timer(this.components); this.tmrstoryline = new System.Windows.Forms.Timer(this.components); this.pgcontents = new ShiftOS.WinForms.Controls.Canvas(); + this.pnlmultiplayerhandshake = new System.Windows.Forms.Panel(); + this.lvotherplayers = new System.Windows.Forms.ListView(); + this.lbmpstatus = new System.Windows.Forms.Label(); + this.pnlintro = new System.Windows.Forms.Panel(); + this.btnmatchmake = new System.Windows.Forms.Button(); + this.Label6 = new System.Windows.Forms.Label(); + this.btnstartgame = new System.Windows.Forms.Button(); + this.Label8 = new System.Windows.Forms.Label(); this.pnlhighscore = new System.Windows.Forms.Panel(); - this.lbhighscore = new System.Windows.Forms.ListBox(); + this.lbhighscore = new System.Windows.Forms.ListView(); this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel(); this.button2 = new System.Windows.Forms.Button(); this.label10 = new System.Windows.Forms.Label(); @@ -111,10 +119,6 @@ namespace ShiftOS.WinForms.Applications this.btnlosetryagain = new System.Windows.Forms.Button(); this.Label5 = new System.Windows.Forms.Label(); this.Label1 = new System.Windows.Forms.Label(); - this.pnlintro = new System.Windows.Forms.Panel(); - this.Label6 = new System.Windows.Forms.Label(); - this.btnstartgame = new System.Windows.Forms.Button(); - this.Label8 = new System.Windows.Forms.Label(); this.lblbeatai = new System.Windows.Forms.Label(); this.lblcountdown = new System.Windows.Forms.Label(); this.ball = new ShiftOS.WinForms.Controls.Canvas(); @@ -125,12 +129,13 @@ namespace ShiftOS.WinForms.Applications this.lblstatsY = new System.Windows.Forms.Label(); this.lblstatsX = new System.Windows.Forms.Label(); this.pgcontents.SuspendLayout(); + this.pnlmultiplayerhandshake.SuspendLayout(); + this.pnlintro.SuspendLayout(); this.pnlhighscore.SuspendLayout(); this.flowLayoutPanel1.SuspendLayout(); this.pnlgamestats.SuspendLayout(); this.pnlfinalstats.SuspendLayout(); this.pnllose.SuspendLayout(); - this.pnlintro.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.paddleHuman)).BeginInit(); this.SuspendLayout(); // @@ -157,11 +162,12 @@ namespace ShiftOS.WinForms.Applications // pgcontents // this.pgcontents.BackColor = System.Drawing.Color.White; + this.pgcontents.Controls.Add(this.pnlmultiplayerhandshake); + this.pgcontents.Controls.Add(this.pnlintro); this.pgcontents.Controls.Add(this.pnlhighscore); this.pgcontents.Controls.Add(this.pnlgamestats); this.pgcontents.Controls.Add(this.pnlfinalstats); this.pgcontents.Controls.Add(this.pnllose); - this.pgcontents.Controls.Add(this.pnlintro); this.pgcontents.Controls.Add(this.lblbeatai); this.pgcontents.Controls.Add(this.lblcountdown); this.pgcontents.Controls.Add(this.ball); @@ -179,6 +185,96 @@ namespace ShiftOS.WinForms.Applications this.pgcontents.Paint += new System.Windows.Forms.PaintEventHandler(this.pgcontents_Paint); this.pgcontents.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pongMain_MouseMove); // + // pnlmultiplayerhandshake + // + this.pnlmultiplayerhandshake.Controls.Add(this.lvotherplayers); + this.pnlmultiplayerhandshake.Controls.Add(this.lbmpstatus); + this.pnlmultiplayerhandshake.Location = new System.Drawing.Point(446, 88); + this.pnlmultiplayerhandshake.Name = "pnlmultiplayerhandshake"; + this.pnlmultiplayerhandshake.Size = new System.Drawing.Size(396, 231); + this.pnlmultiplayerhandshake.TabIndex = 15; + this.pnlmultiplayerhandshake.Visible = false; + // + // lvotherplayers + // + this.lvotherplayers.Dock = System.Windows.Forms.DockStyle.Fill; + this.lvotherplayers.Location = new System.Drawing.Point(0, 45); + this.lvotherplayers.MultiSelect = false; + this.lvotherplayers.Name = "lvotherplayers"; + this.lvotherplayers.Size = new System.Drawing.Size(396, 186); + this.lvotherplayers.TabIndex = 1; + this.lvotherplayers.UseCompatibleStateImageBehavior = false; + this.lvotherplayers.View = System.Windows.Forms.View.Details; + this.lvotherplayers.DoubleClick += new System.EventHandler(this.lvotherplayers_DoubleClick); + // + // lbmpstatus + // + this.lbmpstatus.Dock = System.Windows.Forms.DockStyle.Top; + this.lbmpstatus.Location = new System.Drawing.Point(0, 0); + this.lbmpstatus.Name = "lbmpstatus"; + this.lbmpstatus.Size = new System.Drawing.Size(396, 45); + this.lbmpstatus.TabIndex = 0; + this.lbmpstatus.Tag = "header2"; + this.lbmpstatus.Text = "Waiting for other players..."; + this.lbmpstatus.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // pnlintro + // + this.pnlintro.Controls.Add(this.btnmatchmake); + this.pnlintro.Controls.Add(this.Label6); + this.pnlintro.Controls.Add(this.btnstartgame); + this.pnlintro.Controls.Add(this.Label8); + this.pnlintro.Location = new System.Drawing.Point(0, 0); + this.pnlintro.Name = "pnlintro"; + this.pnlintro.Size = new System.Drawing.Size(595, 303); + this.pnlintro.TabIndex = 13; + this.pnlintro.Tag = "header2"; + // + // btnmatchmake + // + this.btnmatchmake.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnmatchmake.Location = new System.Drawing.Point(188, 253); + this.btnmatchmake.Name = "btnmatchmake"; + this.btnmatchmake.Size = new System.Drawing.Size(242, 28); + this.btnmatchmake.TabIndex = 16; + this.btnmatchmake.Text = "Or, play against another Shifter!"; + this.btnmatchmake.UseVisualStyleBackColor = true; + this.btnmatchmake.Click += new System.EventHandler(this.btnmatchmake_Click); + // + // Label6 + // + this.Label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.Label6.Location = new System.Drawing.Point(3, 39); + this.Label6.Name = "Label6"; + this.Label6.Size = new System.Drawing.Size(589, 159); + this.Label6.TabIndex = 15; + this.Label6.Text = "{PONG_DESC}"; + this.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + this.Label6.Click += new System.EventHandler(this.Label6_Click); + // + // btnstartgame + // + this.btnstartgame.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnstartgame.Location = new System.Drawing.Point(188, 215); + this.btnstartgame.Name = "btnstartgame"; + this.btnstartgame.Size = new System.Drawing.Size(242, 28); + this.btnstartgame.TabIndex = 15; + this.btnstartgame.Text = "{PLAY}"; + this.btnstartgame.UseVisualStyleBackColor = true; + this.btnstartgame.Click += new System.EventHandler(this.btnstartgame_Click); + // + // Label8 + // + this.Label8.AutoSize = true; + this.Label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.Label8.ForeColor = System.Drawing.Color.Black; + this.Label8.Location = new System.Drawing.Point(250, 5); + this.Label8.Name = "Label8"; + this.Label8.Size = new System.Drawing.Size(280, 31); + this.Label8.TabIndex = 14; + this.Label8.Text = "{PONG_WELCOME}"; + this.Label8.Click += new System.EventHandler(this.Label8_Click); + // // pnlhighscore // this.pnlhighscore.Controls.Add(this.lbhighscore); @@ -193,13 +289,11 @@ namespace ShiftOS.WinForms.Applications // lbhighscore // this.lbhighscore.Dock = System.Windows.Forms.DockStyle.Fill; - this.lbhighscore.FormattingEnabled = true; this.lbhighscore.Location = new System.Drawing.Point(0, 36); - this.lbhighscore.MultiColumn = true; this.lbhighscore.Name = "lbhighscore"; - this.lbhighscore.SelectionMode = System.Windows.Forms.SelectionMode.None; this.lbhighscore.Size = new System.Drawing.Size(539, 246); this.lbhighscore.TabIndex = 1; + this.lbhighscore.UseCompatibleStateImageBehavior = false; // // flowLayoutPanel1 // @@ -526,51 +620,6 @@ namespace ShiftOS.WinForms.Applications this.Label1.Text = "You lose!"; this.Label1.TextAlign = System.Drawing.ContentAlignment.TopCenter; // - // pnlintro - // - this.pnlintro.Controls.Add(this.Label6); - this.pnlintro.Controls.Add(this.btnstartgame); - this.pnlintro.Controls.Add(this.Label8); - this.pnlintro.Location = new System.Drawing.Point(1139, 41); - this.pnlintro.Name = "pnlintro"; - this.pnlintro.Size = new System.Drawing.Size(595, 303); - this.pnlintro.TabIndex = 13; - this.pnlintro.Tag = "header2"; - // - // Label6 - // - this.Label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.Label6.Location = new System.Drawing.Point(3, 39); - this.Label6.Name = "Label6"; - this.Label6.Size = new System.Drawing.Size(589, 227); - this.Label6.TabIndex = 15; - this.Label6.Text = "{PONG_DESC}"; - this.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; - this.Label6.Click += new System.EventHandler(this.Label6_Click); - // - // btnstartgame - // - this.btnstartgame.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.btnstartgame.Location = new System.Drawing.Point(186, 273); - this.btnstartgame.Name = "btnstartgame"; - this.btnstartgame.Size = new System.Drawing.Size(242, 28); - this.btnstartgame.TabIndex = 15; - this.btnstartgame.Text = "{PLAY}"; - this.btnstartgame.UseVisualStyleBackColor = true; - this.btnstartgame.Click += new System.EventHandler(this.btnstartgame_Click); - // - // Label8 - // - this.Label8.AutoSize = true; - this.Label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.Label8.ForeColor = System.Drawing.Color.Black; - this.Label8.Location = new System.Drawing.Point(250, 5); - this.Label8.Name = "Label8"; - this.Label8.Size = new System.Drawing.Size(280, 31); - this.Label8.TabIndex = 14; - this.Label8.Text = "{PONG_WELCOME}"; - this.Label8.Click += new System.EventHandler(this.Label8_Click); - // // lblbeatai // this.lblbeatai.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); @@ -688,6 +737,9 @@ namespace ShiftOS.WinForms.Applications this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pongMain_MouseMove); this.pgcontents.ResumeLayout(false); this.pgcontents.PerformLayout(); + this.pnlmultiplayerhandshake.ResumeLayout(false); + this.pnlintro.ResumeLayout(false); + this.pnlintro.PerformLayout(); this.pnlhighscore.ResumeLayout(false); this.pnlhighscore.PerformLayout(); this.flowLayoutPanel1.ResumeLayout(false); @@ -697,8 +749,6 @@ namespace ShiftOS.WinForms.Applications this.pnlfinalstats.ResumeLayout(false); this.pnlfinalstats.PerformLayout(); this.pnllose.ResumeLayout(false); - this.pnlintro.ResumeLayout(false); - this.pnlintro.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.paddleHuman)).EndInit(); this.ResumeLayout(false); @@ -745,7 +795,7 @@ namespace ShiftOS.WinForms.Applications internal System.Windows.Forms.Label Label8; internal System.Windows.Forms.Timer tmrstoryline; private System.Windows.Forms.Panel pnlhighscore; - private System.Windows.Forms.ListBox lbhighscore; + private System.Windows.Forms.ListView lbhighscore; private System.Windows.Forms.Label label10; internal Canvas pgcontents; internal Canvas ball; @@ -753,5 +803,9 @@ namespace ShiftOS.WinForms.Applications internal System.Windows.Forms.Label label12; private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1; private System.Windows.Forms.Button button2; + internal System.Windows.Forms.Button btnmatchmake; + private System.Windows.Forms.Panel pnlmultiplayerhandshake; + private System.Windows.Forms.ListView lvotherplayers; + private System.Windows.Forms.Label lbmpstatus; } } diff --git a/ShiftOS.WinForms/Applications/Pong.cs b/ShiftOS.WinForms/Applications/Pong.cs index 6198cad..04c963f 100644 --- a/ShiftOS.WinForms/Applications/Pong.cs +++ b/ShiftOS.WinForms/Applications/Pong.cs @@ -29,6 +29,7 @@ using System.Data; using System.Drawing; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using Newtonsoft.Json; @@ -81,7 +82,23 @@ namespace ShiftOS.WinForms.Applications private void pongMain_MouseMove(object sender, MouseEventArgs e) { var loc = this.PointToClient(MousePosition); - paddleHuman.Location = new Point(paddleHuman.Location.X, (loc.Y) - (paddleHuman.Height / 2)); + if (IsMultiplayerSession) + { + if (IsLeader) + { + paddleHuman.Location = new Point(paddleHuman.Location.X, (loc.Y) - (paddleHuman.Height / 2)); + ServerManager.Forward(OpponentGUID, "pong_mp_setopponenty", paddleHuman.Top.ToString()); + } + else + { + paddleComputer.Location = new Point(paddleComputer.Location.X, (loc.Y) - (paddleComputer.Height / 2)); + ServerManager.Forward(OpponentGUID, "pong_mp_setopponenty", paddleComputer.Top.ToString()); + } + } + else + { + paddleHuman.Location = new Point(paddleHuman.Location.X, (loc.Y) - (paddleHuman.Height / 2)); + } } private void CenterPanels() @@ -103,7 +120,13 @@ namespace ShiftOS.WinForms.Applications lblstatscodepoints.Top = this.Height - lblstatscodepoints.Height - 5; lblstatscodepoints.Left = (this.Width - lblstatscodepoints.Width) / 2; } - + + int OpponentY = 0; + + bool IsLeader = false; + + int LeaderX = 0; + int LeaderY = 0; // ERROR: Handles clauses are not supported in C# private void gameTimer_Tick(object sender, EventArgs e) @@ -117,39 +140,50 @@ namespace ShiftOS.WinForms.Applications //Check if paddle upgrade has been bought and change paddles accordingly //if (ShiftoriumFrontend.UpgradeInstalled("pong_increased_paddle_size")) //{ - // paddleHuman.Height = 150; - // paddleComputer.Height = 150; + // paddleHuman.Height = 150; + // paddleComputer.Height = 150; //} //I don't know the point of this but I'm fucking 86ing it. - Michael //Set the computer player to move according to the ball's position. - if (aiShouldIsbeEnabled) - if (ball.Location.X > (this.Width - (this.Width / 3)) - xVel * 10 && xVel > 0) - { - if (ball.Location.Y > paddleComputer.Location.Y + 50) - { - paddleComputer.Location = new Point(paddleComputer.Location.X, paddleComputer.Location.Y + computerspeed); - } - if (ball.Location.Y < paddleComputer.Location.Y + 50) - { - paddleComputer.Location = new Point(paddleComputer.Location.X, paddleComputer.Location.Y - computerspeed); - } - casualposition = rand.Next(-150, 201); - } + if (IsMultiplayerSession == true) + { + //If we're multiplayer, then we want to set the computer Y to the opponent's Y. + //If we're the leader, we set the AI paddle, else we set the player paddle. + if (IsLeader) + paddleComputer.Top = OpponentY; else - { - //used to be me.location.y - except it's fucking C# and this comment is misleading as fuck. OH WAIT! I didn't write it! And none of the current devs did either! - Michael - if (paddleComputer.Location.Y > this.Size.Height / 2 - paddleComputer.Height + casualposition) + paddleHuman.Top = OpponentY; + } + else + { + if (aiShouldIsbeEnabled) + if (ball.Location.X > (this.Width - (this.Width / 3)) - xVel * 10 && xVel > 0) { - paddleComputer.Location = new Point(paddleComputer.Location.X, paddleComputer.Location.Y - computerspeed); + if (ball.Location.Y > paddleComputer.Location.Y + 50) + { + paddleComputer.Location = new Point(paddleComputer.Location.X, paddleComputer.Location.Y + computerspeed); + } + if (ball.Location.Y < paddleComputer.Location.Y + 50) + { + paddleComputer.Location = new Point(paddleComputer.Location.X, paddleComputer.Location.Y - computerspeed); + } + casualposition = rand.Next(-150, 201); } - //Rylan is hot. Used to be //used to be me.location.y - if (paddleComputer.Location.Y < this.Size.Height / 2 - paddleComputer.Height + casualposition) + else { - paddleComputer.Location = new Point(paddleComputer.Location.X, paddleComputer.Location.Y + computerspeed); + //used to be me.location.y - except it's fucking C# and this comment is misleading as fuck. OH WAIT! I didn't write it! And none of the current devs did either! - Michael + if (paddleComputer.Location.Y > this.Size.Height / 2 - paddleComputer.Height + casualposition) + { + paddleComputer.Location = new Point(paddleComputer.Location.X, paddleComputer.Location.Y - computerspeed); + } + //Rylan is hot. Used to be //used to be me.location.y + if (paddleComputer.Location.Y < this.Size.Height / 2 - paddleComputer.Height + casualposition) + { + paddleComputer.Location = new Point(paddleComputer.Location.X, paddleComputer.Location.Y + computerspeed); + } } - } - + } //Set Xvel and Yvel speeds from decimal if (xVel > 0) xVel = (int)Math.Round(xveldec); @@ -160,121 +194,355 @@ namespace ShiftOS.WinForms.Applications if (yVel < 0) yVel = (int)-Math.Round(yveldec); - // Move the game ball. - ball.Location = new Point(ball.Location.X + xVel, ball.Location.Y + yVel); + bool BallPhysics = true; - // Check for top wall. - if (ball.Location.Y < 0) + if (IsMultiplayerSession) { - ball.Location = new Point(ball.Location.X, 0); - yVel = -yVel; - ShiftOS.Engine.AudioManager.PlayStream(Properties.Resources.typesound); - } - - // Check for bottom wall. - if (ball.Location.Y > pgcontents.Height - ball.Height) - { - ball.Location = new Point(ball.Location.X, pgcontents.Height - ball.Size.Height); - yVel = -yVel; - ShiftOS.Engine.AudioManager.PlayStream(Properties.Resources.typesound); - } - - // Check for player paddle. - if (ball.Bounds.IntersectsWith(paddleHuman.Bounds)) - { - ball.Location = new Point(paddleHuman.Location.X + ball.Size.Width + 1, ball.Location.Y); - //randomly increase x or y speed of ball - switch (rand.Next(1, 3)) + //Logic for moving the ball in Multiplayer. + if (IsLeader) { - case 1: - xveldec = xveldec + incrementx; - break; - case 2: - if (yveldec > 0) - yveldec = yveldec + incrementy; - if (yveldec < 0) - yveldec = yveldec - incrementy; - break; + ball.Location = new Point(ball.Location.X + xVel, ball.Location.Y + yVel); + } + else + { + //Move it to the leader's ball position. + ball.Location = new Point(LeaderX, LeaderY); + BallPhysics = false; } - xVel = -xVel; - ShiftOS.Engine.AudioManager.PlayStream(Properties.Resources.writesound); - } - - // Check for computer paddle. - if (ball.Bounds.IntersectsWith(paddleComputer.Bounds)) + else + {// Move the game ball. + ball.Location = new Point(ball.Location.X + xVel, ball.Location.Y + yVel); + } + if (BallPhysics) { - ball.Location = new Point(paddleComputer.Location.X - paddleComputer.Size.Width - 1, ball.Location.Y); - xveldec = xveldec + incrementx; - xVel = -xVel; - ShiftOS.Engine.AudioManager.PlayStream(Properties.Resources.writesound); + // Check for top wall. + if (ball.Location.Y < 0) + { + ball.Location = new Point(ball.Location.X, 0); + yVel = -yVel; + ShiftOS.Engine.AudioManager.PlayStream(Properties.Resources.typesound); + } + + // Check for bottom wall. + if (ball.Location.Y > pgcontents.Height - ball.Height) + { + ball.Location = new Point(ball.Location.X, pgcontents.Height - ball.Size.Height); + yVel = -yVel; + ShiftOS.Engine.AudioManager.PlayStream(Properties.Resources.typesound); + } + + + // Check for player paddle. + if (ball.Bounds.IntersectsWith(paddleHuman.Bounds)) + { + ball.Location = new Point(paddleHuman.Location.X + ball.Size.Width + 1, ball.Location.Y); + //randomly increase x or y speed of ball + switch (rand.Next(1, 3)) + { + case 1: + xveldec = xveldec + incrementx; + break; + case 2: + if (yveldec > 0) + yveldec = yveldec + incrementy; + if (yveldec < 0) + yveldec = yveldec - incrementy; + break; + } + xVel = -xVel; + ShiftOS.Engine.AudioManager.PlayStream(Properties.Resources.writesound); + + } + + // Check for computer paddle. + if (ball.Bounds.IntersectsWith(paddleComputer.Bounds)) + { + ball.Location = new Point(paddleComputer.Location.X - paddleComputer.Size.Width - 1, ball.Location.Y); + xveldec = xveldec + incrementx; + xVel = -xVel; + ShiftOS.Engine.AudioManager.PlayStream(Properties.Resources.writesound); + } } + + // Check for left wall. if (ball.Location.X < -100) { - ball.Location = new Point(this.Size.Width / 2 + 200, this.Size.Height / 2); - paddleComputer.Location = new Point(paddleComputer.Location.X, ball.Location.Y); - if (xVel > 0) - xVel = -xVel; - pnllose.Show(); - gameTimer.Stop(); - counter.Stop(); - lblmissedout.Text = Localization.Parse("{YOU_MISSED_OUT_ON}:") + Environment.NewLine + lblstatscodepoints.Text.Replace(Localization.Parse("{CODEPOINTS}: "), "") + Localization.Parse(" {CODEPOINTS}"); - if (ShiftoriumFrontend.UpgradeInstalled("pong_upgrade_2")) + //If we are in multiplayer, and not the leader, we won. + if (IsMultiplayerSession) { - totalreward = levelrewards[level - 1] + beatairewardtotal; - double onePercent = (totalreward / 100); - lblbutyougained.Show(); - lblbutyougained.Text = Localization.Parse("{BUT_YOU_GAINED}:") + Environment.NewLine + onePercent.ToString("") + (Localization.Parse(" {CODEPOINTS}")); - SaveSystem.TransferCodepointsFrom("pong", (totalreward / 100)); + if (IsLeader) + { + //We lost. + NotifyLoseToTarget(); + } + else + { + //We won. + NotifyWinToTarget(); + Win(); + } } else { - lblbutyougained.Hide(); + ball.Location = new Point(this.Size.Width / 2 + 200, this.Size.Height / 2); + paddleComputer.Location = new Point(paddleComputer.Location.X, ball.Location.Y); + if (xVel > 0) + xVel = -xVel; + pnllose.Show(); + gameTimer.Stop(); + counter.Stop(); + lblmissedout.Text = Localization.Parse("{YOU_MISSED_OUT_ON}:") + Environment.NewLine + lblstatscodepoints.Text.Replace(Localization.Parse("{CODEPOINTS}: "), "") + Localization.Parse(" {CODEPOINTS}"); + if (ShiftoriumFrontend.UpgradeInstalled("pong_upgrade_2")) + { + totalreward = levelrewards[level - 1] + beatairewardtotal; + double onePercent = (totalreward / 100); + lblbutyougained.Show(); + lblbutyougained.Text = Localization.Parse("{BUT_YOU_GAINED}:") + Environment.NewLine + onePercent.ToString("") + (Localization.Parse(" {CODEPOINTS}")); + SaveSystem.TransferCodepointsFrom("pong", (totalreward / 100)); + } + else + { + lblbutyougained.Hide(); + } } } // Check for right wall. if (ball.Location.X > this.Width - ball.Size.Width - paddleComputer.Width + 100) { - ball.Location = new Point(this.Size.Width / 2 + 200, this.Size.Height / 2); - paddleComputer.Location = new Point(paddleComputer.Location.X, ball.Location.Y); - if (xVel > 0) - xVel = -xVel; - beatairewardtotal = beatairewardtotal + beataireward; - lblbeatai.Show(); - lblbeatai.Text = Localization.Parse($"{{PONG_BEAT_AI_REWARD_SECONDARY}}: {beataireward}"); - tmrcountdown.Start(); - gameTimer.Stop(); - counter.Stop(); + if (IsMultiplayerSession) + { + //If we are the leader we won. + if (IsLeader) + { + NotifyWinToTarget(); + Win(); + } + else + { + NotifyLoseToTarget(); + } + } + else + { + Win(); + } } - //lblstats.Text = "Xspeed: " & Math.Abs(xVel) & " Yspeed: " & Math.Abs(yVel) & " Human Location: " & paddleHuman.Location.ToString & " Computer Location: " & paddleComputer.Location.ToString & Environment.NewLine & " Ball Location: " & ball.Location.ToString & " Xdec: " & xveldec & " Ydec: " & yveldec & " Xinc: " & incrementx & " Yinc: " & incrementy - lblstatsX.Text = Localization.Parse("{H_VEL}: ") + xveldec; - lblstatsY.Text = Localization.Parse("{V_VEL}: ") + yveldec; - lblstatscodepoints.Text = Localization.Parse("{CODEPOINTS}: ") + (levelrewards[level - 1] + beatairewardtotal).ToString(); - lbllevelandtime.Text = Localization.Parse("{LEVEL}: " + level + " - " + secondsleft + " {SECONDS_LEFT}"); - - if (xVel > 20 || xVel < -20) + if (IsMultiplayerSession) { - paddleHuman.Width = Math.Abs(xVel); - paddleComputer.Width = Math.Abs(xVel); + if (IsLeader) + { + ServerManager.Forward(OpponentGUID, "pong_mp_setballpos", JsonConvert.SerializeObject(ball.Location)); + } } - else + + if (IsLeader) { - paddleHuman.Width = 20; - paddleComputer.Width = 20; + //lblstats.Text = "Xspeed: " & Math.Abs(xVel) & " Yspeed: " & Math.Abs(yVel) & " Human Location: " & paddleHuman.Location.ToString & " Computer Location: " & paddleComputer.Location.ToString & Environment.NewLine & " Ball Location: " & ball.Location.ToString & " Xdec: " & xveldec & " Ydec: " & yveldec & " Xinc: " & incrementx & " Yinc: " & incrementy + lblstatsX.Text = Localization.Parse("{H_VEL}: ") + xveldec; + lblstatsY.Text = Localization.Parse("{V_VEL}: ") + yveldec; + lblstatscodepoints.Text = Localization.Parse("{CODEPOINTS}: ") + (levelrewards[level - 1] + beatairewardtotal).ToString(); + lbllevelandtime.Text = Localization.Parse("{LEVEL}: " + level + " - " + secondsleft + " {SECONDS_LEFT}"); + + if (xVel > 20 || xVel < -20) + { + paddleHuman.Width = Math.Abs(xVel); + paddleComputer.Width = Math.Abs(xVel); + } + else + { + paddleHuman.Width = 20; + paddleComputer.Width = 20; + } + } + if (!IsMultiplayerSession) + { + computerspeed = Math.Abs(yVel); } - - computerspeed = Math.Abs(yVel); - - // pgcontents.Refresh() - // pgcontents.CreateGraphics.FillRectangle(Brushes.Black, ball.Location.X, ball.Location.Y, ball.Width, ball.Height) - } } + public void ServerMessageReceivedHandler(ServerMessage msg) + { + if (msg.Name == "pong_mp_setballpos") + { + var pt = JsonConvert.DeserializeObject(msg.Contents); + LeaderX = pt.X; + LeaderY = pt.Y; + } + else if(msg.Name == "pong_mp_left") + { + this.Invoke(new Action(() => + { + AppearanceManager.Close(this); + })); + Infobox.Show("Opponent has closed Pong.", "The opponent has closed Pong, therefore the connection between you two has dropped."); + } + else if (msg.Name == "pong_mp_youlose") + { + this.Invoke(new Action(LoseMP)); + } + else if (msg.Name == "pong_mp_setopponenty") + { + int y = Convert.ToInt32(msg.Contents); + OpponentY = y; + } + else if (msg.Name == "pong_handshake_matchmake") + { + if (!PossibleMatchmakes.Contains(msg.Contents)) + PossibleMatchmakes.Add(msg.Contents); + this.Invoke(new Action(ListMatchmakes)); + } + else if (msg.Name == "pong_handshake_resendid") + { + ServerManager.Forward("all", "pong_handshake_matchmake", YouGUID); + } + else if (msg.Name == "pong_handshake_complete") + { + IsLeader = true; + + OpponentGUID = msg.Contents; + LeaveMatchmake(); + this.Invoke(new Action(() => + { + pnlmultiplayerhandshake.Hide(); + StartLevel(); + })); + } + else if (msg.Name == "pong_handshake_chosen") + { + IsLeader = false; + LeaveMatchmake(); + OpponentGUID = msg.Contents; + YouGUID = ServerManager.thisGuid.ToString(); + SendFollowerGUID(); + this.Invoke(new Action(() => + { + pnlmultiplayerhandshake.Hide(); + })); + } + else if (msg.Name == "pong_handshake_left") + { + if (this.PossibleMatchmakes.Contains(msg.Contents)) + this.PossibleMatchmakes.Remove(msg.Contents); + this.Invoke(new Action(ListMatchmakes)); + } + else if (msg.Name == "pong_mp_youwin") + { + this.Invoke(new Action(Win)); + } + } + + public void ListMatchmakes() + { + lvotherplayers.Items.Clear(); + var c = new ColumnHeader(); + c.Width = lvotherplayers.Width; + c.Text = "Player"; + lvotherplayers.Columns.Clear(); + lvotherplayers.Columns.Add(c); + + lvotherplayers.FullRowSelect = true; + foreach (var itm in PossibleMatchmakes) + { + if (itm != YouGUID) + { + var l = new ListViewItem(); + l.Text = itm; + lvotherplayers.Items.Add(l); + } + } + + if (PossibleMatchmakes.Count > 0) + { + lbmpstatus.Text = "Select a player."; + } + else + { + lbmpstatus.Text = "Waiting for players..."; + } + } + + public void NotifyLoseToTarget() + { + ServerManager.Forward(OpponentGUID, "pong_mp_youwin", null); + } + + public void NotifyWinToTarget() + { + ServerManager.Forward(OpponentGUID, "pong_mp_youlose", null); + } + + public void LeaveMatchmake() + { + ServerManager.Forward("all", "pong_handshake_left", YouGUID); + } + + List PossibleMatchmakes = new List(); + + public void SendLeaderGUID(string target) + { + ServerManager.Forward(target, "pong_handshake_chosen", YouGUID); + } + + + public void StartMultiplayer() + { + IsMultiplayerSession = true; + YouGUID = ServerManager.thisGuid.ToString(); + ServerManager.SendMessage("pong_handshake_matchmake", YouGUID); + StartMatchmake(); + } + + public void StartMatchmake() + { + pnlmultiplayerhandshake.Show(); + pnlmultiplayerhandshake.CenterParent(); + pnlmultiplayerhandshake.BringToFront(); + + ServerManager.Forward("all", "pong_handshake_resendid", null); + + } + + + public void SendFollowerGUID() + { + ServerManager.Forward(OpponentGUID, "pong_handshake_complete", YouGUID); + } + + public void LoseMP() + { + ball.Location = new Point(this.Size.Width / 2 + 200, this.Size.Height / 2); + if(IsLeader) + if (xVel > 0) + xVel = -xVel; + lblbeatai.Show(); + lblbeatai.Text = "The opponent has beaten you!"; + tmrcountdown.Start(); + gameTimer.Stop(); + counter.Stop(); + + } + + public void Win() + { + ball.Location = new Point(this.Size.Width / 2 + 200, this.Size.Height / 2); + paddleComputer.Location = new Point(paddleComputer.Location.X, ball.Location.Y); + if (xVel > 0) + xVel = -xVel; + beatairewardtotal = beatairewardtotal + beataireward; + lblbeatai.Show(); + lblbeatai.Text = Localization.Parse($"{{PONG_BEAT_AI_REWARD_SECONDARY}}: {beataireward}"); + tmrcountdown.Start(); + gameTimer.Stop(); + counter.Stop(); + + } + // ERROR: Handles clauses are not supported in C# private void counter_Tick(object sender, EventArgs e) { @@ -310,15 +578,9 @@ namespace ShiftOS.WinForms.Applications SetupStats(); } + [Obsolete("This method does nothing. Use UniteClient for highscore queries.")] public void SendHighscores() { - var highscore = new PongHighscore - { - UserName = $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}", - HighestLevel = level, - HighestCodepoints = totalreward - }; - ServerManager.SendMessage("pong_sethighscore", JsonConvert.SerializeObject(highscore)); } // ERROR: Handles clauses are not supported in C# @@ -611,27 +873,58 @@ namespace ShiftOS.WinForms.Applications SetupHighScores(); } + bool IsMultiplayerSession = false; + + string YouGUID = ""; + string OpponentGUID = ""; + public void SetupHighScores() { lbhighscore.Items.Clear(); - ServerManager.MessageReceived += (msg) => + lbhighscore.View = View.Details; + lbhighscore.FullRowSelect = true; + lbhighscore.Columns.Clear(); + var n = new ColumnHeader(); + n.Text = "Player"; + n.Width = lbhighscore.Width / 3; + var l = new ColumnHeader(); + l.Text = "Level"; + l.Width = n.Width; + var c = new ColumnHeader(); + c.Text = "Codepoints"; + c.Width = n.Width; + lbhighscore.Columns.Add(n); + lbhighscore.Columns.Add(l); + lbhighscore.Columns.Add(c); + + var t = new Thread(() => { - if(msg.Name == "pong_highscores") + try { - var hs = JsonConvert.DeserializeObject>(msg.Contents); - var orderedhs = hs.OrderByDescending(i => i.HighestLevel); - - foreach(var score in orderedhs) + var unite = new ShiftOS.Unite.UniteClient("http://getshiftos.ml", SaveSystem.CurrentSave.UniteAuthToken); + var hs = unite.GetPongHighscores(); + foreach (var score in hs.Highscores) { + string username = unite.GetDisplayNameId(score.UserId); this.Invoke(new Action(() => { - lbhighscore.Items.Add($"{score.UserName}\t\t\t{score.HighestLevel}\t\t{score.HighestCodepoints} CP"); + var name_item = new ListViewItem(); + name_item.Text = username; + lbhighscore.Items.Add(name_item); + name_item.SubItems.Add(score.Level.ToString()); + name_item.SubItems.Add(score.CodepointsCashout.ToString()); })); } } - }; - ServerManager.SendMessage("pong_gethighscores", null); + catch + { + Infobox.Show("Service unavailable.", "The Pong Highscore service is unavailable at this time."); + this.Invoke(new Action(pnlgamestats.BringToFront)); + return; + } + }); + t.Start(); pnlhighscore.Show(); } @@ -647,6 +940,11 @@ namespace ShiftOS.WinForms.Applications newgame(); } + public void StartLevel() + { + newgame(); + } + // ERROR: Handles clauses are not supported in C# private void btnstartgame_Click(object sender, EventArgs e) { @@ -704,6 +1002,7 @@ namespace ShiftOS.WinForms.Applications pnlfinalstats.Hide(); CenterPanels(); lblbeatai.Hide(); + ServerManager.MessageReceived += this.ServerMessageReceivedHandler; } public void OnSkinLoad() @@ -717,6 +1016,14 @@ namespace ShiftOS.WinForms.Applications public bool OnUnload() { + if(IsMultiplayerSession == true) + { + if(!string.IsNullOrWhiteSpace(OpponentGUID)) + ServerManager.Forward(OpponentGUID, "pong_mp_left", null); + } + LeaveMatchmake(); + ServerManager.MessageReceived -= this.ServerMessageReceivedHandler; + return true; } @@ -729,5 +1036,22 @@ namespace ShiftOS.WinForms.Applications { pnlhighscore.Hide(); } + + private void btnmatchmake_Click(object sender, EventArgs e) + { + this.StartMultiplayer(); + pnlintro.Hide(); + lblbeatai.Text = "Beat the other player to earn Codepoints."; + lblcountdown.Text = "Waiting for another player..."; + lblcountdown.Left = (this.Width - lblcountdown.Width) / 2; + } + + private void lvotherplayers_DoubleClick(object sender, EventArgs e) + { + if(lvotherplayers.SelectedItems.Count > 0) + { + SendLeaderGUID(lvotherplayers.SelectedItems[0].Text); + } + } } } diff --git a/ShiftOS_TheReturn/Commands.cs b/ShiftOS_TheReturn/Commands.cs index 87aacd6..57d1d34 100644 --- a/ShiftOS_TheReturn/Commands.cs +++ b/ShiftOS_TheReturn/Commands.cs @@ -283,7 +283,7 @@ namespace ShiftOS.Engine if (args.ContainsKey("amount")) try { - int codepointsToAdd = Convert.ToInt32(args["amount"].ToString()); + Int64 codepointsToAdd = Convert.ToInt64(args["amount"].ToString()); SaveSystem.TransferCodepointsFrom("dev", codepointsToAdd); return true; } diff --git a/ShiftOS_TheReturn/SaveSystem.cs b/ShiftOS_TheReturn/SaveSystem.cs index 945869b..c3289ea 100644 --- a/ShiftOS_TheReturn/SaveSystem.cs +++ b/ShiftOS_TheReturn/SaveSystem.cs @@ -198,6 +198,52 @@ namespace ShiftOS.Engine Thread.Sleep(50); Console.WriteLine("{SYSTEM_INITIATED}"); + Sysname: + bool waitingForNewSysName = false; + bool gobacktosysname = false; + + if (string.IsNullOrWhiteSpace(CurrentSave.SystemName)) + { + Infobox.PromptText("Enter a system name", "Your system does not have a name. All systems within the digital society must have a name. Please enter one.", (name)=> + { + if (string.IsNullOrWhiteSpace(name)) + Infobox.Show("Invalid name", "Please enter a valid name.", () => + { + gobacktosysname = true; + waitingForNewSysName = false; + }); + else if (name.Length < 5) + Infobox.Show("Value too small.", "Your system name must have at least 5 characters in it.", () => + { + gobacktosysname = true; + waitingForNewSysName = false; + }); + else + { + CurrentSave.SystemName = name; + if (!string.IsNullOrWhiteSpace(CurrentSave.UniteAuthToken)) + { + var unite = new Unite.UniteClient("http://getshiftos.ml", CurrentSave.UniteAuthToken); + unite.SetSysName(name); + } + SaveSystem.SaveGame(); + gobacktosysname = false; + waitingForNewSysName = false; + } + }); + + + } + + while (waitingForNewSysName) + { + Thread.Sleep(10); + } + + if (gobacktosysname) + { + goto Sysname; + } if (CurrentSave.Users == null) CurrentSave.Users = new List(); diff --git a/ShiftOS_TheReturn/ServerManager.cs b/ShiftOS_TheReturn/ServerManager.cs index d356600..0bdfcd9 100644 --- a/ShiftOS_TheReturn/ServerManager.cs +++ b/ShiftOS_TheReturn/ServerManager.cs @@ -172,6 +172,10 @@ namespace ShiftOS.Engine { Console.WriteLine(msg.Contents); } + else if(msg.Name == "forward") + { + MessageReceived?.Invoke(JsonConvert.DeserializeObject(msg.Contents)); + } else if (msg.Name == "Error") { var ex = JsonConvert.DeserializeObject(msg.Contents); @@ -239,6 +243,16 @@ namespace ShiftOS.Engine public static event ServerMessageReceived MessageReceived; + public static void Forward(string targetGUID, string v, string message) + { + var smsg = new ServerMessage + { + GUID = targetGUID, + Name = v, + Contents = message + }; + ServerManager.SendMessage("mud_forward", JsonConvert.SerializeObject(smsg)); + } } public delegate void ServerMessageReceived(ServerMessage msg); diff --git a/ShiftOS_TheReturn/UniteClient.cs b/ShiftOS_TheReturn/UniteClient.cs index cb08382..1136b5c 100644 --- a/ShiftOS_TheReturn/UniteClient.cs +++ b/ShiftOS_TheReturn/UniteClient.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; +using Newtonsoft.Json; namespace ShiftOS.Unite { @@ -12,6 +13,16 @@ namespace ShiftOS.Unite public string Token { get; private set; } public string BaseURL { get; private set; } + public string GetDisplayNameId(string id) + { + return MakeCall("/API/GetDisplayName/" + id); + } + + public PongHighscoreModel GetPongHighscores() + { + return JsonConvert.DeserializeObject(MakeCall("/API/GetPongHighscores")); + } + public UniteClient(string baseurl, string usertoken) { BaseURL = baseurl; @@ -100,4 +111,17 @@ namespace ShiftOS.Unite MakeCall("/API/SetCodepoints/" + value.ToString()); } } + + public class PongHighscoreModel + { + public int Pages { get; set; } + public PongHighscore[] Highscores { get; set; } + } + + public class PongHighscore + { + public string UserId { get; set; } + public int Level { get; set; } + public long CodepointsCashout { get; set; } + } }