Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- // I didn't HAVE to do this but I wanted to because it's fun.
- public struct EmployeeAccess
- {
- // Masks are used to only affect certain bits during a bitwise operation
- private const byte MASK_OVERRIDE = 128; // 1 000 (00 00)
- private const byte MASK_CLASS = 112; // 0 111 (00 00)
- private const byte MASK_PER_AREA = 12; // 0 000 (11 00)
- private const byte MASK_PER_LEVEL = 3; // 0 000 (00 11)
- private const byte MASK_PER = 15; // 0 000 (11 11)
- private const byte OVERRIDE = 128; // 1 000 (00 00)
- public enum Job { Public = 0, Security = 16, Engineer = 32, Scientist = 48, Janitor = 64, Tutor = 80, Medic = 96, Boss = 112 };
- public enum ElevationPermission { Public = 0, Medical = 4, Tech = 8, Security = 12};
- public enum ElevationLevel { Minimum = 0, Partial = 1, Trusted = 2, VIP = 3 };
- public byte access { get; private set; }
- public void AccessOverride(bool active)
- {
- access = (byte)((access & MASK_OVERRIDE) | ((active ? 1 : 0) << 7));
- }
- public void AccessJob(Job job)
- {
- access = (byte)((access & MASK_CLASS) | (byte)job);
- }
- public void AccessPermissions(ElevationPermission permission)
- {
- access = (byte)((access & MASK_PER_AREA) | (byte)permission);
- }
- public void AccessPermissions(ElevationLevel level)
- {
- access = (byte)((access & MASK_PER_LEVEL) | (byte)level);
- }
- public void AccessPermissions(ElevationPermission permission, ElevationLevel level)
- {
- access = (byte)((access & MASK_PER) | (byte)permission | (byte)level);
- }
- public bool AccessCompare(byte access)
- {
- // If there is an override active then ignore everything and return true.
- if ((access & MASK_OVERRIDE) == MASK_OVERRIDE)
- {
- Debug.Log("Override Active!");
- return true;
- }
- // If the jobs match then return true, else check if the user has special permissions
- if ((access & MASK_CLASS) == (this.access & MASK_CLASS))
- {
- Debug.LogFormat("Correct Class! {0} | {1}", access & MASK_CLASS, this.access & MASK_CLASS);
- return true;
- }
- else
- {
- // Does the user have permission to use this kind of device?
- if ((access & MASK_PER_AREA) == (this.access & MASK_PER_AREA))
- {
- Debug.LogFormat("Correct Area! {0} | {1}", access & MASK_PER_AREA, this.access & MASK_PER_AREA);
- // Is the user of an equal or higher permission level?
- if ((access & MASK_PER_LEVEL) <= (this.access & MASK_PER_LEVEL))
- {
- Debug.LogFormat("All Clear! {0} | {1}", access & MASK_PER_LEVEL, this.access & MASK_PER_LEVEL);
- return true; // All clear!
- }
- else
- {
- Debug.LogFormat("Invalid level! {0} | {1}", access & MASK_PER_LEVEL, this.access & MASK_PER_LEVEL);
- return false;
- }
- }
- else
- {
- Debug.LogFormat("Invalid area! {0} | {1}", access & MASK_PER_AREA, this.access & MASK_PER_AREA);
- return false;
- }
- }
- }
- /*
- * Access Level Layout
- * Activate on access match (Buttons, Doors, etc)
- * O1 C123 E1212
- * [0] {000} (00 | 00)
- *
- * [] = Override (Always open if true)
- *
- * {} = Classes (Always open for this class)
- *
- * 000: Public (Always Open)
- * 001: Security
- * 010: Engineer
- * 011: Scientist
- * 100: Janitor
- * 101: Tutors
- * 110: Medic
- * 111: Boss
- *
- * (|) = Elevated Permissions (If not overriding and not of class, then compare allowed access areas and granted level)
- *
- * 00: PUBLIC
- * 01: MEDICAL
- * 10: TECH
- * 11: SECURITY
- *
- * 00: LEVEL I
- * 01: LEVEL II
- * 10: LEVEL III
- * 11: LEVEL IV
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement