[Tux4kids-commits] r1064 - tuxmath/branches/lan/server

akash gangil gangil-guest at alioth.debian.org
Wed Jun 17 22:53:56 UTC 2009


Author: gangil-guest
Date: 2009-06-17 22:53:55 +0000 (Wed, 17 Jun 2009)
New Revision: 1064

Modified:
   tuxmath/branches/lan/server/server.c
   tuxmath/branches/lan/server/server.h
   tuxmath/branches/lan/server/testclient.c
Log:
Uniform command switch in both client and server , and beginning of command line tuxmath

Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c	2009-06-17 14:49:45 UTC (rev 1063)
+++ tuxmath/branches/lan/server/server.c	2009-06-17 22:53:55 UTC (rev 1064)
@@ -26,16 +26,18 @@
 #include "server.h" 
 
 TCPsocket sd, csd; /* Socket descriptor, Client socket descriptor */
-
+int SendMessage(int ,int );
+ 
 int main(int argc, char **argv)
 { 
   IPaddress ip, *remoteIP;
   int quit, quit2;
   char buffer[NET_BUF_LEN];
-  int network_function = -1;
+  int command_type = -1;
   //     size_t length;
   MC_FlashCard flash;
   static int initialize = 0;
+  int id;
 
   printf("Started tuxmathserver, waiting for client to connect:\n");
 
@@ -89,54 +91,86 @@
       quit2 = 0;
       while (!quit2)
       {
+        char command[NET_BUF_LEN];
         if (SDLNet_TCP_Recv(csd, buffer, NET_BUF_LEN) > 0)
         {
-          network_function = -1;
+          command_type = -1;
           printf("Client say: %s\n", buffer);
-                                       
+          sscanf (buffer,"%s %d\n",
+                         command,
+                         &id);  
+     
+          if(strcmp(command,"CORRECT_ANSWER") == 0)
+          {
+            initialize=1; 
+            command_type = CORRECT_ANSWER;              
+          }                             
+          
           //'a' for the setting up the question list                                           
-          if(strcmp(buffer,"a") == 0)
+          if(strcmp(command,"a") == 0)
           {
             initialize=1; 
-            network_function = NEW_GAME;              
+            command_type = NEW_GAME;              
           } 
                                        
           //'b' for asking for a question(flashcard)
-          if(strcmp(buffer,"b") == 0)
+          if(strcmp(command,"b") == 0)
           {
-            if(!initialize)
-             {SendMessage(NO_QUESTION_LIST);
-              continue;
-             }
-            network_function = SEND_A_QUESTION;              
+           if(!initialize)
+            {
+             command_type=LIST_NOT_SETUP;                    
+            }
+           else
+           command_type = SEND_A_QUESTION;              
           } 
 
-          if(strcmp(buffer, "exit") == 0) /* Terminate this connection */
+          if(strcmp(command, "exit") == 0) /* Terminate this connection */
           {
             quit2 = 1;
             printf("Terminate connection\n");
           }
 
-          if(strcmp(buffer, "quit") == 0) /* Quit the program */
+          if(strcmp(command, "quit") == 0) /* Quit the program */
           {
             quit2 = 1;
             quit = 1;
             printf("Quit program\n");
           }
-
-          switch(network_function)
+          
+          switch(command_type)
           {
             case NEW_GAME:  //mainly to setup the question list
             {
               if (!MC_StartGame())
               {
                 fprintf(stderr, "\nMC_StartGame() failed!");
-                return 0;
-              }                                                                                  
+              }
+              if(!SendMessage(LIST_SET_UP,0))
+             {
+              printf("Unable to communicate to the client\n");
+             }                                                                                  
                                                  
               break;                                           
             } 
 
+            case CORRECT_ANSWER:
+            {
+             if(!SendMessage(ANSWER_CORRECT,0))
+             {
+              printf("Unable to communicate to the client\n");
+             }
+             break;
+            }
+
+            case LIST_NOT_SETUP:                    //to send any message to the client 
+            {              
+             if(!SendMessage(NO_QUESTION_LIST,id))
+             {
+              printf("Unable to communicate to the client\n");
+             }
+             break;
+            }
+
             case SEND_A_QUESTION:
             {
               if (!MC_NextQuestion(&flash))
@@ -209,29 +243,60 @@
 
 /*Function to send any messages to the client be it any warnings
   or anything the client is made to be informed*/
-int SendMessage(int message)         
+int SendMessage(int message,int z)         
 {
- char *msg;
  int x,len;
-  char buf[NET_BUF_LEN];
+ char buf[NET_BUF_LEN];
 
  switch(message)
  {
-   case NO_QUESTION_LIST:
-   {
-     msg = "Please! first setup the question list by typing <a>";
-     len = strlen(msg) + 1;    // add one for the terminating NULL
-     snprintf(buf, NET_BUF_LEN, 
-                   "%s\t%s\n",
-                   "SEND_MESSAGE",
-                   msg);
-     printf("buf is: %s\n", buf);
-     x = SDLNet_TCP_Send(csd, buf, sizeof(buf));
-     printf("SendQuestion() - buf sent:::: %d bytes\n", x);
-     break;
-   }
+  case NO_QUESTION_LIST:
+  {
+   char msg[100] = "Please! first setup the question list by typing <a>";
+   snprintf(buf, NET_BUF_LEN, 
+                 "%s\t%s\n",
+                 "SEND_MESSAGE",
+                 msg);
+   printf("buf is: %s\n", buf);
+   x = SDLNet_TCP_Send(csd, buf, sizeof(buf));
+   printf("SendQuestion() - buf sent:::: %d bytes\n", x);
+   break;
   }
   
+  case ANSWER_CORRECT:
+  {
+    char msg[100];  
+    sprintf(msg,"%s   %d   %s",
+                "Question ID:",
+                z,
+                "was answered correctly by the client");
+    snprintf(buf, NET_BUF_LEN, 
+                  "%s\t%s\n",
+                  "SEND_MESSAGE",
+                  msg);
+    printf("buf is: %s\n", buf);
+    x = SDLNet_TCP_Send(csd, buf, sizeof(buf));
+    printf("SendQuestion() - buf sent:::: %d bytes\n", x);
+    break;
+  } 
+  
+  case LIST_SET_UP:
+  {
+   char msg[100] = "Question list was successfully setup";
+   snprintf(buf, NET_BUF_LEN, 
+                 "%s\t%s\n",
+                 "SEND_MESSAGE",
+                 msg);
+   printf("buf is: %s\n", buf);
+   x = SDLNet_TCP_Send(csd, buf, sizeof(buf));
+   printf("SendQuestion() - buf sent:::: %d bytes\n", x);
+   break;
+  } 
+  
+  default :
+  break;
+ }
+  
   return 1;
 }
 

Modified: tuxmath/branches/lan/server/server.h
===================================================================
--- tuxmath/branches/lan/server/server.h	2009-06-17 14:49:45 UTC (rev 1063)
+++ tuxmath/branches/lan/server/server.h	2009-06-17 22:53:55 UTC (rev 1064)
@@ -19,12 +19,16 @@
 
 enum {
   NEW_GAME,
-  SEND_A_QUESTION
+  SEND_A_QUESTION,
+  LIST_NOT_SETUP,
+  CORRECT_ANSWER
 };
 
 
 /*enum for messages for SendMessage*/
 enum {
+  ANSWER_CORRECT,
+  LIST_SET_UP,
   NO_QUESTION_LIST
 };
 

Modified: tuxmath/branches/lan/server/testclient.c
===================================================================
--- tuxmath/branches/lan/server/testclient.c	2009-06-17 14:49:45 UTC (rev 1063)
+++ tuxmath/branches/lan/server/testclient.c	2009-06-17 22:53:55 UTC (rev 1064)
@@ -30,18 +30,20 @@
 
 MC_FlashCard flash;
 int Make_Flashcard(char *buf, MC_FlashCard* fc);
+int MC_AnsweredCorrectly(MC_FlashCard* fc);
 
+char buffer[512];  // for command-line input
+
 int main(int argc, char **argv)
 {
 //  MC_FlashCard* fclist;
   IPaddress ip;           /* Server address */
   int quit, len, sockets_used;
   int numready;
-  char buffer[512];  // for command-line input
   char buf[512];     // for network messages from server
   int x, i = 0;
   int command_type;
-
+  int ans;
   /* Simple parameter checking */
   if (argc < 3)
   {
@@ -86,7 +88,7 @@
   quit = 0;
   while (!quit)
   {
-    //Get user input from command line and send it to server: 
+   //Get user input from command line and send it to server: 
     printf("Write something:\n>");
     scanf("%s", buffer);
 
@@ -145,17 +147,31 @@
           {
             command_type=SEND_QUESTION;        //from the enum in testclient.h
           }
+
              
           switch(command_type)
           {
-            case SEND_QUESTION:
-             { 
-              if(!Make_Flashcard(buf, &flash))  /* function call to parse buffer into MC_FlashCard */
-              printf("Unable to parse buffer into FlashCard\n");
+           case SEND_QUESTION:                                   
+           { 
+            if(!Make_Flashcard(buf, &flash))  /* function call to parse buffer into MC_FlashCard */
+            printf("Unable to parse buffer into FlashCard\n");
+            while(1)
+            { 
+             printf("Enter ur answer...\n");
+             scanf("%d",&ans);
+             if(ans==flash.answer)
+             {  
+              if(!MC_AnsweredCorrectly(&flash))
+              printf("Unable to communicate the same to server\n");
               break;
              }
-            default :
-             break;
+             else
+             printf("Sorry try again....At present the game won't move forward unless u answer this =D \n");
+            }          
+            break;
+           }
+           default :
+           break;
           }
        }
       }
@@ -170,7 +186,23 @@
   return EXIT_SUCCESS;
 }
 
-
+int MC_AnsweredCorrectly(MC_FlashCard* fc)
+{
+ int len;
+ snprintf(buffer, NET_BUF_LEN, 
+                  "%s %d\n",
+                  "CORRECT_ANSWER",
+                  fc->question_id);
+ len = strlen(buffer) + 1;
+ if (SDLNet_TCP_Send(sd, (void *)buffer, len) < len)
+ {
+  fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
+  exit(EXIT_FAILURE);
+ }
+ return 1;
+}
+                
+ 
 int Make_Flashcard(char* buf, MC_FlashCard* fc)
 {
   int i, j, tab = 0, s = 0;




More information about the Tux4kids-commits mailing list