Page 4 of 6 FirstFirst ... 2 3 4 5 6 LastLast
Results 31 to 40 of 51

Thread: Writing to file?

  1. #31
    Minion Bale's Avatar
    Join Date
    Jun 2008
    Posts
    13,224

    Default

    Two things. First... OMG, please don't turn a string into a string. It just makes me feel woozy.

    Secondly, you might want to use a function called get_player_id() which will turn "cowmanbob" into "840337".

  2. #32

    Default

    FYI, if you're going to be parsing pages for information, it would probably behoove you to pick up some regex, which is supported by mafia. I whipped up the below script which, if you save this as "showplayer.ash" in your mafia relay\ folder, will parse out the player's name, id, level, and class, and spit that info back to the CLI as well as store it in properties. You can modify this to suit your needs, or if you want to just use this as your showplayer.ash, you could then call your visit_url by itself without bothering to parse it yourself and pull the info you need from the appropriate properties.

    Code:
    // showplayer.ash
    
    buffer process_profile(buffer page) {
    	matcher m = create_matcher( "<center><b>([^<]+)</b> \\(\\#(\\d+)\\)<br>[^\\d]*(\\d+)[^\\d]*<br>", page );
    	if( m.find() ) {
    		print("Name: " + m.group(1));
    		print("ID: " + m.group(2));
    		print("Level: " + m.group(3));
    		set_property( "lastViewedName", m.group(1) );
    		set_property( "lastViewedID", m.group(2) );
    		set_property( "lastViewedLevel", m.group(3) );
    	}
    	m = create_matcher( "<b>Class:</b></td><td>([^<]+)</td>", page );
    	if( m.find() ) {
    		print("Class: " + m.group(1));
    		set_property( "lastViewedClass", m.group(1) );
    	}
    	return page;
    }
    
    void main() {
    	visit_url().process_profile().write();
    }
    Even if you don't really understand those regexes yet, you can probably see how understanding them and pulling the info out of the page as it's displayed is a better option than running a bunch of contains_text's looking for specific classes - unless the actual code of the page changes, these regexes will work for all current and future class paths as well.

    As far as putting all your info on one line, that's pretty straightforward:

    Code:
    file_to_map( "searchplayerbot.txt" , my_list);
    string temp = "";
    for i from 0 to (count(my_list) - 1) {
       print( my_list[i] );
       if( i > 0 ) temp += ", ";
       temp += my_list[i];
    }
    kmail("pazsox", my_list[i], 0, map);

    Edit: After checking a few profiles this doesn't always work due to the sheer number of effects and things on a user's profile page. A better option might be a feature request for an ash function to do a /whois and return a string containing the response, maybe something like slash_whois(string player) or get_player_info(string player).
    Last edited by Smelltastic; 04-05-2017 at 08:45 PM.

  3. #33
    Developer
    Join Date
    Aug 2009
    Posts
    2,630

    Default

    showplayer.php works just fine for me, using the link you specified:

    Code:
    record PlayerStat {
      int lvl;
      class cls;
    };
    
    PlayerStat get_player(string playerName) {
      string playerId = get_player_id(playerName);
      string page = visit_url("searchplayer.php?searching=Yep.&searchstring="+playerName+"&searchlevel=&searchranking=&hardcoreonly=0");
    
      // We don't care about the player's capitalization, and this is possibly more efficient than converting the page to lowercase.
      // Group 1 is the player level; group 2 is the player class.
      matcher m = create_matcher('showplayer\\.php\\?who=' + playerId + '">[^<]+</a></b> (?:<br>\\(<a target=mainpane href="showclan\\.php\\?whichclan=\\d+">[^>]+</a>\\))?</td><td valign=top class=small>' + playerId + '</td><td valign=top class=small>(\\d+)</td><td class=small valign=top>([^<]+)</td></tr>', page);
      if (m.find()) {
        return new PlayerStat(m.group(1).to_int(), m.group(2).to_class());
      }
      // No player found.
      return new PlayerStat(0, $class[none]);
    }

  4. #34
    Senior Member
    Join Date
    Feb 2009
    Posts
    139

    Default

    As far as putting all your info on one line, that's pretty straightforward:

    Code:
    file_to_map( "searchplayerbot.txt" , my_list);
    string temp = "";
    for i from 0 to (count(my_list) - 1) {
       print( my_list[i] );
       if( i > 0 ) temp += ", ";
       temp += my_list[i];
    }
    kmail("pazsox", my_list[i], 0, map);
    Originally Posted by Smelltastic View Post
    This code didn't work for me. I'll play around with it today
    Last edited by Pazleysox; 04-06-2017 at 11:24 AM. Reason: oops

  5. #35
    Senior Member
    Join Date
    Feb 2009
    Posts
    139

    Default

    showplayer.php works just fine for me, using the link you specified:

    Code:
    record PlayerStat {
      int lvl;
      class cls;
    };
    
    PlayerStat get_player(string playerName) {
      string playerId = get_player_id(playerName);
      string page = visit_url("searchplayer.php?searching=Yep.&searchstring="+playerName+"&searchlevel=&searchranking=&hardcoreonly=0");
    
      // We don't care about the player's capitalization, and this is possibly more efficient than converting the page to lowercase.
      // Group 1 is the player level; group 2 is the player class.
      matcher m = create_matcher('showplayer\\.php\\?who=' + playerId + '">[^<]+</a></b> (?:<br>\\(<a target=mainpane href="showclan\\.php\\?whichclan=\\d+">[^>]+</a>\\))?</td><td valign=top class=small>' + playerId + '</td><td valign=top class=small>(\\d+)</td><td class=small valign=top>([^<]+)</td></tr>', page);
      if (m.find()) {
        return new PlayerStat(m.group(1).to_int(), m.group(2).to_class());
      }
      // No player found.
      return new PlayerStat(0, $class[none]);
    }
    Originally Posted by heeheehee View Post
    umm... I tried to make this my searchplayer.ash file, and it didn't seem to care for it... I tried mixing it with smelltastic's script, but I'm not savvy enough to figure it out...

  6. #36

    Default

    This code didn't work for me. I'll play around with it today
    Originally Posted by Pazleysox View Post
    looking back at it it's clear the my_list[i] in the last kmail line should've been changed to temp

  7. #37
    Senior Member
    Join Date
    Feb 2009
    Posts
    139

    Default

    looking back at it it's clear the my_list[i] in the last kmail line should've been changed to temp
    Originally Posted by Smelltastic View Post
    Works like a dream, but it's sending multiple k-mails. 1 per line of the map. Progressively adding more players with a ",". (I currently have 3 names)

    k-mail 1:
    name 1
    k-mail 2:
    name 1, name 2
    k-mail 3
    name 1, name 2, name 3

    I really only need k-mail X, where X is the number of names in the map.

  8. #38
    Senior Member Theraze's Avatar
    Join Date
    Mar 2010
    Posts
    8,656

    Default

    Sounds like you have the kmail inside the loop.

  9. #39
    Senior Member
    Join Date
    Feb 2009
    Posts
    139

    Default

    Sounds like you have the kmail inside the loop.
    Originally Posted by Theraze View Post
    having
    Code:
    kmail("pazsox", my_list[i], 0, map);
    outside the loop didn't work, so I put it inside. Switching the my_list[i] to temp, and then putting it outside (as you suggested) it worked like a dream.

  10. #40

    Default

    so, extracting player info from a search seems like a generally useful thing so I made this, not sure if you're already satisfied with what you've got but here it is anyway. I wasn't even thinking about using searchplayer.php before which is much better than using the profile page. heeheehee already did the level etc but I just wanted to make it more generalized.

    Code:
    // getplayers.ash (not a relay script, import it into your own script and call GetPlayer or GetPlayers)
    
    record playerdata {
    	string name;
    	string id;
    	string cclass;
    	int level;
    	string clanname;
    	string clanid;
    };
    
    playerdata[int] GetPlayers(string search) {
    	playerdata[int] ret;
    	
    	buffer page = visit_url( "searchplayer.php?searching=Yep.&searchstring=" + search.url_encode() );
    	
    	matcher m = create_matcher( "href=\"showplayer.php\\?who=(\\d+)\">([^<]+)</a></b> (<br>\\(<a target=mainpane href=\"showclan.php\\?whichclan=(\\d+)\">([^<]+)" +
    						"</a>\\))?</td><td valign=top class=small>(\\d+)</td><td valign=top class=small>(\\d+)</td><td class=small valign=top>([^<]+)</td>", page );
    	
    	// Groups:
    	// 1: Player ID, 2: Player Name, 3: Clan HTML, 4: Clan ID, 5: Clan Name, 6: Player ID again, 7: Player Level, 8: Player Class
    	
    	while( m.find() ) {
    		int i = ret.count();
    		ret[i].name = m.group(2);
    		ret[i].id = m.group(1);
    		ret[i].cclass = m.group(8);
    		ret[i].level = m.group(7).to_int();
    		ret[i].clanname = m.group(5);
    		ret[i].clanid = m.group(4);
    	}
    	
    	return ret;
    }
    
    // The only way to get a specific player is to search on their name and check the responses for a match.
    playerdata GetPlayer(string pname) {
    	playerdata[int] players = GetPlayers(pname);
    	foreach i,p in players {
    		if( p.name.to_lower_case() == pname.to_lower_case() )
    			return p;
    	}
    	print("Player \"" + pname + "\" not found!", "red");
    	return new playerdata;
    }
    
    void PrintPlayers(string search) {
    	playerdata[int] players = GetPlayers(search);
    	
    	foreach i,p in players {
    		print( "Player found: " + p.name + " the level " + p.level.to_string() + " " + p.cclass + (p.clanname!=""?", in clan " + p.clanname:"") + "!" );
    	}
    }
    Then to use it you'd do something like

    Code:
    import "getplayers.ash"; // At top of script
    
    // rest of your script goes here
    
    file_to_map( "searchplayerbot.txt" , my_list);
    playerdata player;
    string temp = "";
    foreach i,playername in my_list {
    	player = GetPlayer( playername );
    	if( player.name != "" ) {
    		if( temp != "" ) temp += ", ";
    		temp += player.name + " the level " + player.level + " " + player.cclass;
    	} else {
    		print( "Error: Player " + playername + " not found in search!", "red" );
    	}
    }
    kmail("pazsox", temp, 0, map);
    Last edited by Smelltastic; 04-06-2017 at 06:16 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •