Coverage for src/romfarmer/arcade/classifier.py: 0%

105 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-09-03 11:31 -0700

1"""Arcade game classifier for clone type detection. 

2 

3Classifies arcade games into categories: 

4- Parent: Original/main version 

5- Regional: USA, Japan, World, Europe variants 

6- Revision: Version updates (r1, r2, etc.) 

7- Bootleg: Unauthorized copies 

8- Hack: Modified versions (Rainbow Edition, turbo hacks, etc.) 

9- Prototype: Pre-release versions 

10- Homebrew: Fan-made games 

11- Demo: Demo/sample versions 

12""" 

13 

14import re 

15from dataclasses import dataclass 

16from enum import Enum 

17 

18from ..dat_parser.models import ArcadeCloneType, DATGame 

19 

20 

21class CloneImportance(str, Enum): 

22 """Cultural/historical importance of a clone.""" 

23 

24 ESSENTIAL = "essential" # Must include (famous hacks like Rainbow Edition) 

25 RECOMMENDED = "recommended" # Good to include if space allows 

26 OPTIONAL = "optional" # Include only for completeness 

27 SKIP = "skip" # Generally skip (low-quality bootlegs, broken hacks) 

28 

29 

30@dataclass 

31class ArcadeClassification: 

32 """Classification result for an arcade game.""" 

33 

34 clone_type: ArcadeCloneType 

35 importance: CloneImportance 

36 region: str | None = None 

37 revision: str | None = None 

38 is_working: bool = True 

39 notes: str | None = None 

40 

41 

42# Famous hacks/bootlegs that are culturally important and should be preserved 

43# Note: Many "hacks" in arcade history were actually bootleg boards 

44ESSENTIAL_HACKS = { 

45 # Street Fighter II - Rainbow Edition (iconic bootleg, legendary in arcades) 

46 "sf2rb", 

47 "sf2rb2", 

48 "sf2rb3", 

49 "sf2rb4", 

50 "sf2rb5", 

51 "sf2rb6", 

52 # Street Fighter II - Other famous mods 

53 "sf2koryu", 

54 "sf2koryu2", 

55 "sf2koryu3", # Koryu (Korean hack) 

56 "sf2m1", 

57 "sf2m2", 

58 "sf2m3", 

59 "sf2m4", 

60 "sf2m5", 

61 "sf2m6", 

62 "sf2m7", # Magic variants 

63 "sf2yyc", 

64 "sf2yyc2", # YYC 

65 # Ms. Pac-Man (originally a hack kit, became official) 

66 "mspacman", 

67 "mspacmab", 

68 "mspacmat", 

69 # Pac-Man Plus 

70 "pacplus", 

71 # Donkey Kong hacks 

72 "dkong3b", # Bootleg 

73 "dkrainbow", # Rainbow 

74 # JoJo Rainbow Edition (popular modern hack) 

75 "jojobanrb", 

76 # Various other famous bootlegs/hacks 

77 "1943kai", # 1943 Kai 

78} 

79 

80# Quality Neo-Geo homebrew that are complete games (not demos) 

81QUALITY_HOMEBREW = { 

82 # Commercial quality releases 

83 "xenocrisis", # Xeno Crisis - Full commercial game 

84 "lasthope", # Last Hope - Full shmup 

85 "b2b", # Bang Bang Busters - Commercial release 

86 "ironclad", # Ironclad (commercial, also called Brikin'ger) 

87 "sbp", # Super Bubble Pop 

88 "frogfeast", # Frog Feast - Full game 

89 # Quality conversions/ports 

90 "shinobing", # Shinobi Neo Geo - Complete conversion 

91 "goldnaxeng", # Golden Axe Neo Geo - Complete conversion 

92 "cabalng", # Cabal Neo Geo - Complete conversion 

93 "karnovng", # Karnov Neo Geo - Complete conversion 

94 "columnsn", # Columns Neo Geo 

95 "neopang", # Neo Pang - Complete puzzle game 

96 "neotet", # NeoGeo 2-Player Tetris 

97 "looptrsp", # Looptris Plus 

98 "hypernoid", # Hypernoid - Complete game 

99 "neo2048", # Neo 2048 - Complete puzzle 

100 "knightsch", # Knight's Chance 

101 "timesup", # Time's UP! - Complete game 

102 "gladmort", # GladMort - Complete game 

103 "poknight", # Poker Night 

104 "yoyoshkn", # Yo-Yo Shuriken 

105} 

106 

107# Regional priority order (higher = better for English speakers) 

108REGION_PRIORITY = { 

109 "world": 100, 

110 "usa": 90, 

111 "us": 90, 

112 "europe": 80, 

113 "euro": 80, 

114 "asia": 70, 

115 "japan": 60, 

116 "korea": 50, 

117 "taiwan": 40, 

118 "hong kong": 40, 

119 "hispanic": 30, 

120 "brazil": 30, 

121} 

122 

123 

124class ArcadeClassifier: 

