Update al users with UserManager plugin

Hello,

I’m developing a game.app that shows the state of some objects by color (e.g state 1 = green, 2 = yellow etc…). The users are asked to make some choices on some objects and when they are finished they have to check a field. As soon as the last user finish the round a global method is fired to update the objects in the game. After that I try to fire another method to update the colors for every user and sends a message that they can begin with the second round.

I tried to do the last with the UserManager plugin, but when we tested the game the interface was updated only in the last user (the one who fires the method) and he got also 4 times the message instead of one.

//if all players are finished
		if (datasetarrayU.getMaxRowIndex() == 4)
		{	
			var serverName = databaseManager.getDataSourceServerName(forms.game_objects1.controller.getDataSource());
			var noyears = forms.game_settings.set_no_years;
			//updates the objects
			globals.markovCode(serverName,noyears);
			
			//updates the game and send a message to every user 
			// Returns all clients an execute the script
			var vClients = plugins.UserManager.getClients()
			var len = vClients.length
			for (var i = 1; i<=len; i++)
			{
				var clientID = vClients[0].clientId
				application.output(clientID);
				var client = plugins.UserManager.getClientByUID(clientID)
				client.executeMethod(globals.updateClients);
				client.sendMessage('please start a new round.')				
			}
			}
		
			else
			{
				var title = "";
				var message = i18n.getI18NMessage('i18n:ssg.not.all.players.finished');
				var btn1 = i18n.getI18NMessage('i18n:ssg.ok');
				plugins.dialogs.showInfoDialog(title, message, btn1);
				
			}

Do I miss something in my code or is this not the right way?

First of all, your loop is completely wrong. You are always getting the first client (you always request vClients[0]!). Second you ask for the clients twice. First when you ask getClients(), then in your loop when you call getClientByUID(clientID). That’s unnecessary. getClients() already gives you all the clients. Change it to:

var vClients = plugins.UserManager.getClients()
for (var i = 0; i < vClients .length; i++)
{
    var client = vClients [i];
    client.executeMethod(globals.updateClients);
    client.sendMessage('please start a new round.')            
}

Note that this will only work in smart clients!

Ok, now I feel like an idiot :oops: ! I shouldn’t work so late at night.

But thank you Patrik

Patrick,

You mention above that executeMethod() only works for smart clients. Is there any way to make client.executeMethod() work when the client is a headless client (batch processor)?

The last time I checked on this the API was only implemented for smart clients, so I suppose the answer is no.

Hi Patrick,

patrick:
The last time I checked on this the API was only implemented for smart clients, so I suppose the answer is no.

I want to be able to use ‘executeMethod’ on web clients, is there any way to do that, or do you have another way of doing that? (I have a server side batch process running that I want to fire a method for each active web client)

Thanks

Rafi