diff --git a/ShiftOS.WinForms/Applications/Pong.cs b/ShiftOS.WinForms/Applications/Pong.cs index 04c963f..d63f406 100644 --- a/ShiftOS.WinForms/Applications/Pong.cs +++ b/ShiftOS.WinForms/Applications/Pong.cs @@ -123,7 +123,7 @@ namespace ShiftOS.WinForms.Applications int OpponentY = 0; - bool IsLeader = false; + bool IsLeader = true; int LeaderX = 0; int LeaderY = 0; @@ -368,75 +368,124 @@ namespace ShiftOS.WinForms.Applications public void ServerMessageReceivedHandler(ServerMessage msg) { - if (msg.Name == "pong_mp_setballpos") + if (IsMultiplayerSession) { - var pt = JsonConvert.DeserializeObject(msg.Contents); - LeaderX = pt.X; - LeaderY = pt.Y; - } - else if(msg.Name == "pong_mp_left") - { - this.Invoke(new Action(() => + if (msg.Name == "pong_mp_setballpos") { - 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; + 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(() => + OpponentGUID = msg.Contents; + LeaveMatchmake(); + this.Invoke(new Action(() => + { + pnlmultiplayerhandshake.Hide(); + StartLevel(); + })); + } + else if(msg.Name == "pong_mp_levelcompleted") { - 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(() => + level = Convert.ToInt32(msg.Contents) + 1; + this.Invoke(new Action(CompleteLevel)); + } + else if (msg.Name == "pong_handshake_chosen") { - 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)); + IsLeader = false; + LeaveMatchmake(); + OpponentGUID = msg.Contents; + YouGUID = ServerManager.thisGuid.ToString(); + //Start the timers. + counter.Start(); + SendFollowerGUID(); + this.Invoke(new Action(() => + { + pnlmultiplayerhandshake.Hide(); + })); + } + else if(msg.Name == "pong_mp_cashedout") + { + this.Invoke(new Action(() => + { + btncashout_Click(this, EventArgs.Empty); + })); + Infobox.Show("Cashed out.", "The other player has cashed out their Codepoints. Therefore, we have automatically cashed yours out."); + } + else if(msg.Name == "pong_mp_startlevel") + { + OpponentAgrees = true; + if(YouAgree == false) + { + Infobox.PromptYesNo("Play another level?", "The opponent wants to play another level. Would you like to as well?", (answer)=> + { + YouAgree = answer; + ServerManager.Forward(OpponentGUID, "pong_mp_level_callback", YouAgree.ToString()); + }); + } + } + else if(msg.Name == "pong_mp_level_callback") + { + bool agreed = bool.Parse(msg.Contents); + OpponentAgrees = agreed; + if (OpponentAgrees) + { + if (IsLeader) + { + //this.Invoke(new Action(())) + } + } + } + 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_clockupdate") + { + secondsleft = Convert.ToInt32(msg.Contents); + } + else if (msg.Name == "pong_mp_youwin") + { + this.Invoke(new Action(Win)); + } } } + bool OpponentAgrees = false; + bool YouAgree = false; + public void ListMatchmakes() { lvotherplayers.Items.Clear(); @@ -543,37 +592,50 @@ namespace ShiftOS.WinForms.Applications } + public void CompleteLevel() + { + if (SaveSystem.CurrentSave.UniteAuthToken != null) + { + try + { + var unite = new ShiftOS.Unite.UniteClient("http://getshiftos.ml", SaveSystem.CurrentSave.UniteAuthToken); + if (unite.GetPongLevel() < level) + unite.SetPongLevel(level); + } + catch { } + } + //Only set these stats if the user is the leader. + if (IsLeader) + { + secondsleft = 60; + level = level + 1; + generatenextlevel(); + } + + pnlgamestats.Show(); + pnlgamestats.BringToFront(); + pnlgamestats.Location = new Point((pgcontents.Width / 2) - (pnlgamestats.Width / 2), (pgcontents.Height / 2) - (pnlgamestats.Height / 2)); + + counter.Stop(); + gameTimer.Stop(); + + } + // ERROR: Handles clauses are not supported in C# private void counter_Tick(object sender, EventArgs e) { - if (this.Left < Screen.PrimaryScreen.Bounds.Width) + if (IsLeader) { - secondsleft = secondsleft - 1; - if (secondsleft == 1) + if (this.Left < Screen.PrimaryScreen.Bounds.Width) { - secondsleft = 60; - level = level + 1; - if (SaveSystem.CurrentSave.UniteAuthToken != null) + secondsleft = secondsleft - 1; + if (secondsleft == 1) { - try - { - var unite = new ShiftOS.Unite.UniteClient("http://getshiftos.ml", SaveSystem.CurrentSave.UniteAuthToken); - if (unite.GetPongLevel() < level) - unite.SetPongLevel(level); - } - catch { } + CompleteLevel(); } - generatenextlevel(); - pnlgamestats.Show(); - pnlgamestats.BringToFront(); - pnlgamestats.Location = new Point((pgcontents.Width / 2) - (pnlgamestats.Width / 2), (pgcontents.Height / 2) - (pnlgamestats.Height / 2)); - counter.Stop(); - gameTimer.Stop(); - SendHighscores(); + lblstatscodepoints.Text = Localization.Parse("{CODEPOINTS}: ") + (levelrewards[level - 1] + beatairewardtotal).ToString(); } - - lblstatscodepoints.Text = Localization.Parse("{CODEPOINTS}: ") + (levelrewards[level - 1] + beatairewardtotal).ToString(); } SetupStats(); } @@ -824,6 +886,19 @@ namespace ShiftOS.WinForms.Applications unite.SetPongCP(totalreward); } } + if (IsMultiplayerSession) + { + ServerManager.Forward(OpponentGUID, "pong_mp_cashedout", null); + StopMultiplayerSession(); + } + } + + public void StopMultiplayerSession() + { + IsMultiplayerSession = false; + IsLeader = true; + OpponentGUID = ""; + YouGUID = ""; } private void newgame() diff --git a/ShiftOS.WinForms/DesktopWidgets/HeartbeatWidget.Designer.cs b/ShiftOS.WinForms/DesktopWidgets/HeartbeatWidget.Designer.cs new file mode 100644 index 0000000..c6a7d83 --- /dev/null +++ b/ShiftOS.WinForms/DesktopWidgets/HeartbeatWidget.Designer.cs @@ -0,0 +1,62 @@ +namespace ShiftOS.WinForms.DesktopWidgets +{ + partial class HeartbeatWidget + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.lbheartbeat = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // lbheartbeat + // + this.lbheartbeat.AutoSize = true; + this.lbheartbeat.Dock = System.Windows.Forms.DockStyle.Fill; + this.lbheartbeat.Location = new System.Drawing.Point(0, 0); + this.lbheartbeat.Name = "lbheartbeat"; + this.lbheartbeat.Size = new System.Drawing.Size(35, 13); + this.lbheartbeat.TabIndex = 0; + this.lbheartbeat.Text = "label1"; + // + // HeartbeatWidget + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.AutoSize = true; + this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; + this.Controls.Add(this.lbheartbeat); + this.Name = "HeartbeatWidget"; + this.Size = new System.Drawing.Size(35, 13); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Label lbheartbeat; + } +} diff --git a/ShiftOS.WinForms/DesktopWidgets/HeartbeatWidget.cs b/ShiftOS.WinForms/DesktopWidgets/HeartbeatWidget.cs new file mode 100644 index 0000000..d38cdc3 --- /dev/null +++ b/ShiftOS.WinForms/DesktopWidgets/HeartbeatWidget.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Drawing; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using ShiftOS.WinForms.Tools; +using ShiftOS.Engine; + +namespace ShiftOS.WinForms.DesktopWidgets +{ + [DesktopWidget("Server ping", "See the time spent between client requests and server replies in the digital society.")] + public partial class HeartbeatWidget : UserControl, IDesktopWidget + { + public HeartbeatWidget() + { + InitializeComponent(); + tmr.Interval = 1; + tmr.Tick += (o, a) => + { + if(ts != ServerManager.DigitalSocietyPing) + { + ts = ServerManager.DigitalSocietyPing; + lbheartbeat.Text = "Server ping: " + ts.ToString(); + } + }; + } + + //Fun fact. I misspelled this as "TimeSpam." + TimeSpan ts; + + public void OnSkinLoad() + { + ControlManager.SetupControls(this); + } + + public void OnUpgrade() + { + } + + Timer tmr = new Timer(); + + public void Setup() + { + tmr.Start(); + } + } +} diff --git a/ShiftOS.WinForms/DesktopWidgets/HeartbeatWidget.resx b/ShiftOS.WinForms/DesktopWidgets/HeartbeatWidget.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/ShiftOS.WinForms/DesktopWidgets/HeartbeatWidget.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ShiftOS.WinForms/ShiftOS.WinForms.csproj b/ShiftOS.WinForms/ShiftOS.WinForms.csproj index 9fc237f..1607ae9 100644 --- a/ShiftOS.WinForms/ShiftOS.WinForms.csproj +++ b/ShiftOS.WinForms/ShiftOS.WinForms.csproj @@ -298,6 +298,12 @@ CodepointsWidget.cs + + UserControl + + + HeartbeatWidget.cs + UserControl @@ -502,6 +508,9 @@ CodepointsWidget.cs + + HeartbeatWidget.cs + TerminalWidget.cs diff --git a/ShiftOS_TheReturn/ServerManager.cs b/ShiftOS_TheReturn/ServerManager.cs index 0bdfcd9..792b38d 100644 --- a/ShiftOS_TheReturn/ServerManager.cs +++ b/ShiftOS_TheReturn/ServerManager.cs @@ -35,6 +35,7 @@ using ShiftOS; using static ShiftOS.Engine.SaveSystem; using Newtonsoft.Json; using System.Net.Sockets; +using System.Diagnostics; namespace ShiftOS.Engine { @@ -54,6 +55,12 @@ namespace ShiftOS.Engine private static NetObjectClient client { get; set; } private static bool UserDisconnect = false; + public static TimeSpan DigitalSocietyPing + { + get; + private set; + } + public static void Disconnect() { UserDisconnect = true; @@ -139,6 +146,11 @@ namespace ShiftOS.Engine }; client.OnReceived += (o, a) => { + if (PingTimer.IsRunning) + { + DigitalSocietyPing = PingTimer.Elapsed; + PingTimer.Reset(); + } var msg = a.Data.Object as ServerMessage; if (msg.Name == "Welcome") { @@ -207,6 +219,8 @@ namespace ShiftOS.Engine } } + private static Stopwatch PingTimer = new Stopwatch(); + public static void SendMessage(string name, string contents) { var sMsg = new ServerMessage @@ -215,7 +229,7 @@ namespace ShiftOS.Engine Contents = contents, GUID = thisGuid.ToString(), }; - + PingTimer.Start(); client.Send(new NetObject("msg", sMsg)); } diff --git a/ShiftOS_TheReturn/UserManagementCommands.cs b/ShiftOS_TheReturn/UserManagementCommands.cs index 62735a3..1c3c0ed 100644 --- a/ShiftOS_TheReturn/UserManagementCommands.cs +++ b/ShiftOS_TheReturn/UserManagementCommands.cs @@ -53,7 +53,68 @@ namespace ShiftOS.Engine return true; } + [Command("set_acl")] + [RequiresArgument("user")] + [RequiresArgument("val")] + public static bool SetUserPermission(Dictionary args) + { + int permission = 0; + string username = args["user"].ToString(); + try + { + permission = Convert.ToInt32(args["val"].ToString()); + } + catch + { + Console.WriteLine("Error: Permission value must be an integer."); + return true; + } + if(SaveSystem.CurrentSave.Users.FirstOrDefault(x=>x.Username==username) == null) + { + Console.WriteLine("Error: User not found."); + return true; + } + + UserPermissions uperm = UserPermissions.Guest; + + switch (permission) + { + case 0: + uperm = UserPermissions.Guest; + break; + case 1: + uperm = UserPermissions.User; + break; + case 2: + uperm = UserPermissions.Admin; + break; + case 3: + uperm = UserPermissions.Root; + break; + default: + Console.WriteLine("Permission value must be between 0 and 4."); + return true; + } + + //Permissions are backwards... oops... + if(uperm < SaveSystem.CurrentUser.Permissions) + { + Console.WriteLine("Error: Cannot set user permissions to values greater than your own!"); + return true; + } + + var oldperm = SaveSystem.Users.FirstOrDefault(x => x.Username == username).Permissions; + if (SaveSystem.CurrentUser.Permissions > oldperm) + { + Console.WriteLine("Error: Can't set the permission of this user. They have more rights than you."); + return true; + } + + SaveSystem.CurrentSave.Users.FirstOrDefault(x => x.Username == username).Permissions = uperm; + Console.WriteLine("User permissions updated."); + return true; + } }