public bool DoStep(NetworkClient networkClient, GameClient client) { networkClient.OutLogMessage("Getting a servers list..."); //Send greeting string greeting = Packet.BuildPacket(FromClient.GREETING); networkClient.SendData(greeting); //Get response Packet packet = networkClient.InputQueue.Pop(); if (packet != null) { //Get available servers List<string> servers = packet.GetValues("S/@host"); //Log message StringBuilder sServers = new StringBuilder(); //Store available servers foreach (string server in servers) { sServers.AppendFormat("{0}{1}", sServers.Length > 0 ? ", " : "", server); client.AdditionalData.Add(ObjectPropertyName.GAME_SERVER, server); } networkClient.OutLogMessage(sServers.Insert(0, "Available servers: ").ToString()); return true; } networkClient.ThrowError("Failed to getting a servers list or the connection was terminated"); return false; }
public static string GetCurrentLocationIdent(GameClient client) { //Format: X: + Y: + BuildingId return (string)client.AdditionalData[ObjectPropertyName.LOCATION_X][0] + ":" + (string)client.AdditionalData[ObjectPropertyName.LOCATION_Y][0] + ":" + (string)client.AdditionalData[ObjectPropertyName.LOCATION_BUILDING][0]; }
public bool DoStep(NetworkClient networkClient, GameClient client) { DateTime now = DateTime.Now; ChatMessage[] outChatMessages = _outChatMessages.Where(cm => cm.MessageTime <= now ).ToArray(); if (outChatMessages.Length > 0) { foreach (ChatMessage chatMessage in outChatMessages) { _outChatMessages.Remove(chatMessage); //1: From, 2: To, 3: Type, 4: Message string message = Packet.BuildPacket(FromClient.CHAT_MESSAGE, Chat.POST, client.Login, chatMessage.Sender, chatMessage.Type, chatMessage.Message); networkClient.SendChatData(message); //Play notification for private message _soundPlayer.Play(); //Out chat message string logMessage = string.Format("[{0}] {1} [{2}] {3}", client.Login, chatMessage.Type == ChatMessageType.Pivate ? "private" : "to", chatMessage.Sender, chatMessage.Message); networkClient.OutChatMessage(logMessage); } return true; } return false; }
public bool DoStep(NetworkClient networkClient, GameClient client) { //Get game servers list List<object> gameServersList = client.AdditionalData[ObjectPropertyName.GAME_SERVER]; //If servers list is available if (gameServersList != null) { //Connect to the game server while (gameServersList.Count > 0) { //Pop a first game server in the list string server = (string) gameServersList[0]; gameServersList.Remove(server); //Out log message networkClient.OutLogMessage(string.Format("Connecting to the game server: {0}...", server)); //Try to connect bool isConnected = networkClient.ConnectToGameServer(server); if (isConnected) { if (networkClient.DoAuthorization()) { return true; } } } } //Connection failed networkClient.Disconnect(); return false; }
public bool DoStep(NetworkClient networkClient, GameClient client) { bool chatStarted = false; networkClient.OutLogMessage("Starting chat..."); //Get chat info string getInfo = Packet.BuildPacket(FromClient.CHAT_CTRL, Chat.START); networkClient.SendData(getInfo); //Start chat Packet chat = networkClient.InputQueue.Pop(FromServer.CHAT_CTRL); if (chat != null) { string chatServer = chat["@server"]; string sessionId = (string)client.AdditionalData[ObjectPropertyName.SESSION_ID][0]; chatStarted = networkClient.StartChat(chatServer, sessionId); if (chatStarted) { //1: Session ID, 2: Login string chatAuth = Packet.BuildPacket(FromClient.CHAT_CTRL, Chat.AUTH, sessionId, client.Login); networkClient.SendChatData(chatAuth); } } networkClient.OutLogMessage(!chatStarted ? "WARNING: chat wasn`t started" : "Chat was successfully started"); return true; }
public bool DoStep(NetworkClient networkClient, GameClient client) { if (IsReadyForAction) { _gameItemsGroups = Instance.GameItemsGroups; //Current location must be quction, check it string locationIdent = Helper.GetCurrentLocationIdent(client); if (Locations.Auctions.Contains(locationIdent)) { //Sell items from auc DoSellingItemsFromAuc(networkClient, client); //Sell items from inventory DoSellingItemsFromInv(networkClient, client); } //Clear auction items cache _auctionItems.Clear(); _prewSellingTime = Environment.TickCount; return true; } return false; }
public bool DoStep(NetworkClient networkClient, GameClient client) { if (IsReadyForAction) { DateTime now = DateTime.Now; //Collect garbage IEnumerable<Packet> packets = networkClient.InputQueue.PeakAll(null). Where(p => now.Subtract(p.ReceiveTime).TotalSeconds >= TTL_OF_PACKER_SEC); networkClient.InputQueue.RemoveAll(packets); //Generate list of unknown packets Packet[] unkPackets = packets.Where(p => !_sysPackets.Contains(p.Type)).ToArray(); if (unkPackets.Length > 0) { //Generate log message StringBuilder message = new StringBuilder(); foreach (Packet packet in unkPackets) { message.AppendFormat("{0}{1}", message.Length == 0 ? "" : ", ", packet.Type); } message.Insert(0, "GC -> unknown packets: "); //Send log message networkClient.OutLogMessage(message.ToString()); } _prewGCCollectTime = Environment.TickCount; } return true; }
public bool DoStep(NetworkClient networkClient, GameClient client) { Packet[] packets = networkClient.InputQueue.PopAll(FromServer.ERROR); foreach (Packet packet in packets) { string errorCode = packet["@code"]; string errorMessage = Errors.GameError.GetErrorMessage(errorCode); switch (errorCode) { case Errors.GameError.E_USER_HAS_DROPPED: case Errors.GameError.E_PLAYER_ON_ANOTHER_SERVER: case Errors.GameError.E_CONNECTION_ERROR: { networkClient.ThrowError(errorMessage); break; } default: { networkClient.OutLogMessage(string.Format("ERROR: {0}", errorMessage)); break; } } } return true; }
public bool DoStep(NetworkClient networkClient, GameClient client) { int curTickCount = Environment.TickCount; //Just started if (_prewPingTime == 0) { _prewPingTime = curTickCount; return false; } if (curTickCount - _prewPingTime >= PING_DELAY_MS) { //Query params: 0: is a first time ping?, [1]: I1, [2]: ID2, [3]: ID1 string ping = Packet.BuildPacket(FromClient.PING, _firstTime, client.AdditionalData[ObjectPropertyName.I1][0], client.AdditionalData[ObjectPropertyName.ID2][0], client.AdditionalData[ObjectPropertyName.ID1][0]); networkClient.SendData(ping); networkClient.SendChatData(ping); _firstTime = false; _prewPingTime = Environment.TickCount; return true; } return false; }
public bool DoStep(NetworkClient networkClient, GameClient client) { if (IsReadyForAction) { _firstStartJoined = true; return DoJoin(networkClient, client); } return false; }
public static bool DoJoin(NetworkClient networkClient, GameClient client) { Dictionary<string, InventoryItemList> items = new Dictionary<string, InventoryItemList>(); foreach (InventoryItem inventoryItem in client.InventoryItems) { //Get the item ident string ident = inventoryItem.Ident; InventoryItemList inventoryItemsList; //Get items list for the item if (items.ContainsKey(ident)) { inventoryItemsList = items[ident]; } else { items.Add(ident, inventoryItemsList = new InventoryItemList()); } //Add the item to items list inventoryItemsList.Add(inventoryItem); } //Do join items foreach (string ident in items.Keys) { InventoryItemList inventoryItemsList = items[ident]; int itemsCount = inventoryItemsList.Count; if (itemsCount > 1) { InventoryItem item2 = inventoryItemsList[0]; for (int i = 1; i < itemsCount; i++) { InventoryItem item1 = inventoryItemsList[i]; string joinInventory = Packet.BuildPacket(FromClient.JOIN_INVENTORY, item1.ID, item2.ID); networkClient.SendData(joinInventory); item2.Count += item1.Count; client.InventoryItems.Remove(item1); } } } return true; }
public bool DoStep(NetworkClient networkClient, GameClient client) { networkClient.OutLogMessage("Getting my information..."); //Get my information string getInfo = Packet.BuildPacket(FromClient.GET_MY_INFO); networkClient.SendData(getInfo); //Read and store current location information Packet myInfo = networkClient.InputQueue.Pop(FromServer.MY_INFO); if (myInfo != null) { string data = myInfo.Data; //Store game client additional info Match locationInfo = _regexLocationInfo.Match(data); client.AdditionalData.Add(ObjectPropertyName.LOCATION_BUILDING, locationInfo.Groups["B"].Value); client.AdditionalData.Add(ObjectPropertyName.LOCATION_X, locationInfo.Groups["X"].Value); client.AdditionalData.Add(ObjectPropertyName.LOCATION_Y, locationInfo.Groups["Y"].Value); client.AdditionalData.Add(ObjectPropertyName.ID1, locationInfo.Groups["ID1"].Value); client.AdditionalData.Add(ObjectPropertyName.ID2, locationInfo.Groups["ID2"].Value); client.AdditionalData.Add(ObjectPropertyName.I1, locationInfo.Groups["I1"].Success ? locationInfo.Groups["I1"].Value : "0"); //Update inventory client.InventoryItems = Helper.ParseInventoryItems(data); return true; } networkClient.ThrowError("Failed to getting my info or the connection was terminated"); return false; }
private void ProcessGroupPagesFull(NetworkClient networkClient, GameClient client, string locationIdent, bool isAuction, IEnumerable<KeyValuePair<string, string>> fullList) { //For all items groups... foreach (KeyValuePair<string, string> item in fullList) { //Get group ID string groupId = item.Key; //Ignore for shopping? GameItemsGroup group = _gameItemsGroups[groupId]; if (group == null || group.IgnoreForShopping) { continue; } //If number of items is more than 0 if (int.Parse(item.Value) > 0) { DateTime startTime = DateTime.Now; string groupName = group.Name; int subGroupsCount = group.SubGroupsCount; List<string> subGroupsOrderList = new List<string>(); //Get cached group page ident string cachedGroupPageIdent = GetCachedGroupPageIdent(locationIdent, groupId); //Get cached group pages List<Packet> cachedGroupPages; if (!_cachedGroupPages.ContainsKey(cachedGroupPageIdent)) { cachedGroupPages = new List<Packet>(); _cachedGroupPages.Add(cachedGroupPageIdent, cachedGroupPages); } else { cachedGroupPages = _cachedGroupPages[cachedGroupPageIdent]; } //There is one page available at least int pagesCount = 1; //For each page of group for (int groupPage = 0; groupPage < pagesCount; groupPage++) { //Query page[i] of group //Query params: 1: group ID, 2: filter, 3: page number, 4: is auction? string getGroupPagesList = Packet.BuildPacket(FromClient.SHOP, Shop.ITEMS_GET_LIST, groupId, string.Empty, groupPage, isAuction); networkClient.SendData(getGroupPagesList); //Get group page Packet groups = networkClient.InputQueue.Pop(FromServer.SHOP_DATA); if (groups != null) { //Calculate pages count if (groupPage == 0) { int cnt = int.Parse(groups["@m"]); pagesCount = cnt / 8 + (cnt % 8 > 0 ? 1 : 0); } //Process items list ProcessSubGroupsList(networkClient, client, groupId, groupPage, isAuction, groups, subGroupsOrderList); //Store cached group page cachedGroupPages.Add(groups); } } //Reorder subgroups group.ReorderSubGroups(subGroupsOrderList); //Out action message string endTime = DateTime.Now.Subtract(startTime).ToString(); string message = string.Format("'{0}' processed. Subgroups count: {1}, duration: {2}", groupName, subGroupsCount, Helper.RemoveMilliseconds(endTime)); networkClient.OutActionLogMessage(this, message); } } }
private bool GetOwnItem(NetworkClient networkClient, GameClient client, ShopItem shopItem) { string message = string.Format("Trying to get: '{0}'...", shopItem.Parent); networkClient.OutLogMessage(message); //Try to get the item //Query params: 1: item ID string getGroupsItemsList = Packet.BuildPacket(FromClient.SHOP, Shop.ITEM_GET_OWN, shopItem.ID); networkClient.SendData(getGroupsItemsList); //Get a result string[] packetTypes = new[] { FromServer.ITEM_ADD_ONE, FromServer.SHOP_ERROR }; Packet getResult = networkClient.InputQueue.PopAny(packetTypes); //Check the result if (getResult != null) { switch (getResult.Type) { //Getting is failed case FromServer.SHOP_ERROR: { string errorCode = getResult["@code"]; string errorMessage = Errors.ShopError.GetErrorMessage(errorCode); networkClient.ThrowError(errorMessage, false); break; } //Successful default: { //Add the item to the inventory or, if it`s bad, out log message InventoryItem inventoryItem = Helper.ParseInventoryItem(getResult.Data); if (inventoryItem != null) { //Add the item to inventory client.InventoryItems.Add(inventoryItem); //Join inventory items GameStep_JoinInventory.DoJoin(networkClient, client); //Out log message networkClient.OutLogMessage("Successful"); //Done return true; } string errorMessage = string.Format("GET OWN ITEM: BAD INVENTORY ITEM: {0}", getResult.Data); networkClient.ThrowError(errorMessage); break; } } } return false; }
public bool DoStep(NetworkClient networkClient, GameClient client) { //Get password key Packet data = networkClient.InputQueue.Pop(); if (data != null) { string key = data["@s"]; //Store password key client.AdditionalData.Add(ObjectPropertyName.PASSWORD_KEY, key); //Get hashed password string passwordHash = client.GetPasswordHash(key); networkClient.OutLogMessage( string.Format("Trying to login: U = {0}, P = {1}, K = {2}, H = {3}...", client.Login, client.Password, key, passwordHash)); //Do login string login = Packet.BuildPacket(FromClient.LOGIN_DATA, networkClient.LocalIPAddress, client.Version2, client.Version, passwordHash, client.Login); networkClient.SendData(login); //Get login result data = networkClient.InputQueue.Pop(); if (data != null) { //Check on login error string errorCode = data["@code"]; if (errorCode != null) { string errorMessage = Errors.GameError.GetErrorMessage(errorCode); switch (errorCode) { //Should be login to another server case Errors.GameError.E_PLAYER_ON_ANOTHER_SERVER: { networkClient.ThrowError(errorMessage, false); break; } //General login error default: { networkClient.ThrowError(errorMessage); break; } } return false; } //No errors? Continue... string sessionId = data["@ses"]; client.AdditionalData.Add(ObjectPropertyName.SESSION_ID, sessionId); networkClient.OutLogMessage(string.Format("User login successful")); return true; } } networkClient.ThrowError("Failed to login or the connection was terminated"); return false; }
private void ProcessMyAucItemsList(NetworkClient networkClient, GameClient client, Packet itemsList) { //Get all nodes of items XmlNodeList items = itemsList.GetNodes("O"); if (items != null) { //For each node of items foreach (XmlNode item in items) { //Create new my shop item ShopItem myItem = Helper.ParseShopItem(item, _gameItemsGroups); //Validate the item if (myItem != null && !string.IsNullOrEmpty(myItem.SubGroupID)) { string groupId = _gameItemsGroups.SubGroupToGroupId(myItem.SubGroupID, myItem.SubGroupType); if (!string.IsNullOrEmpty(groupId)) { //Get subgroup GameItemsSubGroup sg = _gameItemsGroups[groupId].GetSubGroup( myItem.SubGroupID, myItem.SubGroupType); //Cache items list from auction CacheAuctionItems(networkClient, groupId, myItem.SubGroupID, sg.Type, sg.Level); //Do dumping DoDumping(networkClient, client, myItem, true); } } } } }
public bool Connect(GameClient client) { try { if (!_intermediateState) { OutLogMessage("Trying to establish connection..."); } //Terminate current connection Disconnect(); //Establish TCP connection _tcpClient = new TcpClient(); _tcpClient.Connect(_server, _port); _networkStream = _tcpClient.GetStream(); //Create data thread _dataThread = new Thread(() => ReceiveData(_tcpClient, _networkStream)); _dataThread.Start(); //Assign variables _client = client; _connected = 1; //Fire OnConnected event if (!_intermediateState && OnConnected != null) { OnConnected(); } OutLogMessage("A connection was successfully established"); } catch (Exception ex) { _connected = 0; OutLogMessage(string.Format("CONNECTION ERROR: {0}\r\n", ex)); } //Done return Connected; }
public bool DoStep(NetworkClient networkClient, GameClient client) { if (IsReadyForAction) { _gameItemsGroups = Instance.GameItemsGroups; DateTime startShoppingTime = DateTime.Now; //Refresh list of items owners _listOfItemsOwners.Refresh(); //Refresh list of purchased items with their owners _listOfPurchasedItemsWithOwners.Refresh(); //Check full update requirement bool fullUpdate = _prewFullUpdateTime == 0 || Environment.TickCount - _prewFullUpdateTime >= SHOPPING_FULL_UPDATE_MIN * 60000; //Get location ident string locationIdent = Helper.GetCurrentLocationIdent(client); //Check on shop bool isShop = Locations.Shops.Contains(locationIdent); //Check on auction bool isAuction = Locations.Auctions.Contains(locationIdent); //Out action message string message = string.Format(@"started, {0} • in a shop: {1} • is it an auction: {2} • full update: {3} ", DateTime.Now, isShop ? "YES" : "NO", isAuction ? "YES" : "NO", fullUpdate ? "YES" : "NO").TrimEnd(); networkClient.OutActionLogMessage(this, message); //Current location must be a shop, check it if (isShop) { //Make a little shopping :) DoShopping(networkClient, client, locationIdent, isAuction, fullUpdate); } else { //Out action message message = "You are not inside a shop. Shopping unavailable."; networkClient.OutActionLogMessage(this, message); } //Store last time of full update _prewShoppingnTime = Environment.TickCount; if (fullUpdate) { _prewFullUpdateTime = _prewShoppingnTime; } //Out action message string shoppingTime = DateTime.Now.Subtract(startShoppingTime).ToString(); message = string.Format("completed. Duration: {0}", Helper.RemoveMilliseconds(shoppingTime)); networkClient.OutActionLogMessage(this, message); return true; } return false; }
private void ProcessSubGroupsList(NetworkClient networkClient, GameClient client, string groupId, int groupPage, bool isAuction, Packet groups, List<string> subGroupsOrderList) { //Get all subgroups nodes XmlNodeList itemsAndSubGroups = groups.GetNodes("O"); if (itemsAndSubGroups != null) { //For each subgroup node foreach (XmlNode itemOrSubGroup in itemsAndSubGroups) { //If it`s valid node if (itemOrSubGroup.Attributes != null) { XmlAttribute name; switch (groupId) { case GameItemsGroupID.PLANT_PACKS: name = itemOrSubGroup.Attributes["namef"]; break; case GameItemsGroupID.DOCUMENTS: name = itemOrSubGroup.Attributes["namef"] ?? itemOrSubGroup.Attributes["name"]; break; default: name = itemOrSubGroup.Attributes["name"]; break; } XmlAttribute type = itemOrSubGroup.Attributes["type"]; //If the node doesn`t have own name, ignore this node if (name == null || type == null) { continue; } string subGroupId = name.InnerText; string subGroupType = type.InnerText.Replace(".", ""); //Store subgroup->group relation _gameItemsGroups.StoreSubGroupIDToGroupRelation(groupId, subGroupId, subGroupType); //If we in the full processing mode (subGroupsOrderList != null), //groupId should be stored in the appropriate list if (subGroupsOrderList != null) { string ident = GameItemsSubGroup.GetSubGroupIdent(subGroupId, subGroupType); subGroupsOrderList.Add(ident); } //Is this a subgroup of items? XmlAttribute auc = itemOrSubGroup.Attributes["auc"]; if (auc != null) { //Check IgnoreForShopping flag GameItemsSubGroup subGroup = _gameItemsGroups[groupId].GetSubGroup(subGroupId, subGroupType); if (subGroup != null && (subGroup.IgnoreForShopping || subGroup.ItemsCount == 0)) { continue; } //okay, now go inside the subgroup XmlAttribute lvl = itemOrSubGroup.Attributes["lvl"]; XmlAttribute txt = itemOrSubGroup.Attributes["txt"]; //Verify parameters if (txt != null) { string subGroupName = GameItem.GetPureItemText(txt.InnerText); string subGroupLevel = lvl != null ? lvl.InnerText : ""; //There is one page available at least int pagesCount = 1; //For each page of items for (int subGroupPage = 0; subGroupPage < pagesCount; subGroupPage++) { //Make the filter string string filter = string.Format(@"name:{0},type:{1},lvl:{2}", subGroupId, subGroupType, subGroupLevel); //Get items list //Query params: 1: item group ID, 2: filter, 3: page number, 4: is auction? string getGroupsItemsListQuery = Packet.BuildPacket(FromClient.SHOP, Shop.ITEMS_GET_LIST, groupId, filter, subGroupPage, isAuction); networkClient.SendData(getGroupsItemsListQuery); //Get items list Packet groupItemsList = networkClient.InputQueue.Pop(FromServer.SHOP_DATA); if (groupItemsList != null) { //Calculate pages count or set according to ShopPagesLimit value if (subGroupPage == 0) { if (subGroup == null || subGroup.ShopPagesLimit == 0) { int itemsCount = int.Parse(groupItemsList["@m"]); pagesCount = itemsCount / 8 + (itemsCount % 8 > 0 ? 1 : 0); } else { pagesCount = subGroup.ShopPagesLimit; } } //Process items list ProcessItemsList(networkClient, client, groupId, groupPage, subGroupId, subGroupName, subGroupType, subGroupLevel, groupItemsList); } } } } //otherwise let`s process standalone item else { //Check IgnoreForShopping flag GameItemsSubGroup subGroup = _gameItemsGroups[groupId].GetSubGroup(subGroupId, subGroupType); if (subGroup != null && (subGroup.IgnoreForShopping || subGroup.ItemsCount == 0)) { continue; } XmlAttribute txt = itemOrSubGroup.Attributes["txt"]; XmlAttribute lvl = itemOrSubGroup.Attributes["lvl"]; //Verify parameters if (txt != null) { string subGroupName = GameItem.GetPureItemText(txt.InnerText); string subGroupLevel = lvl != null ? lvl.InnerText : ""; //Process item ProcessItem(networkClient, client, groupId, 0, subGroupId, subGroupName, subGroupType, subGroupLevel, itemOrSubGroup); } } } } } }
private void DoSellingItemsFromInv(NetworkClient networkClient, GameClient client) { List<InventoryItem> clonnedList = new List<InventoryItem>(client.InventoryItems); foreach (InventoryItem inventoryItem in clonnedList) { string subGroupId = inventoryItem.SubGroupID; string subGroupType = inventoryItem.SubGroupType; string groupId = _gameItemsGroups.SubGroupToGroupId(subGroupId, subGroupType); if (!string.IsNullOrEmpty(groupId)) { GameItem gameItem = _gameItemsGroups.GetItem(groupId, subGroupId, subGroupType, inventoryItem.PureName, inventoryItem.Level); if (gameItem != null && !gameItem.IgnoreForSelling) { if (string.IsNullOrEmpty(gameItem.SubGroupID)) { gameItem.SubGroupID = subGroupId; } //Get subgroup GameItemsSubGroup sg = _gameItemsGroups[groupId].GetSubGroup( subGroupId, subGroupType); if (!sg.IgnoreForSelling) { //Cache items list from auction CacheAuctionItems(networkClient, groupId, subGroupId, sg.Type, sg.Level); //Create shop item ShopItem myItem = ShopItem.FromInventoryItem(gameItem, inventoryItem, client.Login); //Do dumping DoDumping(networkClient, client, myItem, false); } } } } }
private void SellItem(NetworkClient networkClient, GameClient client, string itemID, string itemName, int itemCount, float cost, bool isSingleItem) { string sellItem = isSingleItem ? Packet.BuildPacket(FromClient.SHOP, Shop.ITEM_SELL, itemID, cost) : Packet.BuildPacket(FromClient.SHOP, Shop.ITEM_SELL, itemID, cost, itemCount); networkClient.SendData(sellItem); string message = string.Format("Trying to sell: '{0}', cost: {1}, count: {2}...", itemName, cost, itemCount); networkClient.OutLogMessage(message); //Get a result string[] packetTypes = new[] { FromServer.SHOP_OK, FromServer.SHOP_ERROR }; Packet getResult = networkClient.InputQueue.PopAny(packetTypes); //Check the result if (getResult != null) { switch (getResult.Type) { //Selling is failed case FromServer.SHOP_ERROR: { string errorCode = getResult["@code"]; string errorMessage = Errors.ShopError.GetErrorMessage(errorCode); networkClient.ThrowError(errorMessage, false); break; } //Done default: { //Remove the item from inventory InventoryItem inventoryItem = client.InventoryItems.Find(ii => ii.ID == itemID); if (inventoryItem != null) { client.InventoryItems.Remove(inventoryItem); } //Play alert _soundPlayer.Play(); //Out log message networkClient.OutLogMessage("Successful"); break; } } } }
private void DoShopping(NetworkClient networkClient, GameClient client, string locationIdent, bool isAuction, bool fullUpdate) { //Full update if (fullUpdate) { //Clear all of cached group pages _cachedGroupPages.Clear(); //Clear all SubGroupIDToGroup relations _gameItemsGroups.ClearSubGroupToGroupIDList(); //Send: get full groups list string getFullGroupsList = Packet.BuildPacket(FromClient.SHOP, Shop.ITEMS_GET_FULL); networkClient.SendData(getFullGroupsList); //Get response Packet fullGroupsList = networkClient.InputQueue.Pop(FromServer.SHOP_DATA); if (fullGroupsList != null) { //Populate items list, full1: [groupId, itemsNum] string full = fullGroupsList.GetValue("@full"); IEnumerable<KeyValuePair<string, string>> fullList = Helper.SplitStringItems(full, ":", ","); //Process all items ProcessGroupPagesFull(networkClient, client, locationIdent, isAuction, fullList); } } //or lets use cached group pages else { //Process every cached group foreach (GameItemsGroup group in _gameItemsGroups.Groups) { if (!group.IgnoreForShopping) { ProcessGroupPagesFromCache(networkClient, client, locationIdent, isAuction, group); } } } }
private void ProcessGroupPagesFromCache(NetworkClient networkClient, GameClient client, string locationIdent, bool isAuction, GameItemsGroup gameItemsGroup) { string groupId = gameItemsGroup.ID; string groupName = gameItemsGroup.Name; int subGroupsCount = gameItemsGroup.SubGroupsCount; string cachedGroupPageIdent = GetCachedGroupPageIdent(locationIdent, groupId); //If group pages were cached for the location if (_cachedGroupPages.ContainsKey(cachedGroupPageIdent)) { DateTime startTime = DateTime.Now; //Get cached group pages List<Packet> cachedGroupPages = _cachedGroupPages[cachedGroupPageIdent]; //For each cached page of group int pagesCount = cachedGroupPages.Count; for (int groupPage = 0; groupPage < pagesCount; groupPage++) { Packet groups = cachedGroupPages[groupPage]; ProcessSubGroupsList(networkClient, client, groupId, groupPage, isAuction, groups, null); } //Out action message string endTime = DateTime.Now.Subtract(startTime).ToString(); string message = string.Format("'{0}' processed. Subgroups count: {1}, duration: {2}", groupName, subGroupsCount, Helper.RemoveMilliseconds(endTime)); networkClient.OutActionLogMessage(this, message); } }
private void InstantBuyItem(NetworkClient networkClient, GameClient client, string groupId, int groupPage, XmlNode item, GameItem gameItem, string owner) { if (item != null && item.Attributes != null) { XmlAttribute cost = item.Attributes["cost"]; if (cost != null) { float qualityMdf = 1f; float fCost = float.Parse(cost.InnerText); //If InstantPurchaseCost isn`t very low, take into an item quality if (gameItem.FactoryCost > 0 && gameItem.InstantPurchaseCost > 0 && gameItem.FactoryCost / gameItem.InstantPurchaseCost > 10f) { XmlAttribute quality = item.Attributes["quality"]; XmlAttribute maxQuality = item.Attributes["maxquality"]; if (quality != null && maxQuality != null) { float fQuality = float.Parse(quality.InnerText); float fMaxQuality = float.Parse(maxQuality.InnerText); qualityMdf = fQuality / fMaxQuality; } } //If an item cost is lower than InstantPurchaseCost, let`s try to buy the item if (fCost <= gameItem.InstantPurchaseCost * qualityMdf) { XmlAttribute id = item.Attributes["id"]; XmlAttribute count = item.Attributes["count"]; if (id != null && count != null) { string sId = id.InnerText; int iCount = int.Parse(count.InnerText); //Check on antibot maneuvers if (!PassAntibotManeuvers(gameItem, iCount, owner)) { return; } //Out log message int totalCost = (int) Math.Ceiling(iCount * fCost); string groupName = _gameItemsGroups[groupId].Name; string message = string.Format("Trying to buy: '{0}', owner: [{1}], group: {2}, page: {3}, count: {4}, cost: {5}, total cost: {6}...", gameItem, owner, groupName, groupPage + 1, iCount, fCost, totalCost); networkClient.OutLogMessage(message); //Try to buy the item //Query params: 1: item ID, 2: count, 3: cost string getGroupsItemsList = Packet.BuildPacket(FromClient.SHOP, Shop.ITEMS_BUY, sId, iCount, fCost); networkClient.SendData(getGroupsItemsList); //Get a purchase result string[] packetTypes = new[] {FromServer.ITEM_ADD_ONE, FromServer.SHOP_ERROR}; Packet purchaseResult = networkClient.InputQueue.PopAny(packetTypes); //Check the purchase result if (purchaseResult != null) { switch (purchaseResult.Type) { //Purchasing is failed case FromServer.SHOP_ERROR: { string errorCode = purchaseResult["@code"]; string errorMessage = Errors.ShopError.GetErrorMessage(errorCode); networkClient.ThrowError(errorMessage, false); break; } //Successful default: { //Add the item to the inventory or, if it`s bad, out log message InventoryItem inventoryItem = Helper.ParseInventoryItem(purchaseResult.Data); if (inventoryItem != null) { //Add inventory item record client.InventoryItems.Add(inventoryItem); //Join inventory items GameStep_JoinInventory.DoJoin(networkClient, client); } else { string errorMessage = string.Format("BUY: BAD INVENTORY ITEM: {0}", purchaseResult.Data); networkClient.ThrowError(errorMessage); } //Play alert _soundPlayer.Play(); //Out log message networkClient.OutLogMessage("Successful"); break; } } } } } } } }
public bool DoStep(NetworkClient networkClient, GameClient client) { List<string> messages = new List<string>(); Packet[] packets = networkClient.InputQueue.PopAll(FromServer.IMS); if (!networkClient.OutInstantMessages || packets.Length == 0) { return false; } foreach (Packet packet in packets) { string sMessages = packet["@m"]; messages.AddRange(sMessages.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries)); } if (messages.Count > 0) { networkClient.OutInstantMessage(string.Format("Instant message(s), {0} received:", messages.Count)); foreach (string message in messages) { string logMessage; string[] messageParts = message.Split('\t'); //Switch IMS command type switch (messageParts[1]) { //Private IM case "100": { logMessage = string.Format("\t• {0}. Private from [{1}]: {2}", messageParts[0], messageParts[2], messageParts[3]); break; } //Shop message case "217": { logMessage = string.Format("\t• {0}. Was received Coins[{1}] from [{2}]. Target: {3}", messageParts[0], messageParts[3], messageParts[4], messageParts[6]); break; } default: { logMessage = message; break; } } //Play alert _soundPlayer.Play(); networkClient.OutInstantMessage(logMessage); } //Clear all IMS on server string clearIMS = Packet.BuildPacket(FromClient.CLEAR_IMS); networkClient.SendData(clearIMS); return true; } return false; }
private void DoDumping(NetworkClient networkClient, GameClient client, ShopItem myItem, bool itemInAuc) { bool needToSellItem = !itemInAuc; //Need to join if (itemInAuc && !myItem.IsSingle) { InventoryItem[] inventoryItem = client.InventoryItems.Where ( ii => ii.Ident == myItem.Ident ).ToArray(); needToSellItem = inventoryItem.Length > 0; } //Need to dumping ShopItem[] shopItems = _auctionItems[myItem.SubGroupID].Where ( si => si.Ident == myItem.Ident ).ToArray(); needToSellItem |= shopItems.Length > 0; if (needToSellItem) { //Calculate target cost float itemCost = CalculateItemCost(client, myItem, shopItems); if (itemCost > 0f) { //Prevent to sell bla-bla if (!itemInAuc && !myItem.IsSingle && ((itemCost <= 1f && myItem.Count <= 100 ) || (itemCost <= 20f && myItem.Count <= 50 ) || (itemCost <= 50f && myItem.Count <= 10 ))) { return; } string itemID = myItem.ID; string itemName = myItem.Parent.ToString(); int itemCount = myItem.Count; //Get the item from auction if (itemInAuc) { if (!GetOwnItem(networkClient, client, myItem)) { return; } InventoryItem inventoryItem = client.InventoryItems.FirstOrDefault ( ii => ii.ID == myItem.ID ); if (inventoryItem == null) { return; } itemID = inventoryItem.ID; itemCount = inventoryItem.Count; } //Don`t sell the item if it`s cost lower than 50 if (itemCount > 1 && itemCount * itemCost < 50) { return; } //Sell the item SellItem(networkClient, client, itemID, itemName, itemCount, itemCost, myItem.IsSingle); } } }
private bool ProcessItem(NetworkClient networkClient, GameClient client, string groupId, int groupPage, string subGroupId, string subGroupName, string subGroupType, string subGroupLevel, XmlNode item) { //If it`s valid node if (item.Attributes != null) { //Ingore own item string sOwner = string.Empty; XmlAttribute owner = item.Attributes["owner"]; if (owner != null) { sOwner = owner.InnerText; if (client.Login.Equals(sOwner, StringComparison.InvariantCultureIgnoreCase)) { return false; } } XmlAttribute txt = item.Attributes["txt"]; XmlAttribute lvl = item.Attributes["lvl"]; XmlAttribute made = item.Attributes["made"]; if (txt != null && (made == null || made.InnerText != "Public Factory")) { //Get item data string sTxt; switch (groupId) { case GameItemsGroupID.PLANT_PACKS: { XmlNode txtNode = item.SelectSingleNode("O/@txt"); sTxt = txtNode != null ? string.Format("{0} {1}", subGroupName, GameItem.GetModificationString(txtNode.InnerText)).TrimEnd() : subGroupName; break; } default: { sTxt = txt.InnerText; break; } } string sLvl = lvl != null ? lvl.InnerText : ""; //Get item or create new one GameItem gameItem = _gameItemsGroups.GetItem(groupId, subGroupId, subGroupType, sTxt, sLvl); if (gameItem == null) { if (item.Attributes != null) { XmlAttribute massa = item.Attributes["massa"]; float fMassa = massa == null ? 0 : float.Parse(massa.InnerText); gameItem = _gameItemsGroups.AddItem(groupId, subGroupId, subGroupType, subGroupName, subGroupLevel, sTxt, sLvl, fMassa); } } else { //Update the item last review time gameItem.LastReviewDate = DateTime.Now; } //Process game item if (gameItem != null && !gameItem.IgnoreForShopping && item.Attributes != null) { //Get factory cost XmlAttribute factoryCost = item.Attributes["max_p"]; if (factoryCost != null && !string.IsNullOrEmpty(factoryCost.InnerText)) { gameItem.FactoryCost = float.Parse(factoryCost.InnerText); } //Instant buy the item InstantBuyItem(networkClient, client, groupId, groupPage, item, gameItem, sOwner); } } } return true; }
private void DoSellingItemsFromAuc(NetworkClient networkClient, GameClient client) { //There is one page available at least int pagesCount = 1; //For each page of group for (int itemsPage = 0; itemsPage < pagesCount; itemsPage++) { //Query page[i] of group //Query params: 1: group ID, 2: filter, 3: page number, 4: is auction? string getGroupPagesList = Packet.BuildPacket(FromClient.SHOP, Shop.ITEMS_GET_LIST, GameItemsGroupID.MY_THINGS, "", itemsPage, true); networkClient.SendData(getGroupPagesList); //Get items Packet itemsList = networkClient.InputQueue.Pop(FromServer.SHOP_DATA); if (itemsList != null) { //Calculate items pages count if (itemsPage == 0) { int cnt = int.Parse(itemsList["@m"]); pagesCount = cnt / 8 + (cnt % 8 > 0 ? 1 : 0); } //Process items list ProcessMyAucItemsList(networkClient, client, itemsList); } } }
private void ProcessItemsList(NetworkClient networkClient, GameClient client, string groupId, int groupPage, string subGroupId, string subGroupName, string subGroupType, string subGroupLevel, Packet itemsList) { //Get all nodes of items XmlNodeList items = itemsList.GetNodes("O"); if (items != null) { //For each node of items foreach (XmlNode item in items) { if (!ProcessItem(networkClient, client, groupId, groupPage, subGroupId, subGroupName, subGroupType, subGroupLevel, item)) { return; } } } }
private float CalculateItemCost(GameClient client, ShopItem myItem, ShopItem[] shopItems) { //Get basic item cost float cost = myItem.FactoryCost > 0f ? myItem.FactoryCost * 1.5f : 0f; //Last shop item with a lower quality ShopItem lqShopItem; foreach (ShopItem shopItem in shopItems) { //Detect "antidumping" if (!AssertAntidumping(shopItem, shopItems)) { continue; } if (!myItem.IsSingle) { if (shopItem.IsOwnItem(client.Login)) { if (myItem.ID == shopItem.ID) { return 0f; } continue; } //Calculate subs. value float subsValue = shopItem.Cost <= 50f ? 0.01f : 1f; //Calculate a count ratio double countRatio = myItem.Count / shopItem.Count; //Calculate a shop item cost float shopItemCost = shopItem.Count * shopItem.Cost; //My item is more expensive if (myItem.Cost > shopItem.Cost && shopItemCost > 100 && countRatio < 10f) { return shopItem.Cost - subsValue; } //Equal cost if (myItem.Cost == shopItem.Cost) { return countRatio < 1.3f ? shopItem.Cost - subsValue : 0f; } //My item is chiper if (myItem.Cost < shopItem.Cost) { cost = countRatio < 1.3f ? shopItem.Cost - subsValue : shopItem.Cost; return cost != myItem.Cost ? cost : 0f; } } else { if (shopItem.IsOwnItem(client.Login)) { return 0f; } if (shopItem.Quality < myItem.Quality) { continue; } lqShopItem = shopItem; if (lqShopItem.Quality == myItem.Quality) { return lqShopItem.Cost - 1f; } Dictionary<float, List<ShopItem>> dictShopItems = new Dictionary<float, List<ShopItem>>(); foreach (ShopItem uniqueShopItem in shopItems) { List<ShopItem> list; if (!dictShopItems.ContainsKey(uniqueShopItem.Quality)) { list = new List<ShopItem>(); dictShopItems.Add(uniqueShopItem.Quality, list); } else { list = dictShopItems[uniqueShopItem.Quality]; } list.Add(uniqueShopItem); } List<ShopItem> resultList = new List<ShopItem>(); foreach (List<ShopItem> list in dictShopItems.Values) { int itemsCnt = list.Count; if (itemsCnt >= 3) { int truncNum = (int)Math.Ceiling(itemsCnt * 0.2f); int truncCnt = Math.Max(itemsCnt - (truncNum * 2), 1); resultList.AddRange(list.Skip(truncNum).Take(truncCnt)); } else { resultList.AddRange(list); } } if (resultList.Count < 5) { return 0f; } float sumCost = 0f, sumQuality = 0f; foreach (ShopItem resultShopItem in resultList) { sumCost += resultShopItem.Cost; sumQuality += resultShopItem.Quality; } double qualityUnitCost = sumQuality / sumCost; float qualityDiff = lqShopItem.Quality - myItem.Quality; float newCost = lqShopItem.Cost - ((float)qualityUnitCost * qualityDiff); return newCost; } } return cost; }