[Freeciv-Dev] Re: (PR#7246) Send origin of chat messages
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
<URL: http://rt.freeciv.org/Ticket/Display.html?id=7246 >
On Thu, Jan 15, 2004 at 09:03:31AM -0800, Raimar Falke wrote:
>
> <URL: http://rt.freeciv.org/Ticket/Display.html?id=7246 >
>
>
> I would like to color the chat messages which come from other players
> according to their player color. For this the chat packet has to
> contain the player. I would also like to have another propery: a
> type. IMHO there are 3 types:
> - out-of-game messages from other player
> - out-of-game messages from the server ("Welcome to server", "Unknown
> command", "Game saved",...)
> - in game messages from the server or client ("Next year", "Unit was
> attacked",...)
The patch. Straightforward except some things:
- a new field has to be add to the packet. This causes the addition
of a parameter to a _lot_ of notify_* and other functions. And this
would cause the change of a lot of callers. The other solution (which
was used by the patch) requires less changes but is a but uncleaner:
the source of every outgoing chat packet is set to the id of the
current_connection. This global variable is set to !=NULL during the
processing of each packet.
- the added check in tech.c was necessary. We should really thing
about a proper solution here.
- adding player_id to connection: required since the player info is
sent but in pregame game.nplayers isn't valid yet and so conn->player
gets NULL.
- only gtk2 client changed
Raimar
--
email: rf13@xxxxxxxxxxxxxxxxx
"Just because you put a flag on the moon doesn't make it yours, it just
puts a hole in the moon."
Index: client/chatline_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/chatline_common.c,v
retrieving revision 1.5
diff -u -u -d -H -r1.5 chatline_common.c
--- client/chatline_common.c 28 Nov 2003 17:37:19 -0000 1.5
+++ client/chatline_common.c 18 Apr 2004 13:41:49 -0000
@@ -19,13 +19,24 @@
#include <string.h>
#include "astring.h"
+#include "log.h"
#include "packets.h"
#include "support.h"
#include "chatline_g.h"
+#include "clinet.h"
+#include "tilespec.h"
#include "chatline_common.h"
-#include "clinet.h"
+
+
+static struct {
+ int lines;
+ struct {
+ char *text;
+ int conn_id;
+ } *line;
+} remaining;
/**************************************************************************
Send the message as a chat to the server.
@@ -36,7 +47,6 @@
}
static int frozen_level = 0;
-static struct astring remaining = ASTRING_INIT;
/**************************************************************************
Turn on buffering, using a counter so that calls may be nested.
@@ -46,9 +56,7 @@
frozen_level++;
if (frozen_level == 1) {
- assert(remaining.str == NULL);
- astr_minsize(&remaining, 1);
- remaining.str[0] = '\0';
+ assert(remaining.lines == 0);
}
}
@@ -63,11 +71,14 @@
assert(frozen_level >= 0);
if (frozen_level == 0) {
- if (remaining.n > 2) {
- /* +1 to skip the initial '\n' */
- append_output_window(remaining.str + 1);
+ int i;
+
+ for (i = 0; i < remaining.lines; i++) {
+ base_append_output_window(remaining.line[i].text,
+ remaining.line[i].conn_id);
+ free(remaining.line[i].text);
}
- astr_free(&remaining);
+ remaining.lines = 0;
}
}
@@ -87,20 +98,26 @@
**************************************************************************/
void append_output_window(const char *astring)
{
+ base_append_output_window(astring, -1);
+}
+
+void base_append_output_window(const char *astring, int conn_id)
+{
if (frozen_level == 0) {
- real_append_output_window(astring);
- } else {
- /*
- * len_src doesn't include the trailing '\0'
- * len_dst does include the trailing '\0'
- */
- size_t len_src = strlen(astring), len_dst = remaining.n;
+ struct connection *conn = find_conn_by_id(conn_id);
+ enum color_std color = COLOR_STD_BLACK;
- /* +1 for the "\n" */
- astr_minsize(&remaining, len_dst + 1 + len_src);
- remaining.str[len_dst - 1] = '\n';
+ if (conn && get_player(conn->player_id)) {
+ color = player_color(get_player(conn->player_id));
+ }
- /* +1 for the "\0" */
- memcpy(&remaining.str[len_dst], astring, len_src + 1);
+ real_append_output_window(astring, conn, color);
+ } else {
+ remaining.lines++;
+ remaining.line =
+ fc_realloc(remaining.line,
+ remaining.lines * sizeof(*remaining.line));
+ remaining.line[remaining.lines - 1].text = mystrdup(astring);
+ remaining.line[remaining.lines - 1].conn_id = conn_id;
}
}
Index: client/chatline_common.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/chatline_common.h,v
retrieving revision 1.2
diff -u -u -d -H -r1.2 chatline_common.h
--- client/chatline_common.h 18 Jul 2003 22:02:24 -0000 1.2
+++ client/chatline_common.h 18 Apr 2004 13:41:49 -0000
@@ -17,6 +17,7 @@
void send_chat(const char *message);
+void base_append_output_window(const char *astring, int conn_id);
void append_output_window(const char *astring);
void output_window_freeze(void);
Index: client/climisc.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/climisc.c,v
retrieving revision 1.131
diff -u -u -d -H -r1.131 climisc.c
--- client/climisc.c 24 Mar 2004 05:39:01 -0000 1.131
+++ client/climisc.c 18 Apr 2004 13:41:50 -0000
@@ -978,7 +978,7 @@
my_vsnprintf(message, sizeof(message), format, ap);
va_end(ap);
- handle_chat_msg(message, x, y, event);
+ handle_chat_msg(message, x, y, event, aconnection.id);
}
/**************************************************************************
Index: client/packhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/packhand.c,v
retrieving revision 1.362
diff -u -u -d -H -r1.362 packhand.c
--- client/packhand.c 14 Apr 2004 17:18:36 -0000 1.362
+++ client/packhand.c 18 Apr 2004 13:41:52 -0000
@@ -839,7 +839,7 @@
/**************************************************************************
...
**************************************************************************/
-void handle_chat_msg(char *message, int x, int y, enum event_type event)
+void handle_chat_msg(char *message, int x, int y, enum event_type event, int
conn_id)
{
int where = MW_OUTPUT; /* where to display the message */
@@ -851,7 +851,7 @@
}
if (BOOL_VAL(where & MW_OUTPUT)) {
- append_output_window(message);
+ base_append_output_window(message,conn_id);
}
if (BOOL_VAL(where & MW_MESSAGES)) {
add_notify_window(message, x, y, event);
@@ -1677,6 +1677,7 @@
pconn->observer = pinfo->observer;
pconn->access_level = pinfo->access_level;
pconn->player = pplayer;
+ pconn->player_id = pinfo->player_num;
sz_strlcpy(pconn->username, pinfo->username);
sz_strlcpy(pconn->addr, pinfo->addr);
sz_strlcpy(pconn->capability, pinfo->capability);
Index: client/gui-gtk-2.0/chatline.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/chatline.c,v
retrieving revision 1.9
diff -u -u -d -H -r1.9 chatline.c
--- client/gui-gtk-2.0/chatline.c 1 Apr 2004 01:20:20 -0000 1.9
+++ client/gui-gtk-2.0/chatline.c 18 Apr 2004 13:41:52 -0000
@@ -28,6 +28,7 @@
#include "climisc.h"
#include "clinet.h"
+#include "colors.h"
#include "gui_main.h"
#include "gui_stuff.h"
@@ -67,14 +68,15 @@
/**************************************************************************
...
**************************************************************************/
-void real_append_output_window(const char *astring)
+void real_append_output_window(const char *astring, struct connection *conn,
+ enum color_std color)
{
GtkWidget *sw;
GtkAdjustment *slider;
bool scroll;
-
GtkTextBuffer *buf;
GtkTextIter i;
+ GtkTextTag *tag;
sw = gtk_widget_get_parent(GTK_WIDGET(main_message_area));
slider = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(sw));
@@ -84,9 +86,11 @@
(slider->upper - slider->step_increment));
buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(main_message_area));
+ tag = gtk_text_buffer_create_tag(buf, NULL, "foreground-gdk",
+ colors_standard[color], NULL);
gtk_text_buffer_get_end_iter(buf, &i);
gtk_text_buffer_insert(buf, &i, "\n", -1);
- gtk_text_buffer_insert(buf, &i, astring, -1);
+ gtk_text_buffer_insert_with_tags(buf, &i, astring, -1, tag, NULL);
/* have to use a mark, or this won't work properly */
if (scroll) {
Index: client/include/chatline_g.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/include/chatline_g.h,v
retrieving revision 1.2
diff -u -u -d -H -r1.2 chatline_g.h
--- client/include/chatline_g.h 27 Jun 2002 00:59:21 -0000 1.2
+++ client/include/chatline_g.h 18 Apr 2004 13:41:52 -0000
@@ -13,9 +13,12 @@
#ifndef FC__CHATLINE_G_H
#define FC__CHATLINE_G_H
+#include "colors_g.h"
+
#include "chatline_common.h"
-void real_append_output_window(const char *astring);
+void real_append_output_window(const char *astring, struct connection *conn,
+ enum color_std color);
void log_output_window(void);
void clear_output_window(void);
Index: common/capstr.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/capstr.c,v
retrieving revision 1.164
diff -u -u -d -H -r1.164 capstr.c
--- common/capstr.c 14 Apr 2004 11:19:44 -0000 1.164
+++ common/capstr.c 18 Apr 2004 13:41:53 -0000
@@ -77,7 +77,7 @@
#define CAPABILITY "+1.14.delta +last_turns_shield_surplus veteran +orders " \
"+starter +union +iso_maps +orders2client " \
"+change_production +tilespec1 +no_earth +trans " \
- "+want_hack invasions killstack bombard"
+ "+want_hack invasions killstack bombard +chat_conn"
/* "+1.14.delta" is the new delta protocol for 1.14.0-dev.
*
@@ -114,6 +114,9 @@
* "killstack" means ruleset option to ignore unit killstack effect
*
* "bombard" means units support the bombard ability.
+ *
+ * "chat_conn" means that chat message also contain the connection id
+ * of the source.
*/
void init_our_capability(void)
Index: common/connection.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/connection.c,v
retrieving revision 1.37
diff -u -u -d -H -r1.37 connection.c
--- common/connection.c 14 Jan 2004 11:58:12 -0000 1.37
+++ common/connection.c 18 Apr 2004 13:41:53 -0000
@@ -58,6 +58,8 @@
neccesary as removing a random connection while we are iterating through
a connection list might corrupt the list. */
int delayed_disconnect = 0;
+
+struct connection *current_connection;
/**************************************************************************
Command access levels for client-side use; at present, they are only
Index: common/connection.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/connection.h,v
retrieving revision 1.34
diff -u -u -d -H -r1.34 connection.h
--- common/connection.h 10 Apr 2004 03:47:49 -0000 1.34
+++ common/connection.h 18 Apr 2004 13:41:53 -0000
@@ -103,6 +103,7 @@
int sock;
bool used;
bool established; /* have negotiated initial packets */
+ int player_id;
struct player *player; /* NULL for connections not yet associated
with a specific player */
/*
@@ -223,6 +224,7 @@
} statistics;
};
+extern struct connection *current_connection;
const char *cmdlevel_name(enum cmdlevel_id lvl);
enum cmdlevel_id cmdlevel_named(const char *token);
Index: common/packets.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/packets.c,v
retrieving revision 1.267
diff -u -u -d -H -r1.267 packets.c
--- common/packets.c 14 Jan 2004 11:58:12 -0000 1.267
+++ common/packets.c 18 Apr 2004 13:41:53 -0000
@@ -604,6 +604,12 @@
void pre_send_packet_chat_msg(struct connection *pc,
struct packet_chat_msg *packet)
{
+ if (current_connection) {
+ packet->conn_id = current_connection->id;
+ } else {
+ packet->conn_id = 255;
+ }
+
if (packet->x == -1 && packet->y == -1) {
/* since we can currently only send unsigned ints... */
assert(!is_normal_map_pos(255, 255));
@@ -619,6 +625,9 @@
/* unsigned encoding for no position */
packet->x = -1;
packet->y = -1;
+ }
+ if (packet->conn_id == 255) {
+ packet->conn_id = -1;
}
}
Index: common/packets.def
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/packets.def,v
retrieving revision 1.20
diff -u -u -d -H -r1.20 packets.def
--- common/packets.def 14 Apr 2004 11:19:44 -0000 1.20
+++ common/packets.def 18 Apr 2004 13:41:54 -0000
@@ -359,6 +359,7 @@
STRING message[MAX_LEN_MSG];
COORD x, y;
EVENT event;
+ CONNECTION conn_id;
end
PACKET_CHAT_MSG_REQ=19;cs,handle-per-conn,dsend
Index: common/tech.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/tech.c,v
retrieving revision 1.70
diff -u -u -d -H -r1.70 tech.c
--- common/tech.c 31 Jan 2004 17:52:41 -0000 1.70
+++ common/tech.c 18 Apr 2004 13:41:58 -0000
@@ -188,6 +188,9 @@
{
enum tech_flag_id flag;
+ if(get_invention(pplayer, A_NONE) != TECH_KNOWN){
+ return;
+ }
/* This assert does not work. Triggered by AI players in new
* games. */
/* assert(get_invention(pplayer, A_NONE) == TECH_KNOWN); */
Index: server/connecthand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/connecthand.c,v
retrieving revision 1.19
diff -u -u -d -H -r1.19 connecthand.c
--- server/connecthand.c 10 Apr 2004 03:47:49 -0000 1.19
+++ server/connecthand.c 18 Apr 2004 13:42:12 -0000
@@ -701,8 +701,11 @@
}
pconn->player = pplayer;
+
conn_list_insert_back(&pplayer->connections, pconn);
conn_list_insert_back(&game.game_connections, pconn);
+
+ send_player_info_c(NULL, &game.game_connections);
return TRUE;
}
Index: server/sernet.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/sernet.c,v
retrieving revision 1.116
diff -u -u -d -H -r1.116 sernet.c
--- server/sernet.c 17 Mar 2004 00:22:44 -0000 1.116
+++ server/sernet.c 18 Apr 2004 13:42:12 -0000
@@ -609,6 +609,7 @@
err = gettimeofday(&start, &tz);
assert(!err);
#endif
+ current_connection = pconn;
connection_do_buffer(pconn);
start_processing_request(pconn,
pconn->server.
@@ -618,6 +619,7 @@
finish_processing_request(pconn);
connection_do_unbuffer(pconn);
+ current_connection = NULL;
if (!command_ok) {
close_connection(pconn);
}
- [Freeciv-Dev] Re: (PR#7246) Send origin of chat messages,
Raimar Falke <=
|
|