private async void Player_NetUserDataChanged(Player obj) { if (_char != null) { return; } _char = await ServerDB.SelectCharacterAsync(_user.Char); if (_char == null) { _player.Error($"Error while retrieving pony"); } else { _player.SetBounds(); _player.SetVersion(); _object = new WO_Player(this); _save = new AutoSaveChar(this); _items = _object.GetComponent <ItemsMgr>(); _trade = _object.GetComponent <TradeMgr>(); _skills = _object.GetComponent <SkillsMgr>(); SetPet(); _user.Map = _server.Map.Id; _char.Map = _user.Map; _player.SynchNetData(); await CharsMgr.SaveCharacterAsync(_char); } }
public void OnSerialize(NetMessage message) { message.Write(m_data.Talents.Count); foreach (var item in m_data.Talents) { message.Write((uint)item.Key); message.Write(item.Value.Exp); message.Write((uint)item.Value.Level); message.Write((uint)item.Value.Points); if (item.Value.Level == 0) { var exp = CharsMgr.GetExpForLevel(item.Value.Level + 1); message.Write(exp); message.Write(exp); message.Write(0u); } else if (item.Value.Level == CharsMgr.MaxLevel) { var exp = CharsMgr.GetExpForLevel(CharsMgr.MaxLevel); message.Write(exp); message.Write(0u); message.Write(exp); } else { var exp = CharsMgr.GetExpForLevel(item.Value.Level + 1); message.Write(item.Value.Exp); message.Write(exp); message.Write(0); } } }
public void Destroy() { _player.NetUserDataChanged -= Player_NetUserDataChanged; _save.Destroy(); _object.Destroy(); _pet?.Destroy(); CharsMgr.SaveCharacter(_char); if (_dialog != null) { _dialog.Dialog.OnDialogEnd(this); } if (_shop != null) { _shop.Movement.Unlock(); } foreach (var item in _clones.Values.ToArray()) { item.Destroy(); } _pet = null; _save = null; _shop = null; _user = null; _char = null; _trade = null; _items = null; _clones = null; _dialog = null; _server = null; _player = null; _object = null; _skills = null; }
private bool CalculateTalentLevel(TalentData talent, uint exp) { if (talent.Level >= CharsMgr.MaxLevel) { return(false); } var cExp = talent.Exp + exp; var level = (short)(talent.Level <= 0 ? 1 : talent.Level); while (level < CharsMgr.MaxLevel) { var nExp = CharsMgr.GetExpForLevel(level); if (cExp < nExp) { break; } cExp -= nExp; level++; } talent.Exp = cExp; if (level != talent.Level) { talent.Points += (short)((level - talent.Level) * CharsMgr.TalentPointsPerLevel); talent.Level = level; return(true); } return(false); }
public static async Task <Character> CreateCharacterAsync(int user, PonyData pony, short level = 1) { try { using (var connection = await GetConnectionAsync()) { using (var command = connection.CreateCommand()) { var data = CharsMgr.CreateCharacterData(pony, level); command.CommandText = $"INSERT INTO {tb_02} (user, level, race, gender, name, vdata, gdata) VALUES (@user, {level}, @race, @gender, @name, @vdata, @gdata);"; command.Parameters.AddWithValue("user", user); command.Parameters.AddWithValue("name", pony.Name); command.Parameters.AddWithValue("race", pony.Race); command.Parameters.AddWithValue("gender", pony.Gender); command.Parameters.AddWithValue("vdata", pony.GetBytes()); command.Parameters.AddWithValue("gdata", data.GetBytes()); if (await command.ExecuteNonQueryAsyncEx() == 1) { return(new Character((int)command.LastInsertedId, user, level, 0, pony, data)); } } } return(null); } catch (Exception exp) { ServerLogger.LogException(exp); return(null); } }
public async Task <bool> UpdateCharacter(int index, PonyData pony) { if (m_data == null || index >= m_data.Count) { return(false); } CharsMgr.ValidatePonyData(pony); if (index == -1) { if (m_data.Count >= CharsMgr.MaxChars) { return(false); } var character = await ServerDB.CreateCharacterAsync(_user.ID, pony); if (character == null) { return(false); } m_data.Add(character); } else { var character = m_data[index]; character.Pony = pony; if (!await ServerDB.UpdatePonyAsync(character)) { return(false); } } return(true); }
public bool UpdateCharacter(int index, PonyData pony) { if (_data == null || index >= _data.Count) { return(false); } Character character; if (index == -1) { if (_data.Count >= CharsMgr.MaxChars) { return(false); } if (!CharsMgr.CreateCharacter(_user.ID, pony, out character)) { return(false); } _data.Add(character); } else { character = _data[index]; character.Pony = pony; if (!ServerDB.UpdatePony(character)) { return(false); } } return(true); }
private async void RPC_001(NetMessage arg1, NetMessageInfo arg2) { var player = m_server[arg2.Sender.Id]; var pony = new PonyData(); var index = arg1.ReadInt32(); pony.OnDeserialize(arg1); if (CharsMgr.CheckName(pony.Name)) { if (await player.UpdateCharacter(index, pony)) { var character = index == -1 ? player.Data.Last() : player.Data[index]; player.User.Char = character.Id; if (character.Map == 0 || !DataMgr.Select(character.Map, out DB_Map map)) { arg2.Sender.ChangeRoom(StartMaps[(byte)pony.Race]); } else { arg2.Sender.ChangeRoom(map.Name); } arg2.Sender.SynchNetData(); } else { arg2.Sender.Error("Error while saving pony, most likely character isn't unique."); } } else { arg2.Sender.Error($"Name \"{pony.Name}\" disapproved!"); } }
public Character(PonyData pony) { ID = -1; Level = 1; Pony = pony; CharsMgr.CreateCharacterData(this); }
public Task <bool> PrepareForMapSwitch() { _save.Destroy(); _char.Data.Position = _object.Position; _char.Data.Rotation = _object.Rotation; return(CharsMgr.SaveCharacterAsync(_char)); }
public void Destroy() { _player.NetUserDataChanged -= Player_NetUserDataChanged; _data.RemoveAll(x => x.ID == _user.Char); CharsMgr.RemoveCharacters(_data); _data.Clear(); _data = null; _user = null; _player = null; _server = null; }
public bool DeleteCharacter(int index) { if (index >= _data.Count || index < 0) { return(false); } if (!CharsMgr.DeleteCharacter(_data[index].ID)) { return(false); } _data.RemoveAt(index); return(true); }
private void Player_NetUserDataChanged(Player obj) { if (_data != null) { return; } if (CharsMgr.SelectAllUserCharacters(_user.ID, out _data)) { this.SendPonies(); } else { _player.Error($"Error while retrieving ponies from data base"); } }
private void Player_NetUserDataChanged(Player obj) { if (_user.Char != 0 && (_user.Char != (_char?.ID ?? -1))) { m_name = null; if (!CharsMgr.SelectCharacter(_user.Char, out _char)) { ServerLogger.LogServer(_server, $"{obj.Id} couldn't load character {_user.Char}"); } else { m_name = _char.Pony.Name; } } }
private async void Player_NetUserDataChanged(Player obj) { if (m_data != null) { return; } m_data = await ServerDB.SelectAllUserCharactersAsync(_user.ID); if (m_data != null) { foreach (var item in m_data) { CharsMgr.ValidatePonyData(item.Pony); } this.SendPonies(); } else { _player.Error($"Error while retrieving ponies from data base"); } }
public void Destroy() { //MasterPlayer target; _player.ClearSubscriptions(); CharsMgr.RemoveCharacter(_user.Char); ServerDB.UpdateUserSave(_user.ID, _save); _player.NetUserDataChanged -= Player_NetUserDataChanged; _player.FinishedSwitchingRooms -= Player_FinishedSwitchingRooms; //foreach (var item in _save.Friends) //{ // if (item.Value.Item1 == 1 && _server.TryGetByUserId(item.Key, out target)) // target.Player.UpdateFriend(_status.Fill(this, OnlineStatus.Offline)); //} _char = null; _save = null; _user = null; _status = null; _player = null; _server = null; _rPlayer = null; _lastWhisper = null; }
private void InitializeCMD() { #region Map _cmdList["map"] = (string[] args) => { if (args?.Length >= 1 && args[0] != "?") { switch (args[0]) { case "reload": if (args.Length >= 3) { switch (args[1]) { case "objects": int id; if (int.TryParse(args[2], out id)) { var maps = _maps.Where(x => x.Map.ID == id).ToArray(); if (maps.Length > 0) { DataMgr.LoadAll(); foreach (var item in maps) { item.Objects.Reload(); } } else { Console.WriteLine($"Error: can't find maps with id {id}"); } } else { Console.WriteLine("Using: map reload objects mapID"); } break; default: Console.WriteLine("Using: map reload objects mapID"); break; } } else { Console.WriteLine("Using: map reload objects mapID"); } break; } } else { Console.WriteLine("Using: map reload args"); } }; #endregion #region User _cmdList["user"] = (string[] args) => { if (args?.Length >= 1 && args[0] != "?") { switch (args[0]) { case "create": byte access = 1; if (args.Length < 3 || (args.Length > 3 && !byte.TryParse(args[3], out access))) { Console.WriteLine("Using: user create login password <access>"); } else if (!ServerDB.CreateUser(args[1], args[2], access)) { Console.WriteLine($"Error: can't create user {args[1]}:{args[2]}"); } else { Console.WriteLine($"Created user {args[1]}"); } break; case "info": { int id = -1; DB_User user; if (args.Length < 2 || (args.Length > 1 && !int.TryParse(args[1], out id))) { Console.WriteLine("Using: user info id"); } else if (!ServerDB.SelectUser(id, out user)) { Console.WriteLine($"Error: can't load user {id}"); } else { Console.WriteLine($"User {id}, {user.Name} access {user.Access}, {(user.SID == null ? "offline" : (_master?.IsOnline(id) ?? false ? "online" : "offline/undefined"))}"); } break; } case "list": { if (_master != null) { Console.WriteLine("Online[Global]:"); foreach (var item in _master.GetPlayers()) { var user = item.User; var @char = item.Char; if (user != null) { Console.WriteLine($"User {user.ID}, \"{user.Name}\", access {user.Access}{(@char != null ? $", char level {@char.Level}, \"{@char.Pony.Name}\", map {@char.Map}" : string.Empty)}"); } } } else { Console.WriteLine("Online[Local]:"); foreach (var item in _characters.GetPlayers()) { var user = item.User; if (user != null) { Console.WriteLine($"User {user.ID}, {user.Name} access {user.Access}, characters screen"); } } foreach (var map in _maps) { foreach (var item in map.GetPlayers()) { var user = item.User; var @char = item.Char; if (user != null) { Console.WriteLine($"User {user.ID}, \"{user.Name}\", access {user.Access}{(@char != null ? $", char level {@char.Level}, \"{@char.Pony.Name}\", map {@char.Map}" : string.Empty)}"); } } } } break; } default: Console.WriteLine("Using: user info|create|list args"); break; } } else { Console.WriteLine("Using: user info|create args"); } }; #endregion #region Data _cmdList["data"] = (string[] args) => { if (args?.Length >= 1 && args[0] != "?") { if (_master != null) { switch (args[0]) { case "create": if (args.Length >= 2) { ushort level, dialog, movement; byte flags, index; int id; switch (args[1]) { case "npc": if (args.Length == 8 && ushort.TryParse(args[2], out level) && byte.TryParse(args[3], out flags) && ushort.TryParse(args[4], out dialog) && byte.TryParse(args[5], out index) && ushort.TryParse(args[6], out movement)) { try { if (ServerDB.CreateNPC(level, flags, dialog, index, movement, args[7].ToPonyData(), out id)) { Console.WriteLine($"new NPC id {id} created!"); } else { Console.WriteLine($"Error: can't create new npc"); } } catch { Console.WriteLine($"Error: bad ponycode"); } } else { Console.WriteLine("Using: data create npc level flags dialogID dialogIndex movementID ponycode"); } break; default: Console.WriteLine("Using: data create npc args"); break; } } else { Console.WriteLine("Using: data create npc args"); } break; } } else { Console.WriteLine("Error: can't find master server in this instance"); } } else { Console.WriteLine("Using: data create args"); } }; #endregion #region Payer _cmdList["player"] = (string[] args) => { if (args?.Length >= 1 && args[0] != "?") { if (_master != null) { ushort id; switch (args[0]) { case "items": if (args.Length >= 2) { switch (args[1]) { case "clear": if (args.Length >= 3 && ushort.TryParse(args[2], out id)) { var pClass = _master[id]; if (pClass != null && pClass.OnMap) { pClass.Object.Player.Items.RemoveAllItems(); } else { Console.WriteLine($"Error: can't find player {id}"); } } else { Console.WriteLine("Using: player items clear playerID"); } break; case "print": if (args.Length >= 3 && ushort.TryParse(args[2], out id)) { var pClass = _master[id]; if (pClass != null && pClass.OnMap) { foreach (var item in pClass.Char.Data.Items) { Console.WriteLine($"{item.Key:X4}[{item.Value.Item.Id:X8}:{item.Value.Amount:X8}]"); } } else { Console.WriteLine($"Error: can't find player {id}"); } } else { Console.WriteLine("Using: player items print playerID"); } break; default: Console.WriteLine("Using: player items clear|print args"); break; } } else { Console.WriteLine("Using: player items clear|print args"); } break; case "dialogs": if (args.Length >= 2) { switch (args[1]) { case "clear": if (args.Length >= 3 && ushort.TryParse(args[2], out id)) { var pClass = _master[id]; if (pClass != null && pClass.OnMap) { pClass.Char.Data.Dialogs.Clear(); } else { Console.WriteLine($"Error: can't find player {id}"); } } else { Console.WriteLine("Using: player dialogs clear playerID"); } break; case "print": if (args.Length >= 3 && ushort.TryParse(args[2], out id)) { var pClass = _master[id]; if (pClass != null && pClass.OnMap) { foreach (var item in pClass.Char.Data.Dialogs) { Console.WriteLine($"{item.Key:X8}:{item.Value:X4}"); } } else { Console.WriteLine($"Error: can't find player {id}"); } } else { Console.WriteLine("Using: player dialogs print playerID"); } break; default: Console.WriteLine("Using: player dialogs clear|print args"); break; } } else { Console.WriteLine("Using: player dialogs clear|print args"); } break; case "kill": if (args.Length >= 1 && ushort.TryParse(args[1], out id)) { var pClass = _master[id]; if (pClass != null && pClass.OnMap) { (pClass.RoomPlayer as MapPlayer).Object.Despawn(); } else { Console.WriteLine($"Error: can't find player {id}"); } } else { Console.WriteLine("Using: player kill playerID"); } break; default: Console.WriteLine("Using: player kill|dialogs|items args"); break; } } else { Console.WriteLine("Error: can't find master server in this instance"); } } else { Console.WriteLine("Using: player kill|dialogs|items args"); } }; #endregion #region Status _cmdList["status"] = (string[] args) => { if (args?.Length >= 1 && args[0] != "?") { switch (args[0]) { case "servers": if (_master != null) { Console.WriteLine(_master.Status); } if (_characters != null) { Console.WriteLine(_characters.Status); } foreach (var item in _maps) { Console.WriteLine(item.Status); } break; case "player": ushort player; if (args.Length >= 2 && ushort.TryParse(args[1], out player)) { if (_master != null) { var pClass = _master[player]; if (pClass != null && pClass.RoomPlayer != null) { Console.WriteLine(pClass.RoomPlayer.Status); } else { Console.WriteLine($"Error: can't determine status for {player}"); } } else { Console.WriteLine("Error: can't find master server in this instance"); } } else { Console.WriteLine("Using: status player id"); } break; default: Console.WriteLine("Using: status servers|player args"); break; } } else { Console.WriteLine("Using: status servers|player args"); } }; #endregion #region Object _cmdList["object"] = (string[] args) => { if (args?.Length >= 1 && args[0] != "?") { if (_master != null) { switch (args[0]) { case "create": if (args.Length >= 3 && args[1] == "at") { ushort id, guid; byte type, flags; float time; int objectID, data01, data02, data03; switch (args[2]) { case "player": if (args.Length >= 9 && ushort.TryParse(args[3], out id) && ushort.TryParse(args[4], out guid) && int.TryParse(args[5], out objectID) && byte.TryParse(args[6], out type) && byte.TryParse(args[7], out flags) && float.TryParse(args[8], out time)) { var pClass = _master[id]; if (pClass != null && pClass.OnMap) { if (args.Length < 10 || !int.TryParse(args[09], out data01)) { data01 = -1; } if (args.Length < 11 || !int.TryParse(args[10], out data02)) { data02 = -1; } if (args.Length < 12 || !int.TryParse(args[11], out data03)) { data03 = -1; } if (ServerDB.CreateObjectAt(pClass.Object, pClass.User.Map, guid, objectID, type, flags, time, data01, data02, data03)) { Console.WriteLine($"Object [{guid}:{objectID}] created at map {pClass.User.Map} pos {pClass.Object.Position}"); } else { Console.WriteLine($"Error: can't create object [{guid}:{objectID}]"); } } else { Console.WriteLine($"Error: can't determine position for {id}"); } } else { Console.WriteLine("Using: object create at player playerID guid objectID type flags time dataArgs"); } break; default: Console.WriteLine("Using: object create at player args"); break; } } else { Console.WriteLine("Using: object create at player args"); } break; } } else { Console.WriteLine("Error: can't find master server in this instance"); } } else { Console.WriteLine("Using: object create args"); } }; #endregion #region Restart _cmdList["restart"] = (string[] args) => { if (args?.Length >= 1 && args[0] != "?") { switch (args[0]) { case "all": _master?.Stop(); _characters?.Stop(); foreach (var item in _maps) { item.Stop(); } CharsMgr.Clean(); DataMgr.LoadAll(); _master?.Start(); _characters?.Start(); foreach (var item in _maps) { item.Start(); } break; case "maps": foreach (var item in _maps) { item.Restart(); } break; } } else { Console.WriteLine("Using: restart all|maps"); } }; #endregion #region Small CMD _cmdList["exit"] = (string[] args) => { IsRunning = false; }; _cmdList["clear"] = (string[] args) => { Console.Clear(); }; _cmdList["help"] = (string[] args) => { Console.WriteLine("Available commands:"); foreach (var item in _cmdList.Keys) { Console.WriteLine(item); } }; #endregion }
public async override void OnFire() { _data.Data.Position = _data.Object.Position; _data.Data.Rotation = _data.Object.Rotation; await CharsMgr.SaveCharacterAsync(_data.Char); }
public bool SaveCharacter() { m_char.Data.Position = m_position; m_char.Data.Rotation = m_rotation; return(CharsMgr.SaveCharacter(m_char)); }
public override void OnFire() { _data.Data.Position = _data.Object.Position; _data.Data.Rotation = _data.Object.Rotation; CharsMgr.SaveCharacter(_data.Char); }
public Task <bool> SaveCharacter() { m_char.Data.Position = m_position; m_char.Data.Rotation = m_rotation; return(CharsMgr.SaveCharacterAsync(m_char)); }