[common] add PackId
This commit is contained in:
@@ -9,11 +9,10 @@ class LevelBundle implements ILevelBundle {
|
||||
|
||||
public function new() {}
|
||||
|
||||
public function get(type:GameType, name:String):LevelPack {
|
||||
var bytes = Assets.getBytes('levels/${type}_${name}.zip');
|
||||
public function get(id:PackId):LevelPack {
|
||||
var bytes = Assets.getBytes('levels/${id}.zip');
|
||||
return {
|
||||
type: type,
|
||||
name: name,
|
||||
id: id,
|
||||
data: LevelUtil.unpack(bytes),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package ru.m.tankz.storage;
|
||||
|
||||
import haxework.storage.SharedObjectStorage;
|
||||
import ru.m.tankz.game.GameProgress;
|
||||
import ru.m.tankz.Type.GameType;
|
||||
import ru.m.tankz.game.PackProgress;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
class GameStorage extends SharedObjectStorage {
|
||||
|
||||
@@ -12,11 +12,11 @@ class GameStorage extends SharedObjectStorage {
|
||||
super('game_${VERSION}');
|
||||
}
|
||||
|
||||
public function get(type:GameType):GameProgress {
|
||||
return exists(type) ? read(type) : new GameProgress(type);
|
||||
public function get(id:PackId):PackProgress {
|
||||
return exists(id) ? read(id) : new PackProgress(id);
|
||||
}
|
||||
|
||||
public function set(progress:GameProgress):Void {
|
||||
write(progress.type, progress);
|
||||
public function set(progress:PackProgress):Void {
|
||||
write(progress.id, progress);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,10 @@ import ru.m.tankz.control.Control;
|
||||
|
||||
class SettingsStorage extends SharedObjectStorage {
|
||||
|
||||
private static inline var VERSION = 3;
|
||||
|
||||
public function new() {
|
||||
super("settings-3");
|
||||
super('settings_${VERSION}');
|
||||
}
|
||||
|
||||
public function getBinding(index:Int):Binding {
|
||||
|
||||
@@ -12,6 +12,7 @@ import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.sound.SoundManager;
|
||||
import ru.m.tankz.storage.GameStorage;
|
||||
import ru.m.tankz.storage.SettingsStorage;
|
||||
import ru.m.tankz.Type;
|
||||
import ru.m.tankz.view.game.GameView;
|
||||
import ru.m.tankz.view.GamepadView;
|
||||
|
||||
@@ -28,7 +29,7 @@ import ru.m.tankz.view.GamepadView;
|
||||
@:provide var state:GameState;
|
||||
@:provide var record:GameRecord;
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var gameStorage:GameStorage;
|
||||
@:provide static var gameStorage:GameStorage;
|
||||
@:provide static var settings:SettingsStorage;
|
||||
|
||||
@:provide var game:IGame;
|
||||
@@ -75,12 +76,28 @@ import ru.m.tankz.view.GamepadView;
|
||||
switch event {
|
||||
case COMPLETE(state, winner):
|
||||
this.state = state;
|
||||
updateProgress(game, winner);
|
||||
stop();
|
||||
switcher.change(ResultFrame.ID);
|
||||
case _:
|
||||
}
|
||||
}
|
||||
|
||||
// ToDo:
|
||||
private static function updateProgress(game:IGame, winner:TeamId):Void {
|
||||
var complete = true;
|
||||
for (rule in game.config.game.complete) {
|
||||
if (rule.team != null && rule.team != winner) {
|
||||
complete = false;
|
||||
}
|
||||
}
|
||||
if (complete) {
|
||||
var progress = gameStorage.get(new PackId(game.state.type));
|
||||
progress.completeLevel(game.level.id, game.state.presetId);
|
||||
gameStorage.set(progress);
|
||||
}
|
||||
}
|
||||
|
||||
public function onHide():Void {
|
||||
stop();
|
||||
soundManager.stopAll();
|
||||
|
||||
@@ -11,6 +11,7 @@ import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.game.IGame;
|
||||
import ru.m.tankz.local.LocalGame;
|
||||
import ru.m.tankz.storage.GameStorage;
|
||||
import ru.m.tankz.Type;
|
||||
import ru.m.tankz.view.popup.LevelPopup;
|
||||
|
||||
@:template class LevelFrame extends VGroupView {
|
||||
@@ -25,11 +26,12 @@ import ru.m.tankz.view.popup.LevelPopup;
|
||||
@:provide var levelBundle:ILevelBundle;
|
||||
@:provide var storage:GameStorage;
|
||||
|
||||
private var pack:LevelPack;
|
||||
private var levelPopup:LevelPopup;
|
||||
|
||||
public function onShow():Void {
|
||||
header.text = state.type;
|
||||
var pack = levelBundle.get(state.type, "standard");
|
||||
pack = levelBundle.get(new PackId(state.type));
|
||||
levels.data = pack.data;
|
||||
}
|
||||
|
||||
@@ -41,7 +43,7 @@ import ru.m.tankz.view.popup.LevelPopup;
|
||||
}
|
||||
|
||||
private function levelViewFactory(index:Int, level:LevelConfig):ButtonView {
|
||||
var progress = storage.get(state.type);
|
||||
var progress = storage.get(pack.id);
|
||||
var result = new ButtonView();
|
||||
result.skinId = "button.level";
|
||||
var presetsLine = [for (p in state.config.presets) progress.isPresetCompleted(level.id, p.id) ? '*' : '_'].join('');
|
||||
@@ -51,7 +53,7 @@ import ru.m.tankz.view.popup.LevelPopup;
|
||||
}
|
||||
|
||||
private function onLevelSelect(index:Int, level:LevelConfig, view:ButtonView):Void {
|
||||
if (!storage.get(state.type).isLevelAvailable(level.id)) {
|
||||
if (!storage.get(pack.id).isLevelAvailable(level.id)) {
|
||||
return;
|
||||
}
|
||||
if (levelPopup == null) {
|
||||
@@ -61,7 +63,7 @@ import ru.m.tankz.view.popup.LevelPopup;
|
||||
level,
|
||||
state.config.presets,
|
||||
state.config.controls,
|
||||
storage.get(state.type)
|
||||
storage.get(pack.id)
|
||||
);
|
||||
levelPopup.show().then(function(result) result != null ? start(level, result.preset, result.control) : {});
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ private typedef Result = {
|
||||
for (v in typeView.dataViews) {
|
||||
v.on = v == view;
|
||||
}
|
||||
levelView.data = levelBundle.get(value, "standard").data;
|
||||
levelView.data = levelBundle.get(new PackId(value)).data;
|
||||
onLevelSelect(0, levelView.data[0], levelView.dataViews[0]);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package ru.m.tankz.view.popup;
|
||||
|
||||
import haxework.view.ToggleButtonView;
|
||||
import haxework.view.ButtonView;
|
||||
import haxework.view.DataView;
|
||||
import haxework.view.LabelView;
|
||||
import haxework.view.popup.PopupView;
|
||||
import haxework.view.ToggleButtonView;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.game.GameProgress;
|
||||
import ru.m.tankz.game.PackProgress;
|
||||
|
||||
private typedef Result = {
|
||||
var control:ControlPreset;
|
||||
@@ -16,14 +16,14 @@ private typedef Result = {
|
||||
@:template class LevelPopup extends PopupView<Result> {
|
||||
|
||||
private var level:LevelConfig;
|
||||
private var progress:GameProgress;
|
||||
private var progress:PackProgress;
|
||||
|
||||
@:view var name:LabelView;
|
||||
@:view("presets") var presetsView:DataView<GamePreset, ButtonView>;
|
||||
@:view("controls") var controlsView:DataView<ControlPreset, ToggleButtonView>;
|
||||
private var control:ControlPreset;
|
||||
|
||||
public function setData(level:LevelConfig, presets:Array<GamePreset>, controls:Array<ControlPreset>, progress:GameProgress):Void {
|
||||
public function setData(level:LevelConfig, presets:Array<GamePreset>, controls:Array<ControlPreset>, progress:PackProgress):Void {
|
||||
this.level = level;
|
||||
this.progress = progress;
|
||||
name.text = '${level.id}. ${level.name != null ? level.name : "#"}';
|
||||
|
||||
@@ -39,5 +39,33 @@ abstract PlayerId(Array<Dynamic>) {
|
||||
}
|
||||
}
|
||||
|
||||
abstract PackId(Array<Dynamic>) {
|
||||
public static inline var DEFAULT = "standard";
|
||||
|
||||
public var type(get, never):GameType;
|
||||
public var name(get, never):String;
|
||||
|
||||
public function new(type:GameType, name:String = DEFAULT) {
|
||||
this = [type, name];
|
||||
}
|
||||
|
||||
private inline function get_type():GameType return this[0];
|
||||
|
||||
private inline function get_name():String return this[1];
|
||||
|
||||
@:from static public inline function fromArray(value:Array<Dynamic>):PackId {
|
||||
return new PackId(value[0], value[1]);
|
||||
}
|
||||
|
||||
@:to public inline function toString():String {
|
||||
return '${type}_${name}';
|
||||
}
|
||||
|
||||
@:op(X == Y) static public inline function equals(x:PackId, y:PackId):Bool {
|
||||
return x.type == y.type && x.name == y.name;
|
||||
}
|
||||
}
|
||||
|
||||
typedef LevelId = Int;
|
||||
|
||||
typedef PresetId = Int;
|
||||
|
||||
@@ -4,5 +4,5 @@ import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
interface ILevelBundle {
|
||||
public function get(type:GameType, name:String):LevelPack;
|
||||
public function get(id:PackId):LevelPack;
|
||||
}
|
||||
|
||||
@@ -115,8 +115,7 @@ typedef LevelConfig = {
|
||||
}
|
||||
|
||||
typedef LevelPack = {
|
||||
var type:GameType;
|
||||
var name:String;
|
||||
var id:PackId;
|
||||
var data:Array<LevelConfig>;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ interface IGame extends GameListener {
|
||||
public var config(default, null):Config;
|
||||
public var winner(default, null):Null<TeamId>;
|
||||
public var state(default, null):GameState;
|
||||
public var level(default, null):LevelConfig;
|
||||
public var controlFactory(default, null):IControlFactory;
|
||||
public var pause(default, set):Bool;
|
||||
public var controls(default, null):Map<String, Control>;
|
||||
|
||||
@@ -11,13 +11,14 @@ typedef LevelProgress = {
|
||||
var presets:Map<Int, LevelResult>;
|
||||
}
|
||||
|
||||
class GameProgress {
|
||||
class PackProgress {
|
||||
|
||||
public var id(default, null):PackId;
|
||||
|
||||
public var type(default, null):GameType;
|
||||
private var completed(default, null):Map<Int, LevelProgress>;
|
||||
|
||||
public function new(type:GameType) {
|
||||
this.type = type;
|
||||
public function new(id:PackId) {
|
||||
this.id = id;
|
||||
this.completed = new Map();
|
||||
}
|
||||
|
||||
@@ -11,12 +11,11 @@ class ServerLevelBundle implements ILevelBundle {
|
||||
|
||||
public function new() {}
|
||||
|
||||
public function get(type:GameType, name:String):LevelPack {
|
||||
var path = FileSystem.absolutePath('./levels/${type}_${name}.zip');
|
||||
public function get(id:PackId):LevelPack {
|
||||
var path = FileSystem.absolutePath('./levels/${id}.zip');
|
||||
var bytes = File.getBytes(path);
|
||||
return {
|
||||
type: type,
|
||||
name: name,
|
||||
id: id,
|
||||
data: LevelUtil.unpack(bytes),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package ru.m.tankz.server.game;
|
||||
|
||||
import ru.m.tankz.bundle.ILevelBundle;
|
||||
import ru.m.tankz.proto.room.RoomSlotProto;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.core.EntityType;
|
||||
import ru.m.tankz.game.EventUtil;
|
||||
@@ -10,8 +9,10 @@ import ru.m.tankz.game.GameRunner;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.proto.core.UserProto;
|
||||
import ru.m.tankz.proto.room.RoomProto;
|
||||
import ru.m.tankz.proto.room.RoomSlotProto;
|
||||
import ru.m.tankz.proto.room.SlotProto;
|
||||
import ru.m.tankz.server.control.ServerControlFactory;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
class ServerGame extends GameRunner {
|
||||
|
||||
@@ -22,7 +23,7 @@ class ServerGame extends GameRunner {
|
||||
@:provide static var levelBundle:ILevelBundle;
|
||||
|
||||
public function new(room:RoomProto) {
|
||||
super(new GameState(room.game.type, 0), levelBundle.get(room.game.type, "standard").data[room.game.level]);
|
||||
super(new GameState(room.game.type, 0), levelBundle.get(new PackId(room.game.type)).data[room.game.level]);
|
||||
this.controlFactory = new ServerControlFactory();
|
||||
this.room = room;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user