lg-set: Add a broadcast whenever a user finds a set.
[loudgame] / lg-set.c
index c5ebbfb67b7f199d0347cb2ae3846fbeabea4b0c..a6594b27a3de4bc9ad9a621f68755f97e9a985ec 100644 (file)
--- a/lg-set.c
+++ b/lg-set.c
@@ -249,21 +249,89 @@ static void
 set_game_handle_show (set_game_t       *game,
                      const char        *peer)
 {
+    LmMessage *reply;
+    LmMessageNode *html, *body, *span;
+    gboolean result;
+    GError *error = NULL;
     board_t *board = &game->board;
     char board_str[BOARD_MAX_SLOTS * (NUM_ATTRIBUTES + 1)];
     char *s;
-    int i, j;
+    int slot, pos, attr;
+
+    reply = lm_message_new (peer, LM_MESSAGE_TYPE_MESSAGE);
+    html = lm_message_node_add_child (reply->node, "html", "");
+    lm_message_node_set_attribute (html,
+                                  "xmlns",
+                                  "http://jabber.org/protocol/xhtml-im");
+    body = lm_message_node_add_child (html, "body", "");
+    lm_message_node_set_attribute (body,
+                                  "xmlns",
+                                  "http://www.w3.org/1999/xhtml");
 
     s = board_str;
-    for (i = 0; i < board->num_slots; i++)
-       if (board->slots[i].has_card) {
-           for (j = 0; j < NUM_ATTRIBUTES; j++)
-               *s++ = '0' + board->slots[i].card.attributes[j];
+    for (slot = 0; slot < board->num_slots; slot++) {
+       if (slot % 4 == 0)
+           lm_message_node_add_child (body, "br", "");
+       if (board->slots[slot].has_card) {
+           char card_str[5];
+           char *style;
+           card_t card = board->slots[slot].card;
+           for (pos = 0; pos < 3; pos++) {
+               if (pos <= card.attributes[ATTRIBUTE_INDEX_NUMBER]) {
+                   switch (card.attributes[ATTRIBUTE_INDEX_SYMBOL]) {
+                   case SYMBOL_OVAL:
+                       card_str[pos] = 'O';
+                       break;
+                   case SYMBOL_SQUIGGLE:
+                       card_str[pos] = 'S';
+                       break;
+                   case SYMBOL_DIAMOND:
+                   default:
+                       card_str[pos] = 'X';
+                       break;
+                   }
+               } else {
+                   card_str[pos] = ' ';
+               }
+           }
+           card_str[3] = ' ';
+           card_str[4] = '\0';
+           span = lm_message_node_add_child (body, "span",
+                                             card_str);
+           style = g_strdup_printf ("font-family: Monospace; color: %s;%s%s",
+                                    attribute_values[ATTRIBUTE_INDEX_COLOR][
+                                        card.attributes[ATTRIBUTE_INDEX_COLOR]],
+                                    card.attributes[ATTRIBUTE_INDEX_SHADING]
+                                    == SHADING_SOLID ? "font-weight: bold;" : "",
+                                    card.attributes[ATTRIBUTE_INDEX_SHADING]
+                                    == SHADING_STRIPED ? "text-decoration: underline;" : "");
+           lm_message_node_set_attribute (span, "style", style);
+           free (style);
+           for (attr = 0; attr < NUM_ATTRIBUTES; attr++)
+               *s++ = '0' + board->slots[slot].card.attributes[attr];
+           *s++ = ' ';
+       } else {
+           span = lm_message_node_add_child (body, "span", "    ");
+           lm_message_node_set_attribute (span, "style",
+                                          "font-family: Monospace;");
+           *s++ = '-';
+           *s++ = '-';
+           *s++ = '-';
            *s++ = ' ';
        }
+    }
     *s = '\0';
 
-    loudgame_send (&game->lg, peer, board_str);
+    lm_message_node_add_child (reply->node, "body", board_str);
+
+    result = lm_connection_send (game->lg.connection, reply, &error);
+    lm_message_unref (reply);
+
+    if (! result) {
+       g_error ("lm_connection_send failed: %s\n",
+                error->message);
+       loudgame_quit (&game->lg, 1);
+    }
 }
 
 static void
@@ -362,6 +430,10 @@ set_game_handle_set (set_game_t    *game,
 
     loudgame_sendf (&game->lg, peer,
                    "Yes, that's a set!");
+    loudgame_broadcastf (&game->lg,
+                        "%s found the set: %d %d %d. Please send a new 'show' command.",
+                        peer,
+                        slots[0], slots[1], slots[2]);
 
     for (i = 0; i < 3; i++)
        game->board.slots[slots[i]].has_card = 0;
@@ -371,6 +443,25 @@ set_game_handle_set (set_game_t    *game,
     deal (&game->deck, &game->board);
 }
 
+static void
+set_game_handle_help (set_game_t *game,
+                     const char *peer)
+{
+    loudgame_sendf (&game->lg, peer,
+                   "I'm a bot that allows you to play the game of SET.\n"
+                   "Here are some commands I understand:\n"
+                   "\tshow     \tShow the current cards on the board\n"
+                   "\thint     \tIndicate how many sets are currently possible\n"
+                   "\tshuffle  \tReturn the cards to the deck, shuffle and deal\n"
+                   "\t         \t(this is only allowed if no sets are possible)\n"
+                   "\tset X Y Z\tClaim three cards as a set. The cards are numbered\n"
+                   "\t         \tleft-to-right, and top-to-bottom from 0 to 11.\n"
+                   "\n"
+                   "For more information about SET, or to purchase your own deck, visit\n"
+                   "http://setgame.com . Please note that this server is unaffiliated\n"
+                   "with Set Enterprises, Inc., who publish the SET card game.");
+}
+
 static void
 set_game_handle_message (loudgame_t *lg,
                         const char *peer,
@@ -380,6 +471,8 @@ set_game_handle_message (loudgame_t *lg,
 
     if (strcmp (message, "show") == 0)
        set_game_handle_show (game, peer);
+    else if (strcmp (message, "help") == 0)
+       set_game_handle_help (game, peer);
     else if (strcmp (message, "hint") == 0)
        set_game_handle_hint (game, peer);
     else if (strcmp (message, "shuffle") == 0)
@@ -387,7 +480,7 @@ set_game_handle_message (loudgame_t *lg,
     else if (strncmp (message, "set", 3) == 0)
        set_game_handle_set (game, peer, message + 3);
     else
-       loudgame_sendf (lg, peer, "Unknown command: '%s'", message);
+       loudgame_sendf (lg, peer, "Unknown command: '%s'. Use 'help' to get a list of valid commands.", message);
 }
 
 /* Begin a new game */