Skip to content

Commit 3fabae3

Browse files
authored
ban, silence: support redis v3+ (MinoMino#36)
1 parent 9461868 commit 3fabae3

11 files changed

Lines changed: 138 additions & 11 deletions

File tree

.github/workflows/test-pypa-ubuntu.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ jobs:
2727
- uses: actions/checkout@v3
2828
with:
2929
submodules: 'true'
30+
- name: Set up Redis server
31+
run: sudo apt-get install redis
3032
- name: Include ppa repository
3133
run: sudo add-apt-repository ppa:deadsnakes/ppa
3234

@@ -46,11 +48,6 @@ jobs:
4648
- name: Install pip
4749
run: python3 get-pip.py
4850

49-
# TODO: try to remove this after unpining redis
50-
- name: Install setuptools
51-
if: contains(fromJSON('["3.12"]'), matrix.python-version)
52-
run: python3 get-pip.py "setuptools"
53-
5451
- name: Install requirements
5552
run: python3 -m pip install -r requirements.txt
5653

.github/workflows/test-ubuntu.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ jobs:
1313
- uses: actions/checkout@v3
1414
with:
1515
submodules: 'true'
16+
- name: Set up Redis server
17+
run: sudo apt-get update && sudo apt-get install redis
1618
- name: Set up Python
1719
run: sudo apt-get install python3
1820
- name: Install requirements

.github/workflows/test-with-docker.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ jobs:
99
dockerfile-suffix: [
1010
"python35", "python36",
1111
]
12+
force-redis-package-version: [
13+
"", "==2.10",
14+
]
1215

1316
runs-on: ubuntu-latest
1417
steps:
@@ -26,6 +29,7 @@ jobs:
2629
push: false
2730
load: true
2831
tags: test:latest
32+
build-args: FORCE_REDIS_PACKAGE_VERSION=${{ matrix.force-redis-package-version }}
2933

3034
- name: Test
3135
run: |
@@ -35,5 +39,5 @@ jobs:
3539
- name: Upload coverage report as artifact
3640
uses: actions/upload-artifact@v4
3741
with:
38-
name: coverage - docker - ${{ matrix.dockerfile-suffix }}.xml
42+
name: coverage - docker - ${{ matrix.dockerfile-suffix }} - ${{ matrix.force-redis-package-version }}.xml
3943
path: ./output/coverage.xml

ban.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import datetime
2121
import time
2222
import re
23+
import redis
2324

2425
LENGTH_REGEX = re.compile(r"(?P<number>[0-9]+) (?P<scale>seconds?|minutes?|hours?|days?|weeks?|months?|years?)")
2526
TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
@@ -199,7 +200,11 @@ def cmd_ban(self, player, msg, channel):
199200
base_key = PLAYER_KEY.format(ident) + ":bans"
200201
ban_id = self.db.zcard(base_key)
201202
db = self.db.pipeline()
202-
db.zadd(base_key, time.time() + td.total_seconds(), ban_id)
203+
ban_time = time.time() + td.total_seconds()
204+
if redis.VERSION < (3,):
205+
db.zadd(base_key, ban_time, ban_id)
206+
else:
207+
db.zadd(base_key, {ban_id: ban_time})
203208
ban = {"expires": expires, "reason": reason, "issued": now, "issued_by": player.steam_id}
204209
db.hmset(base_key + ":{}".format(ban_id), ban)
205210
db.execute()
@@ -239,7 +244,10 @@ def cmd_unban(self, player, msg, channel):
239244
else:
240245
db = self.db.pipeline()
241246
for ban_id, score in bans:
242-
db.zincrby(base_key, ban_id, -score)
247+
if redis.VERSION < (3,):
248+
db.zincrby(base_key, ban_id, -score)
249+
else:
250+
db.zincrby(base_key, -score, ban_id)
243251
db.execute()
244252
channel.reply("^6{}^7 has been unbanned.".format(name))
245253

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
redis>=2.10, <3.0
1+
redis
22
hiredis
33
requests
44
pyzmq<18; python_version < '3.9'

silence.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import datetime
2121
import time
2222
import re
23+
import redis
2324

2425
LENGTH_REGEX = re.compile(r"(?P<number>[0-9]+) (?P<scale>seconds?|minutes?|hours?|days?|weeks?|months?|years?)")
2526
TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
@@ -149,7 +150,10 @@ def cmd_silence(self, player, msg, channel):
149150
silence_id = self.db.zcard(base_key)
150151
score = time.time() + td.total_seconds()
151152
db = self.db.pipeline()
152-
db.zadd(base_key, score, silence_id)
153+
if redis.VERSION < (3,):
154+
db.zadd(base_key, score, silence_id)
155+
else:
156+
db.zadd(base_key, {silence_id: score})
153157
silence = {"expires": expires, "reason": reason, "issued": now, "issued_by": player.steam_id}
154158
db.hmset(base_key + ":{}".format(silence_id), silence)
155159
db.execute()
@@ -194,7 +198,10 @@ def cmd_unsilence(self, player, msg, channel):
194198
else:
195199
db = self.db.pipeline()
196200
for silence_id, score in silences:
197-
db.zincrby(base_key, silence_id, -score)
201+
if redis.VERSION < (3,):
202+
db.zincrby(base_key, silence_id, -score)
203+
else:
204+
db.zincrby(base_key, -score, silence_id)
198205
db.execute()
199206
if ident in self.silenced:
200207
del self.silenced[ident]

tests/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
from .test_balance import TestBalance
2+
from .test_ban import TestBan
3+
from .test_silence import TestSilence

tests/dockerfiles/Dockerfile.python35

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ RUN python3 -m pip install mockito coverage
1111
COPY . /opt/minqlx-plugins
1212
WORKDIR /opt/minqlx-plugins
1313

14+
ARG FORCE_REDIS_PACKAGE_VERSION
15+
RUN if [ "$FORCE_REDIS_PACKAGE_VERSION" ] ; then python3 -m pip install redis"$FORCE_REDIS_PACKAGE_VERSION" ; fi
16+
1417
RUN python3 -m pip install -r requirements.txt
1518
RUN patch -p1 < ./tests/minqlx_plugin_test/python35.patch
1619
CMD ["./tests/dockerfiles/entrypoint.sh"]

tests/dockerfiles/Dockerfile.python36

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ RUN python3 -m pip install mockito coverage
66
COPY . /opt/minqlx-plugins
77
WORKDIR /opt/minqlx-plugins
88

9+
ARG FORCE_REDIS_PACKAGE_VERSION
10+
RUN if [ "$FORCE_REDIS_PACKAGE_VERSION" ] ; then python3 -m pip install redis"$FORCE_REDIS_PACKAGE_VERSION" ; fi
11+
912
RUN python3 -m pip install -r requirements.txt
1013
CMD ["./tests/dockerfiles/entrypoint.sh"]

tests/test_ban.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import unittest
2+
from time import sleep
3+
4+
from ban import ban, minqlx
5+
from mockito import unstub
6+
from minqlx_plugin_test import (
7+
connected_players,
8+
fake_player,
9+
mocked_channel,
10+
setup_cvars,
11+
setup_plugin,
12+
)
13+
14+
15+
class TestBan(unittest.TestCase):
16+
def setUp(self):
17+
setup_plugin()
18+
connected_players()
19+
setup_cvars(
20+
{
21+
"qlx_database": "redis",
22+
"qlx_redisDatabase": "0",
23+
"qlx_redisUnixSocket": "0",
24+
"qlx_redisAddress": "127.0.0.1",
25+
"qlx_redisPassword": "",
26+
"qlx_owner": "777",
27+
"qlx_leaverBan": "0",
28+
}
29+
)
30+
minqlx.Plugin.database = minqlx.database.Redis
31+
self.plugin = ban()
32+
33+
def tearDown(self):
34+
unstub()
35+
36+
def test_ban(self):
37+
player = fake_player(666, "Woodpecker")
38+
admin = fake_player(777, "Admin")
39+
channel = mocked_channel()
40+
connected_players(player)
41+
42+
self.plugin.cmd_ban(admin, ["!ban", "666", "3", "seconds"], channel)
43+
44+
player_connect_result = self.plugin.handle_player_connect(player)
45+
self.assertTrue(isinstance(player_connect_result, str))
46+
self.assertTrue(player_connect_result.startswith("You are banned until"))
47+
48+
sleep(3.5)
49+
50+
player_connect_result = self.plugin.handle_player_connect(player)
51+
self.assertEqual(player_connect_result, None)

0 commit comments

Comments
 (0)