Skip to content

Commit

Permalink
Bring #157: add the anti-idle feature
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-lg committed Jan 25, 2020
1 parent d38c1d2 commit 51965cb
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"""

import os
from random import randint
import re
import socket
from telnetlib import Telnet, WONT, WILL, ECHO, NOP, AYT, IAC, GA
Expand Down Expand Up @@ -71,6 +72,7 @@ def connectionMade(self):
self.has_GA = False
self.queue = b""
self.defer = None
self.anti_idle = None
host = self.transport.getPeer().host
port = self.transport.getPeer().port
log = logger("client")
Expand All @@ -83,8 +85,11 @@ def connectionMade(self):
for command in self.factory.commands:
self.transport.write(command.encode() + b"\r\n")

self.factory.stopTrying()

def connectionLost(self, reason):
"""The connection was lost."""
self.send_queue()
host = self.transport.getPeer().host
port = self.transport.getPeer().port
log = logger("client")
Expand Down Expand Up @@ -136,6 +141,33 @@ def handle_GA(self, *args, **kwargs):
self.applicationDataReceived(queue)
self.has_GA = True

def reverse_anti_idle(self, verbose=False, to_panel=False):
"""Reverse anti-idle."""
if self.anti_idle:
# Terminate
if verbose:
self.handle_message("Anti idle off.")
self.anti_idle.cancel()
self.anti_idle = None
if to_panel:
self.factory.panel.window.gameMenu.Check(
self.factory.panel.window.chk_anti_idle.GetId(), False)
else:
# Begin anti idle
if verbose:
self.handle_message("Anti idle on.")
self.anti_idle = reactor.callLater(1, self.keep_anti_idle)
if to_panel:
self.factory.panel.window.gameMenu.Check(
self.factory.panel.window.chk_anti_idle.GetId(), True)

def keep_anti_idle(self):
"""Keep the anti-idle active."""
self.transport.write(b"\r\n")
next_time = randint(30, 60)
next_time += randint(1, 100) / 100
self.anti_idle = reactor.callLater(next_time, self.keep_anti_idle)

def run(self):
"""Run the thread."""
# Try to connect to the specified host and port
Expand Down
2 changes: 2 additions & 0 deletions src/sharp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sharp.functions.alias import Alias
from sharp.functions.channel import Channel
from sharp.functions.feed import Feed
from sharp.functions.idle import Idle
from sharp.functions.macro import Macro
from sharp.functions.play import Play
from sharp.functions.randplay import RandPlay
Expand All @@ -17,6 +18,7 @@
"alias": Alias,
"channel": Channel,
"feed": Feed,
"idle": Idle,
"macro": Macro,
"play": Play,
"randplay": RandPlay,
Expand Down
47 changes: 47 additions & 0 deletions src/sharp/functions/idle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright (c) 2016-2020, LE GOFF Vincent
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:

# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.

# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.

# * Neither the name of ytranslate nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Module containing the idle function class."""

from sharp import Function

class Idle(Function):

"""Function SharpScript 'anti_idle'.
This function can be used to switch the anti-idle on or off for
this client.
"""

description = "Enable/Disable anti-idle"

def run(self):
"""Switch anti-idle."""
if self.client:
self.client.reverse_anti_idle(verbose=True, to_panel=True)
1 change: 1 addition & 0 deletions src/translations/en/sharp/idle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
description: Enable/Disable the anti-idle
1 change: 1 addition & 0 deletions src/translations/en/sharp/tts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
description: Enable/Disable TTS
1 change: 1 addition & 0 deletions src/translations/en/ui/menu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ notepad_world: "For this &world..."
notepad_character: "For this &character..."
character: Change this character's setting...
sounds: Play trigger sounds
anti_idle: Anti idle
clear_output: Clear the output window
connection: "&Connection"
disconnect: "&Disconnect from this world"
Expand Down
1 change: 1 addition & 0 deletions src/translations/es/sharp/idle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
description: Enable/Disable the anti-idle
1 change: 1 addition & 0 deletions src/translations/es/ui/menu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ notepad_world: "Para este &mundo..."
notepad_character: "Para el &personage..."
character: Configurar este personage...
sounds: Reproducir sonidos de disparadores
anti_idle: Anti idle
clear_output: Limpiar ventana de salida
connection: "&Conexión"
disconnect: "&Desconectar de este mundo"
Expand Down
1 change: 1 addition & 0 deletions src/translations/fr/sharp/idle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
description: Activer/Désactiver l'anti-idle
1 change: 1 addition & 0 deletions src/translations/fr/ui/menu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ notepad_world: "Pour cet &univers..."
notepad_character: "Pour ce &personnage..."
character: Changer les options de ce personnage...
sounds: Jouer les sons des triggers
anti_idle: Anti idle
clear_output: Nettoyer la fenêtre de contenu
connection: "&Connexion"
disconnect: "Se &déconnecter de cet univers"
Expand Down
14 changes: 13 additions & 1 deletion src/ui/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def CreateMenuBar(self):

# Differemtn menus
fileMenu = wx.Menu()
gameMenu = wx.Menu()
self.gameMenu = gameMenu = wx.Menu()
connectionMenu = wx.Menu()
toolsMenu = wx.Menu()
helpMenu = wx.Menu()
Expand Down Expand Up @@ -212,6 +212,12 @@ def CreateMenuBar(self):
gameMenu.Check(self.chk_sounds.GetId(), True)
self.Bind(wx.EVT_MENU, self.ToggleSounds, self.chk_sounds)

# Anti-idle
self.chk_anti_idle = gameMenu.Append(wx.ID_ANY, t("ui.menu.anti_idle"),
t("ui.menu.anti_idle"), kind=wx.ITEM_CHECK)
gameMenu.Check(self.chk_anti_idle.GetId(), False)
self.Bind(wx.EVT_MENU, self.ToggleAntiIdle, self.chk_anti_idle)

# Clear
clear = wx.MenuItem(gameMenu, -1, t("ui.menu.clear_output"))
self.Bind(wx.EVT_MENU, self.OnClear, clear)
Expand Down Expand Up @@ -439,6 +445,12 @@ def ToggleSounds(self, e):
"""Toggle the "play sounds" checkbox."""
self.engine.sounds = self.chk_sounds.IsChecked()

def ToggleAntiIdle(self, e):
"""Toggle the "play sounds" checkbox."""
panel = self.panel
if panel and panel.client:
panel.client.reverse_anti_idle(verbose=True)

def OnClear(self, e):
"""Force clear the output."""
if self.panel:
Expand Down

0 comments on commit 51965cb

Please sign in to comment.