43 lines
1.8 KiB
Markdown
43 lines
1.8 KiB
Markdown
# Two‑Pass Peer Assignment Algorithm
|
||
|
||
## Score function
|
||
|
||
```python
|
||
def score(row, j):
|
||
s = 0
|
||
name = row["name"].lower()
|
||
if name and name in j["hostname"]:
|
||
s += 3
|
||
elif j["hostname"] and j["hostname"] in name:
|
||
s += 2
|
||
if j["os"] and j["os"] == row["os"].lower():
|
||
s += 1
|
||
return s
|
||
```
|
||
|
||
## Pass 1 – name‑based match (score ≥ 2)
|
||
|
||
1. For each HTML row, scan all remaining JSON peers.
|
||
2. Compute `score(row, j)` as above.
|
||
3. Pick the peer with the highest score.
|
||
4. If `best_score ≥ 2`, mark the peer as used and attach its `online`, `last_handshake`, `version` to the row.
|
||
5. If no peer reaches `≥ 2`, the row gets `online: null, last_handshake: null, version: ""`.
|
||
|
||
## Pass 2 – unique OS match (only when exactly one row and one JSON peer share the OS)
|
||
|
||
1. After pass 1, collect remaining open rows and JSON peers.
|
||
2. For each row where `online` is still missing:
|
||
* Count how many open rows have the same `os` (lower‑cased).
|
||
* Count how many open JSON peers have the same `os`.
|
||
* If **both counts are exactly 1**, assign that peer to that row (score ≥ 1 is sufficient).
|
||
* Otherwise, skip the row – it will be filled with `online: null` later.
|
||
|
||
## Why two passes?
|
||
|
||
* A pure greedy name‑match can mis‑assign peers whose hostnames share substrings (e.g., `android-termux` vs `pixel`).
|
||
* Enforcing a 1‑to‑1 OS‑match in pass 2 guarantees that a peer is only claimed when it is the *sole* candidate for that OS, preventing double‑booking and the “pixel‑slot‑theft” bug that appeared in earlier versions.
|
||
|
||
## Local‑device handling
|
||
|
||
The current device (`android-termux`) is excluded from the JSON peer pool before assignment begins. On the HTML page it is displayed with the badge `lokal` and `online: –‑` to avoid it competing with real peers.
|