Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - // The entry file of your WebAssembly module.
- import { JSON } from "json-as/assembly";
- import { quickSort } from "./sort";
- import { log } from "./myConsole";
- /**
- * This root funciton processes a full query result of all players into the
- * data that we need for the board
- */
- export function processPlayers ( allPlayerData: Array<PlayerData> ): Array<PlayerData> {
- if(allPlayerData.length){
- log(JSON.stringify(allPlayerData[0]));
- }
- let players = sortPlayersByField(allPlayerData);
- const playerDataForBoard: Array<PlayerData> = new Array<PlayerData>(0);
- for ( let i = 0; i < allPlayerData.length; ++i) {
- playerDataForBoard.push( processPlayer(allPlayerData[i]) );
- }
- return playerDataForBoard;
- };
- function comparePlayerData(a: PlayerData, b: PlayerData, field: string, sfield: string): i32 {
- log("We are comparing PlayerData")
- log("a")
- log(a.player_id.toString())
- log("b")
- log(b.player_id.toString())
- // --- Handle when the field isn't set or has no value
- if(!a[field] && !b[field]){
- return 0;
- }
- if(!a[field]){
- return 1;
- }
- if(!b[field]){
- return -1;
- }
- // --- Handle if they are not equal
- if (a[field] > b[field]){
- return -1;
- } else {
- return 1;
- }
- // --- If they are equal and there's no secondary field, it's even
- if (!secondaryField){
- return 0;
- }
- // --- If they're equal and there is a secondary field use it
- if(!a[secondaryField] && !b[secondaryField]){
- return 0;
- }
- if(!a[secondaryField]){
- return 1;
- }
- if(!b[secondaryField]){
- return -1;
- }
- return (a[field] > b[field]) ?
- -1 :
- (
- (a[field] < b[field]) ?
- 1 :
- 0
- );
- }
- /**
- * Sorts the member variable $players by the designated field
- * @param string $field Which field we want to sort by
- * @param string $secondaryField A secondary field to sort by
- * @return void
- */
- function sortPlayersByField (
- players: Array<PlayerData>,
- field: string = 'fantasy_points',
- secondaryField: string = ''
- ): Array<PlayerData>{
- quickSort<PlayerData>(players, comparePlayerData, field, secondaryField);
- // players.sort((p1, p2) => comparePlayerData(p1, p2, field, secondaryField));
- return players;
- }
- const processPlayer = function(playerData: PlayerData): PlayerData {
- return playerData;
- }
- export function test(): string {
- let array = new Array<PlayerData>(3);
- array[0] = new PlayerData();
- array[1] = new PlayerData();
- array[2] = new PlayerData();
- array[0].player_id = 1;
- array[1].player_id = 2;
- array[2].player_id = 3;
- array[0].rank = 5;
- array[1].rank = 4;
- array[2].rank = 6;
- log("Player id");
- log(array[0]["player_id"].toString());
- // assert(array[0].player_id == 1);
- for (let i = 0; i < array.length; i++) {
- log(array[i].player_id.toString());
- }
- sortPlayersByField(array, "player_id", "");
- for (let i = 0; i < array.length; i++) {
- log(array[i].player_id.toString());
- }
- let array2 = sortPlayersByField(array, "rank", "");
- for (let i = 0; i < array.length; i++) {
- log(array[i].player_id.toString());
- }
- for (let i = 0; i < array2.length; i++) {
- log(array2[i].player_id.toString());
- }
- }
- /**
- * Definition for all data for a player that gets returned by the query to get
- * all players in a league for the draft board
- */
- class PlayerData {
- [key: string]: any
- player_id: i32
- hasStats: boolean
- rank: i32
- extra: i32
- last_name: string
- first_name: string
- position: string
- fantasy_position: string
- team_abbr: string
- bye: i32
- average_adp_overall: i32
- round: i32
- pick: i32
- tierOnBoard: i32
- floor_points: i32
- consensus_points: i32
- fantasy_points: i32
- percent_low: i32
- percent_high: i32
- percent_high_odds: i32
- ceiling_points: i32
- weighted_ceiling_points: i32
- mock_pick_indicator: i32
- mvp_board_team_id: i32
- mvp_board_player_team_id: i32
- overall_pick_number: i32
- mvp_points: i32
- mvp_plus_points: i32
- userAdjustmentPercent: i32
- upside_points: i32
- upside_plus_points: i32
- flex_plus_points: i32
- upside_flex_plus_points: i32
- flag_create_time: string
- flag_order: i32
- injury_prob: i32
- positional_rank: i32
- positional_risk_group: i32
- sos_percent_diff: i32
- tip_text: string
- userComment: string
- constructor() {
- this.player_id = 0;
- this.hasStats = false;
- this.rank = 0
- this.extra = 0
- this.last_name = ""
- this.first_name = ""
- this.position = ""
- this.fantasy_position = ""
- this.team_abbr = ""
- this.bye = 0
- this.average_adp_overall = 0
- this.round = 0
- this.pick = 0
- this.tierOnBoard = 0
- this.floor_points = 0
- this.consensus_points = 0
- this.fantasy_points = 0
- this.percent_low = 0
- this.percent_high = 0
- this.percent_high_odds = 0
- this.ceiling_points = 0
- this.weighted_ceiling_points = 0
- this.mock_pick_indicator = 0
- this.mvp_board_team_id = 0
- this.mvp_board_player_team_id = 0
- this.overall_pick_number = 0
- this.mvp_points = 0
- this.mvp_plus_points = 0
- this.userAdjustmentPercent = 0
- this.upside_points = 0
- this.upside_plus_points = 0
- this.flex_plus_points = 0
- this.upside_flex_plus_points = 0
- this.flag_create_time = ""
- this.flag_order = 0
- this.injury_prob = 0
- this.positional_rank = 0
- this.positional_risk_group = 0
- this.sos_percent_diff = 0
- this.tip_text = ""
- this.userComment = ""
- }
- // @operator("[]")
- // foo(k: string): T {
- // var value = load<T>(changetype<usize>(this) + (<usize>index << alignof<T>()));
- // return value;
- // }
- @operator("[]")
- foo(k: string): i32 {
- if (k == "player_id") {return this.player_id;}
- else if (k == "hasStats") {return this.hasStats;}
- else if (k == "rank") {return this.rank;}
- else if (k == "extra") {return this.extra;}
- // else if (k == "last_name") {return this.last_name;}
- // else if (k == "first_name") {return this.first_name;}
- // else if (k == "position") {return this.position;}
- // else if (k == "fantasy_position") {return this.fantasy_position;}
- // else if (k == "team_abbr") {return this.team_abbr;}
- else if (k == "bye") {return this.bye;}
- else if (k == "average_adp_overall") {return this.average_adp_overall;}
- else if (k == "round") {return this.round;}
- else if (k == "pick") {return this.pick;}
- else if (k == "tierOnBoard") {return this.tierOnBoard;}
- else if (k == "floor_points") {return this.floor_points;}
- else if (k == "consensus_points") {return this.consensus_points;}
- else if (k == "fantasy_points") {return this.fantasy_points;}
- else if (k == "percent_low") {return this.percent_low;}
- else if (k == "percent_high") {return this.percent_high;}
- else if (k == "percent_high_odds") {return this.percent_high_odds;}
- else if (k == "ceiling_points") {return this.ceiling_points;}
- else if (k == "weighted_ceiling_points") {return this.weighted_ceiling_points;}
- else if (k == "mock_pick_indicator") {return this.mock_pick_indicator;}
- else if (k == "mvp_board_team_id") {return this.mvp_board_team_id;}
- else if (k == "mvp_board_player_team_id") {return this.mvp_board_player_team_id;}
- else if (k == "overall_pick_number") {return this.overall_pick_number;}
- else if (k == "mvp_points") {return this.mvp_points;}
- else if (k == "mvp_plus_points") {return this.mvp_plus_points;}
- else if (k == "userAdjustmentPercent") {return this.userAdjustmentPercent;}
- else if (k == "upside_points") {return this.upside_points;}
- else if (k == "upside_plus_points") {return this.upside_plus_points;}
- else if (k == "flex_plus_points") {return this.flex_plus_points;}
- else if (k == "upside_flex_plus_points") {return this.upside_flex_plus_points;}
- // else if (k == "flag_create_time") {return this.flag_create_time;}
- else if (k == "flag_order") {return this.flag_order;}
- else if (k == "injury_prob") {return this.injury_prob;}
- else if (k == "positional_rank") {return this.positional_rank;}
- else if (k == "positional_risk_group") {return this.positional_risk_group;}
- else if (k == "sos_percent_diff") {return this.sos_percent_diff;}
- // else if (k == "tip_text") {return this.tip_text;}
- // else if (k == "userComment") {return this.userComment;}
- return 0;
- }
- }
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment                    
                 
                    