Browse Source

Online update

tags/crossplatform
madiwka3 3 years ago
committed by Madiwka
parent
commit
2651e2cf6d
5 changed files with 235 additions and 51 deletions
  1. +152
    -38
      main.lua
  2. +1
    -0
      server/INSTRUCTIONS
  3. +69
    -0
      server/server.lua
  4. +12
    -13
      src/baseGame.lua
  5. +1
    -0
      src/dependencies.lua

+ 152
- 38
main.lua View File

@@ -30,13 +30,14 @@ hitNum = {}
hitNum[1] = 0
hitNum[2] = 0
hitNum[3] = 0
confirmation = "disconnected"
confirmation = "N"
hitNum[4] = 0
p1bonus = 0
p2bonus = 0
hitNum[5] = 0
hitNum[6] = 0
GREEN = 255
IP = '45.76.95.31'
BLUE = 255
updateTEXT = "Chalkboard Update"
maxBalls = 1
@@ -125,6 +126,7 @@ function newWall(wallx, wally, wallwidth, wallheight)
end
speedParameters = {}
buttons = {}
IPselect = {}
difbuttons = {}
settings = {}
walls = {}
@@ -140,6 +142,7 @@ function controlChanger()
end
end
function love.load()
love.keyboard.setKeyRepeat(true)
simpleScale.setWindow(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT)
configfile = io.open("config.lua", "r")
configsave = io.open("config.lua", "w")
@@ -196,7 +199,16 @@ function love.load()
table.insert(
buttons,
newButton(
"Online Test",
"Online",
function()
gameState = "chooseIP"
end
)
)
table.insert(
IPselect,
newButton(
"Host",
function()
globalState = "nettest"
AGAINST_AI = 0
@@ -205,9 +217,9 @@ function love.load()
)
)
table.insert(
buttons,
IPselect,
newButton(
"Client Test",
"Guest",
function()
globalState = "clienttest"
AGAINST_AI = 0
@@ -697,10 +709,10 @@ function speedControl()
end

function love.update(dt)
print("IMPORTANT!!!!!" .. globalState .. gameState)
--print("IMPORTANT!!!!!" .. globalState .. gameState)
staticanimatorcounter(dt)
musicController('norm', 1)
ts = ts + dt
if debug then
displayFPS()
end
@@ -712,75 +724,152 @@ function love.update(dt)
end
if globalState == "nettest" then
basegame(dt)
if ts > updaterate then
--print("Confcode: " .. confirmation)
if confirmation == "N" then
basegame(dt)
end
nettest(dt)
ts = ts - updaterate
end
end
if globalState == "clienttest" then
if confirmation ~= "disconnected" then
ts = ts + dt
if confirmation == "N" then
lastSentKeyP1 = lastSentKeyClient
clientsBaseGame(dt)
end
if ts > updaterate then
end
clienttest(dt)
ts = ts - updaterate
end
end

end
serverinit = false
datawaspassedtimer = 0
clientinit = false
function love.textinput(t)
if gameState == "chooseIP" then
IP = IP .. t
end
end
function nettest(dt)
if serverinit == false then
local socket = require "socket"
local address, port = '45.76.95.31', 12345
local address, port = IP, 12345
print(address)
udp = socket.udp()
udp:setpeername(address, port)
udp:settimeout(0)
serverinit = true
end
for i = 1, maxBalls do
print (tostring(ball[i].dy))
udp:send(tostring(lastSentKey) ..'|'.. tostring(ball[i].dy) .. '|' .. tostring(player2.y) .. '|' .. tostring(player1.y) .. '|' .. tostring(player1score) .. '|' .. tostring(player2score) .. '|' .. tostring(player1nukescore) .. '|' .. tostring(player2nukescore) .. "|confirmed|" .. tostring(ball[i].x) .. '|' .. tostring(ball[i].y) .. '|' .. gameState .. '|' .. tostring(ball[i].dx))
ts = ts + dt
if ts > updaterate then
--print (tostring(ball[i].dy))
udp:send(tostring(lastSentKey) ..
'|' .. tostring(ball[i].dy) ..
'|' .. tostring(player2.y) ..
'|' .. tostring(player1.y) ..
'|' .. tostring(player1score) ..
'|' .. tostring(player2score) ..
'|' .. tostring(player1nukescore) ..
'|' .. tostring(player2nukescore) ..
'|' .. tostring(ball[i].x) ..
'|' .. tostring(ball[i].y) ..
'|' .. gameState ..
'|' .. tostring(ball[i].dx) ..
'|' .. tostring(ballSpeed) ..
"|HOST")
print("SENT: " .. lastSentKey)
ts = 0
end
end
data = udp:receive()
if data then
local p = split(data, '|')

local data
local datanumtest = 0
local datawaspassed = false
repeat
datanumtest = datanumtest + 1
print("LATENCY: " .. tostring(datanumtest))
data = udp:receive()
if data then
datawaspassed = true
print("ReceivedINFO: " .. data)
confirmation = "N"
local p = split(data, '|')
if p[3] ~= "CLIENT" then
confirmation = "U"
end
if tonumber(p[4]) > 90 then
confirmation = "L"
end
lastSentKeyClient = p[1]
end
player2.y = tonumber(p[2])
end
until not data
if not datawaspassed then
datawaspassedtimer = datawaspassedtimer + 1
if datawaspassedtimer > 5 then
confirmation = "D"
datawaspassedtimer = 0
end
else
datawaspassedtimer = 0
end
end
function clienttest(dt)
if clientinit == false then
local socket = require "socket"
local address, port = '45.76.95.31', 12345
local address, port = IP, 12345
udp = socket.udp()
udp:setpeername(address, port)
udp:settimeout(0)
udp:send(tostring(lastSentKey) ..'|' .. player2.y .. "|CLIENT")
clientinit = true
end
udp:send(tostring(lastSentKey))
print("SENT TO SERVER:" .. lastSentKey)
data = udp:receive()
--print(data)
if data then
ts = ts + dt
if ts > updaterate then
udp:send(tostring(lastSentKey) ..'|' .. player2.y .. "|CLIENT")
ts = 0
end
local data
local datanumtest = 0
local datawaspassed = false
repeat
datanumtest = datanumtest + 1
print("LATENCY: " .. tostring(datanumtest))
data = udp:receive()
if data then
print("RECEIVED DATA: " .. data)
datawaspassed = true
print("SENT TO SERVER:" .. lastSentKey)
confirmation = "N"
local p = split(data, '|')
for i = 1, maxBalls do
local die = tonumber(p[2])
print(p[2])
print(p[2] + 0)
print(tonumber(p[11]))
lastSentKeyClient, ball[i].dy, player2.y, player1.y, player1score, player2score, player1nukescore, player2nukescore, confirmation, ball[i].x, ball[i].y, gameState, ball[i].dx = p[1], die, tonumber(p[3]), tonumber(p[4]), tonumber(p[5]), tonumber(p[6]), tonumber(p[7]), tonumber(p[8]), p[9], tonumber(p[10]), tonumber(p[11]), p[12], tonumber(p[13])
if p[14] then
if p[14] ~= "HOST" then
confirmation = "U"
end
if tonumber(p[15]) > 90 then
confirmation = "L"
end
for i = 1, maxBalls do
local die = tonumber(p[2])
lastSentKeyClient, ball[i].dy, player1.y, player1score, player2score, player1nukescore, player2nukescore, ball[i].x, ball[i].y, gameState, ball[i].dx, ballSpeed = p[1], die, tonumber(p[4]), tonumber(p[5]), tonumber(p[6]), tonumber(p[7]), tonumber(p[8]), tonumber(p[9]), tonumber(p[10]), p[11], tonumber(p[12]), tonumber(p[13])
end
end
end
print("GOT: " .. lastSentKeyClient)
until not data
if not datawaspassed then
datawaspassedtimer = datawaspassedtimer + 1
if datawaspassedtimer > 5 then
confirmation = "D"
datawaspassedtimer = 0
end
else
confirmation = "disconnected"
datawaspassedtimer = 0
end
print(confirmation .. " recieved " .. lastSentKeyClient .. " AND ")

end
function wallbreaker(x, y)
if (gameState == "editor") then
@@ -902,6 +991,18 @@ function dangerChecker() --CHECK IF CONTROLS ARE DUPLICATING
end
function love.keypressed(key)
lastSentKey = key
if gameState == "chooseIP" then
if key == "backspace" then
-- get the byte offset to the last UTF-8 character in the string.
local byteoffset = utf8.offset(IP, -1)
if byteoffset then
-- remove the last UTF-8 character.
-- string.sub operates on bytes rather than UTF-8 characters, so we couldn't do string.sub(text, 1, -2).
IP = string.sub(IP, 1, byteoffset - 1)
end
end
end
if gameState == "assign" then
if (req == "p1up") then
p1control.up = key
@@ -1254,8 +1355,21 @@ end

function love.draw()
simpleScale.set()

baseDraw()
if (globalState == "nettest" or globalState == "clienttest") and confirmation == "D" then
love.graphics.clear(50 / 255, 50 / 255, 50 / 255, 255)
love.graphics.printf("WAIT FOR PLAYER 2", 0, VIRTUAL_HEIGHT / 2, VIRTUAL_WIDTH, "center")
end
if (globalState == "nettest" or globalState == "clienttest") and confirmation == "U" then
love.graphics.clear(50 / 255, 50 / 255, 50 / 255, 255)
love.graphics.printf("LOBBY FULL OR WRONG MODE CHOSEN", 0, VIRTUAL_HEIGHT / 2, VIRTUAL_WIDTH, "center")
end
if (globalState == "nettest" or globalState == "clienttest") and confirmation == "L" then
love.graphics.clear(50 / 255, 50 / 255, 50 / 255, 255)
love.graphics.printf("POOR CONNECTION TO SERVER", 0, VIRTUAL_HEIGHT / 2, VIRTUAL_WIDTH, "center")
end
simpleScale.unSet()
end


+ 1
- 0
server/INSTRUCTIONS View File

@@ -0,0 +1 @@
First, install Lua and Lua-Socket. Then, launch the server with "lua server.lua". Afterwards, launch your game, enter "127.0.0.1" as IP on this PC, and your local address on your friends PC (e.g 192.168.1.110). One player picks Host, the other Picks Client!

+ 69
- 0
server/server.lua View File

@@ -0,0 +1,69 @@
local running = true
local socket = require 'socket'
local udp = socket.udp()
local player1ip, player2ip, p1data, p2data, player1port, player2port = "none", "none", nil, nil, nil, nil
udp:settimeout(0)
udp:setsockname('*', 12345)
local p1ping = 0
local p2ping = 0
local data, msg_or_ip, port_or_nil
while running do
data, msg_or_ip, port_or_nil = udp:receivefrom()
if data then
if (player1ip == msg_or_ip) then
p1ping = 0
p1data = data
elseif player2ip == msg_or_ip then
p2data = data
p2ping = 0
else
if (player1ip == "none") then
player1ip = msg_or_ip
p1data = data
player1port = port_or_nil
print("CONNECTED: PLAYER 1 FROM: " .. player1ip)
elseif player2ip == "none" and msg_or_ip ~= player1ip then
player2ip = msg_or_ip
p2data = data
player2port = port_or_nil
print("CONNECTED: PLAYER 2 FROM: " .. player2ip)
elseif (player1ip ~= msg_or_ip and player2ip ~= msg_or_ip) then
print("Lobby Full!" .. player1ip .. player2ip)
end
end


elseif player1ip ~= "none" then
p1ping = p1ping + 1
if p1ping > 5 then
if p2data then
udp:sendto(p2data .. '|' .. p1ping, player1ip, player1port)
end
print("PLAYER 1 DISCONNECTED")
p1data = nil
player1ip = "none"
player1port = nil
end
elseif player2ip ~= "none" then
p2ping = p2ping + 1
if p2ping > 5 then
if p1data then
udp:sendto(p1data .. '|' .. p2ping, player2ip, player2port)
end
print("PLAYER 2 DISCONNECTED")
p2data = nil
player2ip = "none"
player2port = nil
end
end
if data then
print(data .. "FROM " .. msg_or_ip .. "Playerlist: " .. player1ip .. " " .. player2ip)
end
if p1data and p2data then
udp:sendto(p1data .. '|' .. p2ping, player2ip, player2port)
udp:sendto(p2data .. '|' .. p1ping, player1ip, player1port)
print("SENT1: " .. player2ip .. " " .. player2port .. " " .. p1data)
print("SENT2: " .. player1ip .. " " .. player1port .. " " .. p2data)
end
socket.sleep(0.01)
end

+ 12
- 13
src/baseGame.lua View File

@@ -28,11 +28,11 @@ function basegame(dt)
player1.dy = 0
end
if (AGAINST_AI == 0) then
if ((globalState ~= "nettest" and love.keyboard.isDown(p2control.up)) or lastSentKeyClient == p2control.up) then
if ((globalState ~= "nettest" and love.keyboard.isDown(p2control.up)) ) then
player2.dy = (paddle_SPEED + p2bonus) * -1
elseif ((globalState ~= "nettest" and love.keyboard.isDown(p2control.down)) or lastSentKeyClient == p2control.down) then
elseif ((globalState ~= "nettest" and love.keyboard.isDown(p2control.down))) then
player2.dy = paddle_SPEED + p2bonus
else
elseif (globalState ~= "nettest") then
player2.dy = 0
end
end
@@ -456,20 +456,20 @@ end
function powerAvailability()
if (player1nukescore >= 20 and player1nukescore < 140) then
potentialstrike1 = 1
if (((globalState ~= "clienttest" and love.keyboard.isDown(p1control.super)) or (confirmation == "confirmed" and lastSentKeyP1 == p1control.super)) ) then
if (((globalState ~= "clienttest" and love.keyboard.isDown(p1control.super)) or (globalState == "clienttest" and lastSentKeyP1 == p1control.super)) ) then
player1striken = 1
player1reverbav = 0
end
end
if (player1nukescore >= 140) and timeIsSlow2 == false and timeIsSlow == false then
player1reverbav = 1
if ((confirmation == "confirmed" and lastSentKeyP1 == p1control.counter) or (globalState ~= "clienttest" and love.keyboard.isDown(p1control.counter))) then
if ((globalState == "clienttest" and lastSentKeyP1 == p1control.counter) or (globalState ~= "clienttest" and love.keyboard.isDown(p1control.counter))) then
powerControl(1, "special")
end
end
if (player1nukescore >= 200) then
potentialnuke1 = 1
if ((confirmation == "confirmed" and lastSentKeyP1 == p1control.super)or (globalState ~= "clienttest" and love.keyboard.isDown(p1control.super))) then
if ((globalState == "clienttest" and lastSentKeyP1 == p1control.super)or (globalState ~= "clienttest" and love.keyboard.isDown(p1control.super))) then
sounds["nuke"]:play()
if areanuclear == 1 then
maxspeed = maxspeed + 50
@@ -685,6 +685,11 @@ function menuDraw()
mymenu:butt(gameState, VIRTUAL_WIDTH, VIRTUAL_HEIGHT, modeSelectorButtons, sounds, "middle")
love.keyboard.mouseisReleased = false
end
if gameState == "chooseIP" then
mymenu:butt(gameState, VIRTUAL_WIDTH, VIRTUAL_HEIGHT, IPselect, sounds, "middle")
love.graphics.printf(IP, 0, VIRTUAL_HEIGHT / 4, VIRTUAL_WIDTH, "center")
love.keyboard.mouseisReleased = false
end
if gameState == "menu" then
mymenu:butt(gameState, VIRTUAL_WIDTH, VIRTUAL_HEIGHT, buttons, sounds, "middle")
love.keyboard.mouseisReleased = false
@@ -960,12 +965,6 @@ function rules(query, i)
end
end
function clientsBaseGame(dt)
if confirmation == "confirmed" then
player2.RED = 0
print("KEYS ARE: " .. confirmation .. " " .. lastSentKeyP1)
else
TEXT = "DISCONNECTED"
end
if gameMode == "reverse" then
reversegame(dt)
end
@@ -990,7 +989,7 @@ function clientsBaseGame(dt)
print("moving player1 down")
else
player1.dy = 0
print("stopping player")
-- print("stopping player")
end
if ((love.keyboard.isDown(p2control.up))) then
player2.dy = (paddle_SPEED + p2bonus) * -1


+ 1
- 0
src/dependencies.lua View File

@@ -13,4 +13,5 @@ require 'src/constantvars'
require 'src/menus'
require 'src/AI'
require 'src/reverseGame'
utf8 = require("utf8")
serialize = require 'ser'

Loading…
Cancel
Save