Beispiel #1
0
 def get(self):
     self.future = AWaiters().subscribe(AWaiters.WAIT_FOR_CHAT_MESSAGES)
     while self.alive:
         kill = yield self.future
         print(kill)
         self.write(kill)
         self.flush()
Beispiel #2
0
 def get(self):
     while self.alive:
         self.future = AWaiters().subscribe(AWaiters.WAIT_FOR_TIMER)
         ctime = yield self.future
         if self.alive:
             self.write(ctime.encode())
             self.flush()
Beispiel #3
0
    def start_battle(self, p: Player):
        self.Crew = [
            Sectoid("Enemy {}".format(i)) for i in range(
                random.randint(self.data["min_enemies"],
                               self.data["max_enemies"]))
        ]
        i = 0
        for turn in self.battle_loop(p):
            # if turn != 1:
            yield gen.sleep(turn)
            if i != len(self.log):
                AWaiters().deliver(AWaiters.WAIT_FOR_BATTLE_EVENTS,
                                   str(self.log[i:]))
                i = len(self.log)

        AWaiters().deliver(AWaiters.WAIT_FOR_BATTLE_EVENTS, "Done.")
        self.spawn_event.ticks = 0
Beispiel #4
0
 def post(self):
     print(self.request.body)
     post_data = tornado.escape.json_decode(self.request.body)
     channel = post_data["channel"]
     message = post_data["message"]
     r = SharedData().send_message(self.current_user, channel, message)
     AWaiters().deliver(AWaiters.WAIT_FOR_CHAT_MESSAGES, r)
     self.write("{}")
     pass
Beispiel #5
0
 def get(self):
     rep = ""
     while self.alive and rep != "Done.":
         self.future = AWaiters().subscribe(AWaiters.WAIT_FOR_BATTLE_EVENTS)
         rep = yield self.future
         if not self.alive:
             return
         self.write(rep)
         self.flush()
Beispiel #6
0
    def tick(self):
        if self.status != ActionStatus.ACTIVE:
            return

        self.current_time += dt.timedelta(seconds=5)
        self.timeout = self.interval
        AWaiters().deliver(AWaiters.WAIT_FOR_TIMER,
                           self.current_time.strftime("%Y-%b-%d %H:%M:%S"))
        pass
Beispiel #7
0
 def add_object(self, o):
     if not o:
         return
     if type(o) is dict:
         mo = BaseMapObject()
         mo.load_from_JSON(o)
         o = mo
     self.objects[o.id] = o
     AWaiters().deliver(AWaiters.WAIT_FOR_MAP_OBJECTS, o)
     pass
Beispiel #8
0
    def tick(self):
        if self.status != ActionStatus.ACTIVE:
            return

        if self.ticks:
            self.ticks -= 1
        else:
            self.status = ActionStatus.FINISHED

        AWaiters().deliver(AWaiters.WAIT_FOR_MAP_OBJECTS, {})

        self.timeout = self.interval
        pass
Beispiel #9
0
    def tick(self):
        if self.status != ActionStatus.ACTIVE:
            return

        if self.ticks > 0:
            self.ticks -= 1

        if self.ticks == 0:
            self.status = ActionStatus.FINISHED
            return

        self.timeout = self.interval
        AWaiters().deliver(AWaiters.WAIT_FOR_BUNKER_EVENTS, True)
        pass
Beispiel #10
0
    def post(self):
        refresh_all = tornado.escape.json_decode(
            self.request.body)["refresh_all"]

        if refresh_all != 1:
            self.future = AWaiters().subscribe(AWaiters.WAIT_FOR_MAP_OBJECTS)
            yield self.future

        if not self.alive:
            return

        objects = self.current_user.CurrentSector.get_objects_on_map_json()
        self.write(objects)
        pass
Beispiel #11
0
    def post(self):
        npc_org = NPCOrganizations()
        player = Player(
            "Vincent", "Merle",
            SharedData().organizations[next(
                (org for org in SharedData().organizations
                 if SharedData().organizations[org].name == "X-COM"))],
            random.randint(1000000, 10000000),
            SharedData().get_default_sector())
        SharedData().add_online_player(self.session_id, player)
        AWaiters().deliver(AWaiters.WAIT_FOR_CHAT_PLAYERS, player)

        player.CurrentSector.add_object(player.MapObject)

        self.redirect("/")
        pass
