Skip to content

Commit

Permalink
Fixing inconsistency with duplicated votes (#1277)
Browse files Browse the repository at this point in the history
* Fixing the bug and inconsistancy with duplicated votes in the karma app. (MIGRATION ADDED). Removing duplicated records while migrating

* Fixing the bug and inconsistancy with duplicated votes in the karma app. (MIGRATION ADDED). Removing duplicated records while migrating (FIX for multiple duplicates)
  • Loading branch information
tomvlk committed Aug 30, 2023
1 parent 86727ef commit 1ab3241
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
8 changes: 7 additions & 1 deletion pyplanet/apps/contrib/karma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Karma(AppConfig):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.lock = asyncio.Lock()

self.current_votes = []
self.current_karma = 0.0
Expand Down Expand Up @@ -137,7 +138,12 @@ async def player_connect(self, player, is_spectator, source, signal):
await self.widget.display(player=player)

async def player_chat(self, player, text, cmd):
if not cmd:
# Ignore if command is given.
if cmd:
return

# Acquire the lock for the voting array.
async with self.lock:
if text == '+++' or text == '++' or text == '+' or text == '+-' or text == '-+' or text == '-' or text == '--' or text == '---':
expanded_voting = await self.setting_expanded_voting.get_value()
if expanded_voting is False:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging
from peewee import *
from playhouse.migrate import migrate, SchemaMigrator

from ..models.karma import Karma

logger = logging.getLogger(__name__)


def upgrade(migrator: SchemaMigrator):
# Fix the duplicates in the current setup.
total_processed = 0
results = None
while results is None or len(results) > 0:
results = (Karma.select()
.order_by(Karma.created_at.desc())
.group_by(Karma.player_id, Karma.map_id)
.having(fn.Count(Karma.player_id) > 1))

for result in results:
result.delete_instance()
total_processed += 1

logger.warning('Removing duplicated karma votes, processed {} rows! Now going to execute the migration...'.format(
total_processed
))

# Create the indexes.
migrate(
migrator.add_index(Karma._meta.db_table, [
'player_id', 'map_id'
], unique=True),
)


def downgrade(migrator: SchemaMigrator):
pass
7 changes: 6 additions & 1 deletion pyplanet/apps/contrib/karma/models/karma.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Maniaplanet Core Models. This models are used in several apps and should be considered as very stable.
Maniaplanet Core Models. These models are used in several apps and should be considered as very stable.
"""
from peewee import *
from pyplanet.core.db import TimedModel
Expand Down Expand Up @@ -30,3 +30,8 @@ class Karma(TimedModel):
"""
Karma vote (-1, -0.5, 0, 0.5 or 1)
"""

class Meta:
indexes = (
(('player', 'map'), True),
)
2 changes: 1 addition & 1 deletion pyplanet/apps/contrib/local_records/models/local_record.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Maniaplanet Core Models. This models are used in several apps and should be considered as very stable.
Maniaplanet Core Models. These models are used in several apps and should be considered as very stable.
"""
from peewee import *
from pyplanet.core.db import TimedModel
Expand Down

0 comments on commit 1ab3241

Please sign in to comment.