Ejemplo n.º 1
0
    def tribute(self, iso, amount, user: User):
        """
        Country1 gives a certain amount of gold to other country
        """
        world = worlds.get(user.wid)

        if world.rounds < 0 or world.rounds == world.max_rounds:
            # world is not in playing state
            return

        # turns are not checked, as you can tribute at any time during the game!
        country1: Country = countries.get(user.iso, world.wid)
        country2: Country = countries.get(iso, world.wid)

        if not economy.give_tribute(country1, country2, amount):
            return

        countries.save_all([country1, country2])

        self.server.send_to_world(
            user.wid, {
                "route": self.name + ":tribute",
                "iso": user.iso,
                "to_iso": iso,
                "amount": amount
            })
Ejemplo n.º 2
0
    def join(self, wid, user, iso=None):
        world = worlds.get(wid)

        if not world:
            # world ceased to exist, kick user out
            user.wid = None
            user.iso = None
            users.set_world(user.uid, user.wid, user.iso)
            return

        clients = self.server.onlineMatches[str(world.wid)]
        players = {c.user.iso: c.user.to_dict() for c in clients}

        if world is None:
            return

        if not join_world(user, world, players, iso=iso):
            return

        self.server.client_enter_world(user.client)

        self.server.send_to_world(world.wid, {
            "route": "Worlds:joined",
            "user": user.to_dict(),
        })

        dd = world.to_dict()
        dd['players'] = players

        return {
            "world": dd,
            "user": user.to_dict(),
        }
Ejemplo n.º 3
0
    def force_turn(self):
        user = getUser()
        world = worlds.get(user.wid)

        country: Country = countries.get(world.current, world.wid)
        world_countries = world.countries
        dict_countries = OrderedDict((c.iso, c) for c in world_countries)

        if not country:
            # ???
            return

        try:
            round_end_events = turns.end_turn(world, country, dict_countries)
        except turns.TurnException as e:
            return {"err": e.reason}
        except turns.EndGameException as e:
            # fake end game: used for playtesting
            # we don't schedule the world to be deleted and don't give ratings
            return ApiResponse({
                "route": "Game:end_game",
                "winner": e.reason
            })

        worlds.save(world)

        return ApiResponse({
            "route": "Game:end_turn",
            "iso": country.iso,
            "turn_end": {
                "turns": world.turns,
                "current": world.current,
            },
            "round_end": round_end_events.to_dict() if round_end_events else None
        })
Ejemplo n.º 4
0
    def reset(self):
        user = getUser()
        world = worlds.get(user.wid)

        reset_world(world)

        return ApiResponse({})
Ejemplo n.º 5
0
    def run(self):
        wid = '00000000-0000-2000-a000-000000000000'

        # see if sess is cached
        world = worlds.get(wid)

        cc = world.countries

        print(1)
Ejemplo n.º 6
0
    def edit(self, patch: dict, user):
        # edit map + max_players
        # + deny if too many players
        world = worlds.get(user.wid)

        if set_map(world, patch['map']):
            self.server.send_to_world(world.wid, {
                "route": "Worlds:edit",
                "patch": patch,
            })
Ejemplo n.º 7
0
    def debug(self):
        user = getUser()
        if user.wid:
            world = worlds.get(user.wid)
        else:
            world = None

        return render_template('/client/debug.html',
                               conf=settings._conf,
                               world=world,
                               err=request.args.get('err'))
Ejemplo n.º 8
0
def _delete_all():
    users.delete_all(WID)

    w = worlds.get(WID)
    if w is not None:
        worlds.delete(w)
        #areas.delete_all(WID)
        #countries.delete_all(WID)

    h = histories.get(WID)
    if h is not None:
        histories.delete(h)
Ejemplo n.º 9
0
    def index(self):
        user = getUser()

        if user.uid == 'None':
            raise Exception("hifga i eeeeeeeeeeeeeeeeeeeee")

        if not user.wid:
            return redirect('/client/welcome')
        world = worlds.get(user.wid)

        return render_template('/client/index.html',
                               conf=settings._conf,
                               world=world,
                               err=request.args.get('err'))
Ejemplo n.º 10
0
    def load(self):
        user = getUser()

        world = worlds.get(user.wid)
        players = users.list_all(user.wid)
        _countries = world.countries
        _areas = world.areas

        return ApiResponse({
            "world": world.to_dict(),
            "countries": _countries,
            "areas": _areas,
            "players":
            {player.iso: player.to_game_view()
             for player in players},
        })
Ejemplo n.º 11
0
    def finish(self):
        world = worlds.get(self.WID)

        if world:
            l_users = users.list_all(world.wid)

            for user in l_users:
                users.set_world(user.uid, None, None)

            worlds.delete(world)

        history = histories.get(self.WID)

        if history:
            histories.delete(history)

        return ApiResponse({'result': 'OK'})
