Plugin API: how to access battle lobby data + basic debugging

Plugin API: how to access battle lobby data + basic debugging

SpringRTS Perl Autohost for Dedicated Server

Moderators: Moderators, Lobby Developers, SPADS AutoHost

Post Reply
User avatar
Jools
XTA Developer
Posts: 2804
Joined: 23 Feb 2009, 16:29

Plugin API: how to access battle lobby data + basic debugging

Post by Jools » 22 Jan 2015, 15:46

Would it be possible to wish for some new accessors? Specifically I would like to get the battle state from the autohost: the list of unready players, unsynced players, in-game players and ready players. So one accessor called:

getBattleState()

I tried looking at some of the already exiting accessors but I didn't find the correct data:

http://planetspads.free.fr/spads/doc/sp ... #Accessors

The values of some keys in those hash tables are only written as HASH[0x123456], is that because the data is anonymous or because I access it in the wrong way?

For instance, there is a hash like this in getSpringInterface(); players = HASH(0x3a89d50). Should I somehow read this as a list instead of a hash to get the right data? Sorry if this question is very stupid.

Also, when I try to iterate through some data provided by some accessors the whole autohost crashes. This is an example of the code I use and that makes the autohost crash:
my $p_config = getLobbyState();

foreach my $key ( keys %{$p_config} )
{
my $value = $p_config->{$key};
slog("$key = $value",4);
}
Iterating in a similar way with my $p_config = getSpringInterface(); does not produce any crash.

Edit: ok, the last issue regarding the crash seems to occur because getLobbyState() seems to be an integer.
0 x

User avatar
bibim
Lobby Developer
Posts: 901
Joined: 06 Dec 2007, 11:12

Re: SPADS AutoHost

Post by bibim » 22 Jan 2015, 17:50

Jools wrote:Would it be possible to wish for some new accessors? Specifically I would like to get the battle state from the autohost: the list of unready players, unsynced players, in-game players and ready players. So one accessor called:

getBattleState()

I tried looking at some of the already exiting accessors but I didn't find the correct data:

http://planetspads.free.fr/spads/doc/sp ... #Accessors
That is because the "accessors" are just functions which return variable values "as this" without any processing, whereas the data you want aren't directly available as variables in SPADS. You want lobby type data, so you first need to use the getLobbyInterface() accessor to get the lobby object which will contain all the info you want.

You can find a code example which does almost what you want in the AutoSpec plugin:

Code: Select all

my $lobby=getLobbyInterface();

# skip processing if battle is closed or spring is running
return unless(getLobbyState() > 5 && %{$lobby->{battle}} && getSpringPid() == 0);

my (%unsyncedPlayers,%unreadyPlayers,%syncedReadyPlayers);
foreach my $user (keys %{$lobby->{battle}->{users}}) {
  my $p_battleStatus=$lobby->{battle}->{users}->{$user}->{battleStatus};

  # skip spectators
  next unless(defined $p_battleStatus && $p_battleStatus->{mode});

  if($p_battleStatus->{sync} != 1) {
    $unsyncedPlayers{$user}=1;
  }elsif($p_battleStatus->{inGame} || ! $p_battleStatus->{ready}) {
    $unreadyPlayers{$user}=1;
  }else{
    $syncedReadyPlayers{$user}=1;
  }
}
Jools wrote:The values of some keys in those hash tables are only written as HASH[0x123456], is that because the data is anonymous or because I access it in the wrong way?
"HASH[0x123456]" means you asked Perl to print a reference (~pointer). "HASH" indicates that it's a hash reference, and "0x123456" is the address of the data.
Jools wrote:For instance, there is a hash like this in getSpringInterface(); players = HASH(0x3a89d50). Should I somehow read this as a list instead of a hash to get the right data? Sorry if this question is very stupid.
No it's not stupid don't worry, it's just some Perl specific behaviours.
getSpringInterface() gives you the SpringInterface object used by SPADS. This object is implemented by the SpringAutoHostInterface.pm module. In this module you can see that the object is initialized like this:

Code: Select all

my $self = {
conf => $p_conf,
autoHostSock => undef,
state => 0,
gameId => '',
demoName => '',
players => {},
callbacks => {},
preCallbacks => {},
connectingPlayer => { name => "", version => "", address => "" }
};
In particular, "players" is initialized with "{}", which is a Perl notation for an empty hash pointer.
So if you want to print the content of this hash, you must use another loop going through the sub-keys.
But instead of doing that, I recommend using the standard Data::Dumper Perl module which prints correctly any Perl structure recursively.
Basic usage:

Code: Select all

use Data::Dumper;    # at top of your plugin module
(...)
my $unknownVariable=getSpringInterface();
slog("unknownVariable=".(Dumper($unknownVariable)),4);
Jools wrote:Also, when I try to iterate through some data provided by some accessors the whole autohost crashes. This is an example of the code I use and that makes the autohost crash:
my $p_config = getLobbyState();

foreach my $key ( keys %{$p_config} )
{
my $value = $p_config->{$key};
slog("$key = $value",4);
}
That is because getLobbyState() returns a scalar and not a hash pointer.
Trying to deference a scalar into a hash isn't possible and produces a fatal error.
That's why I recommend to use Data::Dumper first when experimenting with unknown variables, so that you directly have an overview of its content.
0 x

User avatar
Jools
XTA Developer
Posts: 2804
Joined: 23 Feb 2009, 16:29

Re: SPADS AutoHost

Post by Jools » 23 Jan 2015, 14:36

Thanks for the help. It's nice to see how flexible spads is with extending functionality with the plugins.

Image
0 x

Post Reply

Return to “SPADS AutoHost”