This site requires JavaScript, please enable it in your browser!
Greenfoot back
danpost
danpost wrote ...

2012/8/18

On 'Private Messaging by danpost'

danpost danpost

2012/8/18

#
First, how to tell which user is logged on: (1) 'UserInfo.isStorageAvailable()' will return false if a user is not logged on and therefore there is no info to get. (2) (if logged on) 'UserInfo.getMyInfo()' will return a UserInfo object which contains all the information available on the current user (userName, userImage, score, the 10 general ints and the 5 general Strings) (3) (UserInfo myInfo = UserInfo.getMyInfo()') 'myInfo.getUserName()' will return the user's callname, 'myInfo.getUserImage()' will return the user's picture.
danpost danpost

2012/8/18

#
Now, sending information from one user to another: To begin, you need to understand how I distinguished each user. Basically, each user is assigned a number. The first user to log on and run the scenario is given the number '1'; the second, '2'; and so on. That number is stored in the Score field of the UserInfo object for that user as a negative number. If a new user runs the scenario, their Score field will show '0', which I change to an extremely large negative number, at which point I use 'myInfo.getRank()' to determine what number to assign this new user. Now that you see how I assign a number to each user, you can understand the rest. This number is converted to a character (whose code is that number) and used as the first character in one of the String fields that contains a message. Since, the Strings are limited to 50 characters, using one for the recipient leaves 49 for the message. The corresponding int field (if using String 3, referring to int 3) will hold the timestamp (in minutes) that the message was recorded. Saved in the corresponding 'int + 5' field (if int 3, referring to int 8) is the timestamp of the last message recieved from that user. So, as you can see, every field in the UserInfo object is used for something. Finally, I had to create a list of every user's UserInfo objects, sort them by name and find all references to the current user's assigned number to adjust the color coding. And you should be able to see how the messages are restricted to the recipient, at this point.
MathManiac MathManiac

2012/8/18

#
greenfoot.UserInfo, huh. New Greenfoot API.
danpost danpost

2012/8/19

#
The basic code used to create the list in 'Private Messaging' follows. It has been modified. I tried to make it somewhat generic. The method is called after checking to see if storage is available and it returns 'true'.
// The following line declared in class
UserInfo[] allUsers;

private void createUserArray()
{
    // get current users info and save score
    UserInfo me = UserInfo.getMyInfo();
    int meScore = me.getScore();
    // set up local variables
    UserInfo latestInfo = null;
    int totCt = 0;
    UserInfo[] holdAll = new UserInfo[15];
    // get top 15 users (or all, if not greater than 15) and build array
    for (Object obj : UserInfo.getTop(15))
    {
        UserInfo user = (UserInfo) obj;
        // add to array, if not current user
        if (!user.getUserName().equals(me.getUserName()))
        {
            latestInfo = user;
            holdAll[totCt++] = user;
        }
    }
    // complete the array of all users
    int lastTotal = 0;
    do
    {
        lastTotal = totCt;
        // set up local variables
        boolean pastLatestInfo = false;
        UserInfo[] holdTemp = new UserInfo[totCt];
        // adjust size of the array to hold more
        for (int i = 0; i < totCt; i++) holdTemp[i] = holdAll[i];
        holdAll = new UserInfo[totCt + 14];
        for (int i = 0; i < totCt; i++) holdAll[i] = holdTemp[i];
        // change the rank of current user so 'getNearby(int) can possibly get more users
        me.setScore(latestInfo.getScore());
        me.store(); // not sure if this is neccessary
        // work with next group of users (will have some duplicates)
        for (Object obj : UserInfo.getNearby(15))
        {
            if (pastLatestInfo)
            {
                UserInfo user = (UserInfo) obj;
                // add to array, if not current user
                if (!user.getUserName().equals(me.getUserName()))
                {
                    latestInfo = user;
                    holdAll[totCt++] = user;
                }
            }
            else
            { // this finds where we left off
                if (latestInfo.getUserName().equals(((UserInfo) obj).getUserName()))
                {
                    pastLatestInfo = true;
                }
            }
        }
    } while (lastTotal != totCt);
    me.setScore(meScore);
    me.store(); // restores current users score
    allUsers = new UserInfo[totCt + 1];
    for (int i = 0; i < totCt; i++) allUsers[i] = holdAll[i];
    allUsers[totCt++] = me; // adds current user to array
}
Please note: this code does not take into account the possibility that a user's score may change between 'gets'. This means that in a game-type scenario, there is a chance, although very slim, a user could be missed. There may be other circumstances that may cause odd results; but that is all about coding it for the specific needs (and taking into account ALL possibilities).
danpost danpost

2012/8/19

#
@MathManiac, I got your message.
MathManiac MathManiac

2012/8/19

#
Also, how did you send the data contining the message?
danpost danpost

2012/8/20

#
The message in saved in one of the Strings fields of the author's UserInfo. I mentioned before that the message is prefixed with a character whose code is the value assigned to the recipient of that message. When the recipient runs the scenario, all the UserInfos are checked for messages going to that person. If one is found, the 'switch' will be filled with blue indicating to him that there is a message there. When the recipient clicks on the sender's name, the Strings of the sender's UserInfo object are checked for any message for the user, and displayed if found.
K_wow K_wow

2013/8/6

#
Using this principal, would it be possible to make an online multiplayer game with greenfoot?
danpost danpost

2013/8/6

#
K_wow wrote...
Using this principal, would it be possible to make an online multiplayer game with greenfoot?
Yes. However, only for turn-based games like checkers, chess, othello, tic-tac-toe, etc davmac wrote the following in a discussion about the UserInfo storage:
wrote...
The intention was never to allow real-time communication. Data is stored in and retrieved from a proper database (Mysql) and this will almost certainly introduce some lag.
joeschmoe joeschmoe

2013/8/6

#
Let's see some turn based multiplayer games then
Gevater_Tod4711 Gevater_Tod4711

2013/8/6

#
I also tried to make a multiplayer game but not turn based. I tried to use Sockets to create a connection between two systems and send the information through these sockets. But I didn't get it to work through the internet. It only was possible in local networks. I think the problem was that I only could connect to peoples router and not to their computer. Does anyone know a way to create a connection with sockets through the internet? That would make real-time games possible for greenfoot. Not only round based games. So if anyone knows a way to do so that would be great.
You need to login to post a reply.