125 """Classifier for arcade game variants.""" 

126 

127 def __init__( 

128 self, 

129 essential_hacks: set[str] | None = None, 

130 quality_homebrew: set[str] | None = None, 

131 ): 

132 """Initialize classifier. 

133 

134 Args: 

135 essential_hacks: Set of ROM names considered essential hacks. 

136 Defaults to built-in list. 

137 quality_homebrew: Set of ROM names for quality homebrew games. 

138 Defaults to built-in list. 

139 """ 

140 self.essential_hacks = essential_hacks or ESSENTIAL_HACKS 

141 self.quality_homebrew = quality_homebrew or QUALITY_HOMEBREW 

142 

143 def classify(self, game: DATGame) -> ArcadeClassification: 

144 """Classify an arcade game. 

145 

146 Args: 

147 game: DATGame to classify 

148 

149 Returns: 

150 ArcadeClassification with clone type, importance, etc. 

151 """ 

152 # Check if it's a working game 

153 is_working = game.driver_status in (None, "good", "imperfect") 

154 

155 # BIOS/Device detection (always first) 

156 if game.is_bios: 

157 return ArcadeClassification( 

158 clone_type=ArcadeCloneType.BIOS, 

159 importance=CloneImportance.ESSENTIAL, 

160 is_working=is_working, 

161 ) 

162 

163 # Check comment for homebrew/demo even for parents 

164 # This allows filtering out homebrew/demo parent games 

165 comment = (game.comment or "").lower() 

166 

167 # Homebrew parent games - distinguish complete games from demos 

168 if not game.cloneof and "homebrew" in comment: 

169 # Check if it's a demo 

170 if "demo" in comment or "tech-demo" in comment or "techdemo" in comment: 

171 return ArcadeClassification( 

172 clone_type=ArcadeCloneType.DEMO, # Treat demo homebrew as DEMO 

173 importance=CloneImportance.SKIP, 

174 is_working=is_working, 

175 notes="Homebrew demo", 

176 ) 

177 # Check if it's quality homebrew 

178 importance = CloneImportance.OPTIONAL 

179 if game.name in self.quality_homebrew: 

180 importance = CloneImportance.RECOMMENDED 

181 # Complete homebrew game 

182 return ArcadeClassification( 

183 clone_type=ArcadeCloneType.HOMEBREW, 

184 importance=importance, 

185 is_working=is_working, 

186 notes="Complete homebrew", 

187 ) 

188 

189 # Demo parent games (non-homebrew demos) 

190 if not game.cloneof and "demo" in comment: 

191 return ArcadeClassification( 

192 clone_type=ArcadeCloneType.DEMO, 

193 importance=CloneImportance.SKIP, 

194 is_working=is_working, 

195 notes="Demo parent", 

196 ) 

197 

198 # Regular parent detection 

199 if not game.cloneof: 

200 return ArcadeClassification( 

201 clone_type=ArcadeCloneType.PARENT, 

202 importance=CloneImportance.ESSENTIAL, 

203 is_working=is_working, 

204 ) 

205 

206 # Classify based on comment field (for clones) 

207 clone_type, importance = self._classify_from_comment(game) 

208 

209 # Override importance for essential hacks 

210 if game.name in self.essential_hacks: 

211 importance = CloneImportance.ESSENTIAL 

212 

213 # Extract region if it's a regional variant 

214 region = self._extract_region(game) 

215 if region and clone_type == ArcadeCloneType.REGIONAL: 

216 # Adjust importance based on region 

217 if region.lower() in ("world", "usa", "us"): 

218 importance = CloneImportance.RECOMMENDED 

219 elif region.lower() in ("europe", "euro"): 

220 importance = CloneImportance.RECOMMENDED 

221 

222 # Extract revision 

223 revision = self._extract_revision(game) 

224 

225 return ArcadeClassification( 

226 clone_type=clone_type, 

227 importance=importance, 

228 region=region, 

229 revision=revision, 

230 is_working=is_working, 

231 ) 

232 

233 def _classify_from_comment(self, game: DATGame) -> tuple[ArcadeCloneType, CloneImportance]: 

234 """Classify based on comment field. 

235 

236 Args: 

237 game: DATGame to classify 

238 

239 Returns: 

240 Tuple of (ArcadeCloneType, CloneImportance) 

241 """ 

242 comment = (game.comment or "").lower() 

243 desc = (game.description or "").lower() 

244 

245 # Check for specific types in order of priority 

246 if "bootleg" in comment: 

247 # Bootlegs with sound/graphics issues are lower priority 

248 if "no sound" in comment or "imperfect" in comment: 

249 return ArcadeCloneType.BOOTLEG, CloneImportance.SKIP 

250 return ArcadeCloneType.BOOTLEG, CloneImportance.OPTIONAL 

251 

252 if "hack" in comment: 

253 return ArcadeCloneType.HACK, CloneImportance.OPTIONAL 

254 

255 # Check description for hack indicators 

