Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, {Component, Fragment} from 'react';
- import ControlItem from './components/control'
- import {Node, Tree} from "./utils/main";
- import PopUp from './components/popUp'
- import './style/style.scss';
- class App extends Component {
- constructor() {
- super();
- this.state = {
- choosenControl: "",
- cheatEnabled: false,
- bonusCount : 30,
- bangMode : false,
- twinMode : false,
- showC2a : false,
- dataTree : [],
- array: [
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 2, 0, 2, 0, 0, 4, 0, 0, 0, 3, 0, 3, 0, 0, 2, 0, 2, 0, 0, 4, 0, 0, 0, 3, 0, 3, 0, 0],
- [0, 0, 0, 2, 0, 0, 4, 4, 4, 0, 0, 0, 3, 0, 0, 0, 0, 2, 0, 0, 4, 4, 4, 0, 0, 0, 3, 0, 0, 0],
- [0, 0, 2, 0, 2, 0, 0, 4, 0, 0, 0, 3, 0, 3, 0, 0, 2, 0, 2, 0, 0, 4, 0, 0, 0, 3, 0, 3, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0],
- [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
- [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 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0],
- [0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0],
- [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
- [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
- [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- ],
- color: {
- 0 : 'rgba(15, 0, 9, 0.83)',
- 1 : 'rgba(255, 15, 167, 1)',
- 2 : 'rgba(157, 21, 253, 1)' ,
- 3 : 'rgba(118, 253, 21, 1)',
- 4 : 'rgba(21, 234, 253, 0.86)'
- }
- }
- }
- componentWillMount(){
- this.getDataFromListTree()
- }
- renderControl = () => {
- return Object.keys(this.state.color).map((item) => {
- let color = this.state.color[item];
- if (this.state.choosenControl === this.state.color[item]) {
- this.color = color;
- return <ControlItem id={item} key={item} color={color} value={item}
- handleControlClick={(e) => this.handleControlClick(e)} bgColor={color}/>
- }
- return <ControlItem id={item} key={item} color={color} value={item}
- handleControlClick={(e) => this.handleControlClick(e)}/>
- })
- };
- getDataFromListTree = () => {
- this.parse(this.treeBuilder(Tree, Node)) // array object with rowPos and cellPoss
- };
- treeBuilder = (Tree, Node) => {
- let result = new Tree(data);
- const data = this.state.array;
- function iterate(arr) {
- arr.map(function (item) {
- if (typeof(item) === 'object') {
- result._root.children.push(new Node(item));
- iterate(item)
- }
- })
- }
- iterate(data);
- return result;
- };
- cellList = (cellList) => {
- let count = 0;
- return cellList.map((item) => {
- count += 1;
- this.cellCount = count;
- return (
- <div className={item.active ? `grid__cell filled` : 'grid__cell'}
- key={count} data-color={this.state.color[item.cell]}
- onClick={(e) => this.handleCellClick(e)}
- data-rowpos={item.rowPos}
- data-cellpos={item.cellPos}
- data-cell={item.cell}
- style={item.active ? {backgroundColor: item.color} : null}>
- <span className={'placeholder'}>{item.cell}</span>
- </div>
- )
- })
- };
- parse = (data) => {
- let parseData = [];
- data._root.children.map((row, index) => {
- let rowIndex = index;
- return row.data.map((cell, index) => {
- return parseData.push({
- cell: cell,
- rowPos: rowIndex,
- cellPos: index,
- active: false,
- color: this.state.color[cell]
- })
- })
- });
- if(this.state.dataTree.toString() === parseData.toString()) {
- return parseData
- }
- this.setState({
- dataTree : parseData
- });
- // console.log(parseData);
- return parseData
- };
- handleControlClick = (e) => {
- let controlId = e.target.id;
- this.setState({choosenControl: this.state.color[controlId]});
- e.target.classList.toggle('active');
- };
- bangFillCell = (cellRow, cellPos, step) => {
- let tree = this.state.dataTree;
- if(this.state.bonusCount <= 0) {
- this.setState({showC2a: true});
- return false
- }
- if (step >= 1 && cellRow >= 0 && cellPos >= 0) {
- for (let i = 0; i < tree.length; i++) {
- let actualRowPos = tree[i].rowPos;
- let actualCellPos = tree[i].cellPos;
- if (cellRow === actualRowPos && cellPos === actualCellPos) {
- tree[i].active = true;
- this.setState({dataTree: tree});
- }
- if (cellRow === actualRowPos && cellPos + step === actualCellPos) {
- tree[i].active = true;
- this.setState({dataTree: tree});
- }
- if (cellRow === actualRowPos && cellPos - step === actualCellPos) {
- tree[i].active = true;
- this.setState({dataTree: tree});
- }
- if (cellRow - step === actualRowPos && cellPos === actualCellPos) {
- tree[i].active = true;
- this.setState({dataTree: tree});
- }
- if (cellRow - step === actualRowPos && cellPos + step === actualCellPos) {
- tree[i].active = true;
- this.setState({dataTree: tree});
- }
- if (cellRow - step === actualRowPos && cellPos - step === actualCellPos) {
- tree[i].active = true;
- this.setState({dataTree: tree});
- }
- if (cellRow + step === actualRowPos && cellPos === actualCellPos) {
- tree[i].active = true;
- this.setState({dataTree: tree});
- }
- if (cellRow + step === actualRowPos && cellPos + step === actualCellPos) {
- tree[i].active = true;
- this.setState({dataTree: tree});
- }
- if (cellRow + step === actualRowPos && cellPos - step === actualCellPos) {
- tree[i].active = true;
- this.setState({dataTree: tree});
- }
- }
- this.bangFillCell(cellRow , cellPos, step - 1);
- this.bangFillCell(cellRow -1, cellPos, step - 1);
- this.bangFillCell(cellRow +1, cellPos, step -1);
- this.bangFillCell(cellRow, cellPos-1, step - 1);
- this.bangFillCell(cellRow, cellPos+1, step -1);
- this.setState({bonusCount : this.state.bonusCount - 1})
- }
- };
- twinFillCell = (cellRow, cellPos, cellNum, steps) => {
- let tree = this.state.dataTree;
- if(this.state.bonusCount <= 0) {
- this.setState({showC2a: true});
- return false
- }
- if (steps >= 1) {
- for (let i = 0; i < tree.length; i++) {
- let actualRowPos = tree[i].rowPos;
- let actualCellPos = tree[i].cellPos;
- if (cellRow === actualRowPos && cellPos === actualCellPos && cellNum === tree[i].cell) {
- tree[i].active = true;
- this.setState({dataTree: tree});
- }
- if (cellRow === actualRowPos && cellPos - 1 === actualCellPos && cellNum === tree[i].cell) {
- tree[i].active = true;
- this.setState({dataTree: tree})
- }
- if (cellRow -1 === actualRowPos && cellPos -1 === actualCellPos && cellNum === tree[i].cell) {
- tree[i].active = true;
- this.setState({dataTree: tree})
- }
- if (cellRow -1 === actualRowPos && cellPos === actualCellPos && cellNum === tree[i].cell) {
- tree[i].active = true;
- this.setState({dataTree: tree})
- }
- if (cellRow -1 === actualRowPos && cellPos + 1 === actualCellPos && cellNum === tree[i].cell) {
- tree[i].active = true;
- this.setState({dataTree: tree})
- }
- if (cellRow === actualRowPos && cellPos + 1 === actualCellPos && cellNum === tree[i].cell) {
- tree[i].active = true;
- this.setState({dataTree: tree})
- }
- if (cellRow + 1 === actualRowPos && cellPos +1 === actualCellPos && cellNum === tree[i].cell) {
- tree[i].active = true;
- this.setState({dataTree: tree})
- }
- if (cellRow + 1 === actualRowPos && cellPos === actualCellPos && cellNum === tree[i].cell) {
- tree[i].active = true;
- this.setState({dataTree: tree})
- }
- if (cellRow+ 1 === actualRowPos && cellPos -1 === actualCellPos && cellNum === tree[i].cell) {
- tree[i].active = true;
- this.setState({dataTree: tree})
- }
- }
- this.twinFillCell(cellRow - 1, cellPos, cellNum, steps - 1);
- this.twinFillCell(cellRow + 1, cellPos, cellNum, steps - 1);
- this.twinFillCell(cellRow, cellPos - 1, cellNum, steps - 1);
- this.twinFillCell(cellRow, cellPos + 1, cellNum, steps - 1);
- this.twinFillCell(cellRow + 1, cellPos + 1, cellNum, steps - 1);
- this.twinFillCell(cellRow - 1, cellPos - 1, cellNum, steps - 1);
- this.setState({bonusCount : this.state.bonusCount - 1})
- }
- };
- fillCell = (cellRow , cellPos) => {
- this.setState({
- dataTree: this.state.dataTree.map((item) => {
- if (cellRow === item.rowPos && cellPos === item.cellPos) {
- item.active = true;
- }
- return item
- })
- })
- };
- handleCellClick = (e) => {
- let cell = e.target;
- let choosenNum = this.state.choosenControl;
- const clickedCellRow = Number(cell.getAttribute('data-rowpos'));
- const clickedCellPos = Number(cell.getAttribute('data-cellpos'));
- const cellNum = Number(cell.getAttribute('data-cell'));
- if (this.state.bangMode) {
- this.bangFillCell(clickedCellRow, clickedCellPos, 2)
- }
- if (this.state.twinMode) {
- this.twinFillCell(clickedCellRow, clickedCellPos, cellNum , 4)
- }
- if (choosenNum === cell.getAttribute('data-color')) {
- this.fillCell(clickedCellRow, clickedCellPos)
- }
- };
- handleCheatClick = (e) => {
- e.target.classList.toggle('active');
- if(!this.state.cheatEnabled) {
- this.setState({
- dataTree: this.state.dataTree.map((item) => {
- item.active = true;
- return item
- })
- });
- this.setState({cheatEnabled: !this.state.cheatEnabled});
- return false;
- }
- this.setState({cheatEnabled: !this.state.cheatEnabled});
- this.setState({
- dataTree: this.state.dataTree.map((item) => {
- item.active = false;
- return item
- })
- })
- };
- handleBangClick = (e) => {
- this.setState({
- bangMode : !this.state.bangMode,
- twinMode : false
- })
- };
- handleTwinClick = (e) => {
- this.setState({
- bangMode : false,
- twinMode : !this.state.twinMode
- })
- };
- handleCloser = (e) => {
- this.setState({showC2a : false})
- };
- render() {
- let bonusCount = this.state.bonusCount >= 0 ? this.state.bonusCount : 0;
- return (
- <Fragment>
- <div className="wrapper">
- <div className="inner-container">
- <div className="controls">
- {this.renderControl()}
- <div className="cheats">
- <a href="javascript:;" className={this.state.twinMode ? 'twins active' : 'twins'} onClick={(e) => this.handleTwinClick(e)}><span className={'bang-0'}>T</span><span className={'bang-1'}>W</span><span className={'bang-2'}>I</span><span className={'bang-3'}>N</span><span className={'bang-4'}>S</span></a>
- <span className={'bang-count'}>{` ${bonusCount}x `}</span>
- <a href="javascript:;" onClick={(e) => this.handleBangClick(e)} className={this.state.bangMode ? 'bang active' : 'bang'}>
- <span className={'bang-0'}>B</span><span className={'bang-1'}>A</span><span
- className={'bang-2'}>N</span><span className={'bang-3'}>G</span><span
- className={'bang-4'}>!</span></a>
- <a href="javascript:;" className={'cheat-btn'}
- onClick={(e) => this.handleCheatClick(e)}>cheat</a>
- </div>
- </div>
- <div className="grid">
- {this.cellList(this.state.dataTree)}
- </div>
- </div>
- </div>
- <PopUp className={this.state.showC2a ? 'pop-up active' : 'pop-up'} innerText="Sorry, you dont have more bonus items!" handleCloser={(e) => this.handleCloser(e)} />
- </Fragment>
- );
- }
- }
- export default App;
Add Comment
Please, Sign In to add comment