Jump to content

nextUntil() use...


celfred
 Share

Recommended Posts

Hello,

 

I'm stuck with a nextUntil() use I can't figure out. The following function works if I use the commented part instead of the last 2 lines (with nextUntil).

function checkStreak($player) {
  // Set Ambassador skill if necessary
  $ok = 1;
  $streak = 0;
  /* $lastEvents = $player->get("name=history")->find("template=event, sort=-date")->not("task.name=donated|donation|absent, limit=10"); */
  /* foreach ($lastEvents as $e) { */
  /*   if ($ok == 1) { */
  /*     if ($e->task->HP >= 0) { */
  /*       $streak++; */
  /*     } else { */
  /*       $ok = 0; */
  /*     } */
  /*   } */
  /* } */
  $lastEvent = $player->get("name=history")->children("template=event, sort=-date")->not("task.name=donated|donation|absent")->first();
  $streak = $lastEvent->nextUntil("task.HP<0")->count();
  return $streak;
}

But I know it's ugly and... I sometimes wish I had a tutor to help me improve my coding style ;)

In words, what I'm doing is :

- Check the last events recorded for a 'player'

- I want to check the number of positive events in a row (before a negative occurs)

- My function gets the last 10 events (the limit to get a bonus) and checks each one, but stops if it encounters a negative event and return the streak.

BUT : looking at the API cheatsheet (thanks @soma), I've noticed this nextUntil() which I would like to use... but my use always returns 0 :(

If someone can understand why and help me improve on that one, I would appreciate. Thanks !

By the way, feel free (if you have a few minutes) to give me advice on how to improve those foreach() I feel like I'm overusing...

Link to comment
Share on other sites

8 hours ago, celfred said:

$streak = $lastEvent->nextUntil("task.HP<0")->count();

Can the "HP" field value be less than zero? I'm not sure what this field holds in your case, but perhaps you just want to stop when the field is empty?

$streak = $lastEvent->nextUntil("task.HP=''")->count();

Incidentally, I don't think there's anything wrong with a simple foreach() over 10 page objects or less - no cause for concern over performance there.

Several ways you can skin the cat, but one alternative is to use "break" to end the loop when your condition is not met.

foreach($lastEvents as $e) {
	if($e->task->HP >= 0) {
		$streak++;
	} else {
		break;
	}
}

 

  • Like 1
Link to comment
Share on other sites

Thanks for the answer.

HP field can be less than zero. It is an 'Integer' field that holds a negative value when the task is... negative :) so I'm not sure why it doesn't return the expected result.

I like the 'break;' tip. Again, I have a feeling I have missed that 'basic' one in my slow discovery of PHP (and coding)... Sorry about that.

Anyway, I'm not at home right now, but I'll do some testing and come back here to edit my post and try and understand something about this nextUntil().

Thanks again !

Edit : After testing further and struggling about half an hour, here's my mistake : prevUntil() was what I was supposed to use (I feel a little bit ashamed...) Yes. I guess my 'sort=-date' in my previous statement was the clue. What I imagined being my first event (hence my nextUntil) was in fact my last event, that's why prevUntil was to be used...

Eventually, the most readable code (to me) is your 'foreach' with the 'break' instruction, so I'll go with that :) Thanks !

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...