<script src="file:///android_asset/client/public/node_modules/socket.io/socket.io.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=0">
<title>ARAGON | BATTLE</title>
<link rel="stylesheet" href="file:///android_asset/client/css/boss.css">
</head>
<body>
<div class="hp-border"></div>
<div class="box">
<div class="hp-line"></div>
</div>
<div class="boss-name">JUNGLER</div>
<div class="chat">
<div class="users-count"></div>
<div class="chat-box"></div>
<div class="user-data">
<input class="user-name" placeholder="nickname" disabled>
<button class="send-to-chat">Send</button>
<textarea class="chat-message" disabled></textarea>
</div>
</div>
<section>
<div class="boss"></div>
<div class="achivements">
<div class="armor"></div>
<div class="sword"></div>
<div class="heal"></div>
</div>
<div class="user-border"></div>
<div class="user-hp"></div>
</section>
<script src="file:///android_asset/client/lib/jquery.min.js"></script>
<script src="file:///android_asset/client/public/node_modules/socket.io/socket.io.js"></script>
<script src="file:///android_asset/client/scripts/boss.js"></script>
</body>
</html>
package com.example.caveshadows;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.View;
public class MainActivity extends Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onStart();
setContentView(R.layout.activity_main);
webView = (WebView)findViewById(R.id.web);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return false;
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.loadUrl("file:///android_asset/client/public/boss.html");
}
@Override
protected void onResume() {
//webView.getSettings().setAppVisible(true, this);
super.onResume();
if (Build.VERSION.SDK_INT >= 19) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
var host = 'http://localhost:3000/';
var socket = io(host, {
upgrade: false,
reconnection: true,
reconnectionDelay: 0,
reconnectionDelayMax : 500,
reconnectionAttempts: 99999
});
var hp,hp2,connections,x,message,msg,x2;
var boss_damage = Math.floor((Math.random() * 1000) + 500);
var user_damage = Math.floor((Math.random() * 31000) + 1000);
var bonus_sword_damage = 10000;
var max_user_hp = 30000,user_hp = max_user_hp,x2,lose,win,draw;
var heal=3000,youlose='Вы проиграли, следите за здоровьем!(Оно восстановится в течении 15 секунд.)';
$.getJSON('https://api.ipify.org?format=json', function(data){
socket.emit('banned',data.ip);
});
socket.emit('hp line for all');
var count=25200;
var counter=setInterval(timer, 1000);
function timer() {
count--;
if (count <= 0 || lose == 'lose' || win == 'win' || draw == 'draw') {
clearInterval(counter);
//localStorage.clear();
localStorage.setItem('hp', max_user_hp);
}
}
socket.on('connect', () => {
user_hp = localStorage.getItem('hp');
x2 = localStorage.getItem('w');
localStorage.clear();
socket.emit('user fight', user_hp,x2);
});
$('.user-name').click(function() {
$('.user-name').css({'top':'0px','position':'absolute','z-index':'9999'});
});
$('.chat').click(function() {
alert('На данный момент чат отключён - попробуйте позже.');
});
$('.boss').click(function() {
window.onfocus = function () {
socket.emit('new session');
};
user_hp = user_hp-boss_damage;
x2 = user_hp/max_user_hp*100;
/*if(user_hp <= 0 || user_hp >= max_user_hp) {
localStorage.setItem('hp', max_user_hp);
user_hp = max_user_hp;
x2 = 100;
}*/
if(user_hp >= max_user_hp) {
user_hp = max_user_hp;
x2 = 100;
}
localStorage.setItem('hp', user_hp);
localStorage.setItem('w', x2);
socket.emit('boss fight', user_damage);
socket.emit('user fight', user_hp,x2);
$('.hp-line').css("width", x+'%');
$('.user-hp').css("width", x2+'%');
});
$('.sword').click(function() {
user_hp = user_hp-boss_damage;
x2 = user_hp/max_user_hp*100;
/*if(user_hp <= 0 || user_hp >= max_user_hp) {
localStorage.setItem('hp', max_user_hp);
user_hp = max_user_hp;
x2 = 100;
}*/
if(user_hp >= max_user_hp) {
user_hp = max_user_hp;
x2 = 100;
}
localStorage.setItem('hp', user_hp);
localStorage.setItem('w', x2);
socket.emit('boss fight', user_damage+bonus_sword_damage);
socket.emit('user fight', user_hp,x2);
$('.hp-line').css("width", x+'%');
$('.user-hp').css("width", x2+'%');
});
$('.heal').click(function() {
user_hp = user_hp+heal;
x2 = user_hp/max_user_hp*100;
/*if(user_hp <= 0 || user_hp >= max_user_hp) {
localStorage.setItem('hp', max_user_hp);
user_hp = max_user_hp;
x2 = 100;
}*/
if(user_hp >= max_user_hp) {
user_hp = max_user_hp;
x2 = 100;
}
localStorage.setItem('hp', user_hp);
localStorage.setItem('w', x2);
socket.emit('user fight', user_hp+heal,x2);
$('.user-hp').css("width", x2+'%');
});
var healtimer = setInterval(timer2, 3000);
function timer2() {
user_hp = user_hp+heal;
x2 = user_hp/max_user_hp*100;
if(user_hp <= 0) {
//localStorage.setItem('hp', max_user_hp);
//user_hp = max_user_hp;
}
localStorage.setItem('hp', user_hp);
localStorage.setItem('w', x2);
socket.emit('user fight', user_hp,x2);
$('.user-hp').css("width", x2+'%');
/*if(user_hp >= max_user_hp || user_hp <= 0) {
user_hp = max_user_hp;
x2 = 100;
}*/
if(user_hp >= max_user_hp) {
user_hp = max_user_hp;
x2 = 100;
}
}
/*$('.send-to-chat').click(function() {
name = $(".user-name").val();
msg = $(".chat-message").val();
socket.emit('chat message', msg);
});*/
socket.on('realtime hp', function(data) {
hp = data;
$('.hp-border').html(hp);
});
$('.hp-border').html(hp);
$('.user-hp').css("width", x2+'%');
socket.on('disconnect', function(data) {
//alert('Вы отключены от сервера!');
//alert(data);
// window.location.replace(host + 'serverfull');
});
socket.on('connections', function(data) {
$('.users-count').html(data);
});
socket.on('hp line width', function(data) {
x = data;
$('.hp-line').css("width", x+'%');
$('.user-hp').css("width", x2+'%');
});
socket.on('w', function(data) {
x2 = data;
$('.user-hp').css("width", x2+'%');
});
socket.on('chat msg', function(data) {
message = data;
$(".chat-box").append("<li class='message'><div class='name'></div>" + message + "</li>");
});
socket.on('ban', function(data) {
alert(data);
});
socket.on('win', function(data) {
alert('You win this battle!');
win = 'win';
//window.location.replace("/");
});
socket.on('lose', function(data) {
$(".chat-box").append("<li class='message'><div class='name'></div>" + youlose + "</li>");
alert('You lose this battle!');
lose = 'lose';
hp = maxhp;
});
socket.on('server full', function(data) {
alert(data);
});
socket.on('draw', function(data) {
alert('DRAW!Revenge!');
draw = 'draw';
});
window.onbeforeunload = function() {
//return "Данные не сохранены. Точно перейти?";
};
var express = require('express');
var app = express();
var http = require('http').Server(app);
var mysql = require('mysql');
var session = require("express-session")({
secret: 'boss',
resave: true,
saveUninitialized: true
});
var sharedsession = require("express-socket.io-session");
var connections=0,max_connections=5,port = 3000,maxhp=300000,hp=maxhp,x,msg,db;
var max_user_hp,user_hp,x2,email,nick;
var hour = 3600000;
let io = require('socket.io')(http, {
});
app.use(express.static(__dirname + '/client'));
app.use(session);
// Use shared session middleware for socket.io
// setting autoSave:true
io.use(sharedsession(session, {
autoSave:true
}));
app.get('/', function(req,res) {
res.sendFile(__dirname + '/client/public/auth.html');
});
app.get('/boss', function(req,res) {
res.sendFile(__dirname + '/client/public/boss.html');
});
http.listen(port, function() {
console.log('listening on *:' + port);
});
db = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'game'
});
db.connect((err) => {
if(err) console.log(err);
console.log("Mysql connected");
});
io.on('connection', function(socket) {
//Контроль подключений
connections+=1;
io.emit('connections', connections);
console.log('connected client:' + connections);
if(connections > max_connections) {
socket.on('forceDisconnect', () => {
socket.disconnect(true);
console.log('Server full!' + max_connections + '/' + max_connections);
});
}
socket.on( 'disconnect', () => {
connections -= 1;
console.log( 'client disconnected:' + connections);
io.emit('connections', connections);
io.emit('server full', 'Вы отключены от сервера!');
});
//io.set("reconnection limit", 5000);
//end
//Хп босса
socket.on('boss fight', function(data) {
hp = hp-data;
if(hp<=0) {
hp = maxhp;
io.emit('win');
}
x = hp/maxhp*100;
io.emit('realtime hp', hp);
if(hp>0) io.emit('hp line width', x);
if(hp<=0) io.emit('hp line width', 0);
if(hp+user_hp <= 0) {
io.emit('draw');
}
});
socket.on('hp line for all', function(data) {
x = hp/maxhp*100;
io.emit('realtime hp', hp);
io.emit('hp line width', x);
if(hp<=0) {
io.emit('hp line width', 0);
io.emit('win');
}
if(hp+user_hp <= 0) {
io.emit('draw');
}
});
//end
socket.handshake.session.maxhp = 300000;
max_user_hp = socket.handshake.session.maxhp;
socket.handshake.session.hp = max_user_hp;
user_hp = max_user_hp;
//Хп игрока
socket.on('user fight', function(data) {
user_hp = user_hp-data;
if(user_hp<=0) {
user_hp = max_user_hp;
socket.emit('lose');
}
x2 = user_hp/max_user_hp*100;
socket.emit('user hp', user_hp);
if(user_hp>0) socket.emit('user hp line width', x2);
if(user_hp<=0) socket.emit('user hp line width', 0);
if(hp+user_hp <= 0) {
socket.emit('draw');
}
});
//io.sockets.connected[socket.id].emit('class', 'alert');
socket.on('hp line for all', function(data) {
x2 = user_hp/max_user_hp*100;
socket.emit('user hp line width', x2);
if(user_hp<=0) {
socket.emit('user hp line width', 0);
socket.emit('lose');
}
if(hp+user_hp <= 0) {
socket.emit('draw');
}
});
//end
//Чат
socket.on('chat message', function(data) {
msg = data;
io.emit('chat msg',msg);
});
//end
//Блокировка пользователей
socket.on('banned', function(data) {
var ban = '';
if(data == ban) {
io.emit('ban', 'Вы забанены!');
socket.disconnect(true);
}
});
//end
//Регистрация
socket.on('email', function(data) {
email = data;
});
socket.on('nick', function(data) {
nick = data;
console.log(data);
});
socket.on('query', function(data) {
db.query("INSERT INTO players (id,nickname,email) VALUES('','" + nick + "','" + email + "')", (err,result) => {
if(err) console.log(err);
console.log("Insert");
});
});
//end
});
Уже и в браузере не работает, я подключаюсь к Mariadb