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

2012/9/4

How to make the most of UserInfo storage

royalfool royalfool

2012/9/4

#
The discussion about UserInfo started in the comments of the Kara Sokoban Game. Here a summary: UserInfo is designed to have only one highscore but I need one highscore for each level of the game. To work around this limitation I use all available ints and Strings to store the users moves for each level. In an int I store 3 levels with 3 digits. In a String I store 15 levels with 3 digits. With the available 10 ints and 5 Strings I can save up to 105 levels. The limit is 999 moves because of the max of 3 digits. It would be great if we could find a way of how to work around the 999 limit.
royalfool royalfool

2012/9/4

#
danpost experimented with UserInfo and found the following:
danpost wrote...
It seems that the only characters supported by the UserInfo Strings are in the range of 0 to 255. There are even some of those that will not store; namely, 13 (carriage return character) and those in the range of 128 to 159, inclusive (not sure why). That leaves only 223 different characters that can be stored (not much to work with). With the above in mind, you can save integers up to 48'728 in a two character string. This would allow you enough room for 125 puzzles whose scores are stored only in the 5 strings.
danpost danpost

2012/9/4

#
Here is the code I used to test it. It is well documented. I had imported my 'Button' class and my 'Text' sub-class extending 'Button' to run it. The following is my 'AWorld' class that extends 'World'.
import greenfoot.*;

/**
 * Class AWorld:
 * 
 * Author: danpost
 * Version: 1.0.0
 */
public class AWorld extends World
{
    Text text = new Text("   ");
    int num = 0; // the integer to be converted to string
    /**
     * Constructor AWorld:
     */
    public AWorld()
    {    
        super(600, 400, 1);
        addObject(text, 300, 200);
    }
    
    // code and decode a number, then check results
    public void act()
    {
        text.setText("Number: " + num);
        // convert int 'num' to a two-character string
        // only 223 allowable characters
        int hi = num / 223; // the number of times 223 goes into 'num'
        int lo = num % 223; // the remainder after dividing 223 into 'num'
        if (hi > 13) hi++; // character 13 not allowed
        if (lo > 13) lo++;
        if (hi > 127) hi += 32; // characters 128 through 159 not allowed
        if (lo > 127) lo += 32;
        // the coversion to string
        String str = "" + (char) hi + (char) lo;
        
        // convert String 'str' back to the integer value
        int hi2 = (int) str.charAt(0); // the coded number of 223's in the value
        int lo2 = (int) str.charAt(1); // the coded remainder
        if (hi2 > 127) hi2 -= 32; // compensate for characers 128 through 159
        if (lo2 > 127) lo2 -= 32;
        if (hi2 > 13) hi2--; // compensate for character 13
        if (lo2 > 13) lo2--;
        // the value of the two-character string
        int num2 = hi2 * 223 + lo2;
        if (num != num2)
        {
            System.out.println("Values do not match");
            Greenfoot.stop();
            return;
        }
        num++;
        if (num == 223 * 223)
        {
            System.out.println("Home run");
            Greenfoot.stop();
        }
    }
}
royalfool royalfool

2012/9/4

#
Wow. Thank you danpost. Have you also tested it with UserInfo and a scenario inside the Greenfoot gallery?
danpost danpost

2012/9/5

#
royalfool wrote...
Wow. Thank you danpost. Have you also tested it with UserInfo and a scenario inside the Greenfoot gallery?
Actually, no. However, I did test it with saving and retrieving the data using Excel, which is what is causing the restriction; at least in my case. I guess if the storage is opened by something else, it could possibly have different restrictions; but, I would have no way of confirming that.
nccb nccb

2012/9/5

#
This problem with not being able to store certain characters is a bug. You should be able to store any unicode char in each character of the string. We will look into it and fix it.
nccb nccb

2012/9/5

#
Ok, it was a simple character set misconfiguration when talking to the database. It's now fixed, so you should be able to store any valid unicode chars. This scenario demonstrates that. This should give you a lot more space to work with.
royalfool royalfool

2012/9/5

#
Thank you nccb for the fast reaction and fix! It's working online in the Greenfoot Gallery, but not inside Greenfoot. Is that correct? If yes, will the fix also make it into the next release of Greenfoot?
nccb nccb

2012/9/5

#
Greenfoot itself has always worked, as far as I know. My test scenario (linked in my last post) works locally for me and online. Try downloading it to your machine -- if you see anything other than "success" when you run it, let us know, but I think it should work fine on all local machines.
danpost danpost

2012/9/5

#
I ran it off my USB and got 'Failed' on most ints passed 255, as well as a the 33 mentioned above. Windows 7 version.
nccb nccb

2012/9/5

#
Gah, you're right. *Hands head in shame*. We'll fix that up ready for the next release. It's another character set issue due to Windows using an irritating non-Unicode default character set. Sorry about that.
royalfool royalfool

2012/9/11

#
I've updated the scenario Sokoban Kara to use the fixed UserInfo. I tried to keep the existing 'legacy' high scores. Hope it worked :-). Thank you nccb for fixing it!
nerdlemon888 nerdlemon888

2012/9/24

#
I am liking the userinfo abilities! I like running your character demonstration scenario, and I usually get the "response" in less than half a second! It's great that we get good response speed because I'm thinking about writing a game that lets you interact with other players in real time. XD I have one question... the code documentation states that you are allowed to store 10 strings to a user account for a particular scenario, but everyone talks as if you're only allowed to store 5 of them, and the constant for that reads "5". I wonder if that is a mistake by the code writers to give it a low value or the code documentation writer giving a false value.
danpost danpost

2012/9/24

#
@nerdlemon888, see this post in another discussion.
You need to login to post a reply.