mathtrainingstuff/calender/Calender.qml

198 lines
8 KiB
QML
Raw Normal View History

2018-12-15 19:44:11 +01:00
import QtQuick 2.11
import QtQuick.Controls 2.4
import "./"
Item {
id: calender
property string nextDate: "31.12.1799"
property string lastDate: "30.12.1799"
property int actualDateCount: 0
property int min: 1800
property int max: 2199
property int tickInterval: 10000
property int dateCount: 10
property int correct: 0
property int wrong: 0
property int weekDay: 1
property bool runningPageState: true
signal pageOpened()
onPageOpened: {
calender.start()
}
states: [
State {
name: "starting"
PropertyChanges {
target: calenderStack
currPage: calenderStartPageComp
}
},
State {
name: "running"
PropertyChanges {
target: calenderStack
currPage: calenderRunningPageComp
}
},
State {
name: "gameOver"
PropertyChanges {
target: calenderStack
currPage: calenderGameOverPageComp
}
}
]
StackView {
id: calenderStack
property var currPage;
anchors.fill: parent
onCurrPageChanged: {
calenderStack.replace(currPage)
}
onCurrentItemChanged: {
calenderStack.currentItem.pageOpened()
}
Component {
id: calenderStartPageComp
CalenderStartPage {
}
}
Component {
id: calenderRunningPageComp
CalenderRunningPage {
}
}
Component {
id: calenderGameOverPageComp
CalenderGameOverPage {
}
}
replaceExit: Transition {
NumberAnimation {
from: 1
to: 0
property: "opacity"
duration: 200
easing.type: Easing.InOutQuad
}
}
replaceEnter: Transition {
NumberAnimation {
from: 0
to: 1
property: "opacity"
duration: 200
easing.type: Easing.InOutQuad
}
}
}
/*-------------------------
---------functions---------
-------------------------*/
function start() {
calender.reset()
calender.state = "starting"
}
function run() {
calender.generateNextDate()
calender.state = "running"
}
function gameOver() {
calender.state = "gameOver"
}
function reset() {
calender.actualDateCount = 0
calender.lastDate = 0
calender.nextDate = 0
calender.correct = 0
calender.wrong = 0
}
function generateNextDate() {
var daysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
var monthCodes = [6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
var yearCode = 0
var randDate = calender.nextDate
var randDay = 31
var randMonth = 12
var randYear = 1799
var range = calender.max - calender.min
var century = 17
var weekDay = 1
calender.lastDate = calender.nextDate
while (randDate === calender.lastDate) {
//console.log("entered mainwhile " + "yearCode: " + yearCode + " randDate: " + randDate + " randDay: " + randDay + " randMonth: " + randMonth + " randYear: " + randYear + " Range: " + range + " century: " + century + " weekDay: " + weekDay)
randYear = Math.floor((Math.random()*(range+1))+calender.min)
//console.log("generated year " + "yearCode: " + yearCode + " randDate: " + randDate + " randDay: " + randDay + " randMonth: " + randMonth + " randYear: " + randYear + " Range: " + range + " century: " + century + " weekDay: " + weekDay)
century = Math.floor(randYear/100)
//console.log("calculated century " + "yearCode: " + yearCode + " randDate: " + randDate + " randDay: " + randDay + " randMonth: " + randMonth + " randYear: " + randYear + " Range: " + range + " century: " + century + " weekDay: " + weekDay)
yearCode = (randYear - century * 100) + Math.floor((randYear - century * 100)/4)
//console.log("generated yearCode " + "yearCode: " + yearCode + " randDate: " + randDate + " randDay: " + randDay + " randMonth: " + randMonth + " randYear: " + randYear + " Range: " + range + " century: " + century + " weekDay: " + weekDay)
while (century > 21) {
century -= 4
//console.log("down count century " + "yearCode: " + yearCode + " randDate: " + randDate + " randDay: " + randDay + " randMonth: " + randMonth + " randYear: " + randYear + " Range: " + range + " century: " + century + " weekDay: " + weekDay)
}
switch (century) {
case 18:
yearCode += 3
break
case 19:
yearCode += 1
break
case 21:
yearCode += 5
break
default:
//console.log("added century specified value to yearCode " + "yearCode: " + yearCode + " randDate: " + randDate + " randDay: " + randDay + " randMonth: " + randMonth + " randYear: " + randYear + " Range: " + range + " century: " + century + " weekDay: " + weekDay)
}
randMonth = Math.floor((Math.random()*(12))+1)
//console.log("generated month " + "yearCode: " + yearCode + " randDate: " + randDate + " randDay: " + randDay + " randMonth: " + randMonth + " randYear: " + randYear + " Range: " + range + " century: " + century + " weekDay: " + weekDay)
if (randYear % 4 === 0 && randMonth <= 2) {
yearCode += 6
//console.log("if leap-year " + "yearCode: " + yearCode + " randDate: " + randDate + " randDay: " + randDay + " randMonth: " + randMonth + " randYear: " + randYear + " Range: " + range + " century: " + century + " weekDay: " + weekDay)
}
while (yearCode >= 7) {
yearCode -= 7
//console.log("down count yearCode " + "yearCode: " + yearCode + " randDate: " + randDate + " randDay: " + randDay + " randMonth: " + randMonth + " randYear: " + randYear + " Range: " + range + " century: " + century + " weekDay: " + weekDay)
}
randDay = Math.floor((Math.random()*(daysPerMonth[randMonth-1]))+1)
//console.log("generated Day " + "yearCode: " + yearCode + " randDate: " + randDate + " randDay: " + randDay + " randMonth: " + randMonth + " randYear: " + randYear + " Range: " + range + " century: " + century + " weekDay: " + weekDay)
randDate = String(randDay) + "." + String(randMonth) + "." + String(randYear)
//console.log("generated randDate " + "yearCode: " + yearCode + " randDate: " + randDate + " randDay: " + randDay + " randMonth: " + randMonth + " randYear: " + randYear + " Range: " + range + " century: " + century + " weekDay: " + weekDay)
}
weekDay = (yearCode + monthCodes[randMonth-1] + randDay) // - 7*Math.floor((yearCode + monthCodes[randMonth-1] + randDay)/7)
//console.log("generated weekday " + "yearCode: " + yearCode + " randDate: " + randDate + " randDay: " + randDay + " randMonth: " + randMonth + " randYear: " + randYear + " Range: " + range + " century: " + century + " weekDay: " + weekDay)
//weekDay -= 7*Math.floor(weekDay/7)
while (weekDay >= 7) {
weekDay -= 7
//console.log("down count weekday " + "yearCode: " + yearCode + " randDate: " + randDate + /*" randDay: " + randDay + " randMonth: " + randMonth + " randYear: " + randYear + " Range: " + range + */ " century: " + century + " weekDay: " + weekDay)
}
console.log("randDate: " + randDate + " yearCode: " + yearCode + " century: " + century + " weekDay: " + weekDay)
calender.weekDay = weekDay
calender.nextDate = randDate
calender.actualDateCount += 1
}
}