Project

Profile

Help

How to connect?
Download (3.57 KB) Statistics View on GitHub Reload from mirrored respository
| Branch: | Tag: | Revision:

github / src / world.py @ a5c338e8

1
# Copyright (c) 2016, LE GOFF Vincent
2
# All rights reserved.
3

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

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

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

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

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

    
29
"""This file contains the World class."""
30

    
31
import os
32
from textwrap import dedent
33

    
34
from configobj import ConfigObj
35

    
36
class World:
37

    
38
    """A class representing a World object.
39

40
    A world is a game (a server).  It conatins a hostname and a port
41
    and optionally characters.
42

43
    """
44

    
45
    def __init__(self, location):
46
        self.location = location
47
        self.name = ""
48
        self.hostname = ""
49
        self.port = 4000
50
        self.characters = {}
51
        self.settings = None
52

    
53
        # World's access to general data
54
        self.client = None
55
        self.engine = None
56
        self.sharp_engine = None
57

    
58
        # World's configuration
59
        self.macros = []
60
        self.triggers = []
61

    
62
    def __repr__(self):
63
        return "<World {} (hostname={}, port={})>".format(
64
                self.name, self.hostname, self.port)
65

    
66
    @property
67
    def path(self):
68
        return "worlds/" + self.location
69

    
70
    def load(self):
71
        """Load the config.set script."""
72
        from game import Level
73
        level = self.engine.level
74
        self.engine.level = Level.world
75
        path = self.path
76
        path = os.path.join(path, "config.set")
77
        if os.path.exists(path):
78
            file = open(path, "r")
79
            content = file.read()
80
            file.close()
81

    
82
            # Execute the script
83
            self.sharp_engine.execute(content)
84

    
85
        # Put the engine level back
86
        self.engine.level = level
87

    
88
    def save(self):
89
        """Save the world in its configuration file."""
90
        if not os.path.exists(self.path):
91
            os.mkdir(self.path)
92

    
93
        spec = dedent("""
94
            [connection]
95
                name = "unknown"
96
                hostname = "unknown.ext"
97
                port = 0
98
        """).strip("\n")
99

    
100
        if self.settings is None:
101
            self.settings = ConfigObj(spec.split("\n"))
102

    
103
        connection = self.settings["connection"]
104
        connection["name"] = self.name
105
        connection["hostname"] = self.hostname
106
        connection["port"] = self.port
107
        self.settings.filename = os.path.join(self.path, "options.conf")
108
        self.settings.write()
(8-8/8)