Welcome

Welcome to my blog. I mainly blog about the basics of running Flash games over the internet, but sometimes I get distracted. Here you will find my efforts at programming turn based Flash games, plus links to all the games I have written. You are free (as in beer) to get all the code, just email me - nicolas_evans at yahoo.com (sorry you cant click on it - I dont want more spam).

Building Multiplayer Games in Flash.

The Posts

Wednesday, March 08, 2006

Hi. How does one player's game communicate with the other? Mine uses the internet, but an intranet or home network would work as well.
Flash has two ways to do this, by http, or by use of a socket. I never found a cheap socket server host, but I already had a free server called http://www.myjavaserver.com/ so I used that.
The program you place on your server handles messages from the game. As mine was a java server, I wrote it in JSP. However you could use ASP, PHP , Perl, Cold Fusion or others, whichever is allowed on your server. It stores the game variables in a file.
My game only uses three types of message, one to empty the file (CLEAR), one to read the file (VIEW), and one to update the file (INSERT). Here is my program:
<%@ page import = "java.io.*" %>

<%
synchronized(page)     {
    int intVariableCount = 12;

    String filename = request.getParameter("filename");
    String action = request.getParameter("action");
       
    int intErrorNumber;
    int intTemp;

    try {
        intTemp = Integer.parseInt(filename);
    }
    catch (NumberFormatException nfe) {
        intErrorNumber = 7;
        out.println ("v=v&nErrorNumber=" + intErrorNumber);
        return;
    }

       
    // Create a Blank File if it doesn't already exist
    File fileObject = new File("/users/nicolasevans/" + filename + ".txt");
    if (!fileObject.exists() ) {
        fileObject.createNewFile();
    }

    // Read the file in
    String[]  arVariables;
    arVariables = new String[intVariableCount];

    FileReader fileRead = new FileReader(fileObject);
    BufferedReader buffFileIn = new BufferedReader(fileRead);
    String strLine  = "";
    int intCount = 0;
    while ((strLine=buffFileIn.readLine()) != null)
        if (intCount < intVariableCount) arVariables[intCount++] = strLine;
    buffFileIn.close();
    fileRead.close();

   
    // Fill in any missing data with none/0
    for (int i = intCount; i < intVariableCount ; i++)     {
        arVariables[i] = " ";
    }

    // Process the actions   

    // Insert a score/name
    if (action.equals("INSERT"))     {
       
        intErrorNumber = 0;
        // Abandon message if not later than the file

        arVariables[0] = request.getParameter("pno");
        if (Integer.parseInt(arVariables[0]) != 1
            && Integer.parseInt(arVariables[0]) != 2) {
            intErrorNumber = 1;
        }
       
        if (!arVariables[1].equals(" ") && Integer.parseInt(arVariables[1])
            >= Integer.parseInt(request.getParameter("nTurn") )) {
            intErrorNumber = 2;
        }
        arVariables[1] = request.getParameter("nTurn");

        try {
            intTemp = Integer.parseInt(request.getParameter("rnd"));
        }
        catch (NumberFormatException nfe) {
            intErrorNumber = 3;
        }
        arVariables[2] = request.getParameter("rnd");
               
        try {
            intTemp = Integer.parseInt(request.getParameter("p2score"));
        }
        catch (NumberFormatException nfe) {
            intErrorNumber = 6;
        }
        arVariables[3]= request.getParameter("p2score");
               
        arVariables[4] = request.getParameter("pwd");
       
        arVariables[5] = request.getParameter("p1hand");
       
        arVariables[6] = request.getParameter("p2hand");
       
        arVariables[7] = request.getParameter("board");
       
        arVariables[8] = request.getParameter("order");
       
        arVariables[9] = request.getParameter("p1toopen");
       
        arVariables[10]= request.getParameter("p2toopen");
       
        try {
            intTemp = Integer.parseInt(request.getParameter("p1score"));
        }
        catch (NumberFormatException nfe) {
            intErrorNumber = 5;
        }           
        arVariables[11]= request.getParameter("p1score");
       
        try {
            intTemp = Integer.parseInt(request.getParameter("messageID"));
        }
        catch (NumberFormatException nfe) {
            intErrorNumber = 8;
        }
               
        if (intErrorNumber == 0) {
        // create a new file with the variables passed in                                   
            fileObject.createNewFile();
            FileOutputStream fileStream = new FileOutputStream(fileObject);
            DataOutputStream dataStream = new DataOutputStream(fileStream);
   
            for (int i=0;i < intVariableCount;i++) {
                dataStream.writeBytes(arVariables[i] + "\n");
            }
            dataStream.close();
            fileStream.close();
            intTemp = Integer.parseInt(request.getParameter("messageID")) + 1;
            out.println ("v=v&messageID=" + intTemp);
        }
        else {
            out.println ("v=v&nErrorNumber=" + intErrorNumber);
            return;
        }
    }
    // Clear the list   
    else if (action.equals("CLEAR"))     {
        out.println("deleting");
        fileObject.delete();
    }
    else if (action.equals("VIEW"))    {
    // return the variables from the file
        out.println ("v=v&nErrorNumber=0");
   
        out.println ("&pno=" + arVariables[0] );
       
        out.println ("&nTurn=" + arVariables[1] );
       
        out.println ("&rnd=" + arVariables[2] );
       
        out.println ("&p2score=" + arVariables[3] );
       
        out.println ("&pwd=" + arVariables[4] );
       
        out.println ("&p1hand=" + arVariables[5] );
       
        out.println ("&p2hand=" + arVariables[6] );
       
        out.println ("&board=" + arVariables[7] );
       
        out.println ("&order=" + arVariables[8] );
   
        out.println ("&p1toopen=" + arVariables[9] );
   
        out.println ("&p2toopen=" + arVariables[10] );
       
        out.println ("&p1score=" + arVariables[11] );
    }
}
%>

If you understand Java you can see its very simple. If not, I hope the comments help.
I will deal with how Flash sends messages in the next blog.

# posted by nicolas_evans @ 8:42 AM
Comments: Post a Comment




This page is powered by Blogger. Isn't yours?