Skip to content

Instantly share code, notes, and snippets.

@jamesona

jamesona/infer stats.html

Last active Feb 27, 2020
Embed
What would you like to do?
<html>
<style>
html, body {
margin: 0;
padding: 0;
background: #ccc;
font-family: sans-serif;
}
h1, #input {
font-size: 48px;
text-align: center;
}
#input {
display: block;
margin: 1rem auto;
padding: 3% 0;
width: 40%;
border: 1px dashed black;
border-radius: 1rem;
background: #fff;
}
players {
display: flex;
flex-flow: row wrap;
justify-content: space-around;
}
players player {
background: #999;
margin-top: 10px;
padding: 3px;
border-radius: 3px;
}
player > * {
padding: 5px;
margin: 0;
}
player > * > * {
margin: 1px;
text-transform: capitalize;
list-style: none;
display: flex;
justify-content: space-between;
}
</style>
<body>
<h1>Paste spy text here</h1>
<div contenteditable="" id="input">
</div>
<players></players>
</body>
<script>
'use strict'
const players = {}
const assignStats = ({ id, name, strength, speed, dexterity, defense, total }) => ({ id, name, strength, speed, dexterity, defense, total })
const numberWithCommas = n => n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
const numberFromStringWithCommas = n => Number(n.replace(/[,$]/g, ''))
const updatePlayer = player => {
const {id, name, strength, speed, dexterity, defense, total} = player
const prevPlayer = assignStats(players[id] || {})
players[id] = inferRemainingStat({
id,
name,
strength: strength || prevPlayer['strength'],
speed: speed || prevPlayer['speed'],
dexterity: dexterity || prevPlayer['dexterity'],
defense: defense || prevPlayer['defense'],
total: total || prevPlayer['total']
})
}
const parsePlayers = text => text.split(/\r\n\r\n|\n\n/).map(text => text
.toLocaleLowerCase()
.split(/\r?\n/)
.filter(row => !(/^you managed/.test(row)))
.filter(row => !(/^information/.test(row)))
.reduce((info, row) => {
const [key, value] = row.split(':')
if (key && value) {
if (/n\/a/.test(value)) {
info[key] = undefined
} else {
info[key] = value
}
}
return info
}, {})
)
const inferRemainingStat = player => {
const battleStats = ['strength', 'speed', 'dexterity', 'defense']
if (battleStats.filter(stat => player[stat] === undefined).length === 1) {
const playerCopy = { ...player }
const definedStats = battleStats.filter(stat => playerCopy[stat] !== undefined)
const missingStat = battleStats.find(stat => playerCopy[stat] === undefined)
playerCopy[missingStat] = numberWithCommas(
definedStats.reduce(
(remaining, stat) => remaining - numberFromStringWithCommas(player[stat]),
numberFromStringWithCommas(player.total)
)
)
return playerCopy
}
return player
}
const setNameAndID = player => {
const playerCopy = {...player}
const [name, id] = player.name.split(' ')
playerCopy.name = name
playerCopy.id = id
return playerCopy
}
const buildPlayerTable = player => {
const element = document.createElement('div')
Object.keys(player).forEach(prop => {
const item = document.createElement('p')
switch (prop) {
case 'id':
break
case 'name': {
const {name, id} = player
item.innerHTML = `
<strong>${prop}:</strong><a
href="https://www.torn.com/profiles.php?XID=${id}#/"
target="_blank"
>${name}</a><span> ${id}</span>
`
break
}
default: {
item.innerHTML = `
<strong>${prop}:</strong> <span>${player[prop]}</span>
`
}
}
element.appendChild(item)
})
return element.outerHTML
}
const render = text => {
const playersContainer = document.querySelector('players')
playersContainer.innerHTML = ''
parsePlayers(text).map(setNameAndID).forEach(updatePlayer)
debugger
const elements = Object.values(players).map((player, index) => {
const element = document.createElement('player')
element.innerHTML = buildPlayerTable(player)
return element
})
elements.forEach(element => playersContainer.appendChild(element))
}
(() => {
const input = document.getElementById('input')
const listener = input.addEventListener('input', ({target}) => {
render(target.innerText)
target.innerHTML = ''
})
})()
</script>
</html>
Name:Arrowshot [2390783]
Level:49
You managed to get the following results:
Money on hand: $210,020
Strength: 8,001,584
Speed: 3,711,888
Dexterity: 31,341
Defense: 14,495,783
Total: 26,240,596
Information
Name:wilees1978 [2331077]
Level:48
You managed to get the following results:
Money on hand: $2,500
Strength: 2,600,583
Speed: 1,003,962
Dexterity: 595,523
Defense: 1,002,032
Total: 5,202,100
Information
Name:Roxy [2308983]
Level:50
You managed to get the following results:
Money on hand: $15,081
Strength: 2,147,294
Speed: 2,298,982
Dexterity: 3,485,571
Defense: 1,024,494
Total: 8,956,341
Name:Sass [2299647]
Level:60
You managed to get the following results:
Money on hand: $3,490,521
Strength: 1,252,248
Speed: 2,003,246
Dexterity: 1,005,245
Defense: 1,890,708
Total: 6,151,446
Information
Name:SMOrc [2289470]
Level:47
You managed to get the following results:
Money on hand: $1,054,619
Strength: 33,879,917
Speed: 50,033,002
Dexterity: 37,840,887
Defense: 61,010,636
Total: 182,764,442
Name:vDOOMv [2284975]
Level:49
You managed to get the following results:
Money on hand: $5,514
Strength: 613,727
Speed: N/A
Dexterity: 402,615
Defense: 141,597,898
Total: 143,068,752
Name:Instagram- [2280532]
Level:56
You managed to get the following results:
Money on hand: $20,292
Strength: 3,006,985
Speed: 3,092,538
Dexterity: 228,777
Defense: 102,134,649
Total: 108,462,949
Name:AiR [2251655]
Level:66
You managed to get the following results:
Money on hand: $1,021,919
Strength: N/A
Speed: 128,090,461
Dexterity: 62,930,826
Defense: 130,262,502
Total: 471,341,783
Name:siuwandy [2249291]
Level:60
You managed to get the following results:
Strength: 50,445,566
Strength: N/A
Speed: 50,465,546
Dexterity: 11,355,055
Defense: 116,015,991
Total: 228,282,158
Name:Deathbychoco [2243656]
Level:51
You managed to get the following results:
Money on hand: $6,747
Strength: 1,959,198
Speed: N/A
Dexterity: 1,004,905
Defense: 1,057,190
Total: 5,097,799
Name:Dalzadar [2242727]
Level:71
You managed to get the following results:
Money on hand: $0
Strength: 157,460,744
Speed: 99,866,578
Dexterity: 160,952,067
Defense: 322,829,959
Total: 741,109,348
Name:BloodIzuh [2242705]
Level:61
You managed to get the following results:
Money on hand: $10,458
Strength: 7,283,748
Speed: 201,655
Dexterity: N/A
Defense: 123,240,632
Total: 130,733,407
Name:-Jay- [2234445]
Level:15
You managed to get the following results:
Money on hand: $1,223,781
Strength: 100,548,911
Speed: 100,285,871
Dexterity: N/A
Defense: 100,071,748
Total: 390,140,828
Name:Tyraon [2202666]
Level:55
You managed to get the following results:
Money on hand: $1,500
Strength: 2,000,010
Speed: N/A
Dexterity: 23,494,825
Defense: 2,000,011
Total: 28,971,730
Name:EffingAwesome [2200587]
Level:63
You managed to get the following results:
Money on hand: $35,399
Strength: 128,913,623
Speed: 139,895,575
Dexterity: N/A
Defense: 52,811,848
Total: 445,679,898
Name:Theory [2190956]
Level:66
You managed to get the following results:
Money on hand: $4,747,534
Strength: 64,622,017
Speed: 70,035,465
Dexterity: 80,556,183
Defense: N/A
Total: 266,231,922
Name:Who_knows_who [2176999]
Level:30
You managed to get the following results:
Money on hand: $69,405
Strength: 96,306,921
Speed: N/A
Dexterity: 181,701,579
Defense: 110,080,945
Total: 517,230,376
Name:WickedPrincess [2176686]
Level:45
You managed to get the following results:
Money on hand: $7,065
Strength: 157,009,467
Speed: 200,542,861
Dexterity: N/A
Defense: 250,446,895
Total: 708,071,124
Name:Archer [2172225]
Level:71
You managed to get the following results:
Money on hand: $7,827
Strength: 161,946,619
Speed: 52,646,371
Dexterity: 30,673,145
Defense: 7,011,227
Total: 252,277,362
Name:hemi [2165616]
Level:69
You managed to get the following results:
Money on hand: $0
Strength: 100,007,051
Speed: 100,497,884
Dexterity: 23,053,461
Defense: 100,014,659
Total: 323,573,055
Name:ElChapo [2157516]
Level:60
You managed to get the following results:
Money on hand: $346,383
Strength: 138,091,843
Speed: N/A
Dexterity: 100,003,661
Defense: 100,013,899
Total: 456,228,383
Name:Hodgely [2156125]
Level:44
You managed to get the following results:
Money on hand: $165,299
Strength: 152,635,499
Speed: 150,219,693
Dexterity: 246,351,766
Defense: N/A
Total: 699,347,762
Name:lPez [2153605]
Level:70
You managed to get the following results:
Money on hand: $18,563,730
Strength: 150,252,225
Speed: 146,993,680
Dexterity: 10,039,785
Defense: 192,334,458
Total: 499,620,148
Name:Lilypop [2149351]
Level:32
You managed to get the following results:
Money on hand: $671,679
Strength: 51,887,454
Speed: 17,623,455
Dexterity: 50,088,658
Defense: 50,280,224
Total: 169,879,790
Name:Teflon_Swan [2136279]
Level:77
You managed to get the following results:
Money on hand: $244,612
Strength: 249,022,442
Speed: 101,371,146
Dexterity: 100,212,348
Defense: N/A
Total: 550,734,910
Name:King [2138593]
Level:71
You managed to get the following results:
Money on hand: $961,952
Strength: 346,946,614
Speed: 258,003,996
Dexterity: 1,970,845
Defense: 361,405,420
Total: 968,326,875
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.