Advertisement
PsyOps

IDIncluded

May 7th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. //Read/Write to file
  6. using System.IO;
  7.  
  8. namespace SpecRPG
  9. {
  10.     public class Scorekeeper
  11.     {
  12.         // Register botname so we know what directory to look in for player scores
  13.         // Do this in initialize method
  14.         private string m_BotName;
  15.         private int NextID = 0;
  16.         public void RegisterBotName(string BotName)
  17.         {
  18.             this.m_BotName = BotName;
  19.             CalculateNextID();
  20.         }
  21.  
  22.         // Check to see if we have any records for PlayerName
  23.         public bool NewPlayer(string PlayerName)
  24.         {
  25.             if (GetPlayerID(PlayerName) == -1)
  26.                 // If player is not found - he is a new player
  27.                 return true;
  28.             else
  29.                 // Player WAS found
  30.                 return false;
  31.         }
  32.  
  33.         // This grabs the info from the Database
  34.         // we are using read/write for now
  35.         // Upload info from file inside /Scorekeeper/Scores
  36.         public SPlayer GetPlayer(string PlayerName)
  37.         {
  38.             SPlayer p = new SPlayer();
  39.             p.PlayerName = PlayerName;
  40.             int PlayerID = GetPlayerID(PlayerName);
  41.  
  42.             string fileToLoad = m_BotName + "/Scorekeeper/Scores/"+PlayerID.ToString()+".info";
  43.  
  44.             // Looking for the score file
  45.             if (File.Exists(fileToLoad))
  46.             {
  47.                 try
  48.                 {
  49.                     //Opening appropriate file
  50.                     StreamReader SR = File.OpenText(fileToLoad);
  51.                     //used to read each line
  52.                     string S;
  53.                     // Looks through all lines in your document
  54.                     while ((S = SR.ReadLine()) != null)
  55.                     {
  56.                         // This is were you are going to fill in all your player info
  57.                         // Just repeat what i do for all vars - ill do weapon and lvl
  58.  
  59.                         // ------------------ Level
  60.                         if (S.StartsWith("Level:"))
  61.                         {
  62.                             // Splits the string into an array
  63.                             // using the ":" at the point to split
  64.                             // we will only have 2 string since we do Setting:Value
  65.                             // so we use info[0] for setting and info[1] for value
  66.                             string[] info = S.Split(':');
  67.  
  68.                             p.Level = int.Parse(info[1]);
  69.  
  70.                             // Use this as a short way of doing the above
  71.                             // You wouldnt need : string[] info = S.Split(':');
  72.                             // You would just use the line below uncommented
  73.                             // p.Level = int.Parse(S.Split(':')[1]);
  74.                         }
  75.  
  76.                         // ------------------ Weapon
  77.                         // Shortened way of splitting a string as shown above
  78.                         else if (S.StartsWith("Weapon:"))
  79.                         {    p.Weapon = S.Split(':')[1];    }
  80.                        
  81.                         // Continue your vars here
  82.                     }
  83.                     // Closing file
  84.                     SR.Close();
  85.  
  86.                     return p;
  87.                 }
  88.                 catch (Exception x)
  89.                 {
  90.                 }
  91.             }
  92.  
  93.             // We should NEVER reach this code
  94.             // If we did, somehow the player list contains a player
  95.             // and ID with no matching player score file
  96.             return null;
  97.         }
  98.  
  99.         public void UpdatePlayer(SPlayer Player)
  100.         {
  101.             // retrieve player id using name
  102.             int ID = GetPlayerID(Player.PlayerName);
  103.  
  104.             // If player just registered we add him to List first
  105.             if (ID == -1)
  106.             {
  107.                 // assign next ID
  108.                 Player.playerid = NextID;
  109.                 // Increment our NextID
  110.                 NextID+=1;
  111.                 // Store Info
  112.                 File.AppendAllText(m_BotName + "/Scorekeeper/Players.info", Player.PlayerName + "ß" + Player.playerid + Environment.NewLine);
  113.                 // Update our method variable
  114.                 ID = Player.playerid;
  115.             }
  116.  
  117.             // Create/Overwrite old file
  118.             using (System.IO.StreamWriter file = new System.IO.StreamWriter(m_BotName + "/Scorekeeper/Scores/" + ID.ToString() + ".info"))
  119.             {
  120.                 // ---------- Update level
  121.                 file.WriteLine("Level:" + Player.Level);
  122.                 // ---------- Update Weapon
  123.                 file.WriteLine("Weapon:" + Player.Weapon);
  124.             }
  125.         }
  126.  
  127.         // Method to look into our master player list on Database
  128.         // We are using file read/write for the time being
  129.         private int GetPlayerID(string PlayerName)
  130.         {
  131.             string fileToLoad = m_BotName + "/Scorekeeper/Players.info";
  132.  
  133.             // Looking for the Master Player List file
  134.             if (File.Exists(fileToLoad))
  135.             {
  136.                 try
  137.                 {
  138.                     //Opening appropriate file
  139.                     StreamReader SR = File.OpenText(fileToLoad);
  140.                     //used to read each line
  141.                     string S;
  142.                     while ((S = SR.ReadLine()) != null)
  143.                     {
  144.                         // We use beta because it is not allowed in an SS name
  145.                         // So we guarantee a good parse
  146.                         // if we use something allowed in an ss name we could return bad info
  147.                         if (S.Contains('ß'))
  148.                         {
  149.                             string[] info = S.Split('ß');
  150.  
  151.                             // If we find player on list he is NOT new
  152.                             // Return false
  153.                             if (info[0] == PlayerName)
  154.                                 return int.Parse(info[1]);
  155.                         }
  156.                     }
  157.                     // Closing file
  158.                     SR.Close();
  159.                 }
  160.                 catch (Exception x)
  161.                 {
  162.                 }
  163.             }
  164.             // If we do not find id we return -1
  165.             return -1;
  166.         }
  167.  
  168.  
  169.         private void CalculateNextID()
  170.         {
  171.             string fileToLoad = m_BotName + "/Scorekeeper/Players.info";
  172.  
  173.             // Looking for the Master Player List file
  174.             if (File.Exists(fileToLoad))
  175.             {
  176.                 try
  177.                 {
  178.                     //Opening appropriate file
  179.                     StreamReader SR = File.OpenText(fileToLoad);
  180.                     //used to read each line
  181.                     string S;
  182.                     while ((S = SR.ReadLine()) != null)
  183.                     {
  184.                         if (S.Contains('ß'))
  185.                         { NextID += 1; }
  186.                     }
  187.                     // Closing file
  188.                     SR.Close();
  189.                 }
  190.                 catch (Exception x){}
  191.             }
  192.         }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement