From be97bca6874cc4a044796387ca9226788681e916 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C5=82awomir=20Lach?= <slawek@lach.art.pl>
Date: Sat, 15 Oct 2022 16:43:46 +0200
Subject: [PATCH] =?UTF-8?q?!OSDN:=20#TICKET:=2045429=20-=20S=C5=82awomir?=
 =?UTF-8?q?=20Lach=20<slawek@lach.art.pl>?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Send city counters (at turn end) for player, who own this city.
Also add support for this packet to client-side. Client routines will
only update information.

diff --git a/client/packhand.c b/client/packhand.c
index 9760b4da77..3d17059aa9 100644
--- a/client/packhand.c
+++ b/client/packhand.c
@@ -5526,3 +5526,28 @@ void handle_ruleset_counter(const struct packet_ruleset_counter *packet)
 
   attach_city_counter(curr);
 }
+
+/**********************************************************************//**
+Handle updating city's counter, when server request
+**************************************************************************/
+void handle_city_update_counter(const struct packet_city_update_counter *packet)
+{
+  int counter = packet->counter;
+  int counter_count = counters_get_city_counters_count();
+  struct city *pcity = game_city_by_number(packet->city);
+
+  if (NULL == pcity) {
+
+    return;
+  }
+
+  if (counter_count <= counter || 0 > counter) {
+
+    return;
+  }
+
+  pcity->counter_values[counter] = packet->value;
+  
+  update_city_description(pcity);
+  city_report_dialog_update_city(pcity);
+}
diff --git a/common/networking/packets.def b/common/networking/packets.def
index 6dfea8b05f..59ad8f289d 100644
--- a/common/networking/packets.def
+++ b/common/networking/packets.def
@@ -3,7 +3,7 @@
 Max used id:
 ============
 
-Max id: 513
+Max id: 514
 
 Packets are not ordered by their id, but by their category. New packet
 with higher id may get added to existing category, and not to the end of file.
@@ -304,6 +304,7 @@ type ACTION_ID          = uint8(action_id)
 type ACTION_TGT         = SINT32
 type ACTION_SUB_TGT     = SINT16
 type BASE               = sint8(Base_type_id)
+type COUNTER            = UINT32
 type ROAD               = sint8(Road_type_id)
 type CITY               = UINT16
 # city id, with space for special values
@@ -773,6 +774,12 @@ PACKET_CITY_INFO = 31; sc, lsend, is-game-info, force, cancel(PACKET_CITY_SHORT_
   UNIT_ORDER rally_point_orders[MAX_LEN_ROUTE:rally_point_length];
 end
 
+PACKET_CITY_UPDATE_COUNTER = 514; sc, lsend
+  CITY city;
+  COUNTER counter;
+  UINT32 value;
+end
+
 PACKET_CITY_SHORT_INFO = 32; sc, lsend, is-game-info, cancel(PACKET_CITY_INFO), cancel(PACKET_WEB_CITY_INFO_ADDITION)
   CITY id; key
   TILE tile;
@@ -1966,6 +1973,7 @@ PACKET_RULESET_DESCRIPTION_PART = 247; sc, lsend
   STRING text[MAX_LEN_CONTENT];
 end
 
+
 /*********************************************************
  Below are the packets that control single-player mode.  
 *********************************************************/
diff --git a/server/cityturn.c b/server/cityturn.c
index 9f6531f581..25a8114cca 100644
--- a/server/cityturn.c
+++ b/server/cityturn.c
@@ -4259,3 +4259,18 @@ void city_style_refresh(struct city *pcity)
 {
   pcity->style = city_style(pcity);
 }
+
+/**********************************************************************//**
+  Send updated (by server) counter information of
+  a given city.
+**************************************************************************/
+void city_counter_refresh(struct city *pcity, int number)
+{
+  struct packet_city_update_counter packet;
+
+  packet.city = pcity->id;
+  packet.counter = number;
+  packet.value = pcity->counter_values[number];
+
+  lsend_packet_city_update_counter(pcity->owner->connections, &packet);
+}
diff --git a/server/cityturn.h b/server/cityturn.h
index f569d7f1a4..42b63c46cd 100644
--- a/server/cityturn.h
+++ b/server/cityturn.h
@@ -57,4 +57,6 @@ void city_style_refresh(struct city *pcity);
 bool player_balance_treasury_units_and_buildings(struct player *pplayer);
 bool player_balance_treasury_units(struct player *pplayer);
 
+void city_counter_refresh(struct city *pcity, int number);
+
 #endif /* FC__CITYTURN_H */
diff --git a/server/srv_main.c b/server/srv_main.c
index 7d45ef629f..b986f1f1dd 100644
--- a/server/srv_main.c
+++ b/server/srv_main.c
@@ -1536,6 +1536,8 @@ static void end_turn(void)
   players_iterate(pplayer) {
     city_list_iterate(pplayer->cities, pcity) {
       city_counters_iterate(pcount) {
+        int old_val = pcity->counter_values[pcount->index];
+
         if (pcount->type == CB_CITY_OWNED_TURNS) {
           pcity->counter_values[pcount->index]++;
         }
@@ -1548,6 +1550,11 @@ static void end_turn(void)
             pcity->counter_values[pcount->index] = 0;
           }
         }
+
+        if (pcity->counter_values[pcount->index] != old_val) {
+
+          city_counter_refresh(pcity, pcount->index);
+        }
       } city_counters_iterate_end;
     } city_list_iterate_end;
   } players_iterate_end;
-- 
2.38.0

