diff sat/plugins/plugin_misc_quiz.py @ 4037:524856bd7b19

massive refactoring to switch from camelCase to snake_case: historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a pre-PEP8 code, to use the same coding style as in Twisted. However, snake_case is more readable and it's better to follow PEP8 best practices, so it has been decided to move on full snake_case. Because Libervia has a huge codebase, this ended with a ugly mix of camelCase and snake_case. To fix that, this patch does a big refactoring by renaming every function and method (including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case. This is a massive change, and may result in some bugs.
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:54:42 +0200
parents be6d91572633
children
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_quiz.py	Fri Apr 07 15:18:39 2023 +0200
+++ b/sat/plugins/plugin_misc_quiz.py	Sat Apr 08 13:54:42 2023 +0200
@@ -44,7 +44,7 @@
 
 
 class Quiz(object):
-    def inheritFromRoomGame(self, host):
+    def inherit_from_room_game(self, host):
         global RoomGame
         RoomGame = host.plugins["ROOM-GAME"].__class__
         self.__class__ = type(
@@ -53,7 +53,7 @@
 
     def __init__(self, host):
         log.info(_("Plugin Quiz initialization"))
-        self.inheritFromRoomGame(host)
+        self.inherit_from_room_game(host)
         RoomGame._init_(
             self,
             host,
@@ -62,39 +62,39 @@
             game_init={"stage": None},
             player_init={"score": 0},
         )
-        host.bridge.addMethod(
-            "quizGameLaunch",
+        host.bridge.add_method(
+            "quiz_game_launch",
             ".plugin",
             in_sign="asss",
             out_sign="",
-            method=self._prepareRoom,
+            method=self._prepare_room,
         )  # args: players, room_jid, profile
-        host.bridge.addMethod(
-            "quizGameCreate",
+        host.bridge.add_method(
+            "quiz_game_create",
             ".plugin",
             in_sign="sass",
             out_sign="",
-            method=self._createGame,
+            method=self._create_game,
         )  # args: room_jid, players, profile
-        host.bridge.addMethod(
-            "quizGameReady",
+        host.bridge.add_method(
+            "quiz_game_ready",
             ".plugin",
             in_sign="sss",
             out_sign="",
-            method=self._playerReady,
+            method=self._player_ready,
         )  # args: player, referee, profile
-        host.bridge.addMethod(
-            "quizGameAnswer",
+        host.bridge.add_method(
+            "quiz_game_answer",
             ".plugin",
             in_sign="ssss",
             out_sign="",
-            method=self.playerAnswer,
+            method=self.player_answer,
         )
-        host.bridge.addSignal(
-            "quizGameStarted", ".plugin", signature="ssass"
+        host.bridge.add_signal(
+            "quiz_game_started", ".plugin", signature="ssass"
         )  # args: room_jid, referee, players, profile
-        host.bridge.addSignal(
-            "quizGameNew",
+        host.bridge.add_signal(
+            "quiz_game_new",
             ".plugin",
             signature="sa{ss}s",
             doc={
@@ -104,8 +104,8 @@
                 "param_2": "%(doc_profile)s",
             },
         )
-        host.bridge.addSignal(
-            "quizGameQuestion",
+        host.bridge.add_signal(
+            "quiz_game_question",
             ".plugin",
             signature="sssis",
             doc={
@@ -117,8 +117,8 @@
                 "param_4": "%(doc_profile)s",
             },
         )
-        host.bridge.addSignal(
-            "quizGamePlayerBuzzed",
+        host.bridge.add_signal(
+            "quiz_game_player_buzzed",
             ".plugin",
             signature="ssbs",
             doc={
@@ -129,8 +129,8 @@
                 "param_3": "%(doc_profile)s",
             },
         )
-        host.bridge.addSignal(
-            "quizGamePlayerSays",
+        host.bridge.add_signal(
+            "quiz_game_player_says",
             ".plugin",
             signature="sssis",
             doc={
@@ -142,8 +142,8 @@
                 "param_4": "%(doc_profile)s",
             },
         )
-        host.bridge.addSignal(
-            "quizGameAnswerResult",
+        host.bridge.add_signal(
+            "quiz_game_answer_result",
             ".plugin",
             signature="ssba{si}s",
             doc={
@@ -155,8 +155,8 @@
                 "param_4": "%(doc_profile)s",
             },
         )
-        host.bridge.addSignal(
-            "quizGameTimerExpired",
+        host.bridge.add_signal(
+            "quiz_game_timer_expired",
             ".plugin",
             signature="ss",
             doc={
@@ -165,8 +165,8 @@
                 "param_1": "%(doc_profile)s",
             },
         )
-        host.bridge.addSignal(
-            "quizGameTimerRestarted",
+        host.bridge.add_signal(
+            "quiz_game_timer_restarted",
             ".plugin",
             signature="sis",
             doc={
@@ -238,7 +238,7 @@
 
     def __start_play(self, room_jid, game_data, profile):
         """Start the game (tell to the first player after dealer to play"""
-        client = self.host.getClient(profile)
+        client = self.host.get_client(profile)
         game_data["stage"] = "play"
         next_player_idx = game_data["current_player"] = (
             game_data["init_player"] + 1
@@ -251,9 +251,9 @@
         mess.firstChildElement().addElement("your_turn")
         client.send(mess)
 
-    def playerAnswer(self, player, referee, answer, profile_key=C.PROF_KEY_NONE):
+    def player_answer(self, player, referee, answer, profile_key=C.PROF_KEY_NONE):
         """Called when a player give an answer"""
-        client = self.host.getClient(profile_key)
+        client = self.host.get_client(profile_key)
         log.debug(
             "new player answer (%(profile)s): %(answer)s"
             % {"profile": client.profile, "answer": answer}
@@ -264,17 +264,17 @@
         answer_elt.addContent(answer)
         client.send(mess)
 
-    def timerExpired(self, room_jid, profile):
+    def timer_expired(self, room_jid, profile):
         """Called when nobody answered the question in time"""
-        client = self.host.getClient(profile)
+        client = self.host.get_client(profile)
         game_data = self.games[room_jid]
         game_data["stage"] = "expired"
         mess = self.createGameElt(room_jid)
         mess.firstChildElement().addElement("timer_expired")
         client.send(mess)
-        reactor.callLater(4, self.askQuestion, room_jid, client.profile)
+        reactor.callLater(4, self.ask_question, room_jid, client.profile)
 
-    def pauseTimer(self, room_jid):
+    def pause_timer(self, room_jid):
         """Stop the timer and save the time left"""
         game_data = self.games[room_jid]
         left = max(0, game_data["timer"].getTime() - time())
@@ -283,9 +283,9 @@
         game_data["previous_stage"] = game_data["stage"]
         game_data["stage"] = "paused"
 
-    def restartTimer(self, room_jid, profile):
+    def restart_timer(self, room_jid, profile):
         """Restart a timer with the saved time"""
-        client = self.host.getClient(profile)
+        client = self.host.get_client(profile)
         game_data = self.games[room_jid]
         assert game_data["time_left"] is not None
         mess = self.createGameElt(room_jid)
@@ -293,15 +293,15 @@
         jabber_client.restarted_elt["time_left"] = str(game_data["time_left"])
         client.send(mess)
         game_data["timer"] = reactor.callLater(
-            game_data["time_left"], self.timerExpired, room_jid, profile
+            game_data["time_left"], self.timer_expired, room_jid, profile
         )
         game_data["time_left"] = None
         game_data["stage"] = game_data["previous_stage"]
         del game_data["previous_stage"]
 
-    def askQuestion(self, room_jid, profile):
+    def ask_question(self, room_jid, profile):
         """Ask a new question"""
-        client = self.host.getClient(profile)
+        client = self.host.get_client(profile)
         game_data = self.games[room_jid]
         game_data["stage"] = "question"
         game_data["question_id"] = "1"
@@ -314,13 +314,13 @@
         )
         client.send(mess)
         game_data["timer"] = reactor.callLater(
-            timer, self.timerExpired, room_jid, profile
+            timer, self.timer_expired, room_jid, profile
         )
         game_data["time_left"] = None
 
-    def checkAnswer(self, room_jid, player, answer, profile):
+    def check_answer(self, room_jid, player, answer, profile):
         """Check if the answer given is right"""
-        client = self.host.getClient(profile)
+        client = self.host.get_client(profile)
         game_data = self.games[room_jid]
         players_data = game_data["players_data"]
         good_answer = game_data["question_id"] == "1" and answer == "42"
@@ -334,11 +334,11 @@
         client.send(mess)
 
         if good_answer:
-            reactor.callLater(4, self.askQuestion, room_jid, profile)
+            reactor.callLater(4, self.ask_question, room_jid, profile)
         else:
-            reactor.callLater(4, self.restartTimer, room_jid, profile)
+            reactor.callLater(4, self.restart_timer, room_jid, profile)
 
-    def newGame(self, room_jid, profile):
+    def new_game(self, room_jid, profile):
         """Launch a new round"""
         common_data = {"game_score": 0}
         new_game_data = {
@@ -349,11 +349,11 @@
             )
         }
         msg_elts = self.__game_data_to_xml(new_game_data)
-        RoomGame.newRound(self, room_jid, (common_data, msg_elts), profile)
-        reactor.callLater(10, self.askQuestion, room_jid, profile)
+        RoomGame.new_round(self, room_jid, (common_data, msg_elts), profile)
+        reactor.callLater(10, self.ask_question, room_jid, profile)
 
     def room_game_cmd(self, mess_elt, profile):
-        client = self.host.getClient(profile)
+        client = self.host.get_client(profile)
         from_jid = jid.JID(mess_elt["from"])
         room_jid = jid.JID(from_jid.userhost())
         game_elt = mess_elt.firstChildElement()
@@ -367,7 +367,7 @@
                 players = []
                 for player in elt.elements():
                     players.append(str(player))
-                self.host.bridge.quizGameStarted(
+                self.host.bridge.quiz_game_started(
                     room_jid.userhost(), from_jid.full(), players, profile
                 )
 
@@ -383,15 +383,15 @@
                 if (
                     list(status.values()).count("ready") == nb_players
                 ):  # everybody is ready, we can start the game
-                    self.newGame(room_jid, profile)
+                    self.new_game(room_jid, profile)
 
             elif elt.name == "game_data":
-                self.host.bridge.quizGameNew(
+                self.host.bridge.quiz_game_new(
                     room_jid.userhost(), self.__xml_to_game_data(elt), profile
                 )
 
             elif elt.name == "question":  # A question is asked
-                self.host.bridge.quizGameQuestion(
+                self.host.bridge.quiz_game_question(
                     room_jid.userhost(),
                     elt["id"],
                     str(elt),
@@ -411,7 +411,7 @@
                 buzzer_elt["pause"] = str(pause)
                 client.send(mess)
                 if pause:
-                    self.pauseTimer(room_jid)
+                    self.pause_timer(room_jid)
                     # and we send the player answer
                     mess = self.createGameElt(room_jid)
                     _answer = str(elt)
@@ -421,16 +421,16 @@
                     say_elt["delay"] = "3"
                     reactor.callLater(2, client.send, mess)
                     reactor.callLater(
-                        6, self.checkAnswer, room_jid, player, _answer, profile=profile
+                        6, self.check_answer, room_jid, player, _answer, profile=profile
                     )
 
             elif elt.name == "player_buzzed":
-                self.host.bridge.quizGamePlayerBuzzed(
+                self.host.bridge.quiz_game_player_buzzed(
                     room_jid.userhost(), elt["player"], elt["pause"] == str(True), profile
                 )
 
             elif elt.name == "player_says":
-                self.host.bridge.quizGamePlayerSays(
+                self.host.bridge.quiz_game_player_says(
                     room_jid.userhost(),
                     elt["player"],
                     str(elt),
@@ -440,15 +440,15 @@
 
             elif elt.name == "answer_result":
                 player, good_answer, score = self.__answer_result_to_signal_args(elt)
-                self.host.bridge.quizGameAnswerResult(
+                self.host.bridge.quiz_game_answer_result(
                     room_jid.userhost(), player, good_answer, score, profile
                 )
 
             elif elt.name == "timer_expired":
-                self.host.bridge.quizGameTimerExpired(room_jid.userhost(), profile)
+                self.host.bridge.quiz_game_timer_expired(room_jid.userhost(), profile)
 
             elif elt.name == "timer_restarted":
-                self.host.bridge.quizGameTimerRestarted(
+                self.host.bridge.quiz_game_timer_restarted(
                     room_jid.userhost(), int(elt["time_left"]), profile
                 )