]> git.cworth.org Git - loudgame/blobdiff - lg-set.c
Add a help command to lg-set
[loudgame] / lg-set.c
index c5ebbfb67b7f199d0347cb2ae3846fbeabea4b0c..3a1a85383ffae31f75eb916c05f18bb0736b3740 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
@@ -371,6 +439,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 12.\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 +467,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 +476,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 */