Hey I need some help for my plugin. I want to save kills of all player in the database.
Could someone give me the code for connecting to a database and saving kills of each player in it?
l2please, please... lol.
Stop asking for code, *I think* no one will give/make it for you.
Just use msql system from EpicZombie/Moonlight shop and put adding line to OnPlayerDamaged when player getting killed.
Whats the reference for
Code:
using System.Data.SQLite;
When you download the files do you see a file with name: "System.Data.SQLite" add that to your refrence!
(12-13-2013, 19:54)SailorMoon Wrote: [ -> ]l2please, please... lol.
Stop asking for code, *I think* no one will give/make it for you.
Just use msql system from EpicZombie/Moonlight shop and put adding line to OnPlayerDamaged when player getting killed.
one of these
http://www.itsmods.com/forum/Thread-Rele...Suite.html
Logs kills and other stuff. Doesn't have the code available but can at least record who killed who.
Anyways if you want the code:
Onplayerdamaged
if damage > victim health
do sql stuff here that commits to a database
catch mysqlexception (do nothing, print to server)
return damage
Do you think this will work?
Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Data.SQLite;
using Addon;
namespace NS_LevelSystem
{
class Main : CPlugin
{
public struct StatElem
{
public String XUID;
public int Kills;
}
public class Statistics
{
public Dictionary<String, StatElem> Stats = new Dictionary<string, StatElem>();
SQLiteConnection connection;
public Statistics()
{
connection = new SQLiteConnection("Data Source=" + Directory.GetCurrentDirectory() + "/plugins/stats_ns.stat");
connection.Open();
SQLiteCommand command = new SQLiteCommand("CREATE TABLE IF NOT EXISTS Statistics (XUID varchar(20),Kills integer)", connection);
command.ExecuteNonQuery();
command.Dispose();
read();
}
~Statistics()
{
connection.Close();
}
private void read()
{
SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Statistics", connection);
SQLiteDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
while (reader.Read())
{
StatElem newEntry = new StatElem();
newEntry.XUID = reader.GetString(reader.GetOrdinal("XUID"));
newEntry.Kills = reader.GetInt32(reader.GetOrdinal("Kills"));
Stats.Add(newEntry.XUID, newEntry);
}
cmd.Dispose();
}
public void insertNewPerson(ServerClient c)
{
SQLiteCommand cmd = new SQLiteCommand("INSERT IGNORE INTO Statistics (XUID, Kills)" +
"VALUES (\'" + c.XUID + "\', 1,0)", connection);
cmd.ExecuteNonQuery();
cmd.Dispose();
StatElem insert = new StatElem();
insert.XUID = c.XUID;
Stats.Add(c.XUID, insert);
}
public void updateKills(int Kills, ServerClient c)
{
StatElem updated = Stats[c.XUID];
updated.Kills += Kills;
SQLiteCommand cmd = new SQLiteCommand("UPDATE Statistics SET Kills=" + updated.Kills + " WHERE XUID=\'" + updated.XUID + "\'", connection);
cmd.ExecuteNonQuery();
cmd.Dispose();
Stats[c.XUID] = updated;
}
}
Statistics stats;
int kill = 1;
public override int OnPlayerDamaged(ServerClient Attacker, ServerClient Victim, string Weapon, int Damage)
{
if (Damage >= Victim.Other.Health && Attacker.Team != Victim.Team)
{
UpdateKills(kill, Victim);
}
return base.OnPlayerDamaged(Attacker, Victim, Weapon, Damage);
}
public void UpdateKills(int inc, ServerClient c)
{
stats.updateKills(inc, c);
}
public int getKills(String XUID)
{
return stats.Stats[XUID].Kills;
}
public override void OnPlayerConnect(ServerClient Client)
{
base.OnPlayerConnect(Client);
if (Client.Team == Teams.Allies)
{
}
}
public override void OnPlayerSpawned(ServerClient Client)
{
base.OnPlayerSpawned(Client);
if (Client.Team == Teams.Allies)
{
}
}
public override ChatType OnSay(string Message, ServerClient Client)
{
if (Message.StartsWith("!kills"))
{
int k = Client.Stats.Kills;
iPrintLnBold("^1You have got ^2" + k + "^1 kills", Client);
return ChatType.ChatNone;
}
return base.OnSay(Message, Client);
}
}
}
I think it doesn't work, if I do !kills nothing happens.
How can I check if it works?