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

View File

@ -54,13 +54,13 @@
<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;
} }
export default { export default {
data() { data(){
return { return{
left: 0, left: 0,
top: 0, top: 0,
money: 1000, money: 1000,
@ -76,349 +76,247 @@ 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
// >>>CREATE TABLE orders(id int, price int, type int, userId int) // >>>CREATE TABLE orders(id int, price int, type int, userId int)
// table created // table created
//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() if(val.length <= 1){
.split("\n") let id = randomInteger(500, 2000000)
.map(i => i.split(", ")); let x = Math.floor(randomInteger(50, window.innerWidth - 50))
val.splice(0, 1); let y = Math.floor(randomInteger(50, window.innerHeight - 50))
console.log(r); window.session.request(`INSERT INTO enemies VALUES(${id}, 100, ${x}, ${y})`).then((r) => {
this.enemies = []; this.enemies.push({
if (val.length <= 1) { left: x,
let id = randomInteger(500, 2000000); top: y,
let x = Math.floor(randomInteger(50, window.innerWidth - 50)); hp: 100,
let y = Math.floor(randomInteger(50, window.innerHeight - 50)); id
window.session })
.request(`INSERT INTO enemies VALUES(${id}, 100, ${x}, ${y})`) })
.then(r => { }else{
this.enemies.push({ val.forEach(v=>{
left: x, if(v){
top: y, this.enemies.push({
hp: 100, left: v[2],
id top: v[3],
}); hp: v[1],
}); id: v[0]
} else { })
val.forEach(v => { }
if (v) { })
this.enemies.push({ }
left: v[2], setTimeout(()=>this.getEnemies(), 1000)
top: v[3], })
hp: v[1],
id: v[0]
});
}
});
}
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() val.forEach(v=>{
.split("\n") console.log(v)
.map(i => i.split(", ")); if(v&&v[0]!==window.localStorage.getItem("game")){
val.splice(0, 1); this.players.push({
this.players = []; left: v[1],
console.log(val); top: v[2],
val.forEach(v => { angle: v[3]
console.log(v); })
if (v && v[0] !== window.localStorage.getItem("game")) { }
this.players.push({ })
left: v[1], setTimeout(()=>this.getPlayers(), 500)
top: v[2], })
angle: v[3]
});
}
});
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) => {
)}` if(r.asString().split("\n")[1]){
) var val = r.asString().split("\n").map(i => i.split(", "))
.result() val.splice(0, 1)
.then(r => { val.unshift([0,0,0,0])
this.money = parseInt(r.asString().split("\n")[1]); this.items = val
}); }
window.session })
.request(
`SELECT * FROM weapeons WHERE userId = ${window.localStorage.getItem(
"game"
)}`
)
.result()
.then(r => {
if (r.asString().split("\n")[1]) {
var val = r
.asString()
.split("\n")
.map(i => i.split(", "));
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, }else {
2000000 alert("Not enough cash. Need 20$")
)}, ${num}, 0, ${window.localStorage.getItem("game")})`
);
this.items.push(num);
} else {
alert("Not enough cash. Need 20$");
} }
}, },
shoot: function(e) { shoot: function(e){
this.bullets.push({ this.bullets.push({
endY: e.y, endY: e.y,
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 if(v.hp - 10 <= 0){
) { this.enemies.splice(j, 1)
this.bullets.splice(i, 1); console.log('killed')
if (v.hp - 10 <= 0) { window.session.request(`DELETE FROM ENEMIES WHERE id = ${v.id}`)
this.enemies.splice(j, 1); this.money += 10
console.log("killed"); console.log("SETTING CASH TO " + this.money + " GAME IS " + window.localStorage.getItem("game"))
window.session.request( window.session.request(`UPDATE users SET cash = ${this.money} WHERE id = ${window.localStorage.getItem("game")}`)
`DELETE FROM ENEMIES WHERE id = ${v.id}` this.level = this.level>5?5:this.level + 1
); for(let l=0; l<this.level; l++){
this.money += 10; let id = randomInteger(500, 2000000)
console.log( let x = Math.floor(randomInteger(50, window.innerWidth - 50))
"SETTING CASH TO " + let y = Math.floor(randomInteger(50, window.innerHeight - 50))
this.money + window.session.request(`INSERT INTO enemies VALUES(${id}, 100, ${x}, ${y})`).then((r) => {
" GAME IS " + this.enemies.push({
window.localStorage.getItem("game") left: x,
); top: y,
window.session.request( hp: 100,
`UPDATE users SET cash = ${ id
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++) {
let id = randomInteger(500, 2000000);
let x = Math.floor(randomInteger(50, window.innerWidth - 50));
let y = Math.floor(
randomInteger(50, window.innerHeight - 50)
);
window.session
.request(
`INSERT INTO enemies VALUES(${id}, 100, ${x}, ${y})`
)
.then(r => {
this.enemies.push({
left: x,
top: y,
hp: 100,
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") {
// down arrow
if (this.top + 50 < window.innerHeight) {
this.top += 50;
}
} else if (e.keyCode == "65") {
// left arrow
if (this.left - 50 >= 0) {
this.left -= 50;
}
} else if (e.keyCode == "68") {
// right arrow
if (this.left + 50 < window.innerWidth) {
this.left += 50;
}
} }
//CREATE TABLE players(id int, posX int, posY int, angle int) }
window.session.request( else if (e.keyCode == '83') {
`UPDATE players SET angle = ${pRad} WHERE id = ${window.localStorage.getItem( // down arrow
"game" if(this.top + 50 < window.innerHeight) {
)}` this.top += 50
); }
window.session.request( }
`UPDATE players SET posX = ${ else if (e.keyCode == '65') {
this.left // left arrow
} WHERE id = ${window.localStorage.getItem("game")}` if(this.left - 50 >= 0) {
); this.left -= 50
window.session.request( }
`UPDATE players SET posY = ${ }
this.top else if (e.keyCode == '68') {
} WHERE id = ${window.localStorage.getItem("game")}` // right arrow
); if(this.left + 50 < window.innerWidth) {
}); this.left += 50
this.getEnemies(); }
this.getPlayers(); }
var self = this; //CREATE TABLE players(id int, posX int, posY int, angle int)
setInterval(function() { window.session.request(`UPDATE players SET angle = ${pRad} WHERE id = ${window.localStorage.getItem("game")}`)
self.regenerate(); window.session.request(`UPDATE players SET posX = ${this.left} WHERE id = ${window.localStorage.getItem("game")}`)
}, 10); window.session.request(`UPDATE players SET posY = ${this.top} WHERE id = ${window.localStorage.getItem("game")}`)
}, 1000); })
this.getEnemies()
this.getPlayers()
var self = this;
setInterval(function(){
self.regenerate();
}, 10);
}, 1000)
} }
}; }
</script> </script>
<style lang="sass" scoped> <style lang="sass" scoped>