-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from jetbrains-academy/refactor-old-school
Refactor the old school project
- Loading branch information
Showing
22 changed files
with
243 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
type: framework | ||
custom_name: Old school | ||
content: | ||
- oldSchoolServerIntroduction | ||
- oldSchoolServerIntroduction | ||
- oldSchoolServerFinishGame | ||
is_template_based: false |
13 changes: 13 additions & 0 deletions
13
...FinishGame/src/main/kotlin/org/jetbrains/kotlin/course/old/school/OldSchoolApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.jetbrains.kotlin.course.old.school | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.boot.runApplication | ||
|
||
@SpringBootApplication | ||
class OldSchoolApplication | ||
|
||
@Suppress("SpreadOperator") | ||
fun main(args: Array<String>) { | ||
runApplication<OldSchoolApplication>(*args) | ||
} | ||
|
36 changes: 36 additions & 0 deletions
36
...src/main/kotlin/org/jetbrains/kotlin/course/old/school/functions/GameFunctionsResource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.jetbrains.kotlin.course.old.school.functions | ||
|
||
import org.springframework.web.bind.annotation.* | ||
|
||
// We can not use a typealias here because the Spring framework can not parse it | ||
class Characters : ArrayList<String>() | ||
class Body ( | ||
val names: Characters, | ||
val color: String | ||
) | ||
|
||
@RestController | ||
@RequestMapping("/api/functions/") | ||
class GameFunctionsResource(val service: GameFunctionsService) { | ||
@CrossOrigin | ||
@GetMapping("/colors") | ||
fun getAllPossibleColors(): List<String> = service.getAllPossibleColors() | ||
|
||
@CrossOrigin | ||
@PostMapping("/find") | ||
fun findPhoto(@RequestBody body: Body): String? = with(service) { | ||
body.names.findPhoto(body.color)?.name?.lowercase() | ||
} | ||
|
||
@CrossOrigin | ||
@PostMapping("/groupByByColor") | ||
fun groupByPhotosByColor(@RequestBody names: List<String>): List<String> = with(service) { | ||
names.groupPhotosByColor().map { it.name } | ||
} | ||
|
||
@CrossOrigin | ||
@PostMapping("/groupByPhotosByHairAndHat") | ||
fun groupByPhotosByHairAndHat(@RequestBody names: List<String>): List<String> = with(service) { | ||
names.groupPhotosByHairAndHat().map { it.name } | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
.../src/main/kotlin/org/jetbrains/kotlin/course/old/school/functions/GameFunctionsService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.jetbrains.kotlin.course.old.school.functions | ||
|
||
import org.jetbrains.kotlin.course.old.school.photo.Accessory | ||
import org.jetbrains.kotlin.course.old.school.photo.Color | ||
import org.jetbrains.kotlin.course.old.school.photo.PhotoCharacter | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class GameFunctionsService { | ||
fun getAllPossibleColors() = Color.entries.map { it.name.lowercase() } | ||
|
||
private fun String.toColor() = Color.valueOf(replaceFirstChar { it.titlecase() }) | ||
|
||
private fun Iterable<String>.toPhotoCharacters() = | ||
map { name -> PhotoCharacter.valueOf(name.replaceFirstChar { it.titlecase() }) } | ||
|
||
fun Iterable<String>.findPhoto(colorStr: String): PhotoCharacter? { | ||
val color = colorStr.toColor() | ||
return toPhotoCharacters().find { it.backgroundColor == color } | ||
} | ||
|
||
fun Iterable<String>.groupPhotosByColor() = toPhotoCharacters() | ||
.groupBy { it.backgroundColor }.map { it.value }.flatten() | ||
|
||
fun Iterable<String>.groupPhotosByHairAndHat() = toPhotoCharacters() | ||
.groupBy { it.hairType } | ||
.mapValues { it.value.groupBy { character -> | ||
character.accessories?.contains(Accessory.Hat) ?: false | ||
} | ||
}.values.flatMap { it.values } | ||
.flatten() | ||
} |
23 changes: 23 additions & 0 deletions
23
...inishGame/src/main/kotlin/org/jetbrains/kotlin/course/old/school/mode/GameModeResource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.jetbrains.kotlin.course.old.school.mode | ||
|
||
import old.school.JsPhoto | ||
import org.springframework.web.bind.annotation.CrossOrigin | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping("/api/mode/") | ||
class CardResource(val service: GameModeService) { | ||
@CrossOrigin | ||
@GetMapping("/list") | ||
fun generateListOfCharacters(): List<JsPhoto> = service.generateListOfCharacters().map { JsPhoto(it.name) } | ||
|
||
@CrossOrigin | ||
@GetMapping("/set") | ||
fun generateSetOfCharacters(): List<JsPhoto> = service.generateSetOfCharacters().map { JsPhoto(it.name) } | ||
|
||
@CrossOrigin | ||
@GetMapping("/map") | ||
fun generateMapOfCharacters(): List<JsPhoto> = service.generateMapOfCharacters().map { JsPhoto(it.key.name, it.value) } | ||
} |
21 changes: 21 additions & 0 deletions
21
...FinishGame/src/main/kotlin/org/jetbrains/kotlin/course/old/school/mode/GameModeService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.jetbrains.kotlin.course.old.school.mode | ||
|
||
import org.jetbrains.kotlin.course.old.school.photo.PhotoCharacter | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class GameModeService { | ||
companion object { | ||
const val MAX_NUMBER_OF_PHOTOS = 12 | ||
} | ||
private fun generateRandomCharacter() = PhotoCharacter.entries.random() | ||
|
||
fun generateListOfCharacters() = List(MAX_NUMBER_OF_PHOTOS) { generateRandomCharacter() } | ||
|
||
// It is better to move common code into a separated function | ||
private fun getRandomCharacters() = PhotoCharacter.entries.shuffled().take(MAX_NUMBER_OF_PHOTOS) | ||
|
||
fun generateSetOfCharacters() = getRandomCharacters().toSet() | ||
|
||
fun generateMapOfCharacters() = getRandomCharacters().associateWith { it.name } | ||
} |
55 changes: 55 additions & 0 deletions
55
...FinishGame/src/main/kotlin/org/jetbrains/kotlin/course/old/school/photo/PhotoCharacter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.jetbrains.kotlin.course.old.school.photo | ||
|
||
enum class PhotoCharacter( | ||
val backgroundColor: Color, | ||
val hairType: HairType, | ||
val accessories: List<Accessory>? = null | ||
) { | ||
Olivia(Color.Purple, HairType.Dark), | ||
Emily(Color.White, HairType.Dark, listOf(Accessory.Earrings, Accessory.Watch)), | ||
Emma(Color.Yellow, HairType.Light), | ||
Charlotte(Color.Pink, HairType.Dark, listOf(Accessory.Earrings, Accessory.Watch)), | ||
Diana(Color.Orange, HairType.Light, listOf(Accessory.Earrings, Accessory.Watch)), | ||
Alice(Color.Green, HairType.Light), | ||
Helena(Color.Green, HairType.Light, listOf(Accessory.Pen)), | ||
Evie(Color.Purple, HairType.Light, listOf(Accessory.Pen)), | ||
|
||
Henry(Color.Pink, HairType.Dark, listOf(Accessory.Glasses)), | ||
Daniel(Color.Purple, HairType.Dark, listOf(Accessory.Hat)), | ||
Noah(Color.Blue, HairType.Dark, listOf(Accessory.Glasses, Accessory.Hat)), | ||
David(Color.Gray, HairType.Dark, listOf(Accessory.Glasses, Accessory.Hat)), | ||
Leo(Color.Purple, HairType.Dark, listOf(Accessory.Glasses, Accessory.Hat)), | ||
Jamie(Color.Gray, HairType.Light, listOf(Accessory.Glasses)), | ||
Larry(Color.Orange, HairType.Light, listOf(Accessory.Glasses)), | ||
Thomas(Color.White, HairType.Light, listOf(Accessory.Hat)), | ||
Oliver(Color.Blue, HairType.Light, listOf(Accessory.Hat)), | ||
Edward(Color.White, HairType.Dark, listOf(Accessory.Glasses, Accessory.Hat)) | ||
; | ||
} | ||
|
||
enum class HairType { | ||
Dark, | ||
Light | ||
; | ||
} | ||
|
||
enum class Color { | ||
White, | ||
Blue, | ||
Orange, | ||
Yellow, | ||
Pink, | ||
Purple, | ||
Green, | ||
Gray | ||
; | ||
} | ||
|
||
enum class Accessory { | ||
Glasses, | ||
Earrings, | ||
Watch, | ||
Pen, | ||
Hat | ||
; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
type: edu | ||
files: | ||
- name: test/Tests.kt | ||
visible: false | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/OldSchoolApplication.kt | ||
visible: true | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/photo/PhotoCharacter.kt | ||
visible: true | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/mode/GameModeService.kt | ||
visible: true | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/mode/GameModeResource.kt | ||
visible: true | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/functions/GameFunctionsService.kt | ||
visible: true | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/functions/GameFunctionsResource.kt | ||
visible: true | ||
- name: test/HairTypeTestClass.kt | ||
visible: false | ||
- name: test/ColorTestClass.kt | ||
visible: false | ||
- name: test/AccessoryTestClass.kt | ||
visible: false | ||
- name: test/GameModeServiceTestClass.kt | ||
visible: false | ||
- name: test/PhotoCharacterTestClass.kt | ||
visible: false | ||
- name: test/GameFunctionsServiceTestClass.kt | ||
visible: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Tasks: | ||
|
||
+ Add enum class: HairType, Color, Accessory | ||
+ Implement generateRandomCharacter + generateListOfCharacters | ||
+ Implement generateSetOfCharacters | ||
+ Implement generateMapOfCharacters | ||
+ Implement getAllPossibleColors | ||
+ Implement toColor, toPhotoCharacters, findPhoto | ||
+ Implement groupPhotosByColor | ||
+ Implement groupPhotosByHairAndHat |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 14 additions & 27 deletions
41
oldSchoolServer/oldSchoolServerIntroduction/task-info.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,15 @@ | ||
type: edu | ||
type: theory | ||
custom_name: Old School - Introduction | ||
files: | ||
- name: test/Tests.kt | ||
visible: false | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/OldSchoolApplication.kt | ||
visible: true | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/photo/PhotoCharacter.kt | ||
visible: true | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/mode/GameModeService.kt | ||
visible: true | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/mode/GameModeResource.kt | ||
visible: true | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/functions/GameFunctionsService.kt | ||
visible: true | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/functions/GameFunctionsResource.kt | ||
visible: true | ||
- name: test/HairTypeTestClass.kt | ||
visible: false | ||
- name: test/ColorTestClass.kt | ||
visible: false | ||
- name: test/AccessoryTestClass.kt | ||
visible: false | ||
- name: test/GameModeServiceTestClass.kt | ||
visible: false | ||
- name: test/PhotoCharacterTestClass.kt | ||
visible: false | ||
- name: test/GameFunctionsServiceTestClass.kt | ||
visible: false | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/OldSchoolApplication.kt | ||
visible: true | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/photo/PhotoCharacter.kt | ||
visible: true | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/mode/GameModeService.kt | ||
visible: true | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/mode/GameModeResource.kt | ||
visible: true | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/functions/GameFunctionsService.kt | ||
visible: true | ||
- name: src/main/kotlin/org/jetbrains/kotlin/course/old/school/functions/GameFunctionsResource.kt | ||
visible: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +0,0 @@ | ||
Tasks: | ||
|
||
+ Add enum class: HairType, Color, Accessory | ||
+ Implement generateRandomCharacter + generateListOfCharacters | ||
+ Implement generateSetOfCharacters | ||
+ Implement generateMapOfCharacters | ||
+ Implement getAllPossibleColors | ||
+ Implement toColor, toPhotoCharacters, findPhoto | ||
+ Implement groupPhotosByColor | ||
+ Implement groupPhotosByHairAndHat | ||