register new player

This commit is contained in:
folex 2019-06-26 18:33:39 +03:00
parent 49080e68b6
commit 69d03b9d80

View File

@ -68,7 +68,7 @@ function getPlayerId() {
playerId = getRandomInt(0, 9999999); playerId = getRandomInt(0, 9999999);
window.localStorage.setItem("game", playerId); window.localStorage.setItem("game", playerId);
} }
return playerId; return playerId.toString();
} }
export default { export default {
data(){ data(){
@ -103,7 +103,6 @@ export default {
window.session.request(`SELECT * FROM enemies`).result().then((r)=>{ window.session.request(`SELECT * FROM enemies`).result().then((r)=>{
var val = r.asString().split("\n").map(i => i.split(", ")) var val = r.asString().split("\n").map(i => i.split(", "))
val.splice(0, 1) val.splice(0, 1)
console.log(r)
this.enemies = [] this.enemies = []
if(val.length <= 1){ if(val.length <= 1){
let id = randomInteger(500, 2000000) let id = randomInteger(500, 2000000)
@ -135,19 +134,24 @@ export default {
}, },
getPlayers: function() { getPlayers: function() {
window.session.request(`SELECT * FROM players`).result().then((r)=>{ window.session.request(`SELECT * FROM players`).result().then((r)=>{
console.log(r.asString())
var val = r.asString().split("\n").map(i => i.split(", ")) var val = r.asString().split("\n").map(i => i.split(", "))
val.splice(0, 1) val.splice(0, 1)
this.players = [] this.players = []
console.log(val)
val.forEach(v=>{ val.forEach(v=>{
console.log(v) function emptyPlayer(player) {
if(v&&v[0]!==this.playerId){ return v[1] == 0 && v[2] == 0 && v[3] == 0;
this.players.push({ }
if(v && v[0] !== this.playerId && !emptyPlayer(v)){
let player = {
left: v[1], left: v[1],
top: v[2], top: v[2],
angle: v[3] angle: v[3]
}) };
console.log(`player: ${v[0]} ${JSON.stringify(player)}`)
this.players.push(player);
} }
}) })
setTimeout(()=>this.getPlayers(), 500) setTimeout(()=>this.getPlayers(), 500)
@ -179,6 +183,10 @@ export default {
let money = parseInt(r.asString().split("\n")[1]) let money = parseInt(r.asString().split("\n")[1])
if (!isNaN(money) && !isNullOrUndefined(money)) { if (!isNaN(money) && !isNullOrUndefined(money)) {
this.money = money this.money = money
} else {
console.log(`creating new player ${this.playerId}`)
window.session.request(`INSERT INTO users VALUES (${this.playerId}, ${this.playerId}, ${this.money})`)
window.session.request(`INSERT INTO players VALUES (${this.playerId}, 0, 0, 0)`)
} }
console.log(`cash for ${this.playerId} is ${this.money}`) console.log(`cash for ${this.playerId} is ${this.money}`)
}) })
@ -234,9 +242,7 @@ export default {
if(v.hp - 10 <= 0){ if(v.hp - 10 <= 0){
this.enemies.splice(j, 1) this.enemies.splice(j, 1)
window.session.request(`DELETE FROM ENEMIES WHERE id = ${v.id}`) window.session.request(`DELETE FROM ENEMIES WHERE id = ${v.id}`)
console.log('killed')
this.money += 10 this.money += 10
console.log("SETTING CASH TO " + this.money + " GAME IS " + this.playerId)
window.session.request(`UPDATE users SET cash = ${this.money} WHERE id = ${this.playerId}`) window.session.request(`UPDATE users SET cash = ${this.money} WHERE id = ${this.playerId}`)
this.level = this.level>5?5:this.level + 1 this.level = this.level>5?5:this.level + 1
for(let l=0; l<this.level; l++){ for(let l=0; l<this.level; l++){
@ -253,9 +259,7 @@ export default {
} }
}else { }else {
v.hp -= 10 v.hp -= 10
console.log("CASH IS " + this.money + " GAME IS " + this.playerId)
window.session.request(`UPDATE enemies SET hp = ${v.hp - 10} WHERE id = ${v.id}`) window.session.request(`UPDATE enemies SET hp = ${v.hp - 10} WHERE id = ${v.id}`)
console.log('dmg')
} }
} }
}) })