Beispiel #12
0
    def post(self):
        print(self.request.body)
        channel = tornado.escape.json_decode(self.request.body)["channel"]
        refresh_all = tornado.escape.json_decode(
            self.request.body)["refresh_all"]
        if refresh_all != 1:
            self.future = AWaiters().subscribe(AWaiters.WAIT_FOR_CHAT_PLAYERS)
            yield self.future
            if not self.alive:
                return

        players = SharedData().get_players_on_channel(channel)
        res = '{ "players" : ['
        for token_id in players:
            player = SharedData().get_player_by_token(token_id)
            res += ',{"name": "%s"}' % player.Name
            pass
        res += "]}"
        self.write(res.replace("[,", "["))
        pass
Beispiel #13
0
    def post(self):
        self.future = AWaiters().subscribe(AWaiters.WAIT_FOR_CHAT_MESSAGES)
        msgs = yield self.future

        if not self.alive:
            return

        print(msgs)

        res = '{ "result": ['
        for channel in msgs:
            res += ', {"channel" : "%s", "messages" : [' % channel
            print(msgs[channel])
            for message in msgs[channel]:
                print(message)
                res += ', {"message" : ["%s", "%s", "%s"]}' % message
                pass
            res += "]}"
            pass
        res += "]}"
        res = res.replace("[,", "[")
        print(res)
        self.write(res)
        pass
Beispiel #14
0
 def tick(self):
     if self.status != ActionStatus.ACTIVE:
         return
     SharedData().spawn_combat_sites(CombatSite)
     AWaiters().deliver(AWaiters.WAIT_FOR_MAP_OBJECTS, {})
     self.timeout = self.interval
Beispiel #15
0
    def battle_loop(self, pl: Player):
        all_p = pl.Crew + self.Crew
        for a in all_p:
            a.x = random.randint(0, 20)
            a.y = random.randint(0, 20)

        print("Battle for {} started.".format(self.id))
        for i in range(3):
            yield 1

        rnd = 1
        AWaiters().deliver(AWaiters.WAIT_FOR_BATTLE_EVENTS,
                           "Battle for {} started.".format(self.id))
        while len([x for x in pl.Crew if x.health]) > 0 and len(
            [y for y in self.Crew if y.health]) > 0:
            shooters = sorted([
                p for p in all_p if p.timeout == 0 and
                p.time_units >= min([a.cost
                                     for a in p.right_arm.shots]) and p.health
            ],
                              key=lambda k: k.reaction,
                              reverse=True)
            next_timeout = 0
            if not shooters:
                timeouters = sorted([p for p in all_p if p.timeout > 0],
                                    key=lambda k: k.timeout,
                                    reverse=False)
                if timeouters:
                    next_timeout = timeouters[0].timeout
                    for p in timeouters:
                        p.timeout -= next_timeout
                else:
                    next_timeout = 1
            else:
                for p in shooters:
                    if not p.health:
                        continue
                    wep = p.right_arm
                    e = [x for x in all_p if x.team != p.team and x.health]

                    if not e:
                        continue

                    e = sorted(e,
                               key=lambda k: max([
                                   s.calc_shot_chance(
                                       wep.range,
                                       math.sqrt(
                                           math.pow(p.x - k.x, 2) + math.pow(
                                               p.y - k.y, 2)), p.accuracy)
                                   for s in wep.shots
                               ]),
                               reverse=True)[0]
                    dist = math.sqrt(
                        math.pow(p.x - e.x, 2) + math.pow(p.y - e.y, 2))
                    shot = sorted([s for s in wep.shots],
                                  key=lambda k: k.calc_shot_chance(
                                      wep.range, dist, p.accuracy),
                                  reverse=True)[0]
                    hit = wep.shoot(shot, dist, p.accuracy)
                    print(rnd, p.id, e.id, e.health, hit)
                    if hit:
                        e.health -= min([hit, e.health])

                    self.log.append("|".join([
                        str(rnd),
                        str(p.id),
                        str(e.id),
                        str(hit),
                        str(e.health)
                    ]))
                    p.time_units -= shot.cost
                    p.timeout = shot.calc_shot_timeout(p.speed)

            [pl.Crew.remove(a) for a in pl.Crew if not a.health]
            [self.Crew.remove(b) for b in self.Crew if not b.health]
            [all_p.remove(p) for p in all_p if not p.health]

            for p in all_p:
                p.time_units += p.TUps * next_timeout

            rnd += 1
            yield next_timeout
Beispiel #16
0
 def remove_object(self, o):
     self.objects[o.id] = None
     del self.objects[o.id]
     AWaiters().deliver(AWaiters.WAIT_FOR_MAP_OBJECTS, {})
     pass