Skip to content

Instantly share code, notes, and snippets.

@JaDenis
Created July 27, 2018 11:32
Show Gist options
  • Save JaDenis/ca247ea8e5bb66989bc6bb45f4079b08 to your computer and use it in GitHub Desktop.
Save JaDenis/ca247ea8e5bb66989bc6bb45f4079b08 to your computer and use it in GitHub Desktop.
11
fun calcWinningPair(odds: List<Map<String, Any>>) : Map<Float, List<Map<String, Any>>> {
if (!odds.isEmpty()) {
var map: Map<Float, Any> = mapOf()
var absSpecialValues: List<Float> = listOf()
// print(odds)
odds.forEach {
val specialValue = it["specialValue"] as! String
val floatSpecialValue = Float(specialValue)
if (floatSpecialValue != null) {
val absSpecValue = abs(floatSpecialValue)
absSpecialValues.append(absSpecValue)
absSpecialValues = absSpecialValues.removeDuplicates()
}
}
// print("absSpecialValues: \(absSpecialValues)")
// 1). составить мапу
for (specialValue in absSpecialValues) {
var oddsSortedBySpecialValue: List<Map<String, Any>> = listOf()
odds.forEach { val floatSpecialValue = Float(it["specialValue"] as! String)
if (floatSpecialValue != null) {
val absSpecValue = abs(floatSpecialValue)
if (absSpecValue == specialValue) {
oddsSortedBySpecialValue.append(it)
}
} }
map[specialValue] = oddsSortedBySpecialValue
}
// for sv in absSpecialValues {
// print("=====================")
// print("\(sv): \(handicapMap[sv]!)")
// print("=====================")
// }
// 2). перебрать мапу так, что остануться только пары
// - заглянуть в каждый value
// - выбрать там пару
// - записать в handicapMap
for (specialValue in absSpecialValues) {
val odds = map[specialValue]!! as! List<Map<String, Any>>
// из odds -> составить мапу по одинаковым specialValue - получим пары
var svMap: Map<Float, Any> = mapOf()
var svArray: List<Float> = listOf()
// собираем массив всех specialValue
odds.forEach {
val specialValue = it["specialValue"] as! String
val floatSpecialValue = Float(specialValue)
if (floatSpecialValue != null) {
svArray.append(floatSpecialValue)
}
}
svArray = svArray.removeDuplicates()
// print(svArray)
// составляем мапу [sv : odds]
for (sv in svArray) {
var pair: List<Map<String, Any>> = listOf()
odds.forEach {
val specialValue = it["specialValue"] as! String
val floatSpecialValue = Float(specialValue)
if (floatSpecialValue != null) {
if (sv == floatSpecialValue) {
pair.append(it)
}
}
}
val pairSortedByOutcome = pair.sorted { val outCome1 = Float(it["outCome"] as! String)
val outCome2 = Float($1["outCome"] as! String)
if (outCome1 != null && outCome2 != null) {
return@sorted outCome1 < outCome2
} else {
return@sorted it["outCome"] as! String > $1["outCome"] as! String
} }
svMap[sv] = pairSortedByOutcome
}
// for sv in svArray {
// print("=====================")
// print("\(sv): \(svMap[sv]!)")
// print("=====================")
// }
// сначала сортируем по diff
var diffMap: Map<Float, Any> = mapOf()
for (sv in svArray) {
val pair = svMap[sv] as? List<Map<String, Any>>
if (pair != null) {
var coeffDiff: Float = 0.0
var coeffArray: List<Float> = listOf()
for (odd in pair) {
val coeff = odd["coefficient"] as? String
if (coeff != null) {
val floatCoeff = Float(coeff)
if (floatCoeff != null) {
coeffArray.append(floatCoeff)
}
} else {
// добавляем коэффициент 0.0, если с бэкенда пришел <null>
coeffArray.append(0.0)
}
}
if (coeffArray.size > 1) {
coeffDiff = abs(coeffArray[0] - coeffArray[1])
// составляем мапу диффов
diffMap[sv] = coeffDiff
} else {
print("пизда")
}
}
}
// print("======= diffMap ========")
// print(diffMap)
// потом сортируем по diff18
// составить мапу sv: diff18
var diff18Map: Map<Float, Any> = mapOf()
for (sv in svArray) {
val pair = map[specialValue] as? List<Map<String, Any>>
if (pair != null) {
var coeffArray: List<Float> = listOf()
for (odd in pair) {
val coeff = odd["coefficient"] as? String
if (coeff != null) {
val floatCoeff = Float(coeff)
if (floatCoeff != null) {
coeffArray.append(floatCoeff)
}
} else {
coeffArray.append(0.0)
}
}
if (coeffArray.size > 1) {
val diff18_1 = abs(coeffArray[0] - 1.8)
val diff18_2 = abs(coeffArray[1] - 1.8)
val diff18 = (diff18_1 + diff18_2) / 2.0
diff18Map[sv] = diff18
}
}
}
// print("======= diff18Map ========")
// print(diff18Map)
// сортировать массив svArray по diff
val svArraySortedByDiff = svArray.sorted {
val diff_1 = diffMap[it] as! Float
val diff_2 = diffMap[$1] as! Float
diff_1 < diff_2
}
// print(svArraySortedByDiff)
// сортировать массив svArray по diff18
val svArraySortedByDiff18 = svArray.sorted {
val diff18_1 = diff18Map[it] as! Float
val diff18_2 = diff18Map[$1] as! Float
diff18_1 < diff18_2
}
// print(svArraySortedByDiff18)
// взять первый итем - это sv выигравшей пары
val svForWinningPair = svArraySortedByDiff18.firstOrNull()!!
// перезаписываем в handicapMap выигравшую пару
// записать ее с этим abs(sv) в handicapMap
map[abs(svForWinningPair)] = svMap[svForWinningPair]
}
// 3). сортируем финальную мапу
// - собираем handicapDiffMap
// - собираем handicapDiff18Map
// -
// for specialValue in absSpecialValues {
// print("=====================")
// print("\(specialValue): \(handicapMap[specialValue]!)")
// print("=====================")
// }
var handicapDiffMap: Map<Float, Any> = mapOf()
for (specialValue in absSpecialValues) {
val pair = map[specialValue] as? List<Map<String, Any>>
if (pair != null) {
var coeffDiff: Float = 0.0
var coeffArray: List<Float> = listOf()
for (odd in pair) {
val coeff = odd["coefficient"] as? String
if (coeff != null) {
val floatCoeff = Float(coeff)
if (floatCoeff != null) {
coeffArray.append(floatCoeff)
}
} else {
coeffArray.append(0.0)
}
}
if (coeffArray.size > 1) {
coeffDiff = abs(coeffArray[0] - coeffArray[1])
handicapDiffMap[specialValue] = coeffDiff
}
}
}
// print("======= diffMap ========")
// print(handicapDiffMap)
var handicapDiff18Map: Map<Float, Any> = mapOf()
for (specialValue in absSpecialValues) {
val pair = map[specialValue] as? List<Map<String, Any>>
if (pair != null) {
var coeffArray: List<Float> = listOf()
for (odd in pair) {
val coeff = odd["coefficient"] as? String
if (coeff != null) {
val floatCoeff = Float(coeff)
if (floatCoeff != null) {
coeffArray.append(floatCoeff)
}
} else {
coeffArray.append(0.0)
}
}
if (coeffArray.size > 1) {
val diff18_1 = abs(coeffArray[0] - 1.8)
val diff18_2 = abs(coeffArray[1] - 1.8)
val diff18 = (diff18_1 + diff18_2) / 2
handicapDiff18Map[specialValue] = diff18
}
}
}
// print("======= diff18Map ========")
// print(handicapDiff18Map)
// сортируем массив absSpecialValue по diff
val absSpecialValuesSortedByDiff = absSpecialValues.sorted {
val diff_1 = handicapDiffMap[it] as! Float
val diff_2 = handicapDiffMap[$1] as! Float
diff_1 < diff_2
}
// print(absSpecialValuesSortedByDiff)
// сортируем массив absSpecialValue по diff18
val absSpecialValuesSortedByDiff18 = absSpecialValues.sorted {
val diff18_1 = handicapDiff18Map[it] as! Float
val diff18_2 = handicapDiff18Map[$1] as! Float
diff18_1 < diff18_2
}
// print(absSpecialValuesSortedByDiff18)
// взять первый итем - это sv выигравшей пары
val svForWinningPair = absSpecialValuesSortedByDiff18.firstOrNull()!!
val winningPair = map[svForWinningPair] as! List<Map<String, Any>>
val handicap = mapOf(svForWinningPair to winningPair)
return handicap
} else {
return mapOf(-1.0 to listOf(mapOf("coefficient" to "-", "outCome" to 1), mapOf("coefficient" to "-", "outCome" to 2)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment