summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/util/GameRecord.java
blob: b39a68dc12f930dc1e7aad431ade8741bbbfc794 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package ch.asynk.rustanddust.util;

import java.util.Date;
import java.text.DateFormat;

import com.badlogic.gdx.utils.Pool;
import com.badlogic.gdx.utils.Disposable;

import ch.asynk.rustanddust.ui.List;
import ch.asynk.rustanddust.game.Config.GameMode;
import ch.asynk.rustanddust.engine.util.Collection;
import ch.asynk.rustanddust.engine.util.IterableArray;

public class GameRecord implements List.ListElement, Disposable, Pool.Poolable
{
    public int id;
    public GameMode mode;
    public int battle;
    public int opponent;
    public int turn;
    public int currentPlayer;
    public Date ts;
    public boolean synched;
    public String players;
    public String playersH;
    public String map;
    public String mapH;
    public String orders;
    public String ordersH;
    //
    public String oName;
    public String bName;
    public String s;

    public static Collection<List.ListElement> list = new IterableArray<List.ListElement>(10);

    private static final Pool<GameRecord> gameRecordPool = new Pool<GameRecord>()
    {
        @Override
        protected GameRecord newObject() {
            return new GameRecord();
        }
    };

    public static void clearPool()
    {
        gameRecordPool.clear();
    }

    public static GameRecord get()
    {
        GameRecord r = gameRecordPool.obtain();
        return r;
    }

    public static GameRecord get(int idx)
    {
        return (GameRecord) list.get(idx);
    }

    public static GameRecord remove(int idx)
    {
        return (GameRecord) list.remove(idx);
    }

    public static void clearList()
    {
        for(List.ListElement r : list)
            ((GameRecord) r).dispose();
        list.clear();
    }

    public GameRecord()
    {
    }

    @Override
    public void reset()
    {
        this.s = null;
    }

    @Override
    public void dispose()
    {
        gameRecordPool.free(this);
    }

    @Override
    public String s()
    {
        if (s == null) {
            if (canPlay())
                s = String.format("# %s - %s - %s - %s", mode.s, bName, oName, DateFormat.getDateInstance().format(ts));
            else {
                    s = String.format("  %s - %s - %s - %s", mode.s, bName, oName, DateFormat.getDateInstance().format(ts));
            }
        }
        return s;
    }

    public boolean canPlay()
    {
        return ((mode == GameMode.SOLO) || (opponent != currentPlayer));
    }
}