Created
September 18, 2014 22:49
-
-
Save JPVenson/194b1f5923ae244c8052 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Sandbox; | |
using Sandbox.ModAPI; | |
namespace ClassLibrary1 | |
{ | |
public class Class1 | |
{ | |
public const string CommandNotFound = "Command not found"; | |
public Class1() | |
{ | |
Sandbox.ModAPI.MyAPIGateway.Utilities.MessageEntered += UtilitiesOnMessageEntered; | |
WrapAction("addprop", AddProp); | |
WrapAction("player", StartPlayerControl); | |
} | |
public class PlayerController | |
{ | |
public PlayerController() | |
{ | |
_delegateLookup.Add("Help", WriteHelp); | |
_delegateLookup.Add("Teleport", Teleport); | |
} | |
public bool Done { get; set; } | |
private Action<string> onReturnRequest; | |
public bool HandleRequest(string req) | |
{ | |
if (!Done && onReturnRequest != null) | |
{ | |
onReturnRequest(req); | |
return Done; | |
} | |
var firstOrDefault = _delegateLookup.Select(s => s as KeyValuePair<string, Action>?).FirstOrDefault(s => req.ToLower().StartsWith("/" + s.Value.Key.ToLower())); | |
if (firstOrDefault != null) | |
{ | |
firstOrDefault.Value.Value(); | |
return Done; | |
} | |
else | |
{ | |
Sandbox.ModAPI.MyAPIGateway.Utilities.SendMessage(Class1.CommandNotFound); | |
return true; | |
} | |
} | |
private Dictionary<string, Action> _delegateLookup = new Dictionary<string, Action>(); | |
private void WriteHelp() | |
{ | |
Sandbox.ModAPI.MyAPIGateway.Utilities.SendMessage(Class1.CommandNotFound + " sry"); | |
} | |
private void Teleport() | |
{ | |
var listOfPlayers = new List<IMyPlayer>(); | |
Sandbox.ModAPI.MyAPIGateway.Players.GetPlayers(listOfPlayers); | |
var query = | |
listOfPlayers.Select(s => s.SteamUserId + ":" + s.DisplayName).Aggregate((s, e) => s + "," + Environment.NewLine + e); | |
Sandbox.ModAPI.MyAPIGateway.Utilities.SendMessage("Select ID of Player: " + Environment.NewLine + query); | |
Done = false; | |
onReturnRequest = s => | |
{ | |
uint outp; | |
var tryParse = uint.TryParse(s, out outp); | |
if (!tryParse) | |
{ | |
Sandbox.ModAPI.MyAPIGateway.Utilities.SendMessage("Wrong ID"); | |
Done = true; | |
return; | |
} | |
Sandbox.ModAPI.MyAPIGateway.Utilities.SendMessage("Selected Player with steam ID:" + outp); | |
}; | |
} | |
} | |
private bool? _onAction; | |
private Action<string> onReturnRequest; | |
PlayerController controller = new PlayerController(); | |
private void WrapAction(string name, Action action) | |
{ | |
this._delegateLookup.Add(name, () => | |
{ | |
if (_onAction == true) | |
return; | |
try | |
{ | |
_onAction = true; | |
action(); | |
} | |
finally | |
{ | |
if (_onAction != null) | |
_onAction = false; | |
} | |
}); | |
} | |
private void StartPlayerControl() | |
{ | |
onReturnRequest = s => | |
{ | |
var isHandeld = controller.HandleRequest(s); | |
if (!isHandeld) | |
StartPlayerControl(); | |
_onAction = null; | |
}; | |
} | |
private void AddProp() | |
{ | |
Sandbox.ModAPI.MyAPIGateway.Utilities.SendMessage("Player Control Active"); | |
return; | |
onReturnRequest = s => | |
{ | |
int result; | |
var tryParse = int.TryParse(s, out result); | |
if (!tryParse) | |
{ | |
Sandbox.ModAPI.MyAPIGateway.Utilities.SendMessage("Möööp wrong ... must be a Number"); | |
} | |
//Sandbox.ModAPI.MyAPIGateway.Entities.AddEntity(new); | |
}; | |
Sandbox.ModAPI.MyAPIGateway.Utilities.SendMessage("Please enter the prop ID"); | |
} | |
private Dictionary<string, Action> _delegateLookup = new Dictionary<string, Action>(); | |
private void UtilitiesOnMessageEntered(string messageText, ref bool sendToOthers) | |
{ | |
if (_onAction == true) | |
{ | |
if (onReturnRequest != null) | |
onReturnRequest(messageText); | |
return; | |
} | |
// ReSharper disable once PossibleInvalidOperationException | |
var firstOrDefault = _delegateLookup.Select(s => s as KeyValuePair<string, Action>?).FirstOrDefault(s => messageText.ToLower().StartsWith("/" + s.Value.Key.ToLower())); | |
if (firstOrDefault != null) | |
{ | |
sendToOthers = false; | |
firstOrDefault.Value.Value(); | |
} | |
else | |
{ | |
Sandbox.ModAPI.MyAPIGateway.Utilities.SendMessage(Class1.CommandNotFound); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment