From 90323173a7282bf55c7392a13ab0d2162880760b Mon Sep 17 00:00:00 2001
From: Shlomi Fish <shlomif@shlomifish.org>
Date: Mon, 29 Apr 2019 15:08:04 +0300
Subject: [PATCH] Extract a common module/library/header.

This is Refactoring / code cleanup.

See:

* https://refactoring.com/catalog/extractMethod.html

* https://en.wikipedia.org/wiki/Code_refactoring

* https://www.refactoring.com/

* https://www.joelonsoftware.com/2002/01/23/rub-a-dub-dub/

Some small optimisations may have slipped in as well.
---
 pysollib/app.py                           | 57 +++------------------
 pysollib/app_stat_result.py               | 62 +++++++++++++++++++++++
 scripts/gen_individual_importing_tests.py |  1 +
 3 files changed, 71 insertions(+), 49 deletions(-)
 create mode 100644 pysollib/app_stat_result.py

diff --git a/pysollib/app.py b/pysollib/app.py
index 9a794c91..2f45d8a0 100644
--- a/pysollib/app.py
+++ b/pysollib/app.py
@@ -29,6 +29,7 @@ import sys
 import traceback
 from pickle import UnpicklingError
 
+from pysollib.app_stat_result import GameStatResult
 from pysollib.gamedb import GAME_DB, GI, loadGame
 from pysollib.images import Images, SubsampledImages
 from pysollib.mfxutil import Struct, destruct
@@ -54,7 +55,7 @@ from pysollib.resource import Sample, SampleManager
 from pysollib.resource import Tile, TileManager
 from pysollib.settings import DEBUG
 from pysollib.settings import PACKAGE, VERSION_TUPLE, WIN_SYSTEM
-from pysollib.settings import TOOLKIT, TOP_SIZE
+from pysollib.settings import TOOLKIT
 from pysollib.util import CARDSET, IMAGE_EXTENSIONS
 from pysollib.winsystems import TkSettings
 if TOOLKIT == 'tk':
@@ -71,49 +72,7 @@ if True:  # This prevents from travis 'error' E402.
 # ************************************************************************
 # * Statistics
 # ************************************************************************
-
-
-class _GameStatResult:
-    def __init__(self):
-        self.min = 0
-        self.max = 0
-        self.top = []
-        self.num = 0
-        self.total = 0  # sum of all values
-        self.average = 0
-
-    def update(self, gameid, value, game_number, game_start_time):
-        # update min & max
-        if not self.min or value < self.min:
-            self.min = value
-        if not self.max or value > self.max:
-            self.max = value
-        # calculate position & update top
-        position = None
-        n = 0
-        for i in self.top:
-            if value < i.value:
-                position = n+1
-                v = Struct(gameid=gameid,
-                           value=value,
-                           game_number=game_number,
-                           game_start_time=game_start_time)
-                self.top.insert(n, v)
-                del self.top[TOP_SIZE:]
-                break
-            n += 1
-        if not position and len(self.top) < TOP_SIZE:
-            v = Struct(gameid=gameid,
-                       value=value,
-                       game_number=game_number,
-                       game_start_time=game_start_time)
-            self.top.append(v)
-            position = len(self.top)
-        # update average
-        self.total += value
-        self.num += 1
-        self.average = float(self.total)/self.num
-        return position
+_GameStatResult = GameStatResult
 
 
 class GameStat:
@@ -126,11 +85,11 @@ class GameStat:
         self.num_won = 0
         self.num_perfect = 0
         #
-        self.time_result = _GameStatResult()
-        self.moves_result = _GameStatResult()
-        self.total_moves_result = _GameStatResult()
-        self.score_result = _GameStatResult()
-        self.score_casino_result = _GameStatResult()
+        self.time_result = GameStatResult()
+        self.moves_result = GameStatResult()
+        self.total_moves_result = GameStatResult()
+        self.score_result = GameStatResult()
+        self.score_casino_result = GameStatResult()
 
     def update(self, game, status):
         #
diff --git a/pysollib/app_stat_result.py b/pysollib/app_stat_result.py
new file mode 100644
index 00000000..5fe476f1
--- /dev/null
+++ b/pysollib/app_stat_result.py
@@ -0,0 +1,62 @@
+#  Copyright (C) 1998-2003 Markus Franz Xaver Johannes Oberhumer
+#  Copyright (C) 2003 Mt. Hood Playing Card Co.
+#  Copyright (C) 2005-2009 Skomoroh
+#
+#  This program is free software: you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation, either version 3 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from pysollib.mfxutil import Struct
+from pysollib.settings import TOP_SIZE
+
+
+class GameStatResult:
+    def __init__(self):
+        self.min = 0
+        self.max = 0
+        self.top = []
+        self.num = 0
+        self.total = 0  # sum of all values
+        self.average = 0
+
+    def update(self, gameid, value, game_number, game_start_time):
+        # update min & max
+        if not self.min or value < self.min:
+            self.min = value
+        if not self.max or value > self.max:
+            self.max = value
+        # calculate position & update top
+        position = None
+        n = 0
+        for i in self.top:
+            if value < i.value:
+                position = n+1
+                v = Struct(gameid=gameid,
+                           value=value,
+                           game_number=game_number,
+                           game_start_time=game_start_time)
+                self.top.insert(n, v)
+                del self.top[TOP_SIZE:]
+                break
+            n += 1
+        if not position and len(self.top) < TOP_SIZE:
+            v = Struct(gameid=gameid,
+                       value=value,
+                       game_number=game_number,
+                       game_start_time=game_start_time)
+            self.top.append(v)
+            position = len(self.top)
+        # update average
+        self.total += value
+        self.num += 1
+        self.average = float(self.total)/self.num
+        return position
diff --git a/scripts/gen_individual_importing_tests.py b/scripts/gen_individual_importing_tests.py
index 7706703a..20d467c4 100644
--- a/scripts/gen_individual_importing_tests.py
+++ b/scripts/gen_individual_importing_tests.py
@@ -14,6 +14,7 @@ for module_name in \
          'pysollib.acard',
          'pysollib.actions',
          'pysollib.app',
+         'pysollib.app_stat_result',
          'pysollib.configobj.configobj',
          'pysollib.configobj.validate',
          'pysollib.customgame',