This commit is contained in:
folex 2019-06-26 17:22:55 +03:00
parent 630fb7537a
commit 7e610879ea

View File

@ -54,7 +54,7 @@
<script> <script>
import * as fluence from "fluence"; import * as fluence from "fluence";
function randomInteger(min, max) { function randomInteger(min, max) {
var rand = min - 0.5 + Math.random() * (max - min + 1); var rand = min - 0.5 + Math.random() * (max - min + 1)
rand = Math.round(rand); rand = Math.round(rand);
return rand; return rand;
} }
@ -76,7 +76,7 @@ export default {
items: [], items: [],
orders: [], orders: [],
players: [] players: []
}; }
}, },
// >>>CREATE TABLE weapeons(id int, type int, level int, userId int) // >>>CREATE TABLE weapeons(id int, type int, level int, userId int)
// table created // table created
@ -87,31 +87,23 @@ export default {
//CREATE TABLE enemies(id int, hp int, posX int, posY int) //CREATE TABLE enemies(id int, hp int, posX int, posY int)
methods: { methods: {
getEnemies: function() { getEnemies: function() {
window.session window.session.request(`SELECT * FROM enemies`).result().then((r)=>{
.request(`SELECT * FROM enemies`) var val = r.asString().split("\n").map(i => i.split(", "))
.result() val.splice(0, 1)
.then(r => { console.log(r)
var val = r this.enemies = []
.asString()
.split("\n")
.map(i => i.split(", "));
val.splice(0, 1);
console.log(r);
this.enemies = [];
if(val.length <= 1){ if(val.length <= 1){
let id = randomInteger(500, 2000000); let id = randomInteger(500, 2000000)
let x = Math.floor(randomInteger(50, window.innerWidth - 50)); let x = Math.floor(randomInteger(50, window.innerWidth - 50))
let y = Math.floor(randomInteger(50, window.innerHeight - 50)); let y = Math.floor(randomInteger(50, window.innerHeight - 50))
window.session window.session.request(`INSERT INTO enemies VALUES(${id}, 100, ${x}, ${y})`).then((r) => {
.request(`INSERT INTO enemies VALUES(${id}, 100, ${x}, ${y})`)
.then(r => {
this.enemies.push({ this.enemies.push({
left: x, left: x,
top: y, top: y,
hp: 100, hp: 100,
id id
}); })
}); })
}else{ }else{
val.forEach(v=>{ val.forEach(v=>{
if(v){ if(v){
@ -120,143 +112,86 @@ export default {
top: v[3], top: v[3],
hp: v[1], hp: v[1],
id: v[0] id: v[0]
}); })
} }
}); })
} }
setTimeout(() => this.getEnemies(), 1000); setTimeout(()=>this.getEnemies(), 1000)
}); })
}, },
getPlayers: function() { getPlayers: function() {
window.session window.session.request(`SELECT * FROM players`).result().then((r)=>{
.request(`SELECT * FROM players`) console.log(r.asString())
.result() var val = r.asString().split("\n").map(i => i.split(", "))
.then(r => { val.splice(0, 1)
console.log(r.asString()); this.players = []
var val = r console.log(val)
.asString()
.split("\n")
.map(i => i.split(", "));
val.splice(0, 1);
this.players = [];
console.log(val);
val.forEach(v=>{ val.forEach(v=>{
console.log(v); console.log(v)
if(v&&v[0]!==window.localStorage.getItem("game")){ if(v&&v[0]!==window.localStorage.getItem("game")){
this.players.push({ this.players.push({
left: v[1], left: v[1],
top: v[2], top: v[2],
angle: v[3] angle: v[3]
}); })
} }
}); })
setTimeout(() => this.getPlayers(), 500); setTimeout(()=>this.getPlayers(), 500)
}); })
}, },
openMarket: function() { openMarket: function() {
this.marketModal = true; this.marketModal = true
window.session window.session.request(`SELECT * FROM orders`).result().then((r) => {
.request(`SELECT * FROM orders`) var val = r.asString().split("\n").map(i => i.split(", "))
.result() val.splice(0, 1)
.then(r => { this.orders = val
var val = r })
.asString()
.split("\n")
.map(i => i.split(", "));
val.splice(0, 1);
this.orders = val;
});
}, },
buyItem: function(o){ buyItem: function(o){
window.session.request( window.session.request(`INSERT INTO weapeons VALUES(${randomInteger(500, 2000000)}, ${o[2]}, 0, ${window.localStorage.getItem("game")})`)
`INSERT INTO weapeons VALUES(${randomInteger(500, 2000000)}, ${
o[2]
}, 0, ${window.localStorage.getItem("game")})`
);
window.session window.session.request(`SELECT * FROM users`).result().then((r) => {
.request(`SELECT * FROM users`) var val = r.asString().split("\n").map(i => i.split(", "))
.result() val.splice(0, 1)
.then(r => { window.session.request(`UPDATE users SET cash = ${parseInt(val.filter(i=>parseInt(i[0])==o[3])[0][2]) + parseInt(o[1])} WHERE id = ${o[3]}`)
var val = r window.session.request(`UPDATE users SET cash = ${val.filter(i=>parseInt(i[0])==window.localStorage.getItem("game"))[0][2] - o[1]} WHERE id = ${window.localStorage.getItem("game")}`)
.asString() })
.split("\n") console.log(o[0])
.map(i => i.split(", ")); window.session.request(`DELETE FROM orders WHERE id = ${o[0]}`)
val.splice(0, 1); this.money -= o[1]
window.session.request(
`UPDATE users SET cash = ${parseInt(
val.filter(i => parseInt(i[0]) == o[3])[0][2]
) + parseInt(o[1])} WHERE id = ${o[3]}`
);
window.session.request(
`UPDATE users SET cash = ${val.filter(
i => parseInt(i[0]) == window.localStorage.getItem("game")
)[0][2] - o[1]} WHERE id = ${window.localStorage.getItem("game")}`
);
});
console.log(o[0]);
window.session.request(`DELETE FROM orders WHERE id = ${o[0]}`);
this.money -= o[1];
}, },
getItems: function() { getItems: function() {
window.session window.session.request(`SELECT cash FROM users WHERE id = ${window.localStorage.getItem("game")}`).result().then((r) => {
.request( this.money = parseInt(r.asString().split("\n")[1] )
`SELECT cash FROM users WHERE id = ${window.localStorage.getItem( })
"game" window.session.request(`SELECT * FROM weapeons WHERE userId = ${window.localStorage.getItem("game")}`).result().then((r) => {
)}`
)
.result()
.then(r => {
this.money = parseInt(r.asString().split("\n")[1]);
});
window.session
.request(
`SELECT * FROM weapeons WHERE userId = ${window.localStorage.getItem(
"game"
)}`
)
.result()
.then(r => {
if(r.asString().split("\n")[1]){ if(r.asString().split("\n")[1]){
var val = r var val = r.asString().split("\n").map(i => i.split(", "))
.asString() val.splice(0, 1)
.split("\n") val.unshift([0,0,0,0])
.map(i => i.split(", ")); this.items = val
val.splice(0, 1);
val.unshift([0, 0, 0, 0]);
this.items = val;
} }
}); })
}, },
sellItem: function() { sellItem: function() {
let iInd = this.marketSellSelected; let iInd = this.marketSellSelected
window.session.request( window.session.request(`INSERT INTO orders VALUES(${randomInteger(500, 2000000)}, ${this.marketSellPrice}, ${this.items[iInd][1]}, ${window.localStorage.getItem("game")})`)
`INSERT INTO orders VALUES(${randomInteger(500, 2000000)}, ${ window.session.request(`DELETE FROM weapeons WHERE id = ${this.items[iInd][0]}`)
this.marketSellPrice this.marketSellPrice = null
}, ${this.items[iInd][1]}, ${window.localStorage.getItem("game")})` this.marketSellSelected = null
); this.items.splice(iInd, 1)
window.session.request(
`DELETE FROM weapeons WHERE id = ${this.items[iInd][0]}`
);
this.marketSellPrice = null;
this.marketSellSelected = null;
this.items.splice(iInd, 1);
}, },
openLootbox: function(){ openLootbox: function(){
if(this.money > 20){ if(this.money > 20){
this.money -= 20; this.money -= 20
let num = randomInteger(1, 4); let num = randomInteger(1,4)
this.show_weapeon = num; this.show_weapeon = num
setTimeout(() => (this.show_weapeon = null), 3000); setTimeout(()=>this.show_weapeon = null, 3000)
window.session.request( window.session.request(`INSERT INTO weapeons VALUES(${randomInteger(500, 2000000)}, ${num}, 0, ${window.localStorage.getItem("game")})`)
`INSERT INTO weapeons VALUES(${randomInteger( this.items.push(num)
500,
2000000
)}, ${num}, 0, ${window.localStorage.getItem("game")})`
);
this.items.push(num);
}else { }else {
alert("Not enough cash. Need 20$"); alert("Not enough cash. Need 20$")
} }
}, },
shoot: function(e){ shoot: function(e){
@ -265,160 +200,123 @@ export default {
endX: e.x, endX: e.x,
left: this.left, left: this.left,
top: this.top top: this.top
}); })
}, },
regenerate: function () { regenerate: function () {
if(this.bullets){ if(this.bullets){
this.bullets.forEach((e, i )=> { this.bullets.forEach((e, i )=> {
let dx = e.endX - e.left; let dx = e.endX - e.left;
let dy = e.endY - e.top; let dy = e.endY - e.top;
let angle = Math.atan2(dy, dx); let angle = Math.atan2(dy, dx)
let velocity = 5; let velocity = 5
e.left += velocity * Math.cos(angle); e.left += velocity * Math.cos(angle)
e.top += velocity * Math.sin(angle); e.top += velocity * Math.sin(angle)
this.enemies.forEach((v, j)=> { this.enemies.forEach((v, j)=> {
if ( if(Math.abs(v.left - e.left) < 30 && Math.abs(v.top - e.top) < 30){
Math.abs(v.left - e.left) < 30 && this.bullets.splice(i, 1)
Math.abs(v.top - e.top) < 30
) {
this.bullets.splice(i, 1);
if(v.hp - 10 <= 0){ if(v.hp - 10 <= 0){
this.enemies.splice(j, 1); this.enemies.splice(j, 1)
console.log("killed"); console.log('killed')
window.session.request( window.session.request(`DELETE FROM ENEMIES WHERE id = ${v.id}`)
`DELETE FROM ENEMIES WHERE id = ${v.id}` this.money += 10
); console.log("SETTING CASH TO " + this.money + " GAME IS " + window.localStorage.getItem("game"))
this.money += 10; window.session.request(`UPDATE users SET cash = ${this.money} WHERE id = ${window.localStorage.getItem("game")}`)
console.log( this.level = this.level>5?5:this.level + 1
"SETTING CASH TO " +
this.money +
" GAME IS " +
window.localStorage.getItem("game")
);
window.session.request(
`UPDATE users SET cash = ${
this.money
} WHERE id = ${window.localStorage.getItem("game")}`
);
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++){
let id = randomInteger(500, 2000000); let id = randomInteger(500, 2000000)
let x = Math.floor(randomInteger(50, window.innerWidth - 50)); let x = Math.floor(randomInteger(50, window.innerWidth - 50))
let y = Math.floor( let y = Math.floor(randomInteger(50, window.innerHeight - 50))
randomInteger(50, window.innerHeight - 50) window.session.request(`INSERT INTO enemies VALUES(${id}, 100, ${x}, ${y})`).then((r) => {
);
window.session
.request(
`INSERT INTO enemies VALUES(${id}, 100, ${x}, ${y})`
)
.then(r => {
this.enemies.push({ this.enemies.push({
left: x, left: x,
top: y, top: y,
hp: 100, hp: 100,
id id
}); })
}); })
} }
}else { }else {
v.hp -= 10; v.hp -= 10
console.log( console.log("CASH IS " + this.money + " GAME IS " + window.localStorage.getItem("game"))
"CASH IS " + window.session.request(`UPDATE enemies SET hp = ${v.hp - 10} WHERE id = ${v.id}`)
this.money + console.log('dmg')
" GAME IS " +
window.localStorage.getItem("game")
);
window.session.request(
`UPDATE enemies SET hp = ${v.hp - 10} WHERE id = ${v.id}`
);
console.log("dmg");
} }
} }
}); })
if(Math.abs(dx) < 10 && Math.abs(dy) < 10){ if(Math.abs(dx) < 10 && Math.abs(dy) < 10){
this.bullets.splice(i, 1); this.bullets.splice(i, 1)
} }
}); });
} }
} }
}, },
mounted(){ mounted(){
let privateKey = let privateKey = "569ae4fed4b0485848d3cf9bbe3723f5783aadd0d5f6fd83e18b45ac22496859"; // Authorization private key
"569ae4fed4b0485848d3cf9bbe3723f5783aadd0d5f6fd83e18b45ac22496859"; // Authorization private key
let contract = "0xeFF91455de6D4CF57C141bD8bF819E5f873c1A01"; // Fluence contract address let contract = "0xeFF91455de6D4CF57C141bD8bF819E5f873c1A01"; // Fluence contract address
let appId = 267; // Deployed database id let appId = 267; // Deployed database id
let ethereumUrl = "http://geth.fluence.one:8545"; // Ethereum light node URL let ethereumUrl = "http://geth.fluence.one:8545"; // Ethereum light node URL
fluence.connect(contract, appId, ethereumUrl, privateKey).then(s => { fluence.connect(contract, appId, ethereumUrl, privateKey).then((s) => {
console.log("Session created"); console.log("Session created");
window.session = s; window.session = s;
this.getItems(); this.getItems()
}); });
setTimeout(()=>{ setTimeout(()=>{
var arrow = document.getElementById("player"); var arrow = document.getElementById("player");
var arrowRects = arrow.getBoundingClientRect(); var arrowRects = arrow.getBoundingClientRect();
var arrowX = arrowRects.left + arrowRects.width / 2; var arrowX = arrowRects.left + arrowRects.width / 2;
var arrowY = arrowRects.top + arrowRects.height / 2; var arrowY = arrowRects.top + arrowRects.height / 2;
this.rotate = this.rotate = "rotate(" + Math.abs(Math.atan2(0 - arrowY, 0 - arrowX)) + "rad)";
"rotate(" + Math.abs(Math.atan2(0 - arrowY, 0 - arrowX)) + "rad)"; addEventListener("mousemove", (e) => {
addEventListener("mousemove", e => {
let eyes = document.getElementById("player"); let eyes = document.getElementById("player");
let mouseX = eyes.getBoundingClientRect().left; let mouseX = (eyes.getBoundingClientRect().left);
let mouseY = eyes.getBoundingClientRect().top; let mouseY = (eyes.getBoundingClientRect().top);
let radianDegrees = Math.atan2(e.pageX - mouseX, e.pageY - mouseY); let radianDegrees = Math.atan2(e.pageX - mouseX, e.pageY - mouseY);
let rotationDegrees = radianDegrees * (180 / Math.PI) * -1 + 180; let rotationDegrees = (radianDegrees * (180/ Math.PI) * -1) + 180;
eyes.style.transform = `rotate(${rotationDegrees}deg)`; eyes.style.transform = `rotate(${rotationDegrees}deg)`
}); });
addEventListener("keydown", e => { addEventListener("keydown", (e) => {
let pRad = Math.atan2(event.clientY - arrowY, event.clientX - arrowX); let pRad = Math.atan2(event.clientY - arrowY, event.clientX - arrowX)
arrow.style.transform = "rotate(" + pRad + "rad)"; arrow.style.transform = "rotate(" + pRad + "rad)";
if (e.keyCode == "87") { if (e.keyCode == '87') {
// up arrow // up arrow
if(this.top - 50 >= 0) { if(this.top - 50 >= 0) {
this.top -= 50; this.top -= 50
} }
} else if (e.keyCode == "83") { }
else if (e.keyCode == '83') {
// down arrow // down arrow
if(this.top + 50 < window.innerHeight) { if(this.top + 50 < window.innerHeight) {
this.top += 50; this.top += 50
} }
} else if (e.keyCode == "65") { }
else if (e.keyCode == '65') {
// left arrow // left arrow
if(this.left - 50 >= 0) { if(this.left - 50 >= 0) {
this.left -= 50; this.left -= 50
} }
} else if (e.keyCode == "68") { }
else if (e.keyCode == '68') {
// right arrow // right arrow
if(this.left + 50 < window.innerWidth) { if(this.left + 50 < window.innerWidth) {
this.left += 50; this.left += 50
} }
} }
//CREATE TABLE players(id int, posX int, posY int, angle int) //CREATE TABLE players(id int, posX int, posY int, angle int)
window.session.request( window.session.request(`UPDATE players SET angle = ${pRad} WHERE id = ${window.localStorage.getItem("game")}`)
`UPDATE players SET angle = ${pRad} WHERE id = ${window.localStorage.getItem( window.session.request(`UPDATE players SET posX = ${this.left} WHERE id = ${window.localStorage.getItem("game")}`)
"game" window.session.request(`UPDATE players SET posY = ${this.top} WHERE id = ${window.localStorage.getItem("game")}`)
)}` })
); this.getEnemies()
window.session.request( this.getPlayers()
`UPDATE players SET posX = ${
this.left
} WHERE id = ${window.localStorage.getItem("game")}`
);
window.session.request(
`UPDATE players SET posY = ${
this.top
} WHERE id = ${window.localStorage.getItem("game")}`
);
});
this.getEnemies();
this.getPlayers();
var self = this; var self = this;
setInterval(function(){ setInterval(function(){
self.regenerate(); self.regenerate();
}, 10); }, 10);
}, 1000); }, 1000)
}
} }
};
</script> </script>
<style lang="sass" scoped> <style lang="sass" scoped>