Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- //Read/Write to file
- using System.IO;
- namespace SpecRPG
- {
- public class Scorekeeper
- {
- // Register botname so we know what directory to look in for player scores
- // Do this in initialize method
- private string m_BotName;
- private int NextID = 0;
- public void RegisterBotName(string BotName)
- {
- this.m_BotName = BotName;
- CalculateNextID();
- }
- // Check to see if we have any records for PlayerName
- public bool NewPlayer(string PlayerName)
- {
- if (GetPlayerID(PlayerName) == -1)
- // If player is not found - he is a new player
- return true;
- else
- // Player WAS found
- return false;
- }
- // This grabs the info from the Database
- // we are using read/write for now
- // Upload info from file inside /Scorekeeper/Scores
- public SPlayer GetPlayer(string PlayerName)
- {
- SPlayer p = new SPlayer();
- p.PlayerName = PlayerName;
- int PlayerID = GetPlayerID(PlayerName);
- string fileToLoad = m_BotName + "/Scorekeeper/Scores/"+PlayerID.ToString()+".info";
- // Looking for the score file
- if (File.Exists(fileToLoad))
- {
- try
- {
- //Opening appropriate file
- StreamReader SR = File.OpenText(fileToLoad);
- //used to read each line
- string S;
- // Looks through all lines in your document
- while ((S = SR.ReadLine()) != null)
- {
- // This is were you are going to fill in all your player info
- // Just repeat what i do for all vars - ill do weapon and lvl
- // ------------------ Level
- if (S.StartsWith("Level:"))
- {
- // Splits the string into an array
- // using the ":" at the point to split
- // we will only have 2 string since we do Setting:Value
- // so we use info[0] for setting and info[1] for value
- string[] info = S.Split(':');
- p.Level = int.Parse(info[1]);
- // Use this as a short way of doing the above
- // You wouldnt need : string[] info = S.Split(':');
- // You would just use the line below uncommented
- // p.Level = int.Parse(S.Split(':')[1]);
- }
- // ------------------ Weapon
- // Shortened way of splitting a string as shown above
- else if (S.StartsWith("Weapon:"))
- { p.Weapon = S.Split(':')[1]; }
- // Continue your vars here
- }
- // Closing file
- SR.Close();
- return p;
- }
- catch (Exception x)
- {
- }
- }
- // We should NEVER reach this code
- // If we did, somehow the player list contains a player
- // and ID with no matching player score file
- return null;
- }
- public void UpdatePlayer(SPlayer Player)
- {
- // retrieve player id using name
- int ID = GetPlayerID(Player.PlayerName);
- // If player just registered we add him to List first
- if (ID == -1)
- {
- // assign next ID
- Player.playerid = NextID;
- // Increment our NextID
- NextID+=1;
- // Store Info
- File.AppendAllText(m_BotName + "/Scorekeeper/Players.info", Player.PlayerName + "ß" + Player.playerid + Environment.NewLine);
- // Update our method variable
- ID = Player.playerid;
- }
- // Create/Overwrite old file
- using (System.IO.StreamWriter file = new System.IO.StreamWriter(m_BotName + "/Scorekeeper/Scores/" + ID.ToString() + ".info"))
- {
- // ---------- Update level
- file.WriteLine("Level:" + Player.Level);
- // ---------- Update Weapon
- file.WriteLine("Weapon:" + Player.Weapon);
- }
- }
- // Method to look into our master player list on Database
- // We are using file read/write for the time being
- private int GetPlayerID(string PlayerName)
- {
- string fileToLoad = m_BotName + "/Scorekeeper/Players.info";
- // Looking for the Master Player List file
- if (File.Exists(fileToLoad))
- {
- try
- {
- //Opening appropriate file
- StreamReader SR = File.OpenText(fileToLoad);
- //used to read each line
- string S;
- while ((S = SR.ReadLine()) != null)
- {
- // We use beta because it is not allowed in an SS name
- // So we guarantee a good parse
- // if we use something allowed in an ss name we could return bad info
- if (S.Contains('ß'))
- {
- string[] info = S.Split('ß');
- // If we find player on list he is NOT new
- // Return false
- if (info[0] == PlayerName)
- return int.Parse(info[1]);
- }
- }
- // Closing file
- SR.Close();
- }
- catch (Exception x)
- {
- }
- }
- // If we do not find id we return -1
- return -1;
- }
- private void CalculateNextID()
- {
- string fileToLoad = m_BotName + "/Scorekeeper/Players.info";
- // Looking for the Master Player List file
- if (File.Exists(fileToLoad))
- {
- try
- {
- //Opening appropriate file
- StreamReader SR = File.OpenText(fileToLoad);
- //used to read each line
- string S;
- while ((S = SR.ReadLine()) != null)
- {
- if (S.Contains('ß'))
- { NextID += 1; }
- }
- // Closing file
- SR.Close();
- }
- catch (Exception x){}
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement