make activity timer actually based on activity and add connection timer for connection timing information
Also optimize "register_location_checks", because I was editing it anyway
This commit is contained in:
		
							parent
							
								
									86e09f1dc2
								
							
						
					
					
						commit
						ffe67c7fa7
					
				| 
						 | 
					@ -47,12 +47,14 @@ class Client:
 | 
				
			||||||
        self.version = [0, 0, 0]
 | 
					        self.version = [0, 0, 0]
 | 
				
			||||||
        self.messageprocessor = ClientMessageProcessor(ctx, self)
 | 
					        self.messageprocessor = ClientMessageProcessor(ctx, self)
 | 
				
			||||||
        self.ctx = weakref.ref(ctx)
 | 
					        self.ctx = weakref.ref(ctx)
 | 
				
			||||||
 | 
					        ctx.client_connection_timers[self.team, self.slot] = datetime.datetime.now(datetime.timezone.utc)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async def disconnect(self):
 | 
					    async def disconnect(self):
 | 
				
			||||||
        ctx = self.ctx()
 | 
					        ctx = self.ctx()
 | 
				
			||||||
        if ctx:
 | 
					        if ctx:
 | 
				
			||||||
            await on_client_disconnected(ctx, self)
 | 
					            await on_client_disconnected(ctx, self)
 | 
				
			||||||
            ctx.clients.remove(self)
 | 
					            ctx.clients.remove(self)
 | 
				
			||||||
 | 
					            ctx.client_connection_timers[self.team, self.slot] = datetime.datetime.now(datetime.timezone.utc)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @property
 | 
					    @property
 | 
				
			||||||
    def wants_item_notification(self):
 | 
					    def wants_item_notification(self):
 | 
				
			||||||
| 
						 | 
					@ -86,7 +88,8 @@ class Context:
 | 
				
			||||||
        self.remaining_mode: str = remaining_mode
 | 
					        self.remaining_mode: str = remaining_mode
 | 
				
			||||||
        self.item_cheat = item_cheat
 | 
					        self.item_cheat = item_cheat
 | 
				
			||||||
        self.running = True
 | 
					        self.running = True
 | 
				
			||||||
        self.client_activity_timers = {}
 | 
					        self.client_activity_timers: typing.Dict[typing.Tuple[int, int], datetime.datetime] = {} # datatime of last new item check
 | 
				
			||||||
 | 
					        self.client_connection_timers: typing.Dict[typing.Tuple[int, int], datetime.datetime] = {} # datetime of last connection
 | 
				
			||||||
        self.client_game_state: typing.Dict[typing.Tuple[int, int], int] = collections.defaultdict(int)
 | 
					        self.client_game_state: typing.Dict[typing.Tuple[int, int], int] = collections.defaultdict(int)
 | 
				
			||||||
        self.er_hint_data: typing.Dict[int, typing.Dict[int, str]] = {}
 | 
					        self.er_hint_data: typing.Dict[int, typing.Dict[int, str]] = {}
 | 
				
			||||||
        self.commandprocessor = ServerCommandProcessor(self)
 | 
					        self.commandprocessor = ServerCommandProcessor(self)
 | 
				
			||||||
