If enabled, all player actions are disabled until they /login which triggers a inventory check against the server. If they fail the check the login doesn't occur and they can't act. Default inventory for new accounts or characters requires a new character's inventory and health. Every inventory change that occurs is stored back into playerData memory and then onLeave it's stored back into the database, to be loaded at next /login
103 lines
3.6 KiB
C#
103 lines
3.6 KiB
C#
/*
|
|
TShock, a server mod for Terraria
|
|
Copyright (C) 2011 The TShock Team
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
using System;
|
|
using System.Data;
|
|
|
|
using MySql.Data.MySqlClient;
|
|
using Terraria;
|
|
|
|
namespace TShockAPI.DB
|
|
{
|
|
public class InventoryManager
|
|
{
|
|
public IDbConnection database;
|
|
|
|
public InventoryManager(IDbConnection db)
|
|
{
|
|
database = db;
|
|
|
|
var table = new SqlTable("Inventory",
|
|
new SqlColumn("Account", MySqlDbType.Int32) { Primary = true },
|
|
new SqlColumn("CharacterName", MySqlDbType.VarChar, 50),
|
|
new SqlColumn("MaxHealth", MySqlDbType.Int32),
|
|
new SqlColumn("MaxMana", MySqlDbType.Int32),
|
|
new SqlColumn("Inventory", MySqlDbType.Text)
|
|
);
|
|
var creator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());
|
|
creator.EnsureExists(table);
|
|
}
|
|
|
|
public PlayerData GetPlayerData(TSPlayer player, int acctid)
|
|
{
|
|
PlayerData playerData = new PlayerData(player);
|
|
|
|
try
|
|
{
|
|
using (var reader = database.QueryReader("SELECT * FROM Inventory WHERE Account=@0 AND CharacterName=@1", acctid, player.Name))
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
playerData.exists = true;
|
|
playerData.maxHealth = reader.Get<int>("MaxHealth");
|
|
playerData.maxMana = reader.Get<int>("MaxMana");
|
|
playerData.inventory = NetItem.Parse(reader.Get<string>("Inventory"));
|
|
return playerData;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex.ToString());
|
|
}
|
|
|
|
return playerData;
|
|
}
|
|
|
|
public bool InsertPlayerData(TSPlayer player, int acctid)
|
|
{
|
|
PlayerData playerData = player.PlayerData;
|
|
|
|
if (!GetPlayerData(player, acctid).exists)
|
|
{
|
|
try
|
|
{
|
|
database.Query("INSERT INTO Inventory (Account, CharacterName, MaxHealth, MaxMana, Inventory) VALUES (@0, @1, @2, @3, @4);", acctid, player.Name, playerData.maxHealth, playerData.maxMana, NetItem.ToString(playerData.inventory));
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex.ToString());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
database.Query("UPDATE Inventory SET MaxHealth = @0, MaxMana = @1, Inventory = @3 WHERE Account = @4 AND CharacterName = @5;", playerData.maxHealth, playerData.maxMana, NetItem.ToString(playerData.inventory), player.Name, acctid);
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex.ToString());
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|