Leaner Battle Data for Splinterlands with JS Arrays


Leaner Battle data

This blog will explain a minor patch for the repository that I am going to release.
In a previous patch, I've removed gold property from cards which reduced the storage by 10 percent. Then I started thinking, what else can I chop off my older data.

Old Battle-dataNew, Lean Battle-data
Card stats as an objectNow they are in array entries, which can be turned to object easily with Object.entries([entries])
removed inactive, match_typeinactive matches will assert constraints on card options, not the cards themselves. & match_type won't affect the outcome('I hope So!!!)
~26 MB size~11 MB size (only half)

Code differences

replaced 'fs' with 'jsonfile', and node-fetch with async-get-json. Since we will handle only JSON files, I didn't like to handle the JSON parsing and stringing during operations. So getBattleHistory changed, but slightly.
extracting cards from teams has improved since we are capturing id and level as arrays. To that effect, removed cleanBattles, extractbattleinfo, extractmonsters, and replaced them with one function.

const __medusa=(m)=>(m.card_detail_id==194&&m.level<3)?17:m.card_detail_id; // medusa and elven mistress are same cards
const xtractTeam=(t)=>[...[t.summoner,...t.monsters].map(m=>[__medusa(m),m.level])]

Proxy to the winner

I learnt a new concept clled proxy.

      const winner = 'somePlayer';
      const won = new Proxy({winner},{get: (t,p,r)=>{//target,prop,reciever
        if(t.winner=='d'){return 'd'}
        return t.winner==p?'w':'l';

I created a proxy object which will take the winner, and return the verdict of the match from the target's perspective. If the player is not the winner, it will return 'l' for lost or draw, if the result was 'DRAW'. I could have used a normal tertiary statement. But when I read about this concept, I used it so I will understand it.

Next patch

By the next patch, I would completely try to use the above data for scores and elsewhere. So, my script will change in the future. I currently saved the above script as a new file in my repo.


Congratulations @azarmadr3! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You got more than 200 replies.
Your next target is to reach 300 replies.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP