Shaik Azhar Madar avatar

Leaner Battle Data for Splinterlands with JS Arrays

azarmadr3

Published: 03 Oct 2021 › Updated: 03 Oct 2021Leaner Battle Data for Splinterlands with JS Arrays

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!!!)
image.pngimage.png
~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.

Leave Leaner Battle Data for Splinterlands with JS Arrays to:

Written by

I am learning about Blockchain technology

Read more #hive-140217 posts


Best Posts From Shaik Azhar Madar

We have not curated any of azarmadr3's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Shaik Azhar Madar