MultiplayerDemo
import Felgo 3.0
import QtQuick 2.3
import "../common"
import "../game"
SceneBase {
id: gameScene
property alias clock: clock
property int multiplayerCodeAddTime: 0
property int multiplayerCodeSetTime: 1
Item {
id: gameLogic
function playNumber(number){
if (multiplayer.myTurn) {
gConsole.printLn('Playing Number ' + number + ' (as Player ' + multiplayer.localPlayer.name + ')');
clock.addTime(number);
multiplayer.sendMessage(multiplayerCodeAddTime, number);
multiplayer.sendNextTurnMessage("Play Number " + number);
} else {
gConsole.printLn("Can't play Number, not my turn. Not sending anything.");
}
}
function getAIMove(){
return Math.floor(Math.random() * 3) + 1;
}
function executeAIMove() {
var aiBone = getAIMove();
var currentTurnPlayerId = multiplayer.activePlayer.userId;
gConsole.printLn('>> Leader (' + multiplayer.leaderPlayer.name + ') playing Number on behalf of ' + multiplayer.activePlayer.name + ' using AI: Number '+ aiBone);
clock.addTime(aiBone);
multiplayer.sendMessage(multiplayerCodeAddTime, aiBone);
}
function startTurnTimer() {
timer.stop();
timerView.remainingTime = 10;
timer.start();
}
function turnStarted(playerId) {
gConsole.printLn("Turn started for " + multiplayer.activePlayer.name)
gameLogic.startTurnTimer();
multiplayer.leaderCode(function() {
if (!multiplayer.activePlayer.connected) {
aiTimeOut.start();
}
});
}
function turnTimedOut(){
timer.stop();
multiplayer.leaderCode(function () {
executeAIMove();
multiplayer.triggerNextTurn();
});
}
function createGame(){
timerView.textOverride = 'Waiting for players...';
multiplayer.createGame();
}
function leaveGame(){
timerView.textOverride = 'Leaving Game';
multiplayer.leaveGame();
aiTimeOut.stop();
timer.stop();
}
function joinGame(room){
timerView.textOverride = 'Waiting for players...';
multiplayer.joinGame(room);
}
function initGame(){
console.debug("initGame() called, multiplayer.initialized:", multiplayer.initialized)
clock.reset()
}
}
Timer {
id: timer
repeat: true
interval: 1000;
onTriggered: {
timerView.remainingTime -= 1;
if (timerView.remainingTime === 0) {
gameLogic.turnTimedOut();
}
}
}
Timer {
id: aiTimeOut
interval: 3000;
onTriggered: {
gameLogic.executeAIMove();
multiplayer.triggerNextTurn();
}
}
Connections {
target: multiplayer
enabled: activeScene === gameScene
onPlayerJoined: {
console.debug("GameScene.onPlayerJoined:", player)
multiplayer.leaderCode(function() {
console.debug("amLeader is true, sending the clock time:", clock.time)
multiplayer.sendMessage(multiplayerCodeSetTime, clock.time)
});
}
onPlayerLeft:{
console.debug("GameScene.onPlayerLeft", player, JSON.stringify(player))
}
onPlayerChanged: {
console.debug("GameScene.onPlayerChanged:", player, JSON.stringify(player))
}
onPlayersReady: {
console.debug("GameScene.onPlayersReady")
}
onGameStarted: {
console.debug("GameScene.onGameStarted in game logic received, gameRestarted:", gameRestarted)
clock.reset()
}
onGameEnded: {
console.debug("GameScene.onGameEnded received")
}
onLeaderPlayerChanged:{
gConsole.printLn("New Leader elected: " + multiplayer.leaderPlayer.name)
}
onActivePlayerChanged:{
}
onTurnStarted:{
gameLogic.turnStarted(playerId);
}
onGamePropertiesChanged: {
console.debug("xxx-onGamePropertiesChanged, changedProps:", JSON.stringify(changedGameProperties), byClient)
}
onMessageReceived: {
console.debug("GameScene.game logic: received message with code", code, ", message:", message)
if (code == multiplayerCodeAddTime) {
clock.addTime(message);
} else if (code == multiplayerCodeSetTime) {
clock.setTime(message);
}
}
}
Rectangle {
anchors.fill: gameWindowAnchorItem
color: clock.sky
Behavior on color {
ColorAnimation { easing.type: Easing.InOutQuad; duration: 400 }
}
}
TimerView {
id: timerView
remainingTime: 0
anchors.right: gameWindowAnchorItem.right
anchors.rightMargin: 10
anchors.bottom: gConsole.top
anchors.bottomMargin: 10
}
Text {
text: "Turn the time by 1, 2 or 3 hours.\nTry to land on midnight or noon."
color: "black"
font.pixelSize: 9
anchors.left: gameWindowAnchorItem.left
anchors.leftMargin: 10
anchors.bottom: gConsole.top
anchors.bottomMargin: 10
}
Column {
anchors.right: gameWindowAnchorItem.right
anchors.rightMargin: 10
anchors.top: gameWindowAnchorItem.top
anchors.topMargin: 10
spacing: 10
Button {
id: closeButton
text: "Close"
radius: 4
backgroundColor: Qt.lighter("#f05352", 1.2)
anchors.right: parent.right
onClicked: {
gameLogic.leaveGame();
backButtonPressed();
}
}
Button {
text: "Toggle GameOpen\nNow: " + multiplayer.gameOpenForLateJoin
radius: 4
backgroundColor: Qt.lighter("#f05352", 1.2)
anchors.right: parent.right
visible: multiplayer.amLeader
onClicked: {
console.debug("old value for isGameOpenForLateJoin:", multiplayer.gameOpenForLateJoin)
multiplayer.setGameOpenForLateJoin(!multiplayer.gameOpenForLateJoin, true)
console.debug("new value for isGameOpenForLateJoin:", multiplayer.gameOpenForLateJoin)
}
}
Text {
text: "Available Player Slots:" + multiplayer.availablePlayerSlots
anchors.right: parent.right
}
}
Rectangle {
z:1
anchors.fill: playerInfoText
color: "white"
visible: playerInfoText.visible
}
Text {
id: playerInfoText
z: 1
anchors.bottom: parent.gameWindowAnchorItem.bottom
text: getShortPlayerInfo()
font.pixelSize: sp(9)
width: parent.width
wrapMode: Text.WrapAnywhere
color: "black"
visible: false
}
function getShortPlayerInfo() {
var text = ""
for (var i=0; i<multiplayer.players.length; i++) {
var p = multiplayer.players[i]
text += "" + i + ": " + p.nickName + "(" + p.userId + "), oldUserId: " + p.oldUserId + ", activeTurn: " + p.activeTurn + ", leader: " + p.leader + "\n"
}
return text
}
Row{
anchors.top: gameWindowAnchorItem.top
anchors.topMargin: 10
anchors.horizontalCenter: gameWindowAnchorItem.horizontalCenter
spacing: 4
z:1
Repeater {
id: actionButtons
model: 3
Button {
width: 40
height: 40
radius: 5
text: index + 1
backgroundColor: multiplayer.myTurn ? "#90119851" : "#90FF032D"
onClicked: {
gameLogic.playNumber(index + 1);
}
}
}
}
Column {
anchors.left: gameWindowAnchorItem.left
anchors.leftMargin: 10
anchors.top: gameWindowAnchorItem.top
anchors.topMargin: 10
spacing: 4
Repeater {
id: players
model: multiplayer.players
PlayerTag {}
}
}
Button {
id: startButton
visible: multiplayer.amLeader
radius: 4
text: "Restart Game"
backgroundColor: "#747474"
anchors.bottom: gConsole.top
anchors.bottomMargin: 10
onClicked: {
multiplayer.restartGame()
}
}
GConsole {
id: gConsole
height: 100
anchors.left: gameWindowAnchorItem.left
anchors.right: gameWindowAnchorItem.right
anchors.bottom: gameWindowAnchorItem.bottom
}
Clock {
id: clock
anchors.horizontalCenter: gameWindowAnchorItem.horizontalCenter
anchors.top: gameWindowAnchorItem.top
anchors.topMargin: 65
}
onVisibleChanged: {
if(visible) {
gameLogic.initGame()