256 # These are modified versions with alternate content (music, graphics, gameplay) 

257 hack_indicators = [ 

258 "music)", # e.g., "1942 (C64 Music)" 

259 "supercharger", # e.g., "Supercharger 1942" 

260 "turbo", # e.g., "Super Turbo" hacks 

261 "rainbow", # e.g., "Rainbow Edition" 

262 "bootleg", # Sometimes in description 

263 ] 

264 if any(indicator in desc for indicator in hack_indicators): 

265 return ArcadeCloneType.HACK, CloneImportance.OPTIONAL 

266 

267 if "prototype" in comment: 

268 return ArcadeCloneType.PROTOTYPE, CloneImportance.RECOMMENDED 

269 

270 if "homebrew" in comment: 

271 return ArcadeCloneType.HOMEBREW, CloneImportance.OPTIONAL 

272 

273 if "demo" in comment: 

274 return ArcadeCloneType.DEMO, CloneImportance.OPTIONAL 

275 

276 if "bios" in comment: 

277 return ArcadeCloneType.BIOS, CloneImportance.ESSENTIAL 

278 

279 # No comment - likely a regional or revision clone 

280 region = self._extract_region(game) 

281 if region: 

282 return ArcadeCloneType.REGIONAL, CloneImportance.RECOMMENDED 

283 

284 revision = self._extract_revision(game) 

285 if revision: 

286 return ArcadeCloneType.REVISION, CloneImportance.RECOMMENDED 

287 

288 # Default: treat as regional variant 

289 return ArcadeCloneType.REGIONAL, CloneImportance.OPTIONAL 

290 

291 def _extract_region(self, game: DATGame) -> str | None: 

292 """Extract region from game name or description. 

293 

294 Args: 

295 game: DATGame to extract region from 

296 

297 Returns: 

298 Region string or None 

299 """ 

300 name = game.name.lower() 

301 desc = (game.description or "").lower() 

302 

303 # Check common regional suffixes in ROM names 

304 region_patterns = [ 

305 (r"[_-]?(usa?|us)$", "USA"), 

306 (r"[_-]?(jpn?|japan)$", "Japan"), 

307 (r"[_-]?(eur?|europe)$", "Europe"), 

308 (r"[_-]?(asia?)$", "Asia"), 

309 (r"[_-]?(world|wld)$", "World"), 

310 (r"[_-]?(korea?|kor)$", "Korea"), 

311 (r"[_-]?(taiwan|tw)$", "Taiwan"), 

312 (r"[_-]?(hispanic|hisp)$", "Hispanic"), 

313 (r"[_-]?(brazil|br)$", "Brazil"), 

314 ] 

315 

316 for pattern, region in region_patterns: 

317 if re.search(pattern, name): 

318 return region 

319 

320 # Check description for region in parentheses 

321 desc_patterns = [ 

322 (r"\(usa\)", "USA"), 

323 (r"\(us\)", "USA"), 

324 (r"\(japan\)", "Japan"), 

325 (r"\(europe\)", "Europe"), 

326 (r"\(world\)", "World"), 

327 (r"\(asia\)", "Asia"), 

328 (r"\(korea\)", "Korea"), 

329 ] 

330 

331 for pattern, region in desc_patterns: 

332 if re.search(pattern, desc): 

333 return region 

334 

335 return None 

336 

337 def _extract_revision(self, game: DATGame) -> str | None: 

338 """Extract revision from game name. 

339 

340 Args: 

341 game: DATGame to extract revision from 

342 

343 Returns: 

344 Revision string or None 

345 """ 

346 name = game.name.lower() 

347 

348 # Check for revision patterns 

349 rev_patterns = [ 

350 (r"r(\d+)$", r"r\1"), # sf2r1 -> r1 

351 (r"rev(\d+)$", r"rev\1"), # sf2rev1 -> rev1 

352 (r"v(\d+)$", r"v\1"), # sf2v1 -> v1 

353 (r"(\d+)$", r"\1"), # sf2ce1 -> 1 (version number at end) 

354 ] 

355 

356 for pattern, _replacement in rev_patterns: 

357 match = re.search(pattern, name) 

358 if match: 

359 return match.group(0) 

360 

361 return None 

362 

363 def get_region_priority(self, region: str | None) -> int: 

364 """Get priority score for a region. 

365 

366 Higher score = better for English-speaking users. 

367 

368 Args: 

369 region: Region string 

370 

371 Returns: 

372 Priority score (0-100) 

373 """ 

374 if not region: 

375 return 50 # Unknown region gets middle priority 

376 

377 return REGION_PRIORITY.get(region.lower(), 50) 

378 

379 

380def classify_arcade_game(game: DATGame) -> ArcadeClassification: 

381 """Convenience function to classify a single game. 

382 

383 Args: 

384 game: DATGame to classify 

385 

386 Returns: 

387 ArcadeClassification 

388 """ 

389 classifier = ArcadeClassifier() 

390 return classifier.classify(game)