|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import absolute_import, print_function, unicode_literals, division |
| 3 | + |
| 4 | +from collections import defaultdict |
| 5 | + |
| 6 | +class SupplyTracker(object): |
| 7 | + def add_to_units_alive(self,event,replay): |
| 8 | + try: ## see if the unit takes supply |
| 9 | + supplyCount = self.unit_name_to_supply[event.unit_type_name] |
| 10 | + new_unit = (supplyCount, event.unit_id) |
| 11 | + self.units_alive[event.control_pid].append(new_unit) |
| 12 | + total_supply = sum([x[0] for x in self.units_alive[event.control_pid]]) |
| 13 | + replay.players[event.control_pid-1].current_food_used[event.second]= total_supply |
| 14 | + print("SECOND",event.second, "Player",replay.players[event.control_pid-1],"SUPPLY",replay.players[event.control_pid-1].current_food_used[event.second]) |
| 15 | + except KeyError: |
| 16 | + try: ## see if the unit provides supply |
| 17 | + supply_gen_count = (self.supply_gen_unit[event.unit_type_name]) |
| 18 | + supply_gen_unit = (supply_gen_count,event.unit_id) |
| 19 | + self.supply_gen[event.control_pid].append(supply_gen_unit) |
| 20 | + total_supply_gen = sum([x[0] for x in self.units_alive[event.control_pid]]) |
| 21 | + replay.players[event.control_pid-1].current_food_made[event.second]= total_supply_gen |
| 22 | + except KeyError: |
| 23 | + print("Unit name {0} does not exist".format(event.unit_type_name)) |
| 24 | + return |
| 25 | + |
| 26 | + def remove_from_units_alive(self,event,replay): |
| 27 | + died_unit_id = event.unit_id |
| 28 | + for player in replay.player: |
| 29 | + dead_unit = filter(lambda x:x[1]==died_unit_id, self.units_alive[player]) |
| 30 | + if dead_unit: |
| 31 | + self.units_alive[player].remove(dead_unit[0]) |
| 32 | + total_supply = sum([x[0] for x in self.units_alive[player]]) |
| 33 | + replay.players[player-1].current_food_used[event.second] = total_supply |
| 34 | + print("Second", event.second, "Killed", event.unit.name,"SUPPLY",replay.players[player-1].current_food_used[event.second]) |
| 35 | + |
| 36 | + dead_supply_gen = filter(lambda x:x[1]==died_unit_id, self.supply_gen[player]) |
| 37 | + if dead_supply_gen: |
| 38 | + self.supply_gen[player].remove(dead_supply_gen[0]) |
| 39 | + total_supply_gen = sum([x[0] for x in self.supply_gen[player]]) |
| 40 | + replay.players[player-1].current_food_made[event.second] = total_supply_gen |
| 41 | + print("Second", event.second, "Killed", event.unit.name,"SUPPLY",replay.players[player-1].current_food_made[event.second]) |
| 42 | + |
| 43 | + def handleInitGame(self, event, replay): |
| 44 | + ## This dictionary contains te supply of every unit |
| 45 | + self.unit_name_to_supply = { "Drone":1,"Zergling":1,"Baneling":1,\ |
| 46 | + "Queen":2,\ |
| 47 | + "SCV":1,"Marine":1,"Marauder":2,"SiegeTank":2} |
| 48 | + self.supply_gen_unit = {"Overlord":8, "SupplyDepot":8,"Pylon":8} |
| 49 | + ## This list contains a turple of the units supply and unit ID. |
| 50 | + self.units_alive = dict() |
| 51 | + self.supply_gen = dict() |
| 52 | + for player in replay.players: |
| 53 | + self.supply_gen[player.pid] = list() |
| 54 | + self.units_alive[player.pid] = list() |
| 55 | + player.current_food_used = defaultdict(int) |
| 56 | + player.current_food_made = defaultdict(int) |
| 57 | + player.time_supply_capped = int() |
| 58 | + |
| 59 | + def handleUnitInitEvent(self,event,replay): |
| 60 | + print ("Init",event.unit_type_name, event.unit_id) |
| 61 | + self.add_to_units_alive(event,replay) |
| 62 | + |
| 63 | + def handleUnitBornEvent(self,event,replay): |
| 64 | + print ("Born",event.unit_type_name,event.unit_id) |
| 65 | + self.add_to_units_alive(event,replay) |
| 66 | + |
| 67 | + def handleUnitDiedEvent(self,event,replay): |
| 68 | + if event.unit.name not in self.unit_name_to_supply: |
| 69 | + return |
| 70 | + self.remove_from_units_alive(event,replay) |
| 71 | + |
| 72 | + def handleEndGame(self, event, replay): |
| 73 | + pass |
0 commit comments