Guest User

Untitled

a guest
Feb 11th, 2023
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.90 KB | None | 0 0
  1. // The entry file of your WebAssembly module.
  2. import { JSON } from "json-as/assembly";
  3. import { quickSort } from "./sort";
  4. import { log } from "./myConsole";
  5.  
  6. /**
  7. * This root funciton processes a full query result of all players into the
  8. * data that we need for the board
  9. */
  10. export function processPlayers ( allPlayerData: Array<PlayerData> ): Array<PlayerData> {
  11. if(allPlayerData.length){
  12. log(JSON.stringify(allPlayerData[0]));
  13. }
  14.  
  15. let players = sortPlayersByField(allPlayerData);
  16.  
  17. const playerDataForBoard: Array<PlayerData> = new Array<PlayerData>(0);
  18.  
  19. for ( let i = 0; i < allPlayerData.length; ++i) {
  20. playerDataForBoard.push( processPlayer(allPlayerData[i]) );
  21. }
  22.  
  23. return playerDataForBoard;
  24. };
  25.  
  26. function comparePlayerData(a: PlayerData, b: PlayerData, field: string, sfield: string): i32 {
  27. log("We are comparing PlayerData")
  28. log("a")
  29. log(a.player_id.toString())
  30. log("b")
  31. log(b.player_id.toString())
  32.  
  33. // --- Handle when the field isn't set or has no value
  34. if(!a[field] && !b[field]){
  35. return 0;
  36. }
  37. if(!a[field]){
  38. return 1;
  39. }
  40. if(!b[field]){
  41. return -1;
  42. }
  43.  
  44. // --- Handle if they are not equal
  45. if (a[field] > b[field]){
  46. return -1;
  47. } else {
  48. return 1;
  49. }
  50.  
  51. // --- If they are equal and there's no secondary field, it's even
  52. if (!secondaryField){
  53. return 0;
  54. }
  55.  
  56. // --- If they're equal and there is a secondary field use it
  57. if(!a[secondaryField] && !b[secondaryField]){
  58. return 0;
  59. }
  60. if(!a[secondaryField]){
  61. return 1;
  62. }
  63. if(!b[secondaryField]){
  64. return -1;
  65. }
  66.  
  67. return (a[field] > b[field]) ?
  68. -1 :
  69. (
  70. (a[field] < b[field]) ?
  71. 1 :
  72. 0
  73. );
  74. }
  75.  
  76. /**
  77. * Sorts the member variable $players by the designated field
  78. * @param string $field Which field we want to sort by
  79. * @param string $secondaryField A secondary field to sort by
  80. * @return void
  81. */
  82. function sortPlayersByField (
  83. players: Array<PlayerData>,
  84. field: string = 'fantasy_points',
  85. secondaryField: string = ''
  86. ): Array<PlayerData>{
  87.  
  88. quickSort<PlayerData>(players, comparePlayerData, field, secondaryField);
  89. // players.sort((p1, p2) => comparePlayerData(p1, p2, field, secondaryField));
  90. return players;
  91. }
  92.  
  93. const processPlayer = function(playerData: PlayerData): PlayerData {
  94. return playerData;
  95. }
  96.  
  97. export function test(): string {
  98. let array = new Array<PlayerData>(3);
  99.  
  100. array[0] = new PlayerData();
  101. array[1] = new PlayerData();
  102. array[2] = new PlayerData();
  103.  
  104. array[0].player_id = 1;
  105. array[1].player_id = 2;
  106. array[2].player_id = 3;
  107.  
  108. array[0].rank = 5;
  109. array[1].rank = 4;
  110. array[2].rank = 6;
  111.  
  112. log("Player id");
  113. log(array[0]["player_id"].toString());
  114.  
  115. // assert(array[0].player_id == 1);
  116.  
  117. for (let i = 0; i < array.length; i++) {
  118. log(array[i].player_id.toString());
  119. }
  120.  
  121. sortPlayersByField(array, "player_id", "");
  122.  
  123. for (let i = 0; i < array.length; i++) {
  124. log(array[i].player_id.toString());
  125. }
  126.  
  127. let array2 = sortPlayersByField(array, "rank", "");
  128.  
  129. for (let i = 0; i < array.length; i++) {
  130. log(array[i].player_id.toString());
  131. }
  132.  
  133. for (let i = 0; i < array2.length; i++) {
  134. log(array2[i].player_id.toString());
  135. }
  136. }
  137.  
  138. /**
  139. * Definition for all data for a player that gets returned by the query to get
  140. * all players in a league for the draft board
  141. */
  142. class PlayerData {
  143. [key: string]: any
  144. player_id: i32
  145. hasStats: boolean
  146. rank: i32
  147. extra: i32
  148. last_name: string
  149. first_name: string
  150. position: string
  151. fantasy_position: string
  152. team_abbr: string
  153. bye: i32
  154. average_adp_overall: i32
  155. round: i32
  156. pick: i32
  157. tierOnBoard: i32
  158. floor_points: i32
  159. consensus_points: i32
  160. fantasy_points: i32
  161. percent_low: i32
  162. percent_high: i32
  163. percent_high_odds: i32
  164. ceiling_points: i32
  165. weighted_ceiling_points: i32
  166. mock_pick_indicator: i32
  167. mvp_board_team_id: i32
  168. mvp_board_player_team_id: i32
  169. overall_pick_number: i32
  170. mvp_points: i32
  171. mvp_plus_points: i32
  172. userAdjustmentPercent: i32
  173. upside_points: i32
  174. upside_plus_points: i32
  175. flex_plus_points: i32
  176. upside_flex_plus_points: i32
  177. flag_create_time: string
  178. flag_order: i32
  179. injury_prob: i32
  180. positional_rank: i32
  181. positional_risk_group: i32
  182. sos_percent_diff: i32
  183. tip_text: string
  184. userComment: string
  185.  
  186. constructor() {
  187. this.player_id = 0;
  188. this.hasStats = false;
  189. this.rank = 0
  190. this.extra = 0
  191. this.last_name = ""
  192. this.first_name = ""
  193. this.position = ""
  194. this.fantasy_position = ""
  195. this.team_abbr = ""
  196. this.bye = 0
  197. this.average_adp_overall = 0
  198. this.round = 0
  199. this.pick = 0
  200. this.tierOnBoard = 0
  201. this.floor_points = 0
  202. this.consensus_points = 0
  203. this.fantasy_points = 0
  204. this.percent_low = 0
  205. this.percent_high = 0
  206. this.percent_high_odds = 0
  207. this.ceiling_points = 0
  208. this.weighted_ceiling_points = 0
  209. this.mock_pick_indicator = 0
  210. this.mvp_board_team_id = 0
  211. this.mvp_board_player_team_id = 0
  212. this.overall_pick_number = 0
  213. this.mvp_points = 0
  214. this.mvp_plus_points = 0
  215. this.userAdjustmentPercent = 0
  216. this.upside_points = 0
  217. this.upside_plus_points = 0
  218. this.flex_plus_points = 0
  219. this.upside_flex_plus_points = 0
  220. this.flag_create_time = ""
  221. this.flag_order = 0
  222. this.injury_prob = 0
  223. this.positional_rank = 0
  224. this.positional_risk_group = 0
  225. this.sos_percent_diff = 0
  226. this.tip_text = ""
  227. this.userComment = ""
  228. }
  229.  
  230. // @operator("[]")
  231. // foo(k: string): T {
  232. // var value = load<T>(changetype<usize>(this) + (<usize>index << alignof<T>()));
  233. // return value;
  234. // }
  235.  
  236. @operator("[]")
  237. foo(k: string): i32 {
  238. if (k == "player_id") {return this.player_id;}
  239. else if (k == "hasStats") {return this.hasStats;}
  240. else if (k == "rank") {return this.rank;}
  241. else if (k == "extra") {return this.extra;}
  242. // else if (k == "last_name") {return this.last_name;}
  243. // else if (k == "first_name") {return this.first_name;}
  244. // else if (k == "position") {return this.position;}
  245. // else if (k == "fantasy_position") {return this.fantasy_position;}
  246. // else if (k == "team_abbr") {return this.team_abbr;}
  247. else if (k == "bye") {return this.bye;}
  248. else if (k == "average_adp_overall") {return this.average_adp_overall;}
  249. else if (k == "round") {return this.round;}
  250. else if (k == "pick") {return this.pick;}
  251. else if (k == "tierOnBoard") {return this.tierOnBoard;}
  252. else if (k == "floor_points") {return this.floor_points;}
  253. else if (k == "consensus_points") {return this.consensus_points;}
  254. else if (k == "fantasy_points") {return this.fantasy_points;}
  255. else if (k == "percent_low") {return this.percent_low;}
  256. else if (k == "percent_high") {return this.percent_high;}
  257. else if (k == "percent_high_odds") {return this.percent_high_odds;}
  258. else if (k == "ceiling_points") {return this.ceiling_points;}
  259. else if (k == "weighted_ceiling_points") {return this.weighted_ceiling_points;}
  260. else if (k == "mock_pick_indicator") {return this.mock_pick_indicator;}
  261. else if (k == "mvp_board_team_id") {return this.mvp_board_team_id;}
  262. else if (k == "mvp_board_player_team_id") {return this.mvp_board_player_team_id;}
  263. else if (k == "overall_pick_number") {return this.overall_pick_number;}
  264. else if (k == "mvp_points") {return this.mvp_points;}
  265. else if (k == "mvp_plus_points") {return this.mvp_plus_points;}
  266. else if (k == "userAdjustmentPercent") {return this.userAdjustmentPercent;}
  267. else if (k == "upside_points") {return this.upside_points;}
  268. else if (k == "upside_plus_points") {return this.upside_plus_points;}
  269. else if (k == "flex_plus_points") {return this.flex_plus_points;}
  270. else if (k == "upside_flex_plus_points") {return this.upside_flex_plus_points;}
  271. // else if (k == "flag_create_time") {return this.flag_create_time;}
  272. else if (k == "flag_order") {return this.flag_order;}
  273. else if (k == "injury_prob") {return this.injury_prob;}
  274. else if (k == "positional_rank") {return this.positional_rank;}
  275. else if (k == "positional_risk_group") {return this.positional_risk_group;}
  276. else if (k == "sos_percent_diff") {return this.sos_percent_diff;}
  277. // else if (k == "tip_text") {return this.tip_text;}
  278. // else if (k == "userComment") {return this.userComment;}
  279. return 0;
  280. }
  281.  
  282. }
  283.  
Advertisement
Add Comment
Please, Sign In to add comment