Advertisement
PsyOps

DisplayManager

Nov 21st, 2015
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using BattleCore.Events;
  7. using BattleCorePsyOps;
  8.  
  9. namespace BaseDuel
  10. {
  11.     public class DisplayManager
  12.     {
  13.         ShortChat msg = new ShortChat();
  14.  
  15.         // Old and new display - compare one to the other to see which lvz to toggle
  16.         private Dictionary<ushort, bool> m_OldGlobalDisplayState = new Dictionary<ushort, bool>();
  17.         private Dictionary<ushort, bool> m_GlobalDisplay = new Dictionary<ushort, bool>();
  18.         /// <summary>
  19.         /// Master list of all score boards created
  20.         /// </summary>
  21.         private Dictionary<string, ushort[]> m_ScoreBoardList = new Dictionary<string, ushort[]>();
  22.         /// <summary>
  23.         /// Single display is nothing more than a word associated with a lvz
  24.         /// If you want to store: ScorboardBackground to be able to load/unload using "scoreBG" or something
  25.         /// </summary>
  26.         private Dictionary<string, ushort> m_SingleDisplayList = new Dictionary<string, ushort>();
  27.  
  28.         // Update timestamp duh
  29.         private DateTime m_UpdateTimeStamp = DateTime.Now;
  30.         // How often do we want to update gfx - ms
  31.         private double m_UpdateInterval = 25;
  32.  
  33.         // Packet allows for 250 updates - give or take
  34.         private int m_PacketLimit = 250;
  35.  
  36.        /// <summary>
  37.        /// Register a single lvz to the public lvz list.
  38.        /// This goes directly into the public display: use this is you dont need any way of accessing other than lvz id
  39.        /// If you prefer to name the lvz and access it that way use RegisterSingleDisplay and load it.
  40.        /// </summary>
  41.        /// <param name="LvzID">Lvz ID Number</param>
  42.         public void RegisterLvz_Pub(ushort LvzID)
  43.         {
  44.             // Add lvz to both lists
  45.             m_OldGlobalDisplayState.Add(LvzID, false);
  46.             m_GlobalDisplay.Add(LvzID, false);
  47.         }
  48.         /// <summary>
  49.         /// Score Board used for a player. Assumption is that it uses 0-9 and x amount of digits
  50.         /// Scoreboards can be displays for points, currency etc.
  51.         /// EXAMPLE: An example of a point scoreboard that has
  52.         /// points (1 - 1million) and the lvz start at 2000
  53.         /// would be configured as follows:
  54.         /// RegisterScoreBoard("PointScoreBoard", 2000 , 7 )
  55.         /// </summary>
  56.         /// <param name="sb_name">Name of your scoreboard: example= "Points"</param>
  57.         /// <param name="start_index">The first lvz number in your display lvz range.
  58.         /// Your lvz should start in the ones place and start from 0 and work their way up to 9.
  59.         /// Starting at lvz id 2000, your 0 digit will be 2000, 1 = 2001, .. 10 = 2010 ... 100 = 2100 etc.
  60.         /// </param>
  61.         /// <param name="digits">
  62.         /// How many places your scoreboard needs. For points ranging from 1 to 9999 it would be 4 digits.
  63.         /// </param>
  64.         public void RegisterScoreBoard(string sb_name, int start_index, int digits)
  65.         {
  66.             List<ushort> lvz_ids = new List<ushort>();
  67.  
  68.             // Run through lvz range and add them to our list
  69.             for (int i = start_index; i < start_index + digits * 10; i += 1)
  70.                 lvz_ids.Add((ushort)i);
  71.  
  72.             // Register list to our local master list
  73.             m_ScoreBoardList.Add(sb_name, lvz_ids.ToArray());
  74.         }
  75.         /// <summary>
  76.         /// This gives you the ability to name a lvz in order to load and unload a display when you want.
  77.         /// After you register you must load it to use it.
  78.         /// </summary>
  79.         /// <param name="DisplayName">Name you want to give the lvz.
  80.         /// Example: ScoreboardBackground</param>
  81.         /// <param name="LvzID">Lvz ID Number</param>
  82.         public void RegisterSingleDisplay(string DisplayName, ushort LvzID)
  83.         {
  84.             m_SingleDisplayList.Add(DisplayName, LvzID);
  85.         }
  86.  
  87.         /// <summary>
  88.         /// (Public)
  89.         /// Toggle a lvz by its display name.
  90.         /// EXAMPLE: "scoreboardbackground", true
  91.         /// </summary>
  92.         /// <param name="DisplayName">Name that you gave your display: "Background"</param>
  93.         /// <param name="state">If you want it on or off:  True/False</param>
  94.         public void SetDisplayState_Pub(string DisplayName, bool state)
  95.         {
  96.             ushort lvzid;
  97.  
  98.             if (m_SingleDisplayList.TryGetValue(DisplayName, out lvzid))
  99.                 m_GlobalDisplay[lvzid] = state;
  100.         }
  101.  
  102.         /// <summary>
  103.         /// (Private)
  104.         /// Toggle a lvz by its display name.
  105.         /// EXAMPLE: "scoreboardbackground", true
  106.         /// </summary>
  107.         /// <param name="dp">User</param>
  108.         /// <param name="DisplayName">Name you gave the display</param>
  109.         /// <param name="state"> on/off : True/False</param>
  110.         public void SetDisplayState_Priv(DevaPlayer dp, string DisplayName, bool state)
  111.         {
  112.             ushort lvzid;
  113.  
  114.             if (m_SingleDisplayList.TryGetValue(DisplayName, out lvzid))
  115.                 dp.LvzDisplay[lvzid] = state;
  116.         }
  117.  
  118.         /// <summary>
  119.         /// Load the display to your public lvz list.
  120.         /// Use SetDisplayState to toggle lvz from here.
  121.         /// </summary>
  122.         /// <param name="DisplayName">Name you gave to your display.</param>
  123.         public void LoadSingleDisplay_Pub(string DisplayName)
  124.         {
  125.             ushort lvzid;
  126.  
  127.             if (m_SingleDisplayList.TryGetValue(DisplayName, out lvzid))
  128.             {
  129.                 // if lvz isnt already included it will add it here
  130.                 m_GlobalDisplay[lvzid] = false;
  131.                 m_OldGlobalDisplayState[lvzid] = false;
  132.             }
  133.         }
  134.  
  135.         /// <summary>
  136.         /// Load the display to a user's lvz list.
  137.         /// Use SetDisplayState to toggle lvz from here.
  138.         /// </summary>
  139.         /// <param name="DisplayName">Name you gave to your display.</param>
  140.         public void LoadSingleDisplay_Priv(string DisplayName, DevaPlayer dp)
  141.         {
  142.             ushort lvzid;
  143.  
  144.             if (m_SingleDisplayList.TryGetValue(DisplayName, out lvzid))
  145.             {
  146.                 // if lvz isnt already included it will add it here
  147.                 dp.LvzDisplay[lvzid] = false;
  148.                 dp.OldLvzDisplayState[lvzid] = false;
  149.             }
  150.         }
  151.  
  152.         /// <summary>
  153.         /// (Private)
  154.         /// Change the lvz to reflect the new score. This will turn all lvz off then
  155.         /// only toggle the lvz associated to new score.
  156.         /// </summary>
  157.         /// <param name="dp">User</param>
  158.         /// <param name="scoreboard">Name you gave scoreboard</param>
  159.         /// <param name="newscore">New score</param>
  160.         public void ScoreChange_Priv(DevaPlayer dp, string scoreboard, int newscore)
  161.         {
  162.             // Container for lvz ids contained in our master list of of scoreboards
  163.             ushort[] alldisplaylvz;
  164.             // Grab the list from our master list
  165.             if (m_ScoreBoardList.TryGetValue(scoreboard, out alldisplaylvz))
  166.             {
  167.                 // Holds the individual digits
  168.                 List<int> listOfInts = new List<int>();
  169.                 // Holds the digits converted into lvz ids
  170.                 List<ushort> newscoreids = new List<ushort>();
  171.  
  172.                 // Splits the score up into individual digits
  173.                 // and inverts them so we can start converting into
  174.                 //lvz starting with ones,tens,hund...
  175.                 while (newscore > 0)
  176.                 {
  177.                     listOfInts.Add(newscore % 10);
  178.                     newscore = newscore / 10;
  179.                 }
  180.  
  181.                 // We grab lvzList[0] and use it for our start index of lvz
  182.                 // and use it to convert to lvz from that point
  183.                 for (int i = 0; i < listOfInts.Count; i += 1)
  184.                     newscoreids.Add((ushort)(alldisplaylvz[0] + (10 * i) + listOfInts[i]));
  185.  
  186.                 // First we toggle all lvz off
  187.                 for (int i = 0; i < alldisplaylvz.Length; i += 1)
  188.                     dp.LvzDisplay[alldisplaylvz[i]] = false;
  189.  
  190.                 // Then we turn on the lvz for our new score
  191.                 for (int i = 0; i < newscoreids.Count; i += 1)
  192.                     dp.LvzDisplay[newscoreids[i]] = true;
  193.             }
  194.         }
  195.  
  196.         /// <summary>
  197.         /// (Public)
  198.         /// Change the lvz to reflect the new score. This will turn all lvz off then
  199.         /// only toggle the lvz associated to new score.
  200.         /// </summary>
  201.         /// <param name="scoreboard">Name you gave scoreboard</param>
  202.         /// <param name="newscore">New score</param>
  203.         public void ScoreChange_Pub( string scoreboard, int newscore)
  204.         {
  205.             // Container for lvz ids contained in our master list of scoreboards
  206.             ushort[] alldisplaylvz;
  207.             // Grab the list from our master list
  208.             if (m_ScoreBoardList.TryGetValue(scoreboard, out alldisplaylvz))
  209.             {
  210.                 // Holds the individual digits
  211.                 List<int> listOfInts = new List<int>();
  212.                 // Holds the digits converted into lvz ids
  213.                 List<ushort> newscoreids = new List<ushort>();
  214.  
  215.                 // Splits the score up into individual digits
  216.                 // and inverts them so we can start converting into
  217.                 //lvz starting with ones,tens,hund...
  218.                 while (newscore > 0)
  219.                 {
  220.                     listOfInts.Add(newscore % 10);
  221.                     newscore = newscore / 10;
  222.                 }
  223.  
  224.                 // We grab lvzList[0] and use it for our start index of lvz
  225.                 // and use it to convert to lvz from that point
  226.                 for (int i = 0; i < listOfInts.Count; i += 1)
  227.                     newscoreids.Add((ushort)(alldisplaylvz[0] + (10 * i) + listOfInts[i]));
  228.  
  229.                 // First we toggle all lvz off
  230.                 for (int i = 0; i < alldisplaylvz.Length; i += 1)
  231.                     m_GlobalDisplay[alldisplaylvz[i]] = false;
  232.  
  233.                 // Then we turn on the lvz for our new score
  234.                 for (int i = 0; i < newscoreids.Count; i += 1)
  235.                     m_GlobalDisplay[newscoreids[i]] = true;
  236.             }
  237.         }
  238.         /// <summary>
  239.         /// This will load a Scoreboard to a users profile so we can check update lvz as they are toggled.
  240.         /// Add/Remove them as status changes.
  241.         /// </summary>
  242.         /// <param name="dp">Player Profile</param>
  243.         /// <param name="scoreboard">Name you gave scoreboard</param>
  244.         /// <param name="q">Chat Q so we can add the lvz toggle commands.</param>
  245.         public void LoadScoreboard_Priv(DevaPlayer dp, string scoreboard)
  246.         {
  247.             ushort[] lvzList;
  248.  
  249.             if (m_ScoreBoardList.TryGetValue(scoreboard, out lvzList))
  250.             {
  251.                 for (int i = 0; i < lvzList.Length; i += 1)
  252.                 {
  253.                     dp.LvzDisplay.Add(lvzList[i], false);
  254.                     dp.OldLvzDisplayState.Add(lvzList[i], false);
  255.                 }
  256.             }
  257.         }
  258.  
  259.         /// <summary>
  260.         /// This will unload a display from a users profile.
  261.         /// </summary>
  262.         /// <param name="dp">Player Profile</param>
  263.         /// <param name="scoreboard">Name you gave scoreboard</param>
  264.         /// <param name="q">Chat Q so we can add the lvz toggle commands.</param>
  265.         public void UnloadScoreboard_Priv(DevaPlayer dp, string scoreboard)
  266.         {
  267.             ushort[] lvzList;
  268.  
  269.             if (m_ScoreBoardList.TryGetValue(scoreboard, out lvzList))
  270.                 for (int i = 0; i < lvzList.Length; i += 1)
  271.                 {
  272.                     if (dp.LvzDisplay.ContainsKey(lvzList[i]))
  273.                     {
  274.                         dp.LvzDisplay.Remove(lvzList[i]);
  275.                         dp.OldLvzDisplayState.Remove(lvzList[i]);
  276.                     }
  277.                 }
  278.         }
  279.  
  280.         /// <summary>
  281.         /// Loads a scoreboard to active lvz list.
  282.         /// We just have to set score from here.
  283.         /// </summary>
  284.         /// <param name="scoreboard">Name you gave scoreboard</param>
  285.         public void LoadScoreboard_Pub(string scoreboard)
  286.         {
  287.             ushort[] lvzList;
  288.  
  289.             if (m_ScoreBoardList.TryGetValue(scoreboard, out lvzList))
  290.             {
  291.                 for (int i = 0; i < lvzList.Length; i += 1)
  292.                 {
  293.                     // if lvz isnt already included it will add it here
  294.                     m_GlobalDisplay[lvzList[i]] = false;
  295.                     m_OldGlobalDisplayState[lvzList[i]] = false;
  296.                 }
  297.             }
  298.         }
  299.  
  300.         /// <summary>
  301.         /// Unloads a scoreboard from the active lvz list
  302.         /// </summary>
  303.         /// <param name="scoreboard">Name you gave scoreboard</param>
  304.         public void UnloadScoreboard_Pub(string scoreboard)
  305.         {
  306.             ushort[] lvzList;
  307.  
  308.             if (m_ScoreBoardList.TryGetValue(scoreboard, out lvzList))
  309.                 for (int i = 0; i < lvzList.Length; i += 1)
  310.                 {
  311.                     if (m_GlobalDisplay.ContainsKey(lvzList[i]))
  312.                     {
  313.                         m_GlobalDisplay.Remove(lvzList[i]);
  314.                         m_OldGlobalDisplayState.Remove(lvzList[i]);
  315.                     }
  316.                 }
  317.         }
  318.         /// <summary>
  319.         /// This sends the needed packet out to change lvz.
  320.         /// This should be sent after you ave finished modifying the display.
  321.         /// Example: Update Points, Update Currency, etc ... Then Refresh Display.
  322.         /// </summary>
  323.         /// <param name="dp"></param>
  324.         /// <param name="q"></param>
  325.         public void RefreshDisplay_Priv(DevaPlayer dp, Queue<EventArgs> q)
  326.         {
  327.             // List of lvz to update
  328.             Dictionary<ushort, bool> updateList = new Dictionary<ushort, bool>();
  329.  
  330.             // Itirate through entire list
  331.             foreach (ushort id in dp.LvzDisplay.Keys)
  332.             {
  333.                 // Check if lvz needs updating
  334.                 if (dp.LvzDisplay[id] != dp.OldLvzDisplayState[id])
  335.                 {
  336.                     // add update to list
  337.                     updateList.Add(id, dp.LvzDisplay[id]);
  338.                     // update the users old display
  339.                     dp.OldLvzDisplayState[id] = !dp.OldLvzDisplayState[id];
  340.                 }
  341.             }
  342.  
  343.             // Pack the event into q
  344.             if (updateList.Count > 0)
  345.             {
  346.                 LVZToggleEvent update = new LVZToggleEvent();
  347.                 update.TargetPlayerId = dp.PlayerID;
  348.                 update.LVZObjects = updateList;
  349.                 q.Enqueue(update);
  350.             }
  351.         }
  352.         /// <summary>
  353.         /// Attach this to your game timer. This will fill the chat q with all the
  354.         /// needed updates for Public lvz.
  355.         /// </summary>
  356.         /// <returns>Chat Q full of lvz update commands - returns null if empty</returns>
  357.         public EventArgs RefreshDisplay_Pub()
  358.         {
  359.             // Dont update unless its time
  360.             if ((DateTime.Now - m_UpdateTimeStamp).TotalMilliseconds < m_UpdateInterval) return null;
  361.            
  362.             // Update Timestamp
  363.             m_UpdateTimeStamp = DateTime.Now;
  364.  
  365.              // List of lvz to update
  366.             Dictionary<ushort, bool> updateList = new Dictionary<ushort, bool>();
  367.  
  368.             // Itirate through entire list
  369.             foreach (ushort id in m_OldGlobalDisplayState.Keys.ToList())
  370.             {
  371.                 // Check if update is needed
  372.                 if (m_OldGlobalDisplayState[id] != m_GlobalDisplay[id])
  373.                 {
  374.                     // Reached limit for a packet
  375.                     if (updateList.Count >= m_PacketLimit)
  376.                     {
  377.                         // Pack it up and send it out
  378.                         LVZToggleEvent update = new LVZToggleEvent();
  379.                         update.TargetPlayerId = 0xFFFF; // pub
  380.  
  381.                         // Send it back - leave the rest of updates for next refresh
  382.                         return update;
  383.                     }
  384.                     // add update to list
  385.                     updateList.Add(id, m_GlobalDisplay[id]);
  386.                     // update the users old display
  387.                     m_OldGlobalDisplayState[id] = m_GlobalDisplay[id];
  388.                 }
  389.             }
  390.  
  391.             // Update is needed - pack it up and send it out
  392.             if (updateList.Count > 0)
  393.             {
  394.                 // Pack it up and send it out
  395.                 LVZToggleEvent update = new LVZToggleEvent();
  396.                 update.TargetPlayerId = 0xFFFF; // pub
  397.                 update.LVZObjects = updateList;
  398.  
  399.                 // send it back
  400.                 return update;
  401.             }
  402.  
  403.             // No update needed
  404.             return null;
  405.         }
  406.     }
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement