Bläddra i källkod

Online update

tags/online
madiwka3 4 år sedan
förälder
incheckning
b6b674f0cf
5 ändrade filer med 169 tillägg och 25 borttagningar
  1. +93
    -25
      main.lua
  2. +1
    -0
      server/INSTRUCTIONS
  3. +69
    -0
      server/server.lua
  4. +5
    -0
      src/baseGame.lua
  5. +1
    -0
      src/dependencies.lua

+ 93
- 25
main.lua Visa fil

@@ -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
@@ -700,7 +712,7 @@ function love.update(dt)
print("IMPORTANT!!!!!" .. globalState .. gameState)
staticanimatorcounter(dt)
musicController('norm', 1)
ts = ts + dt
if debug then
displayFPS()
end
@@ -712,7 +724,11 @@ function love.update(dt)
end
if globalState == "nettest" then
basegame(dt)
print("Confcode: " .. confirmation)
ts = ts + dt
if confirmation == "N" then
basegame(dt)
end
if ts > updaterate then
nettest(dt)
ts = ts - updaterate
@@ -720,7 +736,8 @@ function love.update(dt)
end
if globalState == "clienttest" then
if confirmation ~= "disconnected" then
ts = ts + dt
if confirmation == "N" then
lastSentKeyP1 = lastSentKeyClient
clientsBaseGame(dt)
end
@@ -734,10 +751,16 @@ function love.update(dt)
end
serverinit = false
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)
@@ -745,39 +768,59 @@ function nettest(dt)
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))
print("SENT: " .. lastSentKey)
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) .. "|HOST")
print("SENT: " .. lastSentKey .. " TO ADDRESS: " .. IP)
end
data = udp:receive()
if data then
local p = split(data, '|')
if data then
confirmation = "N"
local p = split(data, '|')
if p[3] ~= "CLIENT" then
confirmation = "U"
end
if tonumber(p[4]) > 5 then
confirmation = "L"
end
lastSentKeyClient = p[1]
end
player2.y = tonumber(p[2])
else
confirmation = "D"
print("NO PLAYER 2!!")
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)
clientinit = true
end
udp:send(tostring(lastSentKey))
print("SENT TO SERVER:" .. lastSentKey)
udp:send(tostring(lastSentKey) .. '|' .. tostring(player2.y) .. "|CLIENT")
print("SENT TO SERVER:" .. lastSentKey .. " AT ADDRESS: " .. IP)
data = udp:receive()
--print(data)
if data then
if data then
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[13] then
if p[13] ~= "HOST" then
confirmation = "U"
end
if tonumber(p[14]) > 5 then
confirmation = "L"
end
for i = 1, maxBalls do
local die = tonumber(p[2])
lastSentKeyClient, ball[i].dy, player2.y, player1.y, player1score, player2score, player1nukescore, player2nukescore, 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]), tonumber(p[9]), tonumber(p[10]), p[11], tonumber(p[12])
end
end
else
confirmation = "disconnected"
confirmation = "D"
end
print(confirmation .. " recieved " .. lastSentKeyClient .. " AND ")

@@ -902,6 +945,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 +1309,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 Visa fil

@@ -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 Visa fil

@@ -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

+ 5
- 0
src/baseGame.lua Visa fil

@@ -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


+ 1
- 0
src/dependencies.lua Visa fil

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

Laddar…
Avbryt
Spara