Advertisement
Guest User

Aw yeah boi

a guest
Jan 21st, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.17 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3.  
  4. // I didn't HAVE to do this but I wanted to because it's fun.
  5. public struct EmployeeAccess
  6. {
  7.     // Masks are used to only affect certain bits during a bitwise operation
  8.     private const byte MASK_OVERRIDE    = 128;  // 1 000 (00 00)
  9.     private const byte MASK_CLASS       = 112;  // 0 111 (00 00)
  10.     private const byte MASK_PER_AREA    = 12;   // 0 000 (11 00)
  11.     private const byte MASK_PER_LEVEL   = 3;    // 0 000 (00 11)
  12.     private const byte MASK_PER         = 15;   // 0 000 (11 11)
  13.  
  14.     private const byte OVERRIDE         = 128;  // 1 000 (00 00)
  15.  
  16.     public enum Job { Public = 0, Security = 16, Engineer = 32, Scientist = 48, Janitor = 64, Tutor = 80, Medic = 96, Boss = 112 };
  17.     public enum ElevationPermission { Public = 0, Medical = 4, Tech = 8, Security = 12};
  18.     public enum ElevationLevel { Minimum = 0, Partial = 1, Trusted = 2, VIP = 3 };
  19.  
  20.     public byte access { get; private set; }
  21.  
  22.     public void AccessOverride(bool active)
  23.     {
  24.         access = (byte)((access & MASK_OVERRIDE) | ((active ? 1 : 0) << 7));
  25.     }
  26.  
  27.     public void AccessJob(Job job)
  28.     {
  29.         access = (byte)((access & MASK_CLASS) | (byte)job);
  30.     }
  31.  
  32.     public void AccessPermissions(ElevationPermission permission)
  33.     {
  34.         access = (byte)((access & MASK_PER_AREA) | (byte)permission);
  35.     }
  36.  
  37.     public void AccessPermissions(ElevationLevel level)
  38.     {
  39.         access = (byte)((access & MASK_PER_LEVEL) | (byte)level);
  40.     }
  41.  
  42.     public void AccessPermissions(ElevationPermission permission, ElevationLevel level)
  43.     {
  44.         access = (byte)((access & MASK_PER) | (byte)permission | (byte)level);
  45.     }
  46.  
  47.     public bool AccessCompare(byte access)
  48.     {
  49.         // If there is an override active then ignore everything and return true.
  50.         if ((access & MASK_OVERRIDE) == MASK_OVERRIDE)
  51.         {
  52.             Debug.Log("Override Active!");
  53.             return true;
  54.         }
  55.  
  56.         // If the jobs match then return true, else check if the user has special permissions
  57.         if ((access & MASK_CLASS) == (this.access & MASK_CLASS))
  58.         {
  59.             Debug.LogFormat("Correct Class! {0} | {1}", access & MASK_CLASS, this.access & MASK_CLASS);
  60.             return true;
  61.         }
  62.         else
  63.         {
  64.             // Does the user have permission to use this kind of device?
  65.             if ((access & MASK_PER_AREA) == (this.access & MASK_PER_AREA))
  66.             {
  67.                 Debug.LogFormat("Correct Area! {0} | {1}", access & MASK_PER_AREA, this.access & MASK_PER_AREA);
  68.                 // Is the user of an equal or higher permission level?
  69.                 if ((access & MASK_PER_LEVEL) <= (this.access & MASK_PER_LEVEL))
  70.                 {
  71.                     Debug.LogFormat("All Clear! {0} | {1}", access & MASK_PER_LEVEL, this.access & MASK_PER_LEVEL);
  72.                     return true; // All clear!
  73.                 }
  74.  
  75.                 else
  76.                 {
  77.                     Debug.LogFormat("Invalid level! {0} | {1}", access & MASK_PER_LEVEL, this.access & MASK_PER_LEVEL);
  78.                     return false;
  79.                 }
  80.             }
  81.             else
  82.             {
  83.                 Debug.LogFormat("Invalid area! {0} | {1}", access & MASK_PER_AREA, this.access & MASK_PER_AREA);
  84.                 return false;
  85.             }
  86.         }
  87.            
  88.     }
  89.  
  90.     /*
  91.      *  Access Level Layout
  92.      *  Activate on access match (Buttons, Doors, etc)
  93.      *  O1  C123  E1212
  94.      *  [0] {000} (00 | 00)
  95.      *  
  96.      *  [] = Override (Always open if true)
  97.      *  
  98.      *  {} = Classes (Always open for this class)
  99.      *  
  100.      *      000: Public (Always Open)
  101.      *      001: Security
  102.      *      010: Engineer
  103.      *      011: Scientist
  104.      *      100: Janitor
  105.      *      101: Tutors
  106.      *      110: Medic
  107.      *      111: Boss
  108.      *      
  109.      *   (|) = Elevated Permissions (If not overriding and not of class, then compare allowed access areas and granted level)
  110.      *
  111.      *      00: PUBLIC
  112.      *      01: MEDICAL
  113.      *      10: TECH
  114.      *      11: SECURITY
  115.      *      
  116.      *      00: LEVEL I
  117.      *      01: LEVEL II
  118.      *      10: LEVEL III
  119.      *      11: LEVEL IV
  120.      */
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement