Skip to content

Commit

Permalink
Extract a common module/library/header.
Browse files Browse the repository at this point in the history
  • Loading branch information
shlomif committed Apr 29, 2019
1 parent 9e148b8 commit 9032317
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 49 deletions.
57 changes: 8 additions & 49 deletions pysollib/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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
Expand All @@ -54,7 +55,7 @@
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':
Expand All @@ -71,49 +72,7 @@
# ************************************************************************
# * 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:
Expand All @@ -126,11 +85,11 @@ def __init__(self, id):
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):
#
Expand Down
62 changes: 62 additions & 0 deletions pysollib/app_stat_result.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions scripts/gen_individual_importing_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'pysollib.acard',
'pysollib.actions',
'pysollib.app',
'pysollib.app_stat_result',
'pysollib.configobj.configobj',
'pysollib.configobj.validate',
'pysollib.customgame',
Expand Down

0 comments on commit 9032317

Please sign in to comment.