优化选座方案

This commit is contained in:
dengtongyu committed 2018-12-16 00:10:09 +08:00
1 parent dc35bc25c4
commit 720e47773e
2 files changed
+154 -58

No files matched your search

+9 -3
View File
@@ -24,17 +24,23 @@
flex: 1; flex: 1;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
justify-content:center; justify-content:space-around;
align-items:center;
} }
.btn { .btn {
width: 100px; /* width: 100px; */
height: 30px;
}
.select-box>select {
height: 30px; height: 30px;
} }
.record { .record {
flex: 10; flex: 3;
overflow-y: auto; overflow-y: auto;
text-align: center;
} }
.group { .group {
+145 -55
View File
@@ -14,14 +14,18 @@ class App extends Component {
this.gap = 2; this.gap = 2;
this.group = 4; this.group = 4;
this.lines = (this.back - this.front) / this.gap; this.lines = (this.back - this.front) / this.gap;
this.rows = [];
this.seats = []; this.seats = [];
this.record = []; this.record = [];
this.state = { this.state = {
seatList: this.seats, seatList: this.seats,
record: this.record record: this.record,
seats:1
}; };
this.handleClickBtn = this.handleClickBtn.bind(this); this.handleClickBeginBtn = this.handleClickBeginBtn.bind(this);
this.handleClickRandomBtn = this.handleClickRandomBtn.bind(this);
this.onChooseSeats = this.onChooseSeats.bind(this);
} }
groupName(g) { groupName(g) {
@@ -47,6 +51,7 @@ class App extends Component {
let index = 0; let index = 0;
for (let g = 0; g < this.group; g++) { for (let g = 0; g < this.group; g++) {
for (let line = this.lines; line >= 0; line--) { for (let line = this.lines; line >= 0; line--) {
let rowArray = [];
for (let seat = 0; seat < (this.back - this.gap * (this.lines - line)); seat++) { for (let seat = 0; seat < (this.back - this.gap * (this.lines - line)); seat++) {
let item = { let item = {
index:index++, index:index++,
@@ -56,9 +61,11 @@ class App extends Component {
row: line, row: line,
disable:false disable:false
}; };
rowArray.push(item);
this.seats.push(item); this.seats.push(item);
//console.log(item) //console.log(item)
} }
this.rows.push(rowArray);
}; };
} }
@@ -66,6 +73,7 @@ class App extends Component {
seatList: this.seats seatList: this.seats
}) })
//console.log(this.state.seatList) //console.log(this.state.seatList)
//console.log(this.rows)
} }
componentDidUpdate() { componentDidUpdate() {
@@ -109,50 +117,113 @@ class App extends Component {
return input; return input;
} }
randomSeat() { randomOne() {
let haveSeats = this.seats.filter((item) => { let haveSeats = this.seats.filter((item) => {
return !item.disable; return !item.disable;
}); });
let index = Math.floor(Math.random() * haveSeats.length); if(haveSeats.length > 0) {
return haveSeats[index]; let index = Math.floor(Math.random() * haveSeats.length);
return haveSeats[index];
}else{
return null;
}
} }
selectSeats(seat, length) { randomPlace(length) {
let rowItems = this.seats.filter((item) => { if(!this.randomOne()) {
return seat.row == item.row && seat.group == item.group; this.record = this.record.concat({
}); result:-1,
let haveSeats = rowItems.filter((item) => { length:length,
return !item.disable; list:false,
}); desc:'已经没有座位了'
});
this.setState({
record: this.record
});
if(haveSeats.length < length) return {result:false,length:length,place:seat,list:[]}; return;
}
let list = null; let haveSeats = this.suitableRows(length);
if(haveSeats) {
haveSeats.forEach((value,key) => { let index = Math.floor(Math.random() * haveSeats.length);
let end = key + length; let places = this.selectPlace(haveSeats[index], length);
if(end <= haveSeats.length) { if(places.length > 0) {
let selects = haveSeats.slice(key, end); this.record = this.record.concat({
let flag = false; result:1,
selects.forEach((val, k) => { length:length,
if(k + 1 <= selects.length - 1) { list:places
if(val.number + 1 == selects[k + 1].number) {
flag = true;
}else{
flag = false;
}
}
}); });
if(flag){ this.setSeats(places);
list = selects;
return;
}
}else{ }else{
return; this.record = this.record.concat({
result:0,
length:length,
list:false,
desc:'返回的连号座位数组为空'
});
}
}else{
this.record = this.record.concat({
result:0,
length:length,
list:false,
desc:'没有符合座位数的连号座位'
});
}
this.setState({
record: this.record
})
}
suitableRows(length) {
let haveSeatRows = [];
this.rows.forEach((list) => {
let haveSeatList = list.filter((item) => {
return !item.disable;
});
if(haveSeatList.length <= 0) return false;
if(this.selectPlace(haveSeatList,length).length > 0) {
haveSeatRows.push(haveSeatList);
} }
}); });
if(haveSeatRows.length > 0) {
return haveSeatRows;
}else{
return false;
}
}
return {result:true,length:length,place:seat,list:list}; selectPlace(list, length) {
let places = [];
if(length == 1) {
places.push(this.randomOne());
return places;
}
for(let i = 0;i < list.length - 1;i++) {
let end = i + length;
if(end > list.length) break;
let selects = list.slice(i, end);
let flag = false;
selects.forEach((val, k) => {
if(k + 1 <= selects.length - 1) {
if(val.number + 1 == selects[k + 1].number) {
flag = true;
}else{
flag = false;
}
}
});
if(flag) {
places = selects;
break;
}
}
return places;
} }
setSeats(seats) { setSeats(seats) {
@@ -162,38 +233,45 @@ class App extends Component {
this.setState({ this.setState({
seatList: this.seats seatList: this.seats
}) })
console.log(this.state.seatList); //console.log(this.state.seatList);
}
outSeats(seats) {
let str = '';
seats.forEach((value) => {
str = str + (value.groupName + value.row + '-' + value.number) + ',';
});
return str;
} }
renderRecord(list) { renderRecord(list) {
let notes = []; let notes = [];
list.forEach((data,key) => { list.forEach((data,key) => {
if(data.result) { if(data.result == 1) {
notes.push(<p key={key}>{'本次抽取'+ data.length +'个座位,随机到'+ data.place.groupName + '区第' + data.place.row + '排'}</p>); notes.push(<p key={key}>{'本次抽取'+ data.length +'个座位,随机选中位置:' + this.outSeats(data.list)}</p>);
}else if(data.result == 0){
notes.push(<p key={key}>{'本次抽取'+ data.length +'个座位,但场内剩余的连号位置不足!请修改人数!'}</p>);
}else{ }else{
notes.push(<p key={key}>{'本次抽取'+ data.length +'个座位,随机到'+ data.place.groupName + '区第' + data.place.row + '排,但剩余的连号位置不足!'}</p>); notes.push(<p key={key}>{'所有座位已经选完!!'}</p>);
} }
}); });
return notes; return notes;
} }
begin() { handleClickBeginBtn() {
let length = this.randomInput(); let length = this.state.seats;
let seat = this.randomSeat(); this.randomPlace(length);
let data = this.selectSeats(seat, length);
let selects = data.list;
this.record = this.record.concat(data);
if(data.result && selects instanceof Array && selects.length > 0) {
this.setSeats(selects);
}
this.setState({
record: this.record
})
} }
handleClickBtn() { handleClickRandomBtn() {
this.begin(); let length = this.randomInput();
this.randomPlace(length);
}
onChooseSeats(event) {
this.setState({
seats: event.target.value
});
} }
render() { render() {
@@ -201,7 +279,19 @@ class App extends Component {
<div className="app"> <div className="app">
<div className="seats">{this.renderGroup(this.state.seatList)}</div> <div className="seats">{this.renderGroup(this.state.seatList)}</div>
<div className="ctrol"> <div className="ctrol">
<div className="btn-box"><button className="btn" onClick={this.handleClickBtn}>群众开始抢票</button></div> <div className="btn-box">
<div className="select-box">
<select defaultValue={this.state.seats} onChange={this.onChooseSeats}>
<option value={1}>1</option>
<option value={2}>2</option>
<option value={3}>3</option>
<option value={4}>4</option>
<option value={5}>5</option>
</select>
<button className="btn" onClick={this.handleClickBeginBtn}>开始抢票</button>
</div>
<button className="btn" onClick={this.handleClickRandomBtn}>随机开始抢票</button>
</div>
<div className="record" ref={(element) => this.element = element}>{this.renderRecord(this.state.record)}</div> <div className="record" ref={(element) => this.element = element}>{this.renderRecord(this.state.record)}</div>
</div> </div>
</div> </div>