1 |
7efcc006
|
Vincent Le Goff
|
|
2 |
c63ff6dd
|
Vincent Le Goff
|
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
"""This file contains the Character class."""
|
30 |
|
|
|
31 |
|
|
import os
|
32 |
|
|
|
33 |
307d123d
|
Vincent Le Goff
|
from ytranslate import t
|
34 |
|
|
|
35 |
c63ff6dd
|
Vincent Le Goff
|
from log import character as logger
|
36 |
307d123d
|
Vincent Le Goff
|
from notepad import Notepad
|
37 |
c63ff6dd
|
Vincent Le Goff
|
from safe import Safe
|
38 |
|
|
|
39 |
|
|
class Character:
|
40 |
|
|
|
41 |
|
|
"""An object to represent a character from a world.
|
42 |
|
|
|
43 |
|
|
A world represents a game, with the information to reach the
|
44 |
|
|
server. A character, on the other hand, represents specific
|
45 |
|
|
information to one player in this world. Worlds may have no
|
46 |
|
|
character (the default). Characters can be set to keep login
|
47 |
|
|
information safely, like the username and password and additional
|
48 |
|
|
commands.
|
49 |
|
|
|
50 |
|
|
Characters may have their specific configuration, like their
|
51 |
|
|
specific set of aliases, macros or triggers.
|
52 |
|
|
|
53 |
|
|
"""
|
54 |
|
|
|
55 |
|
|
def __init__(self, world, location):
|
56 |
|
|
self.world = world
|
57 |
|
|
self.location = location
|
58 |
|
|
self.name = "unknown"
|
59 |
|
|
self.username = ""
|
60 |
|
|
self.password = ""
|
61 |
|
|
self.other_commands = ""
|
62 |
bdd3adcb
|
Vincent Le Goff
|
self.default = False
|
63 |
c63ff6dd
|
Vincent Le Goff
|
|
64 |
|
|
|
65 |
|
|
self.aliases = []
|
66 |
|
|
self.macros = []
|
67 |
|
|
self.triggers = []
|
68 |
307d123d
|
Vincent Le Goff
|
self.notepad = None
|
69 |
c63ff6dd
|
Vincent Le Goff
|
|
70 |
|
|
def __repr__(self):
|
71 |
|
|
return "<Character {} (world={}, location={})>".format(self.name,
|
72 |
|
|
self.world and self.world.name or "unknown", self.location)
|
73 |
|
|
|
74 |
|
|
def __str__(self):
|
75 |
|
|
return self.name
|
76 |
|
|
|
77 |
6d5cf9ef
|
Vincent Le Goff
|
@property
|
78 |
|
|
def path(self):
|
79 |
|
|
return os.path.join(self.world.path, self.location)
|
80 |
|
|
|
81 |
c63ff6dd
|
Vincent Le Goff
|
def create_safe(self):
|
82 |
|
|
"""Create a safe for this character."""
|
83 |
|
|
location = os.path.join(self.world.path, self.location)
|
84 |
|
|
if not os.path.exists(location):
|
85 |
|
|
logger.info("Try to create the {} location for a character".format(
|
86 |
|
|
repr(location)))
|
87 |
|
|
os.makedirs(location)
|
88 |
|
|
|
89 |
|
|
|
90 |
|
|
safe = Safe(file=os.path.join(location, ".passphrase"),
|
91 |
|
|
secret=os.path.join(location, "login"))
|
92 |
|
|
|
93 |
|
|
return safe
|
94 |
|
|
|
95 |
|
|
def load(self):
|
96 |
|
|
"""Load the encrypted configuration.
|
97 |
|
|
|
98 |
|
|
If present, it will be in {world}/{location}/{login}.
|
99 |
|
|
The passphrase will be in {world}/{location}/.passphrase .
|
100 |
|
|
|
101 |
|
|
"""
|
102 |
|
|
location = os.path.join(self.world.path, self.location)
|
103 |
|
|
|
104 |
|
|
|
105 |
|
|
safe = self.create_safe()
|
106 |
de07fb33
|
Vincent Le Goff
|
self.name = safe.retrieve("name", "")
|
107 |
c63ff6dd
|
Vincent Le Goff
|
self.username = safe.retrieve("username", "")
|
108 |
|
|
self.password = safe.retrieve("password", "")
|
109 |
|
|
self.other_commands = safe.retrieve("other_commands", "")
|
110 |
bdd3adcb
|
Vincent Le Goff
|
self.default = safe.retrieve("default", False)
|
111 |
c63ff6dd
|
Vincent Le Goff
|
|
112 |
|
|
def save(self):
|
113 |
|
|
"""Save the character."""
|
114 |
|
|
safe = self.create_safe()
|
115 |
|
|
safe.store("name", self.name)
|
116 |
|
|
safe.store("username", self.username)
|
117 |
|
|
safe.store("password", self.password)
|
118 |
|
|
safe.store("other_commands", self.other_commands)
|
119 |
bdd3adcb
|
Vincent Le Goff
|
safe.store("default", self.default)
|
120 |
307d123d
|
Vincent Le Goff
|
|
121 |
|
|
def open_notepad(self):
|
122 |
|
|
"""Open and return the notepad associated to this character."""
|
123 |
|
|
if self.notepad:
|
124 |
|
|
return self.notepad
|
125 |
|
|
|
126 |
|
|
self.notepad = Notepad(self)
|
127 |
|
|
empty_string = t("ui.message.notepad.character_empty",
|
128 |
|
|
character=self.name, world=self.world.name)
|
129 |
|
|
self.notepad.open(empty_string)
|
130 |
|
|
return self.notepad |