PsyOps

BZMYSQL

Jan 25th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using BattleCore;
  7. using BattleCore.Events;
  8. using PsyModules;
  9. using System.IO;
  10.  
  11. using MySql.Data.MySqlClient;
  12. using System.Diagnostics;
  13.  
  14. namespace Battle
  15. {
  16.     public class BZMYSQL
  17.     {
  18.         private MySqlConnection connection;
  19.         private string server;
  20.         private string database;
  21.         private string uid;
  22.         private string password;
  23.  
  24.         ShortChat msg = new ShortChat();
  25.  
  26.         public void Initialize( Queue<EventArgs> q)
  27.         {
  28.             server = "123.456.1.123";
  29.             database = "db name";
  30.             uid = "username";
  31.             password = "pa55w0rd";
  32.             string connectionString;
  33.             connectionString = "SERVER=" + server + ";" + "DATABASE=" +
  34.             database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
  35.  
  36.             connection = new MySqlConnection(connectionString);
  37.  
  38.             if (this.OpenConnection(q) == true)
  39.             {
  40.                 if (this.CloseConnection(q) == true)
  41.                     q.Enqueue(msg.arena("Battle Database configured and loaded.."));
  42.             }
  43.             else
  44.                 q.Enqueue(msg.arena("SQL Connection Phail."));
  45.         }
  46.  
  47.         //open connection to database
  48.         private bool OpenConnection(Queue<EventArgs> q)
  49.         {
  50.             try
  51.             {
  52.                 connection.Open();
  53.                 return true;
  54.             }
  55.             catch (MySqlException ex)
  56.             {
  57.                 //When handling errors, you can your application's response based
  58.                 //on the error number.
  59.                 //The two most common error numbers when connecting are as follows:
  60.                 //0: Cannot connect to server.
  61.                 //1045: Invalid user name and/or password.
  62.                 switch (ex.Number)
  63.                 {
  64.                     case 0:
  65.                         q.Enqueue(msg.arena("Cannot connect to server.  Contact administrator"));
  66.                         break;
  67.  
  68.                     case 1045:
  69.                         q.Enqueue(msg.arena("Invalid username/password, please try again"));
  70.                         break;
  71.                     default:
  72.                         q.Enqueue(msg.arena("Error: Num[" + ex.Number + "]"));
  73.                         break;
  74.                 }
  75.                 return false;
  76.             }
  77.         }
  78.  
  79.         //Close connection
  80.         private bool CloseConnection(Queue<EventArgs> q)
  81.         {
  82.             try
  83.             {
  84.                 connection.Close();
  85.                 return true;
  86.             }
  87.             catch (MySqlException ex)
  88.             {
  89.                 q.Enqueue(msg.arena(ex.Message));
  90.                 return false;
  91.             }
  92.         }
  93.  
  94.         /*
  95.          *      I need to have a table with a label of "BZ Bank" or something similar
  96.          *      Inside it will have variables:  PlayerName,  Money(bz bux),  Timestamp(when it was last updated)
  97.          *      money uses a double
  98.          */
  99.  
  100.         // If the code reaches here - there is no player on the bot list to add
  101.         // and we are checking here to see if the player is stored on the bz database
  102.         public void CheckDB(PublicPlayer StoredPlayer, Queue<EventArgs> q)
  103.         {
  104.             bool playerfound = FindPlayer(StoredPlayer);
  105.  
  106.             // if player isnt found we just make new one here
  107.             if (!playerfound)
  108.             {
  109.                 // Load player defaults
  110.                 q.Enqueue(msg.pm(StoredPlayer.PlayerName,"You do not have a profile stored. Any points attained will not be saved for this session."));
  111.             }
  112.  
  113.         }
  114.  
  115.         private bool FindPlayer(PublicPlayer StoredPlayer)
  116.         {
  117.             // PlayerName is stored inside StoredPlayer.PlayerName
  118.             // Money is in StoredPlayer.BZBux
  119.  
  120.             // open connection
  121.             // search for player and get info - return  true/false
  122.             // If found update StoredPlayer with money
  123.             // close connection
  124.  
  125.             return false;//just a placeholder
  126.         }
  127.  
  128.         public void UpdatePlayerList(List<PublicPlayer> Pub_List)
  129.         {
  130.             Pub_List.ForEach(delegate(PublicPlayer p)
  131.             {
  132.                 //find player and store updated info and update timestamp
  133.                 /* info to update
  134.                  
  135.                 p.PlayerName;
  136.                 p.BZBux;
  137.                
  138.                  */
  139.             });
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment