Run.lua
DiceTextureLoc = "res/images/OldDie.png"
LastBet = {1, 2}
GuiCurQuantity = LastBet[1] + 1
GuiCurFace = LastBet[2]
yourTurn = 0
gameOverMsg = "You lost!"
gameOver = false
totalDice = 10
cn = ClientNetwork.new()
function mysplit(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
function Init()
if cn:connect("127.0.0.1", 5576) == SocketStatus.Done then
print("Connected!")
cn:send("join")
else
print("Connection to server failed!")
end
LocalPlayer:giveDice(5)
LocalPlayerHand = {0,0,0,0,0,0}
LocalPlayerHand[1] = LocalPlayer:rollDice(1)
LocalPlayerHand[2] = LocalPlayer:rollDice(2)
LocalPlayerHand[3] = LocalPlayer:rollDice(3)
LocalPlayerHand[4] = LocalPlayer:rollDice(4)
LocalPlayerHand[5] = LocalPlayer:rollDice(5)
end
function Update()
message = cn:update()
if message ~= " " then
print(message)
if message == "Full" then
print("Server full")
elseif message == "go" then
yourTurn = 1
elseif string.find(message, ",") then
LastBet = mysplit(message, ",")
LastBet[1] = tonumber(LastBet[1])
LastBet[2] = tonumber(LastBet[2])
GuiCurQuantity = LastBet[1]
GuiCurFace = LastBet[2]
yourTurn = 1
elseif message == "stop" then
yourTurn = 0
elseif message == "chal" then
cn:send("hand," .. LocalPlayerHand[1] .. "," .. LocalPlayerHand[2] .. "," .. LocalPlayerHand[3] .. "," .. LocalPlayerHand[4] .. "," .. LocalPlayerHand[5])
yourTurn = 0
else
gameOver = true
gameOverMsg = message
end
end
ImGui_Begin("Menu")
ImGui_BeginChild("Quantity", ImVec2.new(175, 140), true)
for i = 2, 10 do
if i ~= nil and LastBet[1] ~= nil then
if i >= LastBet[1] then
if ImGui_Selectable(i, GuiCurQuantity == i) then
GuiCurQuantity = i
end
end
end
end
ImGui_EndChild()
ImGui_SameLine(190)
ImGui_BeginChild("Face", ImVec2.new(175, 140), true)
for j = 1, 6 do
if j ~= nil and LastBet[2] ~= nil then
if j >= LastBet[2] then
if ImGui_Selectable(j, GuiCurFace == j) then
GuiCurFace = j
end
end
end
end
ImGui_EndChild()
if (LastBet[1] ~= GuiCurQuantity or LastBet[2] ~= GuiCurFace) and yourTurn == 1 then
if ImGui_Button("Bid") then
LastBet = {GuiCurQuantity, GuiCurFace}
GuiCurQuantity = LastBet[1]
GuiCurFace = LastBet[2]
cn:send(GuiCurQuantity .. "," .. GuiCurFace)
end
ImGui_SameLine(75)
end
if (LastBet[1] ~= 1 or LastBet[2] ~= 2) and yourTurn == 1 then
if ImGui_Button("Challenge Bid") then
cn:send("chal")
end
end
if gameOver then
ImGui_Text(gameOverMsg)
end
ImGui_End()
end
function Close()
cn:disconnect()
end
function Debug()
print(yourTurn)
end
Server.lua
sn = ServerNetwork.new(5576)
PlayersHands = { p1Set = false, p1 = {"hand",0,0,0,0,0}, p2Set = false, p2 = {"hand",0,0,0,0,0} }
LastBet = {1, 2}
Connected = {0, 0}
GameOver = false
function mysplit(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
function printTable(inputTable)
for i = 0, #inputTable do
if (inputTable[i] ~= SocketStatus.Error) then
print(i .. ": " .. inputTable[i])
end
end
end
function CountFaces(hand, faceToCount)
ret = 0
for _, face in ipairs(hand) do
if face == faceToCount or face == 1 then
ret = ret + 1
end
end
return ret
end
function Init()
end
function Update()
message = sn:update()
if message.msg ~= " " then
print(message.msg)
if message.msg == "join" then
if Connected[1] == 0 then
Connected[1] = 1
print(sn:send("go", message.sender))
elseif Connected[2] == 0 then
Connected[2] = 1
else
sn:send("Full", message.sender)
sn:disconnect(message.sender)
end
elseif string.find(message.msg, "hand") then
if not PlayersHands[1] then
PlayersHands[2] = mysplit(message.msg, ",")
PlayersHands[2][2] = tonumber(PlayersHands[2][2])
PlayersHands[2][3] = tonumber(PlayersHands[2][3])
PlayersHands[2][4] = tonumber(PlayersHands[2][4])
PlayersHands[2][5] = tonumber(PlayersHands[2][5])
PlayersHands[2][6] = tonumber(PlayersHands[2][6])
PlayersHands[1] = true
elseif not PlayersHands[3] then
PlayersHands[4] = mysplit(message.msg, ",")
PlayersHands[4][2] = tonumber(PlayersHands[4][2])
PlayersHands[4][3] = tonumber(PlayersHands[4][3])
PlayersHands[4][4] = tonumber(PlayersHands[4][4])
PlayersHands[4][5] = tonumber(PlayersHands[4][5])
PlayersHands[4][6] = tonumber(PlayersHands[4][6])
PlayersHands[3] = true
end
elseif string.find(message.msg, ",") then
LastBet = mysplit(message.msg, ",")
LastBet[1] = tonumber(LastBet[1])
LastBet[2] = tonumber(LastBet[2])
sn:send("stop", message.sender)
sn:broadcast(message.msg, message.sender)
elseif message.msg == "chal" then
challenger = message.sender
sn:broadcast(message.msg, -1)
else
print("Unknown message")
end
end
if PlayersHands[1] and PlayersHands[3] and not GameOver then
FaceCount = CountFaces(PlayersHands[2], LastBet[2])
FaceCount = FaceCount + CountFaces(PlayersHands[4], LastBet[2])
print(FaceCount >= LastBet[1])
if FaceCount >= LastBet[1] then
sn:send("You lost! There were " .. FaceCount .. " " .. LastBet[2] .. "s.", challenger)
sn:broadcast("You won! Your opponent challenged your bid, but there were " .. FaceCount .. " " .. LastBet[2] .. "s.", challenger)
else
sn:send("You won! There were " .. FaceCount .. " " .. LastBet[2] .. "s.", challenger)
sn:broadcast("You lost! Your opponent called your bluff, there were " .. FaceCount .. " " .. LastBet[2] .. "s.", challenger)
end
GameOver = true
end
end
function Close()
--sn:shutdown()
end
function Debug()
print(PlayersHands[2][2] .. " " .. PlayersHands[2][3] .. " " .. PlayersHands[2][4] .. " " .. PlayersHands[2][5] .. " " .. PlayersHands[2][6])
print(PlayersHands[4][2] .. " " .. PlayersHands[4][3] .. " " .. PlayersHands[4][4] .. " " .. PlayersHands[4][5] .. " " .. PlayersHands[4][6])
end