Esempio n. 1
0
		///// <summary>
		///// Renames and retrieves the specified hotkey. 
		///// The name of the hotkey is only changed if the new name is not already taken by another hotkey.
		///// Returns null if no hotkey is found with the id.
		///// </summary>
		///// <param name="id">The unique identifier of the hotkey.</param>
		///// <param name="newName">The new name to assign to the hotkey.</param>
		///// <returns></returns>
		//public Hotkey RenameHotkey(int id, string newName)
		//{
		//	lock (hotkeys)
		//	{
		//		Hotkey hotkey = hotkeys.Get(id);
		//		if (hotkey == null)
		//			return null;
		//		Hotkey conflict = hotkeys.Get(newName);
		//		if (conflict == null)
		//			hotkey.name = newName;
		//		return hotkey;
		//	}
		//}

		///// <summary>
		///// Sets the effect for the specified hotkey and retrieves the hotkey.
		///// Returns null if the hotkey could not be found.
		///// </summary>
		///// <param name="id">The unique identifier of the hotkey.</param>
		///// <param name="effect">The new effect to assign to the hotkey.</param>
		//public Hotkey SetHotkeyEffect(int id, Effect effect)
		//{
		//	lock (hotkeys)
		//	{
		//		Hotkey hotkey = hotkeys.Get(id);
		//		if (hotkey == null)
		//			return null;
		//		hotkey.effect = effect;
		//		return hotkey;
		//	}
		//}

		/// <summary>
		/// Binds the key for the specified hotkey, and retrieves the hotkey.
		/// Returns null if no hotkey is found with the id.
		/// </summary>
		/// <param name="id">The unique identifier of the hotkey.</param>
		/// <param name="key">The new key to assign to the hotkey.</param>
		/// <returns></returns>
		public Hotkey SetHotkeyKey(int id, int? key)
		{
			lock (hotkeys)
			{
				Hotkey hotkey = hotkeys.Get(id);
				if (hotkey != null)
				{
					hotkey.key = key;
					Save();
				}
				return hotkey;
			}
		}
Esempio n. 2
0
        private static object NamedItemAPI <T>(dynamic requestObj, NamedItemCollection <T> collection, Action saveCollection = null) where T : NamedItem, new()
        {
            if (saveCollection == null)
            {
                saveCollection = (Action)(() => { ServiceWrapper.config.Save(); });
            }
            string cmd           = requestObj.cmd;
            int    idxUnderscore = cmd.LastIndexOf('_');

            if (idxUnderscore > -1 && idxUnderscore + 1 < cmd.Length)
            {
                cmd = cmd.Substring(idxUnderscore + 1);
            }
            switch (cmd)
            {
            case "reorder":
            {
                return(new ResultWithData(collection.Reorder(requestObj.ids.ToObject <List <int> >())));
            }

            case "names":
            {
                return(new ResultWithData(collection.Names()));
            }

            case "list":
            {
                return(new ResultWithData(collection.List()));
            }

            case "new":
            {
                object result = collection.New();
                if (result != null)
                {
                    saveCollection();
                    return(new ResultWithData(result));
                }
                else
                {
                    return(new ResultFailWithReason("unable to create new item"));
                }
            }

            case "get":
            {
                T item = collection.Get((int)requestObj.id);
                if (item != null)
                {
                    return(new ResultWithData(item));
                }
                else
                {
                    return(new ResultFailWithReason("the item was not found"));
                }
            }

            case "update":
            {
                string itemReserialized = JsonConvert.SerializeObject(requestObj.item);
                T      item             = JsonConvert.DeserializeObject <T>(itemReserialized);
                if (collection.Update(item))
                {
                    saveCollection();
                    return(new ResultWithData(item));
                }
                else
                {
                    item = collection.Get(item.id);
                    if (item != null)
                    {
                        return(new ResultFailWithData("the name was already taken", item));
                    }
                    else
                    {
                        return(new ResultFailWithReason("the item was not found"));
                    }
                }
            }

            case "delete":
            {
                collection.Delete((int)requestObj.id);
                saveCollection();
                return(new ResultSuccess());
            }

            default:
                return(null);
            }
        }