Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Sorting through multiple arrays (https://forums.graalonline.com/forums/showthread.php?t=134267487)

sssssssssss 12-03-2012 01:47 AM

Sorting through multiple arrays
 
I've got it sorted through like recommended above for the most part, but am now having issues sorting when showing "all" messages with timevar2. timevar2 is the [2] value in each client array for each message.

My logic is probably flawed on here, and/or I'm missing something really easy.

PHP Code:

this.categoryArr = {"mass", "tell", "system", "guild"}; 

PHP Code:

function showAllMessages()
{
  
//showMassMessages();
  //showSystemMessages();
  //showTellMessages();
  //showGuildMessages();
  
  // set size of all messages
  
temp.messageSize = client.mass.messages.size() + client.system.messages.size() + client.tell.messages.size() + client.guild.messages.size();
  
  
// set images to correlate with categoryArr
  
temp.messagesImg = {"pmbubble_mass.png", "pmbubble_normal.png", "pmbubble_admin.png", "pmbubble_guild.png"};
  
  
// random number for image id
  
temp.rdnID = random(1, 99999);
  
  
// sort through all messages set so far per size
  
for (temp.i = 0; i < messageSize - 1; i++)
  {
    
//this.categoryArr = {"mass", "tell", "system", "guild"}; for reference
    // sort through each category at the id set above
    
for (temp.j = 0; j < this.categoryArr.size() - 1; j++)
    {
      
// if the timevar is less than the next one, show
      
if (client.(@this.categoryArr[j]).messages[i][2] < client.(@this.categoryArr[j]).messages[i + 1][2])
      {
        
// since this timevar is smaller than next, check for message on hold that wasn't previously
        
if (holdMessage == null || holdMessage == 0 || holdMessage == "")
        {
          
// no messages on hold, show this message
          
ChatBox_WindowText.addtext("<img src=" @temp.messagesImg[j]@ " id=sendImg." @temp.rndID@ "/>"@client.(@this.categoryArr[j]).messages[i][0]@ ": " @client.(@this.categoryArr[j]).messages[i][1]@ "<br>", false);
          
//temp.holdMessage = client.(@this.categoryArr[j]).messages[i];
        
}else 
        {
          
// there is a message on hold, compare it to this one
          
if (client.(@this.categoryArr[j]).messages[i][2] < holdMessageTime)
          {
            
// this messages timevar is lower than the one on hold, show message
            
ChatBox_WindowText.addtext("<img src=" @temp.messagesImg[j]@ " id=sendImg." @temp.rndID@ "/>"@client.(@this.categoryArr[j]).messages[i][0]@ ": " @client.(@this.categoryArr[j]).messages[i][1]@ "<br>", false);
          }else
          {
            
// the message timevar is higher than the one on hold, show message on hold
            
ChatBox_WindowText.addtext("<img src=" @temp.messagesImg[holdMessageImg]@ " id=sendImg." @temp.rndID@ "/>"@holdMessage[0]@ ": " @holdMessage[1]@ "<br>", false);
            
            
// reset hold message values to null for next message to know
            
player.chat = holdMessage@ ", " @holdMessageTime@ ", " @holdMessageImg;
            
temp.holdMessage = null;
            
temp.holdMessageTime = null;
            
temp.holdMessageImg = null;
            
            
// show message after one on hold, since it was still less than the next
            
ChatBox_WindowText.addtext("<img src=" @temp.messagesImg[j]@ " id=sendImg." @temp.rndID@ "/>"@client.(@this.categoryArr[j]).messages[i][0]@ ": " @client.(@this.categoryArr[j]).messages[i][1]@ "<br>", false);
          }
        }
      }else 
      {
        
// set time and message hold to save and compare to next message
        
temp.holdMessageTime = client.(@this.categoryArr[j]).messages[i][2];
        
temp.holdMessage = client.(@this.categoryArr[j]).messages[i];
        
temp.holdMessageImg = j;
      }
    }
  }
  
ChatBox_WindowText.scrolltobottom();
} 

I have buttons and again, this is supposed to show all messages. I was reluctant to post this lol, but I'm not the first to do it I'm sure so all well.

It does show some things, and switching back and forth from a category to all makes things pop to the top instead of bottom, but it doesn't show "all" things like it's supposed to and I'm just plain stuck on it. Everything else works fine though...

P.S. I moved this because it really should have been in it's own thread. It didn't have much to do with where it was...

cbk1994 12-03-2012 02:02 AM

I just skimmed the code, but it sounds like you just want to have different types of messages? Why not just store them all in the same array?

PHP Code:

player.client.messages = {
  
// {message, category}
  
{"Message one!", "mass"},
  {
"Message two!", "tell"},
  {
"Message three!", "guild"} // etc
}; 

If you keep them all in the same array (use a multi-dimensional array so you can store multiple attributes for each message), you don't have to worry about sorting them later on, plus you keep maintaining the code a lot easier (for example, adding a new message type is a cinch).

(I'm still not really sure why you're not just sending triggers and insist on using arrays)

sssssssssss 12-03-2012 02:09 AM

I am sending triggers. I keep them stored client array because I'm just improving on what was already there, but if I just trigger the message straight through there is no record, and my system shows a record of past messages for the player.

I also have buttons that show a certain category (e.g. masses, or guild messages), which is why I made it like it is as well.

Even in the thing you posted above, I'd still have to separately show mass, tell, guild, and system messages, and then show them all at once. Granted sorting them all in one array might be easier to separate than add together, but also I'm figuring the large amount those variables could turn into on some categories, and prefer them separated.

I have them all sorted out and show, I just can't seem to loop through and pull them together.

cbk1994 12-03-2012 02:19 AM

Quote:

Originally Posted by sssssssssss (Post 1708486)
I am sending triggers. I keep them stored client array because I'm just improving on what was already there, but if I just trigger the message straight through there is no record, and my system shows a record of past messages for the player.

I also have buttons that show a certain category (e.g. masses, or guild messages), which is why I made it like it is as well.

Do you really need to store your message data in a client variable? Does the server really need to be keeping track of all of that? Sounds like you could happily store the messages clientside in a flag of your messages weapon.

Quote:

Even in the thing you posted above, I'd still have to separately show mass, tell, guild, and system messages, and then show them all at once. Granted sorting them all in one array might be easier to separate than add together, but also I'm figuring the large amount those variables could turn into on some categories, and prefer them separated.
There's still no reason that I can discern for storing them the way you are. When you send a trigger to the client, include the message. Let the messages system then add it to the message list you're storing in a weapon flag. If you need to do filtering, just loop through backwards until you get n number of messages of the appropriate category (however many past messages you want to display).

I'm not sure what you mean by the "large amount those variables could turn into".

There's really no need for an array anywhere here as far as I can tell except possibly in the implementation of the messages display clientside.

sssssssssss 12-03-2012 02:27 AM

It's just a chatbox like any other MMORPG out there. So I have categories for All, /Tells, /mass, /guild, and system messages.

I'm not trying to be a jerk or unwilling to do it differently. Does storing it in the weapon flag, db npc, or client var change anything?

The array is because it adds to all players for things like a mass or players in a guild or a tell to a player, so the array holds all the info like the sender, message, and timevar. Doing those separate would be much more cumbersome I would guess.

Again, it could be stored in a txt file, db npc, or as I do it now a client array, but I don't see how it really changes where it is stored because each message contains more than just one value so it would have to be an array at least per my logic, and the location of that array doesn't seem to change too much.

Do you see any reason in my code why it would only show up some of them, and then not in timevar order when leaving the category and going back to it?

Also I updated the code in first post with comments so it shows what I'm at least trying to do...

I'm not great on terminology but I'm pretty sure all I need is a bubble sort, along with a swap holding so it sorts correctly, I just can't seem to wrap my head around it.

sssssssssss 12-04-2012 03:28 AM

PHP Code:

// now set sort value 3 item in array and sort by that value for new array list
  
for (temp.l = 0; temp.l < temp.allValues.size(); temp.l++) { 
    
temp.allValues[temp.l].sort_by = temp.allValues[temp.l][2]; 
  } 
  
temp.allValues.sortbyvalue("sort_by", "float", true); 


That little guy : sortbyvalue, solved it. I added all client arrays into a temp, then set to sort by the timevar held in multidem array id 2 of each, then sortbyvalue.


Maybe it's not how other people would have done it, but it's not a bad way and it works, and now messages can be separated for my anal organization instead of all in one value holder.


All times are GMT +2. The time now is 08:50 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.