| 
						 | 
					@ -335,39 +338,41 @@ def get_remaining(ctx: Context, team: int, slot: int) -> typing.List[int]:
 | 
				
			||||||
    return sorted(items)
 | 
					    return sorted(items)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def register_location_checks(ctx: Context, team: int, slot: int, locations):
 | 
					def register_location_checks(ctx: Context, team: int, slot: int, locations):
 | 
				
			||||||
    ctx.client_activity_timers[team, slot] = datetime.datetime.now(datetime.timezone.utc)
 | 
					 | 
				
			||||||
    found_items = False
 | 
					    found_items = False
 | 
				
			||||||
    for location in locations:
 | 
					    new_locations = set(locations) - ctx.location_checks[team, slot]
 | 
				
			||||||
        if (location, slot) in ctx.locations:
 | 
					    if new_locations:
 | 
				
			||||||
            target_item, target_player = ctx.locations[(location, slot)]
 | 
					        ctx.client_activity_timers[team, slot] = datetime.datetime.now(datetime.timezone.utc)
 | 
				
			||||||
            if target_player != slot or slot in ctx.remote_items:
 | 
					        for location in new_locations:
 | 
				
			||||||
                found = False
 | 
					            if (location, slot) in ctx.locations:
 | 
				
			||||||
                recvd_items = get_received_items(ctx, team, target_player)
 | 
					                target_item, target_player = ctx.locations[(location, slot)]
 | 
				
			||||||
                for recvd_item in recvd_items:
 | 
					                if target_player != slot or slot in ctx.remote_items:
 | 
				
			||||||
                    if recvd_item.location == location and recvd_item.player == slot:
 | 
					                    found = False
 | 
				
			||||||
                        found = True
 | 
					                    recvd_items = get_received_items(ctx, team, target_player)
 | 
				
			||||||
                        break
 | 
					                    for recvd_item in recvd_items:
 | 
				
			||||||
 | 
					                        if recvd_item.location == location and recvd_item.player == slot:
 | 
				
			||||||
 | 
					                            found = True
 | 
				
			||||||
 | 
					                            break
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if not found:
 | 
					                    if not found:
 | 
				
			||||||
                    new_item = ReceivedItem(target_item, location, slot)
 | 
					                        new_item = ReceivedItem(target_item, location, slot)
 | 
				
			||||||
                    recvd_items.append(new_item)
 | 
					                        recvd_items.append(new_item)
 | 
				
			||||||
                    if slot != target_player:
 | 
					                        if slot != target_player:
 | 
				
			||||||
                        broadcast_team(ctx, team, [['ItemSent', (slot, location, target_player, target_item)]])
 | 
					                            broadcast_team(ctx,team, [['ItemSent', (slot, location, target_player, target_item)]])
 | 
				
			||||||
                    logging.info('(Team #%d) %s sent %s to %s (%s)' % (
 | 
					                        logging.info('(Team #%d) %s sent %s to %s (%s)' % (
 | 
				
			||||||
                    team + 1, ctx.player_names[(team, slot)], get_item_name_from_id(target_item),
 | 
					                        team + 1, ctx.player_names[(team, slot)], get_item_name_from_id(target_item),
 | 
				
			||||||
                    ctx.player_names[(team, target_player)], get_location_name_from_address(location)))
 | 
					                        ctx.player_names[(team, target_player)], get_location_name_from_address(location)))
 | 
				
			||||||
                    found_items = True
 | 
					                        found_items = True
 | 
				
			||||||
            elif target_player == slot:  # local pickup, notify clients of the pickup
 | 
					                elif target_player == slot:  # local pickup, notify clients of the pickup
 | 
				
			||||||
                if location not in ctx.location_checks[team, slot]:
 | 
					                    if location not in ctx.location_checks[team, slot]:
 | 
				
			||||||
                    for client in ctx.clients:
 | 
					                        for client in ctx.clients:
 | 
				
			||||||
                        if client.team == team and client.wants_item_notification:
 | 
					                            if client.team == team and client.wants_item_notification:
 | 
				
			||||||
                            asyncio.create_task(
 | 
					                                asyncio.create_task(
 | 
				
			||||||
                                send_msgs(client, [['ItemFound', (target_item, location, slot)]]))
 | 
					                                    send_msgs(client, [['ItemFound', (target_item, location, slot)]]))
 | 
				
			||||||
    ctx.location_checks[team, slot] |= set(locations)
 | 
					        ctx.location_checks[team, slot] |= new_locations
 | 
				
			||||||
    send_new_items(ctx)
 | 
					        send_new_items(ctx)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if found_items:
 | 
					        if found_items:
 | 
				
			||||||
        save(ctx)
 | 
					            save(ctx)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def save(ctx: Context):
 | 
					def save(ctx: Context):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue