[Tux4kids-commits] r1586 - tuxmath/trunk/src

David Bruce dbruce-guest at alioth.debian.org
Mon Oct 12 20:00:35 UTC 2009


Author: dbruce-guest
Date: 2009-10-12 20:00:35 +0000 (Mon, 12 Oct 2009)
New Revision: 1586

Modified:
   tuxmath/trunk/src/globals.h
   tuxmath/trunk/src/mathcards.c
   tuxmath/trunk/src/server.c
Log:
More implementation of server-side time-based scoring

Modified: tuxmath/trunk/src/globals.h
===================================================================
--- tuxmath/trunk/src/globals.h	2009-10-12 18:50:07 UTC (rev 1585)
+++ tuxmath/trunk/src/globals.h	2009-10-12 20:00:35 UTC (rev 1586)
@@ -124,6 +124,7 @@
 #define MAX_BONUS_SPEED_RATIO 3.0
 #define MIN_COMETS 1
 #define MAX_MAX_COMETS 100
+#define SCORE_COEFFICIENT 100
 
 #define DEFAULT_FONT_NAME "AndikaDesRevG.ttf"
 #define FONT_NAME_LENGTH 64

Modified: tuxmath/trunk/src/mathcards.c
===================================================================
--- tuxmath/trunk/src/mathcards.c	2009-10-12 18:50:07 UTC (rev 1585)
+++ tuxmath/trunk/src/mathcards.c	2009-10-12 20:00:35 UTC (rev 1586)
@@ -249,7 +249,16 @@
 static int calc_num_valid_questions(void);
 static MC_MathQuestion* add_all_valid(MC_ProblemType pt, MC_MathQuestion* list, MC_MathQuestion** end_of_list);
 static MC_MathQuestion* find_node(MC_MathQuestion* list, int num);
+//Determine how many points to give player based on question
+//difficulty and how fast it was answered.
+//TODO we may want to play with this a bit
+static int calc_score(int difficulty, float t);
 
+
+
+
+
+
 /*  MC_Initialize() sets up the struct containing all of  */
 /*  settings regarding math questions.  It should be      */
 /*  called before any other function.  Many of the other  */
@@ -511,12 +520,13 @@
 
 /*  MC_AnsweredCorrectly() is how the user interface      */
 /*  tells MathCards that the question has been answered   */
-/*  correctly. Returns 1 if no errors.                    */
+/*  correctly. Returns the number of points earned.       */
 int MC_AnsweredCorrectly(int id, float t)
 {
   DEBUGMSG(debug_mathcards, "\nEntering MC_AnsweredCorrectly()");
 
   MC_MathQuestion* quest = NULL;
+  int points = 0;
 
   if(!active_quests) // No questions currently "in play" - something is wrong:
   {
@@ -537,12 +547,18 @@
     return 0;
   }
 
+  /* Calculate how many points the player should receive, based on */
+  /* difficulty and time required to answer it:                    */
+  points = calc_score(quest->card.difficulty, t);
+
   DEBUGCODE(debug_mathcards)
   {
     printf("\nQuestion was:");
     print_card(quest->card);
+    printf("Player recieves %d points\n", points);
   }
 
+
   //We found a matching question, now we take it out of the 
   //"active_quests" list and either put it back into the 
   //main question list in a random location, or delete it:
@@ -580,7 +596,7 @@
   /* Record the time it took to answer: */ 
   MC_AddTimeToList(t);
 
-  return 1;
+  return points;
 }
 
 
@@ -2529,3 +2545,10 @@
     card->answer = atoi(card->answer_string);
   }
 }
+
+static int calc_score(int difficulty, float t)
+{
+  if (t < 0 || difficulty < 1)
+    return 0;
+  return (difficulty * SCORE_COEFFICIENT)/t;
+}
\ No newline at end of file

Modified: tuxmath/trunk/src/server.c
===================================================================
--- tuxmath/trunk/src/server.c	2009-10-12 18:50:07 UTC (rev 1585)
+++ tuxmath/trunk/src/server.c	2009-10-12 20:00:35 UTC (rev 1586)
@@ -13,7 +13,8 @@
 * (http://gpwiki.org), in a tutorial covered by the GNU Free Documentation License 1.2.
 * No invariant sections were indicated, and no separate license for the example code
 * was listed. The author was also not listed. AFAICT,this scenario allows incorporation of
-* derivative works into a GPLv2+ project like TuxMath - David Bruce 
+* derivative works into a GPLv2+ project like TuxMath.  FWIW, virtually none of
+* the tutorial code is still present here - David Bruce 
 */
 
 #include "globals.h"
@@ -65,6 +66,7 @@
 void game_msg_wrong_answer(int i, char* inbuf);
 void game_msg_quit(int i);
 void game_msg_exit(int i);
+int calc_score(int difficulty, float t);
 
 //message sending:
 int send_counter_updates(void);
@@ -368,6 +370,11 @@
       cleanup_server();
       exit(0);
     }
