public bool InsertPlayer(DBPlayer player) { _Players.Players.Add(player); //_Players[player.DBPlayerId] = player; Save(); return(true); }
public static void RPC_UpdateInventory(ZRpc rpc, ZPackage pkg) { String characterName = pkg.ReadString(); int itemsCount = pkg.ReadInt(); String steamId = rpc.GetSocket().GetEndPointString(); List <Server_ItemData> playerItems = new List <Server_ItemData>(); for (int i = 0; i < itemsCount; i++) { Server_ItemData data = new Server_ItemData { Name = pkg.ReadString(), Stack = pkg.ReadInt(), Quality = pkg.ReadInt(), Variant = pkg.ReadInt() }; playerItems.Add(data); } String dbId = $"{steamId}_{characterName}"; var dbPlayer = Instance.DB.GetPlayerById(dbId); if (dbPlayer == null) { dbPlayer = new DBClasses.DBPlayer() { DBPlayerId = dbId, SteamId = steamId, CharacterName = characterName, Items = new List <DBClasses.DBItem>() }; foreach (var item in playerItems) { dbPlayer.Items.Add(new DBClasses.DBItem() { ItemName = item.Name, PlayerId = dbPlayer.DBPlayerId, StackCount = item.Stack, Quality = item.Quality, Variant = item.Variant }); } Instance.DB.InsertPlayer(dbPlayer); } else { Instance.DB.UpdatePlayer(dbPlayer); } }
public bool UpdatePlayer(DBPlayer player) { for (int i = 0; i < _Players.Players.Count; i++) { if (_Players.Players[i].DBPlayerId == player.DBPlayerId) { _Players.Players[i] = player; Save(); return(true); } } return(false); /*_Players[player.DBPlayerId] = player; * Save();*/ }
private DBPlayer ParseAsPlayer(JsonObject playerObj) { foreach (String requiredField in _PlayerFields.Values.Select(f => f.Name)) { if (!playerObj.ContainsKey(requiredField)) { return(null); } } DBPlayer inst = new DBPlayer() { DBPlayerId = playerObj["DBPlayerId"], CharacterName = playerObj["CharacterName"], SteamId = playerObj["SteamId"], LastLoggedIn = playerObj["LastLoggedIn"], Created = playerObj["Created"], Items = new List <DBItem>() }; if (playerObj["Items"].JsonType != JsonType.Array) { return(null); } foreach (JsonValue itemVal in playerObj["Items"]) { if (itemVal is JsonObject itemObj) { DBItem item = ParseAsItem(itemObj); if (item == null) { return(null);//TODO: Check Configs.JsonStrictParsing } inst.Items.Add(item); } } return(inst); }
//CALLED ON SERVER, Should contain ZDOID and Inventory public static void RPC_CharacterIDX(ZRpc rpc, ZPackage pkg) { ZDOID characterID = pkg.ReadZDOID(); ZNetPeer peer = (ZNetPeer)_GetPeerMethod.Invoke(ZNet.instance, new object[] { rpc }); if (peer != null) { peer.m_characterID = characterID; if (Configs.ShowDebugMessages.Value) { ZLog.Log($"Got character ZDOID with inventory from {peer.m_playerName} : {characterID}!"); } } ZPackage inventoryPackage = pkg.ReadPackage(); String characterName = inventoryPackage.ReadString(); int itemsCount = inventoryPackage.ReadInt(); String steamId = rpc.GetSocket().GetEndPointString(); if (Configs.ShowDebugMessages.Value) { Instance.Logger.LogInfo($"Getting player {characterName}'s ({steamId}) inventory..."); } List <Server_ItemData> playerItems = new List <Server_ItemData>(); for (int i = 0; i < itemsCount; i++) { Server_ItemData data = new Server_ItemData { Name = inventoryPackage.ReadString(), Stack = inventoryPackage.ReadInt(), Quality = inventoryPackage.ReadInt(), Variant = inventoryPackage.ReadInt() }; playerItems.Add(data); } if (Configs.ShowDebugMessages.Value) { Instance.Logger.LogInfo($"Found {playerItems.Count} items in {characterName}'s inventory."); } String dbId = $"{steamId}_{characterName}"; var dbPlayer = Instance.DB.GetPlayerById(dbId); if (dbPlayer == null) { if (Configs.ShowDebugMessages.Value) { Instance.Logger.LogInfo($"{characterName} is a new character!"); } DBClasses.DBPlayer[] characters = Instance.DB.GetPlayersBySteamId(steamId); if (characters.Length >= Configs.MaxCharactersPerPlayer.Value) { rpc.Invoke("Error", new object[] { (int)ZNet.ConnectionStatus.ErrorVersion }); return; } dbPlayer = new DBClasses.DBPlayer() { DBPlayerId = dbId, SteamId = steamId, CharacterName = characterName, Items = new List <DBClasses.DBItem>() }; foreach (var item in playerItems) { dbPlayer.Items.Add(new DBClasses.DBItem() { ItemName = item.Name, PlayerId = dbPlayer.DBPlayerId, StackCount = item.Stack, Quality = item.Quality, Variant = item.Variant }); } Instance.DB.InsertPlayer(dbPlayer); } else { bool isSame = true; if (dbPlayer.Items.Count != playerItems.Count) { isSame = false; } else { for (int i = 0; i < dbPlayer.Items.Count; i++) { if (dbPlayer.Items[i].ItemName != playerItems[i].Name || dbPlayer.Items[i].StackCount != playerItems[i].Stack || dbPlayer.Items[i].Quality != playerItems[i].Quality || dbPlayer.Items[i].Variant != playerItems[i].Variant) { isSame = false; break; } } } if (isSame) { if (Configs.ShowDebugMessages.Value) { Instance.Logger.LogInfo($"{characterName} is still the same"); } dbPlayer.LastLoggedIn = DateTime.Now; Instance.DB.UpdatePlayer(dbPlayer); } else { if (Configs.ShowDebugMessages.Value) { Instance.Logger.LogWarning($"{characterName} is NOT the same!"); } rpc.Invoke("Error", new object[] { (int)ZNet.ConnectionStatus.ErrorVersion }); } } }
public DBItem[] GetPlayerItems(DBPlayer player) { return(player.Items.ToArray()); }
public bool UpdatePlayer(DBPlayer player) { _Players[player.DBPlayerId] = player; Save(); return(true); }