Ejemplo n.º 12
0
    def surrender(self, user: User):
        """
        Country surrenders from game (but does not quit it)
        """
        world = worlds.get(user.wid)

        if world.rounds < 0 or world.rounds == world.max_rounds:
            # world is not in playing state
            return

        country: Country = countries.get(user.iso, user.wid)
        country.shields = 0

        countries.save(country)

        if world.current == country.iso:
            return self.end_turn(user)

        self.server.send_to_world(user.wid, {
            "route": self.name + ":surrender",
            "iso": user.iso
        })
Ejemplo n.º 13
0
    def leave(self, user):
        world = worlds.get(user.wid)

        self.server.send_to_world(world.wid, {
            "route": "Worlds:left",
            "iso": user.iso,
        })

        self.server.client_leave_world(user.client)

        if settings.get('client.leave_world_on_disconnect'):
            user.wid = None
            user.iso = None
            users.set_world(user.uid, None, None)

        # delete match if it hasn't started yet
        if world.rounds == 0:
            clients = self.server.onlineMatches[str(world.wid)]
            if len(clients) == 0:
                print("DELETING MATCH", world.wid)
                worlds.delete(world)

        return {}
Ejemplo n.º 14
0
    def _accessControl(self, user, area_id=None, leave_country=False):
        if user.wid is None:
            return {"err": "not_in_match"}, None, None, None

        world = worlds.get(user.wid)

        if world.current != user.iso:
            return {"err": "not_your_turn"}, None, None, None

        if leave_country:
            country = None
        else:
            country = countries.get(user.iso, user.wid)

        if area_id is not None:
            area: Area = areas.get(area_id, user.wid)

            if area.iso != user.iso:
                return {"err": "not_your_area"}, None, None, None
        else:
            area = None

        return None, world, country, area
Ejemplo n.º 15
0
    def start(self):
        if not request.args['uids']:
            return ApiResponse({'result': False, 'no': -1})

        uids = request.args['uids'].split(',')
        world = worlds.get(self.WID)
        l_users = users.list(uids)

        # delete world
        if world is not None:
            worlds.delete(world)

            h = histories.get(self.WID)
            if h is not None:
                histories.delete(h)

        # create world
        world = World(wid=self.WID,
                      name="Playtest world",
                      map=self.MAP,
                      max_rounds=None)

        isos = load_isos('game/maps/{}.gtml'.format(world.map))
        if len(isos) < len(l_users):
            return ApiResponse({'result': False, 'no': len(isos)})

        # force-enter users:
        for user, iso in zip(l_users, isos):
            user.wid = world.wid
            user.iso = iso

        # Save everything and start the game
        worlds.save(world)
        start_world(world, AI=True)
        users.save_all(l_users)

        return ApiResponse({'result': 'OK'})
Ejemplo n.º 16
0
    def end_turn(self, user: User, timeout=None):
        if user.wid is None:
            return {"err": "not_in_match"}

        world = worlds.get(user.wid)

        if world.current != user.iso:
            # Someone else has requested the turn end
            # Check if the current user is out of its turn time:
            if not turns_serv.check_timeout(world, current_iso=timeout):
                # current user is still in time, this request is refused
                return {"err": "not_your_turn"}

        curr_country = countries.get(world.current, user.wid)

        world_countries = world.countries
        dict_countries = OrderedDict((c.iso, c) for c in world_countries)

        # if len(world.isos) < 2:
        #     return {"err": "waiting_for_players"}

        try:
            round_end_events = turns_serv.end_turn(world, curr_country,
                                                   dict_countries)
            winner_iso = None
        except turns_serv.EndGameException as e:
            winner_iso = e.reason
            round_end_events = e.events
        except turns_serv.TurnException as e:
            return {"err": e.reason}

        self.server.send_to_world(
            user.wid, {
                "route":
                self.name + ":end_turn",
                "iso":
                user.iso,
                "turn_end": {
                    "turns": world.turns,
                    "current": world.current,
                },
                "round_end":
                round_end_events.to_dict() if round_end_events else None
            })

        if winner_iso:
            winner = winner_iso if winner_iso != '-1' else None

            # create match history
            world_users = users.list_all(world.wid)
            endgame.create_match_history(world,
                                         world_users,
                                         world_countries,
                                         winner=winner)

            # change user ratings
            endgame.apply_rating(world_users, winner=winner)
            users.save_all(world_users)

            # this schedules the world to be deleted at a later time
            world.turns = -1

            self.server.send_to_world(user.wid, {
                "route": self.name + ":end_game",
                "winner": winner_iso
            })

        worlds.save(world)
Ejemplo n.º 17
0
def _load_map():
    w = worlds.get(WID)
    a = w.areas
    c = w.countries

    return c, a