+    else if (strcmp(argv[i], "--debug-lan") == 0)
+    {
+      debug_status |= debug_lan;
+    }
+
     else if (strcmp(argv[i], "--copyright") == 0 ||
              strcmp(argv[i], "-c") == 0)
     {
@@ -797,9 +804,10 @@
 void game_msg_correct_answer(int i, char* inbuf)
 {
   char outbuf[NET_BUF_LEN];
-  char* p;
-  int id;
-  float t;
+  char* p = NULL;
+  int id = -1;
+  float t = -1;
+  int points = 0;
 
   if(!inbuf)
     return;
@@ -811,24 +819,31 @@
   p++;
   id = atoi(p);
   //Now get time player took to answer:
-  p = strchr(inbuf, '\t');
+  p = strchr(p, '\t');
   if(!p)
-    return; 
-  p++;
-  t = atof(p);
+    t = -1;
+  else
+  {
+    p++;
+    t = atof(p);
+  }
 
-
   //Tell mathcards so lists get updated:
-  if(!MC_AnsweredCorrectly(id, t))
+  points = MC_AnsweredCorrectly(id, t);
+  if(!points)
     return;
   //If we get to here, the id was successfully parsed out of inbuf
   //and the corresponding question was found.
+  client[i].score += points;
 
   //Announcement for server and all clients:
   snprintf(outbuf, NET_BUF_LEN, 
-          "question id %d was answered in %f seconds by %s\n",
-          id, t, client[i].name);             
+          "question id %d was answered in %f seconds for %d points by %s",
+          id, t, points, client[i].name);             
   broadcast_msg(outbuf);
+
+  DEBUGMSG(debug_lan, "game_msg_correct_answer(): %s\n", outbuf);
+
   //Tell all players to remove that question:
   remove_question(id);
   //send the next question to everyone:
@@ -909,16 +924,12 @@
 //FIXME don't think we want to allow players to shut down the server
 void game_msg_quit(int i)
 {
-  printf("Server has been shut down by %s\n",client[i].name); 
+  printf("Server has been shut down by %s\n", client[i].name); 
   cleanup_server();
   exit(9);                           // '9' means exit ;)  (just taken an arbitary no:)
 }
 
 
-
-
-
-
 /* Now this gets called to actually start the game once all the players */
 /* have indicated that they are ready:                                  */
 void start_game(void)
@@ -1061,47 +1072,6 @@
 }
 
 
-
-
-// /*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 ques_id, char *name, TCPsocket client_sock)         
-// {
-//  int x;
-//  char buf[NET_BUF_LEN];
-//  char msg[100];  
-// 
-//  /* Create appropriate message: */
-//   switch(message)
-//   {
-//     case NO_QUESTION_LIST:
-//       sprintf(msg,"%s", "Please! first setup the question list by typing <a>\n");
-//       break;
-//     case ANSWER_CORRECT:
-//       sprintf(msg,"%s %d %s %s", "Question ID:",
-//               ques_id, "was answered correctly by the client",name);
-//       break;
-//    case LIST_SET_UP:
-//       sprintf(msg,"%s", "Question list was successfully setup\n");
-//       break;
-//    default :
-//      fprintf(stderr, "SendMessage() - unrecognized message type\n");
-//      return 0;
-//   }
-//   //transmit:
-//   snprintf(buf, NET_BUF_LEN, "%s\t%s\n", "SEND_MESSAGE", msg);
-//   x = SDLNet_TCP_Send(client_sock, buf, NET_BUF_LEN);
-// 
-// #ifdef LAN_DEBUG
-//   printf("buf is: %s\n", buf);
-//   printf("SendMessage() - buf sent:::: %d bytes\n", x);
-// #endif
-// 
-//   return 1;
-// }
-
-
-
 /* Sends a string for the client to display to player: */
 int player_msg(int i, char* msg)
 {




More information about the Tux4kids-commits mailing list