Giter Club home page Giter Club logo

baseballteambuilder's Introduction

BUILD YOUR DREAM ROSTER

LIVE

  • Author: Zidian Lyu
  • Apr-2017
  • Credit to: Nick Gassmann && The Harker School

Overviews

Introduction

  • Page View

BuildForm

  • Page View

PrintForm

  • Page View

Features && Implementations

Framework

  • This project is built on React.js

  • Webpack dependencies

    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-1": "^6.24.1",
    "lodash": "^4.17.4",
    "react": "^15.5.4",
    "react-dom": "^15.5.4",
    "react-redux": "^5.0.4",
    "react-router": "^4.1.1",
    "redux": "^3.6.0",
    "webpack": "^2.4.1",
    "webpack-dev-server": "^2.4.2"

Page Structure

  • Homepage

    • loader

    • navbar

    • main content

      • intro section
      • build form section
      • print form section

Component Managements

  • Add event listener on DOM content

    document.addEventListener('DOMContentLoaded', () => {
      ReactDOM.render(
          <Root/>, document.getElementById('main'));
    });
  • Build Component By Part: Separate the table into parts to make the codes easily to read and update

    <PlayerInfo/>
    <TeamInfo/>
    <NumSelector/>
    ...

Logic and Tests

Scaling and time complexity matter

  • Due to the limitations to satisfy all the rules, the innings number should be limited by the player numbers selected

  • With that being said, player numbers, innings numbers and satisfy roles requirements can contradict to each other

    • therefore, in order to satisfy all the roles, I tested and set the upper bound to each playerNum and inningNum combination

    • for example, for 8 players to satisfy all the rules and guarantee the program works efficiently, there should have maximum 6 innings

Selector

  • Update the player number and inning number from user input

  • Pass Back to the parent elements

    • the pitcher('P') option should be excluded from the preferredPositions and avoidPositions options, because the user already have rights to identify each player should pitch or not
  • Update the parent state (props)

//trigger the update in option tag
<select
className="selectpicker"
data-style="btn-primary"
value={numPlayers()}
onChange={updateSelectPlayer}>
//child component
const updateSelectPlayer = (event) => {
    props.updateSelectPlayer(event.target.value);
}
//update the parent and related state
updateSelectPlayer = () => {
    return ((value) => {
        let playerLists = this.state.playerLists;
        playerLists = this.constructTeamLists(parseInt(value));
        this.setState({playerNum: value, playerLists: playerLists});
    });
}

Build Player Form

  • Assign Pitcher

    • if yes, then active the Inning Pitching

    • if no, return "Not Applicable"

    • Selection-exclude algorithm

      • update the selected pitching

      • update the available pitch innings stack

      • display the current selection at the top

      • put the remaining at the bottom and sort

      return ((newSelection) => {
          const newPlayers = this.state.playerLists;
          let newInnings = [...this.state.availablePitchInnings];
      
          if (newSelection !== 'Not Applicable') {
              newSelection = parseInt(newSelection);
              let oldIndex = this.state.availablePitchInnings.indexOf(newSelection);
              newInnings = [
                  ...newInnings.slice(0, oldIndex),
                  ...newInnings.slice(oldIndex + 1)
              ];
          }
      
          if (newPlayers[idx].selectedPitchInning !== 'Not Applicable') {
              newInnings.push(newPlayers[idx].selectedPitchInning);
          }
      
          newPlayers[idx].selectedPitchInning = newSelection;
      
          this.setState({playerLists: newPlayers, availablePitchInnings: newInnings.sort()});
      });
      • exclude taken options

  • Initialize the player form by assigning random preferred roles and avoid rules

this is achieve by random select role from array for each player exclude it from stack if picked to ensure no repeat can happen

  const allPositions = ['P','C',...];
  let prePos = allPositions[Math.floor(Math.random() * allPositions.length)];
  allPositions.splice(allPositions.indexOf(prePos), 1);
  • For Updates on specific position

    • Locate the change; return the index and content

    • update the state

  const fieldName = this.getPositionField(type);
  return ((fieldIdx, selection) => {
      const playerLists = this.state.playerLists;
      playerLists[rowIdx][`${fieldName}`][fieldIdx] = selection;
      this.setState({playerLists});
  })
  • Ensure the modified data is updated

    • update the playerLists in BuildForm

    • update according to the input data change

    • construct the team lists with player details

      updateSelectPlayer = () => {
          return ((value) => {
              let playerLists = this.state.playerLists;
              playerLists = this.constructTeamLists(parseInt(value));
              this.setState({playerNum: value, playerLists: playerLists});
          });
      }

Generate Print Form

  • Generate the form by using Backtracking Algorithm (simular to Sudoku)

    • keep tracking until satisfy all the rules (all return true)
  • handle basic rule

    • there shouldn't be any repetitive role in each inning

    • render the board, return false if find any repetitive

    for (let row = 0; row < props.playerLists.length; row++) {
        if (board[row][j] == c) {
            return false;
        }
    }
  • handle the minimum rule 1

    • Applying Pigeon Hole Principle:

    • satisfy the both infield and outfield minimum requirement without counting the Bench Player

    if (min1CheckIn > 1) {
        min1CheckIn = 2;
    }
    if (min1CheckOut > 0) {
        min1CheckOut = 1;
    }
    if (min1CheckIn + min1CheckOut < 3) {
        return false;
    }
  • handle the minimum rule 2

    • check the player's previous innings role

    • if find a role that have been assigned twice, return false and backtrack

    let min2Check = 0;
    for (let col = 0; col < props.innings.length; col++) {
        if (board[i][col] == c) {
            min2Check += 1;
        }
        if (min2Check === 2) {
            return false;
        }
    }
  • handle the option rule 1

    • if find two bench player, return false
    let opt1check = 0;
    for (let col = 0; col < props.innings.length; col++) {
        if (board[i][col] === 'BN') {
            opt1check += 1;
        }
        if (opt1check > 2) {
            return false;
        }
    }
  • handle the option rule 2

    • keep check on the current role and the previous, return false if both are BN
    for (let col = 1; col < props.innings.length; col++) {
        if (board[i][col] == 'BN' && board[i][col - 1] == 'BN') {
            return false;
        }
    }
  • handle the option rule 3

    • simular to rule 2, except for outfield
    for (let col = 1; col < props.innings.length; col++) {
        if (outfield.has(board[i][col]) && outfield.has(board[i][col - 1])) {
            return false;
        }
    }

Styling

  • Applied styling elements from Bootstrap

  • Designed page icon

  • Designed page loader and pick random random page-loader

          <div className="object-animation-one" id="first_object"></div>
          <div className="object-animation-one" id="second_object"></div>
          <div className="object-animation-one" id="third_object"></div>
          <div className="object-animation-one" id="forth_object"></div>
      loader[Math.floor(Math.random() * loader.length)]
  • Designed animation to give the user more guidelines

    the animation is done by implementing CSS:

    @-webkit-keyframes bounceRight {
      0%, 20%, 60%, 100% {
          -webkit-transform: translateY(0);
      }
      ...
    }
  • Read and hide component

    • assign component to variable and switch them base on condition

      // JS
      if (originForm !== "") {
        selector = "";
        buildOriginFormBtn = ...;
      }
      // HTML
      <div>
      {selector}
      {originForm}
      {buildOriginFormBtn}
      <div>

Future Implementations

Database

  • Apply MySQL or PostgreSQL to store the user information

User Authentication

  • The user can login or signup

Real Time News

  • Update the realtime news from the latest sport's report

baseballteambuilder's People

Contributors

zidianlyu avatar

Watchers

James Cloos avatar Victor Garcia avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.