Commit e80c34c5 authored by Viktor Velev's avatar Viktor Velev

Almost submitting bets

parent 6c63f4d0
......@@ -68,9 +68,7 @@ function getEventIDs() {
}
function getEventByID(eventID) {
console.log(eventID)
return new Promise((accept, reject) => {
console.log("???")
inst.getEventByID.call(eventID, {"from": acc}, function(err, res){
if(!err) {
accept(res);
......@@ -107,7 +105,6 @@ function getEventPlacedBetByIndex(eventID, betIndex) {
function getEventAvailableBets(eventID) {
return new Promise((accept, reject) => {
console.log(eventID)
inst.getEventAvailableBets.call(eventID, {"from": acc}, function(err, res){
if(!err) {
accept(res);
......@@ -132,13 +129,10 @@ function getEventPlacedBetsAmount(eventID) {
async function getEvents(){
let ids = await getEventIDs();
console.log(ids)
objects = []
for(let i=0; i<ids.length; i++) {
console.log("ssss")
let res = await getEventByID(ids[i]);
console.log(res)
objects.push(res);
}
......
......@@ -36,3 +36,7 @@
.active {
background-color: #EEE !important;
}
.bet-item:hover {
background-color: #EEE !important;
}
import React, { Component } from "react";
import { Container, Menu, Image, Dropdown, Header, Segment } from "semantic-ui-react"
import { Input, Button, Container, Menu, Image, Dropdown, Header, Segment } from "semantic-ui-react"
export default class BetPage extends Component {
state = {
fetching: false,
fetched: false,
events: [],
availableBets: [],
chosenBetID: "",
chosenBet: [],
amount: 0,
constructor(props) {
super(props)
this.state = {
fetching: false,
fetched: false,
sorted: false,
events: [],
availableBets: [],
availableTypes: [],
closedTypes: [],
chosenBetID: "",
chosenBet: [],
chosenEventID: [],
yourBet: {},
yourBetID: 0,
amount: 0,
}
this.getAllEvents()
}
betTypes = {
1 : {
name: "Match Results",
bets: [],
},
2: {
name: "Correct Score"
name: "Correct Score",
bets: []
},
3: {
name: "Total Score"
name: "Total Score",
bets: []
},
4: {
name: "Penalty in Match"
name: "Penalty in Match",
bets: []
}
}
handleChange(e, {name, value}) {
this.setState({ [name] : value })
if (name === "amount") {
this.state.yourBet.amount = value;
}
}
parseBetTypes(raw) {
if (raw.length > 0 ){
let raw0 = raw[0].toFixed()
let raw1 = raw[1].toFixed()
let rawBetTypes = [ raw0, raw1 ]
if (raw.length > 0){
let rawBetTypes = []
raw.forEach((betType) => {
rawBetTypes.push(betType.toFixed())
})
let betTypes = []
rawBetTypes.forEach((betType) => {
betTypes.push(this.betTypes[betType])
})
......@@ -43,9 +72,28 @@ export default class BetPage extends Component {
}
}
parseBet(raw) {
// amount
// betType
// values
// odds
// user
// number
let bet = {
id: raw[0].toFixed(),
amount: raw[1].toFixed(),
betType: this.parseBetTypes([raw[2]]),
values: [ raw[3][0].toFixed(), raw[3][1].toFixed() ],
odds: parseInt(raw[4].toFixed()) / 100, userAddress: raw[5],
number: raw[6].toFixed()
}
return bet
}
parseEvent(propArray) {
//bigInt
console.log(propArray)
let id = propArray[0].toFixed()
let ts = propArray[1].toFixed()
......@@ -60,6 +108,10 @@ export default class BetPage extends Component {
//bool
let active = propArray[7]
this.setState({
availableTypes: betTypes,
})
return {
id: id,
timestamp: ts,
......@@ -71,55 +123,71 @@ export default class BetPage extends Component {
}
}
constructor(props) {
super(props)
// Don't ask
this.getAllEvents()
}
handleClick = (e) => {
this.setState({ chosenBetID: e.currentTarget.dataset.id })
for (let i = 0; i < this.state.events.length; i++) {
if (this.state.events[i].id === e.currentTarget.dataset.id ) {
this.setState({ chosenBetID: this.state.events[i] })
this.state.chosenBet = this.state.events[i]
this.state.closedTypes.push(this.state.events[i].closedBetTypes)
}
}
this.setState({ chosenBetID: e.currentTarget.dataset.id })
this.getAvailableBets(e.currentTarget.dataset.id)
}
async getAvailableBets() {
let availableBets = await getEventAvailableBetsObjects(this.state.chosenBetID); //eslint-disable-line
console.log(availableBets)
this.setState({ availableBets: availableBets })
async getAvailableBets(id) {
let availableBets = await getEventAvailableBetsObjects(id); //eslint-disable-line
let parsedBets = []
availableBets.forEach((bet) => {
parsedBets.push(this.parseBet(bet))
})
this.setState({ availableBets: parsedBets })
this.betsByType()
}
async getAllEvents() {
let rawEvents = await getEvents() //eslint-disable-line
console.log(rawEvents)
rawEvents.forEach((event) => {
this.state.events.push(this.parseEvent(event))
})
console.log(this.state.events)
this.setState({ events: this.state.events })
}
betItem () {
betItem (object, i) {
let newObj = {...object, betType: undefined}
return (
<div>
Your bet
</div>
<Segment key={i} className="bet-item" data-obj={JSON.stringify(newObj)} onClick={this.addBet.bind(this)}>
{newObj.values[0] + " : " + newObj.values[1] + " " }
(Odds {newObj.odds})
</Segment>
)
}
betType(type) {
//Sorting
betsByType() {
this.state.availableBets.forEach((bet) => {
this.state.availableTypes.forEach((type) => {
if(bet.betType[0].name === type.name) {
type.bets.push(bet)
}
})
})
this.setState({ solved: true })
}
betType(type, i) {
return (
<div>
<Header>Match Scores</Header>
</div>
<Segment key={i}>
<Header>{type.name}</Header>
{ this.state.availableTypes.includes(this.state.closedTypes[i]) ? "Closed" :
this.state.availableTypes[this.state.availableTypes.indexOf(type)].bets !== 0 ?
this.state.availableTypes[this.state.availableTypes.indexOf(type)].bets.map((object, i) => this.betItem(object, i)) : "No bets" }
</Segment>
)
}
......@@ -134,38 +202,58 @@ export default class BetPage extends Component {
)
}
addBet(e) {
let Bet = JSON.parse(e.currentTarget.dataset.obj)
this.setState({ yourBet: Bet })
}
async submitBet() {
console.log(this.state.yourBet)
console.log(this.state)
let status = await placeBet(this.state.chosenBetID, this.state.yourBetID, this.state.amount) //eslint-disable-line
console.log("success")
}
renderAllEvents() {
return (
this.state.events.map((object, i) => this.eventItem(object, i))
)
}
renderAvailableBets() {
if (this.state.chosenBetID !== "") {
return (
<div className="available-bet">
this vs that
{/* {this.availableBets[0]} vs {this.state.availableBets[0]} */}
{this.state.chosenBet.betTypes.map((object, i) => {
return this.betType(object, i)
})}
</div>
)
}
}
renderChosenBet() {
console.log(this.state.yourBet)
return (
<div className="chosen-bets">
<Segment>
{this.state.chosenBet.away} vs {this.state.chosenBet.home}
odds 1.29
<b>{this.state.chosenBet.away} vs {this.state.chosenBet.home}</b>
<p>Odds: {this.state.yourBet.odds}</p>
<p>Amount to bet:</p>
<Input name="amount" type="numbers" onChange={this.handleChange.bind(this)} value={this.state.amount}/>
<br/>
<p>Potential profit:</p>
{this.state.amount ? this.state.yourBet.odds * this.state.amount : "None"}
</Segment>
<Button color="green" onClick={this.submitBet.bind(this)}>
Submit your bet
</Button>
</div>
)
}
render() {
console.log(this.state.chosenBetID)
render() {
return(
<div className="bet-page">
{/* Nav bar */}
......@@ -184,12 +272,12 @@ export default class BetPage extends Component {
{this.state.events.length !== 0 ? this.renderAllEvents() : "Nothing here now"}
</Segment>
<Segment className="front all-available-bets">
<Header attached className="titles">'Selected Sport' All Bets </Header>
<Header attached className="titles">{this.state.chosenBet.sport ? this.state.chosenBet.sport + ">" : null} Available Bets { this.state.chosenBet.length !== 0 ? " - " + this.state.chosenBet.home + " vs " + this.state.chosenBet.away : null} </Header>
{this.state.chosenBetID !== "" ? this.renderAvailableBets() : "No bet chosen"}
</Segment>
<Segment className="rear bets-to-submit">
<Header attached className="titles"> Current bets </Header>
{this.state.chosenBet.length !== 0 ? this.renderChosenBet() : "No bet chosen"}
{this.state.chosenBetID !== "" ? this.renderChosenBet() : "No bet chosen"}
</Segment>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment