admin.set_acl and other things

This commit is contained in:
Michael 2017-05-05 11:34:17 -04:00
parent a57b5855f5
commit 39b3b4c62c
7 changed files with 477 additions and 85 deletions

View file

@ -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<Point>(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<Point>(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()

View file

@ -0,0 +1,62 @@
namespace ShiftOS.WinForms.DesktopWidgets
{
partial class HeartbeatWidget
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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;
}
}

View file

@ -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();
}
}
}

View file

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View file

@ -298,6 +298,12 @@
<Compile Include="DesktopWidgets\CodepointsWidget.Designer.cs">
<DependentUpon>CodepointsWidget.cs</DependentUpon>
</Compile>
<Compile Include="DesktopWidgets\HeartbeatWidget.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="DesktopWidgets\HeartbeatWidget.Designer.cs">
<DependentUpon>HeartbeatWidget.cs</DependentUpon>
</Compile>
<Compile Include="DesktopWidgets\TerminalWidget.cs">
<SubType>UserControl</SubType>
</Compile>
@ -502,6 +508,9 @@
<EmbeddedResource Include="DesktopWidgets\CodepointsWidget.resx">
<DependentUpon>CodepointsWidget.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="DesktopWidgets\HeartbeatWidget.resx">
<DependentUpon>HeartbeatWidget.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="DesktopWidgets\TerminalWidget.resx">
<DependentUpon>TerminalWidget.cs</DependentUpon>
</EmbeddedResource>

View file

@ -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));
}

View file

@ -53,7 +53,68 @@ namespace ShiftOS.Engine
return true;
}
[Command("set_acl")]
[RequiresArgument("user")]
[RequiresArgument("val")]
public static bool SetUserPermission(Dictionary<string, object> 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;
}
}